Advertisement
Kitomas

renderer.hpp as of 2023-11-16

Nov 17th, 2023
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #ifndef _UTILS_RENDERER_HPP
  2. #define _UTILS_RENDERER_HPP
  3.  
  4.  
  5. #include <SDL2/SDL.h>
  6.  
  7.  
  8.  
  9. #define RENDERER_WINDOWED   (0)
  10. #define RENDERER_FULLSCREEN (1)
  11. #define RENDERER_BORDERLESS (2)
  12.  
  13. class renderer_class {
  14.   SDL_bool _valid = SDL_FALSE;
  15.  
  16.   //'was _window created by constructor?'
  17.   SDL_bool _windowCreated = SDL_FALSE;
  18.  
  19.   SDL_Window*   _window    = nullptr;
  20.   SDL_Renderer* _renderer  = nullptr;
  21.   SDL_Texture*  _texture   = nullptr;
  22.   SDL_Texture*  _target    = nullptr; //user-defined render target
  23.  
  24.   SDL_Point _windowSize;
  25.   SDL_Point _logicalSize = {-1,-1};
  26.  
  27.   Uint32 _windowID = 0;
  28.   Uint32 _windowFlags;
  29.   int _fullscreenMode = 0;
  30.  
  31.  
  32.   void _freeWindowSafely(){
  33.     if(_window!=nullptr && _windowCreated){
  34.       SDL_DestroyWindow(_window);
  35.       _window = nullptr;
  36.     }
  37.   }
  38.  
  39.   void _freeRendererSafely(){
  40.     if(_renderer != nullptr){
  41.       SDL_DestroyRenderer(_renderer);
  42.       _renderer = nullptr;
  43.     }
  44.   }
  45.  
  46.   void _freeTextureSafely(){
  47.     if(_texture != nullptr){
  48.       SDL_DestroyTexture(_texture);
  49.       _texture = nullptr;
  50.     }
  51.   }
  52.  
  53.  
  54. public:
  55.   SDL_bool isValid(){ return _valid; }
  56.  
  57.   renderer_class(){}
  58.   renderer_class(SDL_Point windowSize,
  59.                  Uint32 windowFlags = 0,
  60.                  SDL_Point textureSize = {-1,-1},
  61.                  SDL_Window* window = nullptr);
  62.  
  63.   ~renderer_class(){
  64.     _freeTextureSafely();
  65.     _freeRendererSafely();
  66.     _freeWindowSafely();
  67.     _valid = SDL_FALSE;
  68.   }
  69.  
  70.  
  71.   SDL_Window* getWindow(){ return _window; }
  72.   SDL_Renderer* getRenderer(){ return _renderer; }
  73.   SDL_Texture* getTexture(){ return _texture; }
  74.   SDL_Texture* getTarget(){ return _target; }
  75.  
  76.   SDL_Point getWindowSize(){ return _windowSize; }
  77.   SDL_Point getRendererSize(){ return _logicalSize; }
  78.   SDL_Point getFullscreenSize();
  79.  
  80.   Uint32 getWindowID(){ return _windowID; }
  81.   Uint32 getWindowFlags(){ return _windowFlags; }
  82.   int getFullscreenMode(){ return _fullscreenMode; }
  83.  
  84.  
  85.   void setFullscreen(int mode); //0,1,2 = windowed, fullscreen, windowed borderless
  86.   void setSize(SDL_Point windowSize = {-1,-1}, SDL_Point logicalSize = {-1,-1});
  87.   void setTarget(SDL_Texture* target = nullptr);
  88.   void setDrawBlendMode(SDL_BlendMode blendMode = SDL_BLENDMODE_NONE);
  89.   void setDrawColor(SDL_Color newColor = {0,0,0,255});
  90.  
  91.   void present();
  92.   void clear();
  93.  
  94.   void copy(SDL_Texture* texture,
  95.             const SDL_Rect* srcRect = nullptr,
  96.             const SDL_Rect* dstRect = nullptr);
  97. };
  98.  
  99.  
  100.  
  101.  
  102. #endif /* _UTILS_RENDERER_HPP */
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement