The Battle for Wesnoth  1.19.9+dev
cairo.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2025 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "sdl/point.hpp"
18 
19 #include <cairo.h>
20 #include <memory>
21 
22 namespace cairo
23 {
24 using surface_ptr = std::unique_ptr<cairo_surface_t, void(*)(cairo_surface_t*)>;
25 using context_ptr = std::unique_ptr<cairo_t, void(*)(cairo_t*)>;
26 
27 /** Color format for cairo surfaces. Should be equivalent to the format used by SDL. */
28 constexpr cairo_format_t format = CAIRO_FORMAT_ARGB32;
29 
30 surface_ptr create_surface(uint8_t* buffer, const point& size)
31 {
32  const auto& [width, height] = size;
33  const int stride = cairo_format_stride_for_width(format, width);
34 
35  return {
36  cairo_image_surface_create_for_data(buffer, format, width, height, stride),
37  cairo_surface_destroy
38  };
39 }
40 
42 {
43  return {
44  cairo_create(surf.get()),
45  cairo_destroy
46  };
47 }
48 
49 } // namespace cairo
SDL_Surface * get() const
Definition: surface.hpp:97
Definition: cairo.hpp:23
std::unique_ptr< cairo_surface_t, void(*)(cairo_surface_t *)> surface_ptr
Definition: cairo.hpp:24
context_ptr create_context(const surface_ptr &surf)
Definition: cairo.hpp:41
constexpr cairo_format_t format
Color format for cairo surfaces.
Definition: cairo.hpp:28
std::unique_ptr< cairo_t, void(*)(cairo_t *)> context_ptr
Definition: cairo.hpp:25
surface_ptr create_surface(uint8_t *buffer, const point &size)
Definition: cairo.hpp:30
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
surface surf
Image.
Holds a 2D point.
Definition: point.hpp:25