Rose
Surface.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <exception>
11 #include <filesystem>
12 #include <memory>
13 #include <SDL.h>
14 #include "Color.h"
15 #include "Texture.h"
16 #include "Types.h"
17 
18 namespace rose::gm {
19 
20  class Context;
21 
22  class SurfaceRuntimeError : public std::runtime_error {
23  public:
24  ~SurfaceRuntimeError() override = default;
25 
26  explicit SurfaceRuntimeError(const std::string &what) : std::runtime_error(what) {}
27  explicit SurfaceRuntimeError(const char *what) : std::runtime_error(what) {}
28  };
29 
34  public:
39  void operator()(SDL_Surface *sdlSurface) {
40  if (sdlSurface != nullptr)
41  SDL_FreeSurface(sdlSurface);
42  }
43  };
44 
50  class Surface : public std::unique_ptr<SDL_Surface, SurfaceDestroy> {
51  public:
55  Surface() = default;
56 
61  explicit Surface(SDL_Surface *surface) : std::unique_ptr<SDL_Surface, SurfaceDestroy>(surface) {}
62 
67  explicit Surface(const std::filesystem::path &path);
68 
76  Surface(int width, int height, int depth = 32, SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGBA8888);
77 
78  explicit Surface(Size size, int depth = 32, SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGBA8888)
79  : Surface(size.w, size.h, depth, format) {}
80 
81  Surface(int width, int height, int depth, uint32_t rmask, uint32_t gmask, uint32_t bmask, uint32_t amask);
82 
90  [[nodiscard]] uint32_t &pixel(int x, int y) const;
91 
98  [[nodiscard]] color::RGBA color(int x, int y) const;
99 
106  void setColor(int x, int y, color::RGBA color);
107 
116  bool
117  createWithFormat(int width, int height, int depth = 32, SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGBA8888);
118 
125  int fillRectangle(const Rectangle &rect, const color::RGBA &color);
126 
132  int fillRectangle(const color::RGBA &color);
133 
139  bool textureFromSurface(Context &context, Texture &texture);
140 
146  Texture toTexture(Context &context);
147 
153  int setBlendMode(SDL_BlendMode blendMode) noexcept;
154 
160  int blitSurface(Surface &source);
161  };
162 
167  class SurfaceLock {
168  protected:
169  int status{0};
170  SDL_Surface *mSurface;
171 
172  public:
176  ~SurfaceLock();
177 
178  SurfaceLock() = delete;
179 
184  explicit SurfaceLock(SDL_Surface *surface);
185 
190  explicit operator bool() const noexcept {
191  return status == 0;
192  }
193  };
194 
195 }
196 
Definition: GraphicsModel.cpp:20
A functor to destroy an SDL_Surface.
Definition: Surface.h:33
void operator()(SDL_Surface *sdlSurface)
Call the SDL API to free the SDL_Surface.
Definition: Surface.h:39
A wrapper class for SDL_Surface pointers.
Definition: Surface.h:50
Red Green Blue Alpha representation of a color.
Definition: Color.h:64
A helper class to wrap the SDL_LockSurface and SDL_UnlockSurface API calls.
Definition: Surface.h:167
SDL_Surface * mSurface
The surface being locked.
Definition: Surface.h:170
Abstraction of SDL_Texture.
Definition: Texture.h:46
Context
Definition: GraphicsModel.h:76
Definition: Surface.h:22
Surface(SDL_Surface *surface)
Create a Surface initialized with an SDL_Surface pointer.
Definition: Surface.h:61
A composite of a Position and a Size.
Definition: Types.h:307
int pixel(SDL_Renderer *renderer, Sint16 x, Sint16 y)
Draw pixel in currently set color.
Definition: GfxPrimitives.cpp:20
A size in integer dimensions.
Definition: Types.h:230