The Battle for Wesnoth  1.17.6+dev
surface.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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/rect.hpp"
18 #include "utils/const_clone.hpp"
19 
20 #include <SDL2/SDL.h>
21 
22 #include <ostream>
23 
24 class surface
25 {
26 public:
27  surface() : surface_(nullptr)
28  {}
29 
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) : surface_(s.get())
36  {
38  }
39 
40  surface(surface&& s) noexcept : surface_(s.get())
41  {
42  s.surface_ = nullptr;
43  }
44 
46  {
47  free_surface();
48  }
49 
51  {
53  return *this;
54  }
55 
56  surface& operator=(surface&& s) noexcept
57  {
58  free_surface();
59  surface_ = s.surface_;
60  s.surface_ = nullptr;
61  return *this;
62  }
63 
64  /**
65  * Check that the surface is neutral bpp 32.
66  *
67  * The surface may have an empty alpha channel.
68  *
69  * @returns The status @c true if neutral, @c false if not.
70  */
71  bool is_neutral() const;
72 
73  /**
74  * Converts this surface to a neutral format if it is not already.
75  *
76  * @returns A reference to this object for chaining convenience.
77  */
79 
80  /**
81  * Makes a copy of this surface. The copy will be in the 'neutral' pixel format.
82  *
83  * Note this is creates a new, duplicate surface in memory. Making a copy of this
84  * 'surface' object will *not* duplicate the surface itself since we only hold a
85  * pointer to the actual surface.
86  */
87  surface clone() const;
88 
89  operator SDL_Surface*() const { return surface_; }
90 
91  SDL_Surface* get() const { return surface_; }
92 
93  SDL_Surface* operator->() const { return surface_; }
94 
95 private:
96  static void add_surface_ref(SDL_Surface* surf)
97  {
98  if(surf) {
99  ++surf->refcount;
100  }
101  }
102 
103  void assign_surface_internal(SDL_Surface* surf);
104 
105  void free_surface();
106 
107  SDL_Surface* surface_;
108 
109  static const SDL_PixelFormat neutral_pixel_format;
110 };
111 
112 bool operator<(const surface& a, const surface& b);
113 
114 std::ostream& operator<<(std::ostream& stream, const surface& surf);
115 
116 /**
117  * Helper class for pinning SDL surfaces into memory.
118  * @note This class should be used only with neutral surfaces, so that
119  * the pointer returned by #pixels is meaningful.
120  */
121 template<typename T>
123 {
124 private:
126 
127 public:
128  surface_locker(T& surf) : surface_(surf), locked_(false)
129  {
130  if(SDL_MUSTLOCK(surface_)) {
131  locked_ = SDL_LockSurface(surface_) == 0;
132  }
133  }
134 
136  {
137  if(locked_) {
138  SDL_UnlockSurface(surface_);
139  }
140  }
141 
142  pixel_t* pixels() const { return reinterpret_cast<pixel_t*>(surface_->pixels); }
143 
144 private:
146  bool locked_;
147 };
148 
151 
153 {
154  // if r is nullptr, clip to the full size of the surface.
155  clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
156  {
157  if(operate_ && surface_.get()){
158  SDL_GetClipRect(surface_, &rect_);
159  SDL_Rect final_rect = { 0, 0, 0, 0 };
160 
161  if(r) {
162  SDL_IntersectRect(&rect_, r, &final_rect);
163  } else {
164  final_rect.w = surface_->w;
165  final_rect.h = surface_->h;
166  }
167 
168  SDL_SetClipRect(surface_, &final_rect);
169  }
170  }
171 
173  {
174  if(operate_ && surface_.get()) {
175  SDL_SetClipRect(surface_, &rect_);
176  }
177  }
178 
179 private:
181  SDL_Rect rect_;
182  const bool operate_;
183 };
pixel_t * pixels() const
Definition: surface.hpp:142
SDL_Surface * get() const
Definition: surface.hpp:91
void free_surface()
Definition: surface.cpp:77
#define a
surface_locker(T &surf)
Definition: surface.hpp:128
static void add_surface_ref(SDL_Surface *surf)
Definition: surface.hpp:96
#define h
surface(const surface &s)
Definition: surface.hpp:35
void assign_surface_internal(SDL_Surface *surf)
Definition: surface.cpp:69
surface & operator=(surface &&s) noexcept
Definition: surface.hpp:56
surface clone() const
Makes a copy of this surface.
Definition: surface.cpp:63
#define b
surface()
Definition: surface.hpp:27
surface(surface &&s) noexcept
Definition: surface.hpp:40
surface & operator=(const surface &s)
Definition: surface.hpp:50
surface & make_neutral()
Converts this surface to a neutral format if it is not already.
Definition: surface.cpp:49
utils::const_clone_t< uint32_t, T > pixel_t
Definition: surface.hpp:125
SDL_Rect rect_
Definition: surface.hpp:181
const bool operate_
Definition: surface.hpp:182
bool operator<(const surface &a, const surface &b)
Definition: surface.cpp:84
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition: surface.cpp:89
static map_location::DIRECTION s
SDL_Surface * surface_
Definition: surface.hpp:107
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:122
SDL_Surface * operator->() const
Definition: surface.hpp:93
int w
surface surface_
Definition: surface.hpp:180
bool is_neutral() const
Check that the surface is neutral bpp 32.
Definition: surface.cpp:40
clip_rect_setter(const surface &surf, const SDL_Rect *r, bool operate=true)
Definition: surface.hpp:155
Contains the SDL_Rect helper code.
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:60
static const SDL_PixelFormat neutral_pixel_format
Definition: surface.hpp:109
~surface()
Definition: surface.hpp:45