SDL Window Engine  20200905
swe_surface.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_SURFACE_
24 #define _SWE_SURFACE_
25 
26 #include <memory>
27 
28 #include "swe_object.h"
29 #include "swe_colors.h"
30 #include "swe_rect.h"
31 
33 namespace SWE
34 {
35 
36  class BinaryBuf;
37 
38 #ifdef SWE_SDL12
39  enum { FlipNone = 0, FlipHorizontal = 0x01, FlipVertical = 0x02 };
40 #else
41  enum { FlipNone = SDL_FLIP_NONE, FlipHorizontal = SDL_FLIP_HORIZONTAL, FlipVertical = SDL_FLIP_VERTICAL };
42 #endif
43  enum { Rotate90Degrees = 0x04, Rotate180Degrees = 0x08, Rotate270Degrees = 0x10, FlipRotate = Rotate90Degrees | Rotate180Degrees | Rotate270Degrees };
44 
45 #ifdef SWE_SDL12
46  typedef SDL_Surface SDL_Texture;
47  namespace BlendMode
48  {
49  enum { None, Blend, Add, Mod };
50  }
51 #else
52  namespace BlendMode
53  {
54  enum { None = SDL_BLENDMODE_NONE, Blend = SDL_BLENDMODE_BLEND, Add = SDL_BLENDMODE_ADD, Mod = SDL_BLENDMODE_MOD };
55  }
56 #endif
57 
58  class Surface : public ObjectClass
59  {
60  u32 pixel1(const Point &) const;
61  u32 pixel2(const Point &) const;
62  u32 pixel3(const Point &) const;
63  u32 pixel4(const Point &) const;
64 
65  void draw1(const Point &, u32 pixel);
66  void draw2(const Point &, u32 pixel);
67  void draw3(const Point &, u32 pixel);
68  void draw4(const Point &, u32 pixel);
69 
70  protected:
71  SDL_Surface* ptr;
72 
73  public:
74  Surface();
75  Surface(SDL_Surface*);
76  Surface(const Size &, bool alpha = true);
77  Surface(const Surface &);
78  Surface(const std::string &);
79  Surface(const BinaryBuf &);
80  virtual ~Surface();
81 
82  Surface & operator= (const Surface &);
83 
84  static Surface copy(const Surface &, int flip = FlipNone);
85  static Surface copy(const Surface &, const Rect &);
86  static Surface scale(const Surface &, const Size &);
87 
88  bool isValid(void) const;
89  SDL_Surface* toSDLSurface(void) const;
90 
91  bool operator== (const Surface & sf) const;
92  bool operator!= (const Surface & sf) const;
93 
94  void setSurface(const Surface &);
95  void reset(void);
96 
97 #ifdef SWE_SDL12
98  void convertToDisplayFormat(void);
99 #endif
100 
101  int width(void) const;
102  int height(void) const;
103  Size size(void) const;
104  Rect rect(void) const;
105  int alphaMod(void) const;
106  u32 amask(void) const;
107  int flags(void) const;
108  Color colorKey(void) const;
109  u32 pixel(const Point &) const;
110 
111  u32 mapRGB(const Color &) const;
112  Color RGBmap(u32) const;
113 
114  void blit(const Rect &, const Rect &, Surface &) const;
115  void fill(const Rect &, const Color &);
116  void clear(const Color & col);
117  void drawPoint(const Point &, const Color &);
118  void drawPixel(const Point &, u32);
119  bool save(const std::string &) const;
120  void swap(Surface &);
121 
122  void setColorKey(const Color &);
123  void setAlphaMod(int);
124  void setFlags(int);
125  void resetFlags(int);
126 
127  static Surface renderGrayScale(const Surface &);
128  static Surface renderSepia(const Surface &);
129 
130  std::string toString(void) const;
131  const char* className(void) const override { return "SWE::Surface"; }
132 #ifdef SWE_WITH_JSON
133  JsonObject toJson(void) const override;
134 #endif
135  };
136 
137  class SurfaceRef : public Surface
138  {
139  public:
140  SurfaceRef(SDL_Surface* sf)
141  {
142  if(sf) sf->refcount++;
143  ptr = sf;
144  }
145  };
146 
147  const char* blendModeString(int);
148  //struct SDLTexture;
149 
150 #ifdef SWE_SDL12
151  class Texture : public Surface
152  {
153 #else
154  class Texture : public ObjectClass
155  {
156  protected:
157  std::shared_ptr<SDL_Texture> ptr;
158 #endif
159  public:
160  Texture() {}
161 
162 #ifdef SWE_SDL12
163  Texture(const Surface &);
164 #endif
165  Texture(SDL_Texture*);
166  virtual ~Texture() {}
167 
168 #ifndef SWE_SDL12
169  Texture(const Texture &) = default;
170  Texture & operator=(const Texture &) = default;
171 #endif
172 
173  static Texture scale(const Texture &, const Size &);
174  static Texture copy(const Texture &, const Rect &);
175  static Texture copy(const Texture &, int flip = FlipNone);
176 
177  bool operator== (const Texture &) const;
178  bool operator!= (const Texture &) const;
179 
180  void setTexture(const Texture &);
181  void setAlphaMod(int);
182  void setColorMod(const Color &);
183  void setBlendMode(int);
184 
185  bool isValid(void) const;
186  SDL_Texture* toSDLTexture(void) const;
187 
188  int width(void) const;
189  int height(void) const;
190  int alphaMod(void) const;
191  Color colorMod(void) const;
192  int blendMode(void) const;
193  Size size(void) const;
194  Rect rect(void) const;
195  void reset(void);
196  void swap(Texture &);
197  void fill(const Rect &, const Color &);
198  bool save(const std::string &) const;
199 
200  static Texture renderGrayScale(const Texture &);
201  static Texture renderSepia(const Texture &);
202 
203  std::string toString(void) const;
204  const char* className(void) const override { return "SWE::Texture"; }
205 #ifdef SWE_WITH_JSON
206  JsonObject toJson(void) const override;
207 #endif
208  };
209 
210  class TexturePos : public Texture
211  {
212  protected:
213  Point pos;
214 
215  public:
216  TexturePos() {}
217  TexturePos(const Texture & tx) : Texture(tx) {}
218  TexturePos(const Texture & tx, const Point & pt) : Texture(tx), pos(pt) {}
219 
220  TexturePos & operator= (const Texture & tx);
221  void setPosition(const Point & pt);
222  void setPosition(int posx, int posy);
223 
224  const Point & position(void) const;
225  const Texture & texture(void) const;
226  Rect area(void) const;
227 
228  void swap(TexturePos &);
229 
230  const char* className(void) const override { return "SWE::TexturePos"; }
231 #ifdef SWE_WITH_JSON
232  JsonObject toJson(void) const override;
233 #endif
234  };
235 
236 } // SWE
237 #endif
Definition: swe_surface.h:154
Definition: swe_surface.h:137
пространство SWE.
Definition: swe_binarybuf.cpp:30
класс прямоугольника
Definition: swe_rect.h:144
класс точки с двумя координатами
Definition: swe_rect.h:72
const char * className(void) const override
идентификацинная метка класса
Definition: swe_surface.h:204
класс цвета
Definition: swe_colors.h:65
Definition: swe_surface.h:210
класс бинарного массива
Definition: swe_binarybuf.h:35
класс двухмерной размерности
Definition: swe_rect.h:36
Definition: swe_surface.h:58
const char * className(void) const override
идентификацинная метка класса
Definition: swe_surface.h:131
родительский класс объектов сцены DisplayScene.
Definition: swe_object.h:38
const char * className(void) const override
идентификацинная метка класса
Definition: swe_surface.h:230