The Battle for Wesnoth  1.15.12+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 "utils/const_clone.hpp"
17 
18 #include <SDL2/SDL.h>
19 
20 #include <ostream>
21 
22 class CVideo;
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 
117 {
119  surface_restorer(class CVideo* target, const SDL_Rect& rect);
120  ~surface_restorer();
121 
122  void restore() const;
123  void restore(const SDL_Rect& dst) const;
124  void update();
125  void cancel();
126 
127  const SDL_Rect& area() const { return rect_; }
128 
129 private:
130  class CVideo* target_;
131  SDL_Rect rect_;
133 };
134 
135 /**
136  * Helper class for pinning SDL surfaces into memory.
137  * @note This class should be used only with neutral surfaces, so that
138  * the pointer returned by #pixels is meaningful.
139  */
140 template<typename T>
142 {
143 private:
145 
146 public:
147  surface_locker(T& surf) : surface_(surf), locked_(false)
148  {
149  if(SDL_MUSTLOCK(surface_)) {
150  locked_ = SDL_LockSurface(surface_) == 0;
151  }
152  }
153 
155  {
156  if(locked_) {
157  SDL_UnlockSurface(surface_);
158  }
159  }
160 
161  pixel_t* pixels() const { return reinterpret_cast<pixel_t*>(surface_->pixels); }
162 
163 private:
165  bool locked_;
166 };
167 
170 
172 {
173  // if r is nullptr, clip to the full size of the surface.
174  clip_rect_setter(const surface &surf, const SDL_Rect* r, bool operate = true) : surface_(surf), rect_(), operate_(operate)
175  {
176  if(operate_ && surface_.get()){
177  SDL_GetClipRect(surface_, &rect_);
178  SDL_Rect final_rect = { 0, 0, 0, 0 };
179 
180  if(r) {
181  SDL_IntersectRect(&rect_, r, &final_rect);
182  } else {
183  final_rect.w = surface_->w;
184  final_rect.h = surface_->h;
185  }
186 
187  SDL_SetClipRect(surface_, &final_rect);
188  }
189  }
190 
192  {
193  if(operate_ && surface_.get()) {
194  SDL_SetClipRect(surface_, &rect_);
195  }
196  }
197 
198 private:
200  SDL_Rect rect_;
201  const bool operate_;
202 };
pixel_t * pixels() const
Definition: surface.hpp:161
SDL_Surface * get() const
Definition: surface.hpp:91
void free_surface()
Definition: surface.cpp:76
#define a
Definition: video.hpp:31
surface_locker(T &surf)
Definition: surface.hpp:147
const SDL_Rect & area() const
Definition: surface.hpp:127
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:68
surface & operator=(surface &&s) noexcept
Definition: surface.hpp:56
surface clone() const
Makes a copy of this surface.
Definition: surface.cpp:62
surface surface_
Definition: surface.hpp:132
#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:48
SDL_Rect rect_
Definition: surface.hpp:131
utils::const_clone_t< uint32_t, T > pixel_t
Definition: surface.hpp:144
SDL_Rect rect_
Definition: surface.hpp:200
const bool operate_
Definition: surface.hpp:201
bool operator<(const surface &a, const surface &b)
Definition: surface.cpp:144
std::ostream & operator<<(std::ostream &stream, const surface &surf)
Definition: surface.cpp:149
class CVideo * target_
Definition: surface.hpp:130
static map_location::DIRECTION s
SDL_Surface * surface_
Definition: surface.hpp:107
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:141
SDL_Surface * operator->() const
Definition: surface.hpp:93
int w
surface surface_
Definition: surface.hpp:199
bool is_neutral() const
Check that the surface is neutral bpp 32.
Definition: surface.cpp:39
clip_rect_setter(const surface &surf, const SDL_Rect *r, bool operate=true)
Definition: surface.hpp:174
typename const_clone< D, S >::type const_clone_t
Definition: const_clone.hpp:59
static const SDL_PixelFormat neutral_pixel_format
Definition: surface.hpp:109
~surface()
Definition: surface.hpp:45