Aether
SDL2 based UI Framework for NX
Renderer.hpp
1 #ifndef AETHER_RENDERER_HPP
2 #define AETHER_RENDERER_HPP
3 
4 #include "Aether/types/Colour.hpp"
5 #include <atomic>
6 #include <functional>
7 #include <mutex>
8 #include <stack>
9 #include <string>
10 #include <vector>
11 
12 // Forward declare member types to reduce compilation time
13 namespace Aether {
14  class FontCache;
15  class Drawable;
16 };
17 struct SDL_Rect;
18 struct SDL_Renderer;
19 struct SDL_Surface;
20 struct SDL_Texture;
21 struct SDL_Window;
22 
23 namespace Aether {
30  class Renderer {
31  // Allow access to private members
32  friend Drawable;
33  friend FontCache;
34 
35  public:
37  typedef std::function<void(const std::string, const bool)> LogHandler;
38 
39  private:
40  SDL_Renderer * renderer;
41  SDL_Window * window;
42  unsigned int windowWidth_;
43  unsigned int windowHeight_;
45  LogHandler logHandler;
46  std::atomic<unsigned long> memoryUsage_;
47  std::atomic<unsigned int> surfaceCount_;
48  std::atomic<unsigned int> textureCount_;
50  std::stack<SDL_Rect *> clipStack;
52  FontCache * fontCache;
53  double fontSpacing;
55  std::mutex imgMtx;
56  std::mutex ttfMtx;
64  void logMessage(const std::string & msg, const bool imp);
65 
74  SDL_Texture * convertSurfaceToTexture(SDL_Surface * surf);
75 
83  SDL_Texture * createTexture(const unsigned int width, const unsigned int height);
84 
91  void destroyTexture(SDL_Texture * tex, const bool stats);
92 
99  void destroySurface(SDL_Surface * surf, const bool stats);
100 
110  void drawFilledRect(const Colour & col, const int x, const int y, const int width, const int height);
111 
126  void drawTexture(SDL_Texture * tex, const Colour & col, const int x, const int y, const unsigned int width, const unsigned int height, const int maskX, const int maskY, const unsigned int maskW, const unsigned int maskH);
127 
135  void renderOnTexture(SDL_Texture * tex, const std::function<void(SDL_Renderer *)> & func);
136 
145  SDL_Surface * scaleSurface(SDL_Surface * surface, const size_t width, const size_t height);
146 
147  public:
152  Renderer();
153 
163  void setLogHandler(const LogHandler & func);
164 
175  bool initialize(const std::string & name, const unsigned int windowW, const unsigned int windowH);
176 
181  void cleanup();
182 
189  unsigned int memoryUsage();
190 
196  unsigned int surfaceCount();
197 
203  unsigned int textureCount();
204 
210  unsigned int windowWidth();
211 
217  unsigned int windowHeight();
218 
224  void fillWindow(const Colour & col);
225 
229  void present();
230 
234  void resetClipArea();
235 
244  void setClipArea(const int x1, const int y1, const int x2, const int y2);
245 
253  void setFont(const std::string & path);
254 
261  void setFontSpacing(const double amt);
262 
272  std::pair<int, int> calculateTextDimensions(const std::string & str, const unsigned int size);
273 
284  std::tuple<std::vector<std::string>, int, int> calculateWrappedTextDimensions(const std::string & str, const unsigned int size, const unsigned int width);
285 
291  std::vector<Colour> readSurfacePixels(SDL_Surface * surface);
292 
298  std::vector<Colour> readTexturePixels(SDL_Texture * texture);
299 
309  Drawable * renderImageSurface(const std::string & path, const size_t scaleWidth, const size_t scaleHeight);
310 
320  Drawable * renderImageSurface(const std::vector<unsigned char> & data, const size_t scaleWidth, const size_t scaleHeight);
321 
329  Drawable * renderTextSurface(const std::string str, const unsigned int size);
330 
339  Drawable * renderWrappedTextSurface(const std::string str, const unsigned int size, const unsigned int width);
340 
349  Drawable * renderEllipseTexture(const unsigned int rx, const unsigned int ry, const unsigned int thick);
350 
358  Drawable * renderFilledEllipseTexture(const unsigned int rx, const unsigned int ry);
359 
368  Drawable * renderRectTexture(const int width, const int height, const unsigned int thick);
369 
377  Drawable * renderFilledRectTexture(const int width, const int height);
378 
388  Drawable * renderRoundRectTexture(const int width, const int height, const unsigned int radius, const unsigned int thick);
389 
398  Drawable * renderFilledRoundRectTexture(const int width, const int height, const unsigned int radius);
399 
404  ~Renderer();
405  };
406 };
407 
408 #endif
Drawable * renderEllipseTexture(const unsigned int rx, const unsigned int ry, const unsigned int thick)
Render an ellipse as a texture.
std::pair< int, int > calculateTextDimensions(const std::string &str, const unsigned int size)
Return the dimensions of the given string if rendered using the current font and given font size...
Caches SDL_ttf font objects + surfaces to reduce rendering. This class is not thread-safe, all operations should be protected from the outside by a mutex.
Definition: FontCache.hpp:55
Drawable * renderFilledEllipseTexture(const unsigned int rx, const unsigned int ry)
Render a filled ellipse as a texture.
void setFontSpacing(const double amt)
Set the font spacing for wrapped text. Expects a multiplier for the line height. Defaults to 1...
Drawable * renderFilledRectTexture(const int width, const int height)
Render a filled rectangle as a texture.
void setLogHandler(const LogHandler &func)
Sets the handler to be used for logging. Expects a function that is passed the log message as a strin...
std::tuple< std::vector< std::string >, int, int > calculateWrappedTextDimensions(const std::string &str, const unsigned int size, const unsigned int width)
Return the dimensions of the given string if rendered using the current font and given font size...
std::function< void(const std::string, const bool)> LogHandler
Function to handle log messages.
Definition: Renderer.hpp:37
unsigned int windowWidth()
Returns the width of the window.
bool initialize(const std::string &name, const unsigned int windowW, const unsigned int windowH)
Initialize the rendering backend.
std::vector< Colour > readTexturePixels(SDL_Texture *texture)
Returns the RGBA pixel data stored in the passed surface.
Renderer()
Initializes the renderer object, but does not initialize the actual rendering backend (SDL)...
Aether&#39;s main renderer instance. It provides all methods relating to drawing directly to the screen...
Definition: Renderer.hpp:30
void setClipArea(const int x1, const int y1, const int x2, const int y2)
Set the clipping area for rendered objects.
std::vector< Colour > readSurfacePixels(SDL_Surface *surface)
Returns the RGBA pixel data stored in the passed surface.
void cleanup()
Clean up the rendering backend. This should always be called, even if initialization failed...
~Renderer()
Deletes the renderer object, but does not clean up the backend (SDL). See cleanup().
unsigned int textureCount()
Returns the number of allocated &#39;textures&#39; that haven&#39;t been destroyed.
Stores RGBA values representing a colour. Each component can be within the range 0 to 255 (inclusive)...
Definition: Colour.hpp:9
Stores either a surface or texture which can be drawn on screen by providing a renderer to render()...
Definition: Drawable.hpp:22
void resetClipArea()
Reset the clipping area for rendered objects.
Drawable * renderRectTexture(const int width, const int height, const unsigned int thick)
Render a rectangle (outline) as a texture.
Drawable * renderTextSurface(const std::string str, const unsigned int size)
Render a UTF8 string as a surface.
Base namespace for all Aether related classes and functions.
unsigned int windowHeight()
Returns the height of the window.
unsigned int memoryUsage()
Returns an approximated value for memory used by all rendered textures.
void present()
Present all rendered graphics.
Drawable * renderImageSurface(const std::string &path, const size_t scaleWidth, const size_t scaleHeight)
Render the image at the specified path as a surface.
Drawable * renderFilledRoundRectTexture(const int width, const int height, const unsigned int radius)
Render a filled rounded rectangle as a texture.
unsigned int surfaceCount()
Returns the number of allocated &#39;surfaces&#39; that haven&#39;t been destroyed.
Drawable * renderRoundRectTexture(const int width, const int height, const unsigned int radius, const unsigned int thick)
Render an rounded rectangle (outline) as a texture.
Drawable * renderWrappedTextSurface(const std::string str, const unsigned int size, const unsigned int width)
Render a UTF8 string with automatic text wrapping as a surface.
void setFont(const std::string &path)
Set the font used for text rendering from a file. Pass an empty string to revert to the default font...
void fillWindow(const Colour &col)
Fill the window with the given colour.