Advertisement
Kitomas

renderer.cpp as of 2023-11-16

Nov 17th, 2023
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <utils/renderer.hpp>
  2.  
  3.  
  4.  
  5.  
  6. renderer_class::renderer_class(SDL_Point windowSize,
  7.                                Uint32 windowFlags,
  8.                                SDL_Point textureSize,
  9.                                SDL_Window* window)
  10. {
  11.   //set window, creating one if it isn't provided
  12.   _window = window;
  13.   _windowSize = windowSize;
  14.   _windowFlags = windowFlags;
  15.  
  16.   if(_window == nullptr){
  17.     _window = SDL_CreateWindow(nullptr,
  18.                                SDL_WINDOWPOS_UNDEFINED,
  19.                                SDL_WINDOWPOS_UNDEFINED,
  20.                                windowSize.x, windowSize.y,
  21.                                windowFlags);
  22.  
  23.     if(_window == nullptr) throw SDL_GetError();
  24.     _windowCreated = SDL_TRUE;
  25.   }
  26.  
  27.   _windowID = SDL_GetWindowID(_window);
  28.   if(!_windowID){
  29.     _freeWindowSafely();
  30.     throw SDL_GetError();
  31.   }
  32.  
  33.  
  34.   //create renderer
  35.   Uint32 useTexture = 0;
  36.   if(textureSize.x>0 && textureSize.y>0)
  37.     useTexture = SDL_RENDERER_TARGETTEXTURE;
  38.  
  39.   _renderer = SDL_CreateRenderer(_window, -1, useTexture);
  40.   if(_renderer == nullptr){
  41.     _freeWindowSafely();
  42.     throw SDL_GetError();
  43.   }
  44.  
  45.   if(SDL_SetRenderDrawColor(_renderer,0,0,0,255)){
  46.     _freeRendererSafely();
  47.     _freeWindowSafely();
  48.     throw SDL_GetError();
  49.   }
  50.  
  51.  
  52.   //render to texture if desired
  53.   _logicalSize = (useTexture) ? textureSize : windowSize;
  54.   if(useTexture){
  55.     _texture = SDL_CreateTexture(_renderer, 0,
  56.                                  SDL_TEXTUREACCESS_TARGET,
  57.                                  textureSize.x, textureSize.y);
  58.  
  59.     if(_texture == nullptr ||
  60.        SDL_SetRenderTarget(_renderer,_texture))
  61.     {
  62.       _freeRendererSafely();
  63.       _freeWindowSafely();
  64.       throw SDL_GetError();
  65.     }
  66.   }
  67.  
  68.   _valid = SDL_TRUE;
  69. }
  70.  
  71.  
  72.  
  73.  
  74. SDL_Point renderer_class::getFullscreenSize(){
  75.   if(!_valid) throw "invalid renderer";
  76.   SDL_DisplayMode mode = {0};
  77.  
  78.   if(SDL_GetWindowDisplayMode(_window,&mode)) throw SDL_GetError();
  79.  
  80.   SDL_Point displaySize = { .x=mode.w, .y=mode.h };
  81.   return displaySize;
  82. }
  83.  
  84.  
  85.  
  86.  
  87. void renderer_class::setFullscreen(int mode){
  88.   if(!_valid) throw "invalid renderer";
  89.  
  90.   Uint32 flags;
  91.   if(     mode == 0) flags = 0;
  92.   else if(mode == 1) flags = SDL_WINDOW_FULLSCREEN;
  93.   else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP; //windowed borderless
  94.   else               throw "invalid mode";
  95.  
  96.   if(SDL_SetWindowFullscreen(_window,flags)) throw SDL_GetError();
  97.  
  98.   _fullscreenMode = mode;
  99. }
  100.  
  101.  
  102.  
  103. void renderer_class::setSize(SDL_Point windowSize, SDL_Point logicalSize){
  104.   if(!_valid) throw "invalid renderer";
  105.   if(windowSize.x<1 || windowSize.y<1) windowSize = _windowSize;
  106.   if(logicalSize.x<1 || logicalSize.y<1) logicalSize = _logicalSize;
  107.  
  108.   SDL_SetWindowSize(_window, windowSize.x, windowSize.y);
  109.   if(_texture == nullptr){
  110.     if(SDL_RenderSetLogicalSize(_renderer, logicalSize.x, logicalSize.y)) throw SDL_GetError();
  111.  
  112.   } else {
  113.     SDL_DestroyTexture(_texture);
  114.  
  115.     _texture = SDL_CreateTexture(_renderer, 0, SDL_TEXTUREACCESS_TARGET,
  116.                                  logicalSize.x, logicalSize.y);
  117.     if(_texture == nullptr ||
  118.        SDL_SetRenderTarget(_renderer,_texture))
  119.     {
  120.       throw SDL_GetError();
  121.     }
  122.   }
  123.  
  124.   _windowSize = windowSize;
  125.   _logicalSize = logicalSize;
  126. }
  127.  
  128.  
  129.  
  130. void renderer_class::setTarget(SDL_Texture* target){
  131.   if(!_valid) throw "invalid renderer";
  132.   if(SDL_SetRenderTarget(_renderer,target)) throw SDL_GetError();
  133.   _target = target;
  134. }
  135.  
  136.  
  137.  
  138. void renderer_class::setDrawBlendMode(SDL_BlendMode blendMode){
  139.   if(!_valid) throw "invalid renderer";
  140.   if(SDL_SetRenderDrawBlendMode(_renderer,blendMode)) throw SDL_GetError();
  141. }
  142.  
  143.  
  144.  
  145. void renderer_class::setDrawColor(SDL_Color newColor){
  146.   if(!_valid) throw "invalid renderer";
  147.   if(SDL_SetRenderDrawColor(_renderer,
  148.                             newColor.r, newColor.g,
  149.                             newColor.b, newColor.a))
  150.   {
  151.     throw SDL_GetError();
  152.   }
  153. }
  154.  
  155.  
  156.  
  157.  
  158. void renderer_class::present(){
  159.   if(!_valid) throw "invalid renderer";
  160.  
  161.   if(_texture != nullptr){
  162.     if(SDL_SetRenderTarget(_renderer,_target)) throw SDL_GetError();
  163.  
  164.     if(SDL_RenderCopy(_renderer,_texture,nullptr,nullptr)) throw SDL_GetError();
  165.  
  166.     if(SDL_SetRenderTarget(_renderer,_texture)) throw SDL_GetError();
  167.   }
  168.  
  169.   SDL_RenderPresent(_renderer);
  170. }
  171.  
  172.  
  173.  
  174. void renderer_class::clear(){
  175.   if(!_valid) throw "invalid renderer";
  176.   if(SDL_RenderClear(_renderer)) throw SDL_GetError();
  177. }
  178.  
  179.  
  180.  
  181.  
  182. void renderer_class::copy(SDL_Texture* texture,
  183.                           const SDL_Rect* srcRect,
  184.                           const SDL_Rect* dstRect)
  185. {
  186.   if(!_valid) throw "invalid renderer";
  187.   if(SDL_RenderCopy(_renderer, texture,
  188.                     srcRect, dstRect))
  189.   {
  190.     throw SDL_GetError();
  191.   }
  192. }
  193.  
  194.  
  195.  
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement