The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
surface.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 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 "utils/const_clone.hpp"
18#include "utils/span.hpp"
19
20#include <SDL3/SDL_surface.h>
21
22#include <ostream>
23
24struct point;
25
27{
28public:
29 surface() = default;
30 surface(SDL_Surface* surf);
31
32 /** Allocates a new surface with the given dimensions. */
33 surface(int w, int h);
34
35 surface(const surface& s);
36 surface(surface&& s) noexcept;
37
38 ~surface();
39
40 surface& operator=(const surface& s);
41 surface& operator=(surface&& s) noexcept;
42
43 /**
44 * Creates a new, duplicate surface in memory using the 'neutral' pixel format.
45 *
46 * @note Making a copy of a surface object does *not* duplicate its pixel data,
47 * since we only hold a pointer to the actual buffer. For a true deep copy, use
48 * this method.
49 */
50 surface clone() const;
51
52 /** Dimensions of the surface. */
53 point size() const;
54
55 /** Total area of the surface in square pixels. */
56 std::size_t area() const;
57
58 operator SDL_Surface*() const { return surface_; }
59
60 SDL_Surface* get() const { return surface_; }
61 SDL_Surface* operator->() const { return surface_; }
62
63private:
64 SDL_Surface* surface_{};
65};
66
67std::ostream& operator<<(std::ostream& stream, const surface& surf);
68
70{
71/**
72 * Returns a read-only view over to @a surf's underlying pixel array.
73 */
74inline auto pixel_span(const surface& surf)
75{
76 auto* pixels = reinterpret_cast<const uint32_t*>(surf->pixels);
77 return utils::span{pixels, surf.area()};
78}
79
80/**
81 * Returns a mutable per-pixel view over @a surf's underlying pixel array.
82 */
83inline auto pixel_span(surface& surf)
84{
85 auto* pixels = reinterpret_cast<uint32_t*>(surf->pixels);
86 return utils::span{pixels, surf.area()};
87}
88
89} // namespace surface_helper
90
91/**
92 * Helper class for pinning SDL surfaces into memory.
93 * @note This class should be used only with neutral surfaces, so that
94 * the pointer returned by #pixels is meaningful.
95 */
96template<typename T>
98{
99private:
101
102public:
104 {
105 if(SDL_MUSTLOCK(surface_)) {
106 locked_ = SDL_LockSurface(surface_) == 0;
107 }
108 }
109
111 {
112 if(locked_) {
113 SDL_UnlockSurface(surface_);
114 }
115 }
116
118 {
119 return reinterpret_cast<pixel_t*>(surface_->pixels);
120 }
121
126
127private:
130};
131
Helper class for pinning SDL surfaces into memory.
Definition surface.hpp:98
surface_locker(T &surf)
Definition surface.hpp:103
pixel_t * pixels() const
Definition surface.hpp:117
utils::span< pixel_t > pixel_span() const
Definition surface.hpp:122
utils::const_clone_t< uint32_t, T > pixel_t
Definition surface.hpp:100
~surface()
Definition surface.cpp:74
surface()=default
point size() const
Dimensions of the surface.
Definition surface.cpp:106
surface clone() const
Creates a new, duplicate surface in memory using the 'neutral' pixel format.
Definition surface.cpp:97
SDL_Surface * surface_
Definition surface.hpp:64
SDL_Surface * get() const
Definition surface.hpp:60
std::size_t area() const
Total area of the surface in square pixels.
Definition surface.cpp:115
surface & operator=(const surface &s)
Definition surface.cpp:79
SDL_Surface * operator->() const
Definition surface.hpp:61
auto pixel_span(const surface &surf)
Returns a read-only view over to surf's underlying pixel array.
Definition surface.hpp:74
typename const_clone< D, S >::type const_clone_t
int w
Definition pathfind.cpp:188
surface surf
Image.
Holds a 2D point.
Definition point.hpp:25
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition surface.cpp:120
static map_location::direction s
#define h