Advertisement
Kitomas

soft_rend.cpp

Dec 1st, 2023
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include <utils/soft_rend.hpp>
  2.  
  3.  
  4.  
  5.  
  6. soft_rend::soft_rend(SDL_Point windowSize,
  7.                      Uint32 windowFlags,
  8.                      SDL_Point logicalSize)
  9. {
  10.   if(windowSize.x < 256) throw "windowSize.x < 256";
  11.   if(windowSize.y < 144) throw "windowSize.y < 144";
  12.   if(windowSize.x > 1920) throw "windowSize.x > 1920";
  13.   if(windowSize.y > 1080) throw "windowSize.y > 1080";
  14.   if(logicalSize.x<1 || logicalSize.y<1) logicalSize = {256,144};
  15.  
  16.   _window = SDL_CreateWindow(nullptr,
  17.                              SDL_WINDOWPOS_UNDEFINED,
  18.                              SDL_WINDOWPOS_UNDEFINED,
  19.                              windowSize.x, windowSize.y,
  20.                              windowFlags);
  21.  
  22.   if(_window == nullptr) throw SDL_GetError();
  23.  
  24.  
  25.   _winsurf = SDL_GetWindowSurface(_window);
  26.   if(_winsurf == nullptr){
  27.     _freeWindowSafely();
  28.     throw SDL_GetError();
  29.   }
  30.  
  31.   if(_winsurf->format->format != SDL_PIXELFORMAT_RGB888){
  32.     _freeWindowSafely();
  33.     throw "window surface format != SDL_PIXELFORMAT_RGB888";
  34.   }
  35.  
  36.  
  37.   _target = SDL_CreateRGBSurfaceWithFormat(0, logicalSize.x,logicalSize.y, 32,
  38.                                            SDL_PIXELFORMAT_RGB888);
  39.   if(_target == nullptr){
  40.     _freeWindowSafely();
  41.     throw SDL_GetError();
  42.   }
  43.  
  44.  
  45.   _windowSize = windowSize;
  46.   _logicalSize = logicalSize;
  47.   _windowFlags = windowFlags;
  48.  
  49.   _windowID = SDL_GetWindowID(_window);
  50.   if(!_windowID){
  51.     _freeTargetSafely();
  52.     _freeWindowSafely();
  53.     throw SDL_GetError();
  54.   }
  55.  
  56.   if(windowFlags&SDL_WINDOW_FULLSCREEN){
  57.     _fullscreenMode = 1;
  58.     if(windowFlags&SDL_WINDOW_FULLSCREEN_DESKTOP)
  59.       _fullscreenMode = 2;
  60.   }
  61.  
  62.   _valid = true;
  63.   clear();
  64. }
  65.  
  66.  
  67.  
  68.  
  69. SDL_Point soft_rend::getFullscreenSize(){
  70.   if(!_valid) throw "invalid soft_rend object";
  71.   SDL_DisplayMode mode = {0};
  72.  
  73.   if(SDL_GetWindowDisplayMode(_window,&mode)) throw SDL_GetError();
  74.  
  75.   SDL_Point displaySize = { .x=mode.w, .y=mode.h };
  76.   return displaySize;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. void soft_rend::setWindowPosition(SDL_Point newPos){
  83.   if(!_valid) throw "invalid soft_rend object";
  84.   SDL_SetWindowPosition(_window, newPos.x,newPos.y);
  85. }
  86.  
  87.  
  88.  
  89. void soft_rend::setTitle(const std::string& newTitle){
  90.   if(!_valid) throw "invalid soft_rend object";
  91.   SDL_SetWindowTitle(_window, newTitle.c_str());
  92.   setSize(); //just in case
  93. }
  94.  
  95.  
  96.  
  97. void soft_rend::setFullscreenMode(int mode){
  98.   if(!_valid) throw "invalid soft_rend object";
  99.  
  100.   Uint32 flags;
  101.   if(     mode == 0) flags = 0;
  102.   else if(mode == 1) flags = SDL_WINDOW_FULLSCREEN;
  103.   else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP; //windowed borderless
  104.   else               throw "invalid mode";
  105.  
  106.   if(SDL_SetWindowFullscreen(_window,flags)) throw SDL_GetError();
  107.   _fullscreenMode = mode;
  108.  
  109.   SDL_GetWindowSize(_window, &_windowSize.x, &_windowSize.y);
  110.   setSize(); //just in case
  111. }
  112.  
  113.  
  114.  
  115. void soft_rend::setSize(SDL_Point windowSize, SDL_Point logicalSize){
  116.   if(!_valid) throw "invalid soft_rend object";
  117.   if(windowSize.x<1 || windowSize.y<1) windowSize = _windowSize;
  118.   if(logicalSize.x<1 || logicalSize.y<1) logicalSize = _logicalSize;
  119.  
  120.   SDL_SetWindowSize(_window, windowSize.x, windowSize.y);
  121.   _winsurf = SDL_GetWindowSurface(_window);
  122.  
  123.   _freeTargetSafely();
  124.   _target = SDL_CreateRGBSurfaceWithFormat(0,logicalSize.x,logicalSize.y,32,
  125.                                            SDL_PIXELFORMAT_RGB888);
  126.   if(_target == nullptr){
  127.     _freeWindowSafely();
  128.     _valid = false;
  129.     throw SDL_GetError();
  130.   }
  131.  
  132.   _windowSize = windowSize;
  133.   _logicalSize = logicalSize;
  134. }
  135.  
  136.  
  137.  
  138. void soft_rend::setLock(bool lockState){
  139.   if(!_valid) throw "invalid soft_rend object";
  140.   if(_target == nullptr) throw "target surface = nullptr";
  141.   if(SDL_MUSTLOCK(_target)){
  142.     if(lockState){
  143.       SDL_LockSurface(_target);
  144.       ++_numLocks;
  145.     } else if(_numLocks > 0){
  146.       SDL_UnlockSurface(_target);
  147.       --_numLocks;
  148.     }
  149.   }
  150. }
  151.  
  152.  
  153.  
  154.  
  155. void soft_rend::present(){
  156.   if(!_valid) throw "invalid soft_rend object";
  157.  
  158.   if(SDL_BlitScaled(_target,nullptr,_winsurf,nullptr)) throw SDL_GetError();
  159.   if(SDL_UpdateWindowSurface(_window)) throw SDL_GetError();
  160. }
  161.  
  162.  
  163.  
  164. void soft_rend::clear(soft_color fillColor){
  165.   setLock(true);
  166.  
  167.   size_t width  = _logicalSize.x;
  168.   size_t height = _logicalSize.y;
  169.   size_t length = width * height;
  170.   soft_color* pixels = (soft_color*)_target->pixels;
  171.   if(fillColor.v == UNKNOWN_COLOR) fillColor = _drawColor;
  172.  
  173.   for(size_t p=0; p<length; ++p) pixels[p].v = fillColor.v;
  174.  
  175.   _edges_completely_filled = false;
  176.   for(size_t row=0; row<height; ++row){
  177.     _edge_start[row]  = width;
  178.     _edge_end[row]    = -1;
  179.     _edge_filled[row] = false;
  180.   }
  181.  
  182.   setLock(false);
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement