The Battle for Wesnoth  1.19.19+dev
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 <SDL2/SDL_surface.h>
21 
22 #include <ostream>
23 
24 struct point;
25 
26 class surface
27 {
28 public:
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 
63 private:
64  SDL_Surface* surface_{};
65 };
66 
67 std::ostream& operator<<(std::ostream& stream, const surface& surf);
68 
69 namespace surface_helper
70 {
71 /**
72  * Returns a read-only view over to @a surf's underlying pixel array.
73  */
74 inline 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  */
83 inline 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  */
96 template<typename T>
98 {
99 private:
101 
102 public:
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 
117  pixel_t* pixels() const
118  {
119  return reinterpret_cast<pixel_t*>(surface_->pixels);
120  }
121 
123  {
125  }
126 
127 private:
129  bool locked_;
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::const_clone_t< uint32_t, T > pixel_t
Definition: surface.hpp:100
utils::span< pixel_t > pixel_span() const
Definition: surface.hpp:122
~surface()
Definition: surface.cpp:74
surface()=default
point size() const
Dimensions of the surface.
Definition: surface.cpp:103
SDL_Surface * operator->() const
Definition: surface.hpp:61
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:112
surface & operator=(const surface &s)
Definition: surface.cpp:79
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
Definition: const_clone.hpp:60
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:117
static map_location::direction s
#define h