The Battle for Wesnoth  1.15.5+dev
surface.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "global.hpp"
17 #include "utils/const_clone.hpp"
18 
19 #include <SDL2/SDL.h>
20 
21 #include <ostream>
22 
23 class CVideo;
24 
25 class surface
26 {
27 public:
28  surface() : surface_(nullptr)
29  {}
30 
31  surface(SDL_Surface* surf);
32 
33  /** Allocates a new surface with the given dimensions. */
34  surface(int w, int h);
35 
36  surface(const surface& s) : surface_(s.get())
37  {
39  }
40 
41  surface(surface&& s) noexcept : surface_(s.get())
42  {
43  s.surface_ = nullptr;
44  }
45 
47  {
48  free_surface();
49  }
50 
52  {
54  return *this;
55  }
56 
57  surface& operator=(surface&& s) noexcept
58  {
59  free_surface();
60  surface_ = s.surface_;
61  s.surface_ = nullptr;
62  return *this;
63  }
64 
65  // Intended to be used when SDL has already freed the surface
66  void clear_without_free() { surface_ = nullptr; }
67 
68  /**
69  * Check that the surface is neutral bpp 32.
70  *
71  * The surface may have an empty alpha channel.
72  *
73  * @returns The status @c true if neutral, @c false if not.
74  */
75  bool is_neutral() const;
76 
77  /**
78  * Converts this surface to a neutral format if it is not already.
79  *
80  * @returns A reference to this object for chaining convenience.
81  */
83 
84  /**
85  * Makes a copy of this surface. The copy will be in the 'neutral' pixel format.
86  *
87  * Note this is creates a new, duplicate surface in memory. Making a copy of this
88  * 'surface' object will *not* duplicate the surface itself since we only hold a
89  * pointer to the actual surface.
90  */
91  surface clone() const;
92 
93  operator SDL_Surface*() const { return surface_; }
94 
95  SDL_Surface* get() const { return surface_; }
96 
97  SDL_Surface* operator->() const { return surface_; }
98 
99 private:
100  static void add_surface_ref(SDL_Surface* surf)
101  {
102  if(surf) {
103  ++surf->refcount;
104  }
105  }
106 
107  void assign_surface_internal(SDL_Surface* surf);
108 
109  void free_surface();
110 
111  SDL_Surface* surface_;
112 
113  static const SDL_PixelFormat neutral_pixel_format;
114 };
115 
116 bool operator<(const surface& a, const surface& b);
117 
118 std::ostream& operator<<(std::ostream& stream, const surface& surf);
119 
121 {
123  surface_restorer(class CVideo* target, const SDL_Rect& rect);
124  ~surface_restorer();
125 
126  void restore() const;
127  void restore(const SDL_Rect& dst) const;
128  void update();
129  void cancel();
130 
131  const SDL_Rect& area() const { return rect_; }
132 
133 private:
134  class CVideo* target_;
135  SDL_Rect rect_;
137 };
138 
139 /**
140  * Helper class for pinning SDL surfaces into memory.
141  * @note This class should be used only with neutral surfaces, so that
142  * the pointer returned by #pixels is meaningful.
143  */
144 template<typename T>
146 {
147 private:
149 
150 public:
151  surface_locker(T& surf) : surface_(surf), locked_(false)
152  {
153  if(SDL_MUSTLOCK(surface_)) {
154  locked_ = SDL_LockSurface(surface_) == 0;
155  }
156  }
157 
159  {
160  if(locked_) {
161  SDL_UnlockSurface(surface_);
162  }
163  }
164 
165  pixel_t* pixels() const { return reinterpret_cast<pixel_t*>(surface_->pixels); }
166 
167 private:
169  bool locked_;
170 };
171 
174 
176 {
177  // if r is nullptr, clip to the full size of the surface.
178  clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
179  {
180  if(operate_ && surface_.get()){
181  SDL_GetClipRect(surface_, &rect_);
182  SDL_Rect final_rect = { 0, 0, 0, 0 };
183 
184  if(r) {
185  SDL_IntersectRect(&rect_, r, &final_rect);
186  } else {
187  final_rect.w = surface_->w;
188  final_rect.h = surface_->h;
189  }
190 
191  SDL_SetClipRect(surface_, &final_rect);
192  }
193  }
194 
196  {
197  if(operate_ && surface_.get()) {
198  SDL_SetClipRect(surface_, &rect_);
199  }
200  }
201 
202 private:
204  SDL_Rect rect_;
205  const bool operate_;
206 };
pixel_t * pixels() const
Definition: surface.hpp:165
SDL_Surface * get() const
Definition: surface.hpp:95
void free_surface()
Definition: surface.cpp:89
#define a
Definition: video.hpp:31
surface_locker(T &surf)
Definition: surface.hpp:151
const SDL_Rect & area() const
Definition: surface.hpp:131
static void add_surface_ref(SDL_Surface *surf)
Definition: surface.hpp:100
#define h
surface(const surface &s)
Definition: surface.hpp:36
void assign_surface_internal(SDL_Surface *surf)
Definition: surface.cpp:81
surface & operator=(surface &&s) noexcept
Definition: surface.hpp:57
surface clone() const
Makes a copy of this surface.
Definition: surface.cpp:75
surface surface_
Definition: surface.hpp:136
#define b
surface()
Definition: surface.hpp:28
surface(surface &&s) noexcept
Definition: surface.hpp:41
surface & operator=(const surface &s)
Definition: surface.hpp:51
surface & make_neutral()
Converts this surface to a neutral format if it is not already.
Definition: surface.cpp:61
SDL_Rect rect_
Definition: surface.hpp:135
utils::const_clone_t< uint32_t, T > pixel_t
Definition: surface.hpp:148
SDL_Rect rect_
Definition: surface.hpp:204
const bool operate_
Definition: surface.hpp:205
bool operator<(const surface &a, const surface &b)
Definition: surface.cpp:170
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition: surface.cpp:175
class CVideo * target_
Definition: surface.hpp:134
static map_location::DIRECTION s
SDL_Surface * surface_
Definition: surface.hpp:111
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:145
SDL_Surface * operator->() const
Definition: surface.hpp:97
int w
surface surface_
Definition: surface.hpp:203
bool is_neutral() const
Check that the surface is neutral bpp 32.
Definition: surface.cpp:52
void clear_without_free()
Definition: surface.hpp:66
clip_rect_setter(const surface &surf, const SDL_Rect *r, bool operate=true)
Definition: surface.hpp:178
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:59
static const SDL_PixelFormat neutral_pixel_format
Definition: surface.hpp:113
~surface()
Definition: surface.hpp:46