Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utils/soft_rend.hpp>
- soft_rend::soft_rend(SDL_Point windowSize,
- Uint32 windowFlags,
- SDL_Point logicalSize)
- {
- if(windowSize.x < 256) throw "windowSize.x < 256";
- if(windowSize.y < 144) throw "windowSize.y < 144";
- if(windowSize.x > 1920) throw "windowSize.x > 1920";
- if(windowSize.y > 1080) throw "windowSize.y > 1080";
- if(logicalSize.x<1 || logicalSize.y<1) logicalSize = {256,144};
- _window = SDL_CreateWindow(nullptr,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- windowSize.x, windowSize.y,
- windowFlags);
- if(_window == nullptr) throw SDL_GetError();
- _winsurf = SDL_GetWindowSurface(_window);
- if(_winsurf == nullptr){
- _freeWindowSafely();
- throw SDL_GetError();
- }
- if(_winsurf->format->format != SDL_PIXELFORMAT_RGB888){
- _freeWindowSafely();
- throw "window surface format != SDL_PIXELFORMAT_RGB888";
- }
- _target = SDL_CreateRGBSurfaceWithFormat(0, logicalSize.x,logicalSize.y, 32,
- SDL_PIXELFORMAT_RGB888);
- if(_target == nullptr){
- _freeWindowSafely();
- throw SDL_GetError();
- }
- _windowSize = windowSize;
- _logicalSize = logicalSize;
- _windowFlags = windowFlags;
- _windowID = SDL_GetWindowID(_window);
- if(!_windowID){
- _freeTargetSafely();
- _freeWindowSafely();
- throw SDL_GetError();
- }
- if(windowFlags&SDL_WINDOW_FULLSCREEN){
- _fullscreenMode = 1;
- if(windowFlags&SDL_WINDOW_FULLSCREEN_DESKTOP)
- _fullscreenMode = 2;
- }
- _valid = true;
- clear();
- }
- SDL_Point soft_rend::getFullscreenSize(){
- if(!_valid) throw "invalid soft_rend object";
- SDL_DisplayMode mode = {0};
- if(SDL_GetWindowDisplayMode(_window,&mode)) throw SDL_GetError();
- SDL_Point displaySize = { .x=mode.w, .y=mode.h };
- return displaySize;
- }
- void soft_rend::setWindowPosition(SDL_Point newPos){
- if(!_valid) throw "invalid soft_rend object";
- SDL_SetWindowPosition(_window, newPos.x,newPos.y);
- }
- void soft_rend::setTitle(const std::string& newTitle){
- if(!_valid) throw "invalid soft_rend object";
- SDL_SetWindowTitle(_window, newTitle.c_str());
- setSize(); //just in case
- }
- void soft_rend::setFullscreenMode(int mode){
- if(!_valid) throw "invalid soft_rend object";
- Uint32 flags;
- if( mode == 0) flags = 0;
- else if(mode == 1) flags = SDL_WINDOW_FULLSCREEN;
- else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP; //windowed borderless
- else throw "invalid mode";
- if(SDL_SetWindowFullscreen(_window,flags)) throw SDL_GetError();
- _fullscreenMode = mode;
- SDL_GetWindowSize(_window, &_windowSize.x, &_windowSize.y);
- setSize(); //just in case
- }
- void soft_rend::setSize(SDL_Point windowSize, SDL_Point logicalSize){
- if(!_valid) throw "invalid soft_rend object";
- if(windowSize.x<1 || windowSize.y<1) windowSize = _windowSize;
- if(logicalSize.x<1 || logicalSize.y<1) logicalSize = _logicalSize;
- SDL_SetWindowSize(_window, windowSize.x, windowSize.y);
- _winsurf = SDL_GetWindowSurface(_window);
- _freeTargetSafely();
- _target = SDL_CreateRGBSurfaceWithFormat(0,logicalSize.x,logicalSize.y,32,
- SDL_PIXELFORMAT_RGB888);
- if(_target == nullptr){
- _freeWindowSafely();
- _valid = false;
- throw SDL_GetError();
- }
- _windowSize = windowSize;
- _logicalSize = logicalSize;
- }
- void soft_rend::setLock(bool lockState){
- if(!_valid) throw "invalid soft_rend object";
- if(_target == nullptr) throw "target surface = nullptr";
- if(SDL_MUSTLOCK(_target)){
- if(lockState){
- SDL_LockSurface(_target);
- ++_numLocks;
- } else if(_numLocks > 0){
- SDL_UnlockSurface(_target);
- --_numLocks;
- }
- }
- }
- void soft_rend::present(){
- if(!_valid) throw "invalid soft_rend object";
- if(SDL_BlitScaled(_target,nullptr,_winsurf,nullptr)) throw SDL_GetError();
- if(SDL_UpdateWindowSurface(_window)) throw SDL_GetError();
- }
- void soft_rend::clear(soft_color fillColor){
- setLock(true);
- size_t width = _logicalSize.x;
- size_t height = _logicalSize.y;
- size_t length = width * height;
- soft_color* pixels = (soft_color*)_target->pixels;
- if(fillColor.v == UNKNOWN_COLOR) fillColor = _drawColor;
- for(size_t p=0; p<length; ++p) pixels[p].v = fillColor.v;
- _edges_completely_filled = false;
- for(size_t row=0; row<height; ++row){
- _edge_start[row] = width;
- _edge_end[row] = -1;
- _edge_filled[row] = false;
- }
- setLock(false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement