The Battle for Wesnoth  1.19.11+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 class surface
25 {
26 public:
27  surface() = default;
28  surface(SDL_Surface* surf);
29 
30  /** Allocates a new surface with the given dimensions. */
31  surface(int w, int h);
32 
33  surface(const surface& s);
34  surface(surface&& s) noexcept;
35 
36  ~surface();
37 
38  surface& operator=(const surface& s);
39  surface& operator=(surface&& s) noexcept;
40 
41  /**
42  * Creates a new, duplicate surface in memory using the 'neutral' pixel format.
43  *
44  * @note Making a copy of a surface object does *not* duplicate its pixel data,
45  * since we only hold a pointer to the actual buffer. For a true deep copy, use
46  * this method.
47  */
48  surface clone() const;
49 
50  /** Total area of the surface in square pixels. */
51  std::size_t area() const;
52 
53  operator SDL_Surface*() const { return surface_; }
54 
55  SDL_Surface* get() const { return surface_; }
56  SDL_Surface* operator->() const { return surface_; }
57 
58 private:
59  SDL_Surface* surface_{};
60 };
61 
62 std::ostream& operator<<(std::ostream& stream, const surface& surf);
63 
64 /**
65  * Helper class for pinning SDL surfaces into memory.
66  * @note This class should be used only with neutral surfaces, so that
67  * the pointer returned by #pixels is meaningful.
68  */
69 template<typename T>
71 {
72 private:
74 
75 public:
77  {
78  if(SDL_MUSTLOCK(surface_)) {
79  locked_ = SDL_LockSurface(surface_) == 0;
80  }
81  }
82 
84  {
85  if(locked_) {
86  SDL_UnlockSurface(surface_);
87  }
88  }
89 
90  pixel_t* pixels() const
91  {
92  return reinterpret_cast<pixel_t*>(surface_->pixels);
93  }
94 
96  {
97  return { pixels(), surface_.area() };
98  }
99 
100 private:
102  bool locked_;
103 };
104 
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:71
surface_locker(T &surf)
Definition: surface.hpp:76
pixel_t * pixels() const
Definition: surface.hpp:90
utils::const_clone_t< uint32_t, T > pixel_t
Definition: surface.hpp:73
utils::span< pixel_t > pixel_span() const
Definition: surface.hpp:95
~surface()
Definition: surface.cpp:74
surface()=default
SDL_Surface * operator->() const
Definition: surface.hpp:56
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:59
SDL_Surface * get() const
Definition: surface.hpp:55
std::size_t area() const
Total area of the surface in square pixels.
Definition: surface.cpp:103
surface & operator=(const surface &s)
Definition: surface.cpp:79
int w
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:60
surface surf
Image.
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition: surface.cpp:108
static map_location::direction s
#define h