Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utils/renderer.hpp>
- renderer_class::renderer_class(SDL_Point windowSize,
- Uint32 windowFlags,
- SDL_Point textureSize,
- SDL_Window* window)
- {
- //set window, creating one if it isn't provided
- _window = window;
- _windowSize = windowSize;
- _windowFlags = windowFlags;
- if(_window == nullptr){
- _window = SDL_CreateWindow(nullptr,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- windowSize.x, windowSize.y,
- windowFlags);
- if(_window == nullptr) throw SDL_GetError();
- _windowCreated = SDL_TRUE;
- }
- _windowID = SDL_GetWindowID(_window);
- if(!_windowID){
- _freeWindowSafely();
- throw SDL_GetError();
- }
- //create renderer
- Uint32 useTexture = 0;
- if(textureSize.x>0 && textureSize.y>0)
- useTexture = SDL_RENDERER_TARGETTEXTURE;
- _renderer = SDL_CreateRenderer(_window, -1, useTexture);
- if(_renderer == nullptr){
- _freeWindowSafely();
- throw SDL_GetError();
- }
- if(SDL_SetRenderDrawColor(_renderer,0,0,0,255)){
- _freeRendererSafely();
- _freeWindowSafely();
- throw SDL_GetError();
- }
- //render to texture if desired
- _logicalSize = (useTexture) ? textureSize : windowSize;
- if(useTexture){
- _texture = SDL_CreateTexture(_renderer, 0,
- SDL_TEXTUREACCESS_TARGET,
- textureSize.x, textureSize.y);
- if(_texture == nullptr ||
- SDL_SetRenderTarget(_renderer,_texture))
- {
- _freeRendererSafely();
- _freeWindowSafely();
- throw SDL_GetError();
- }
- }
- _valid = SDL_TRUE;
- }
- SDL_Point renderer_class::getFullscreenSize(){
- if(!_valid) throw "invalid renderer";
- SDL_DisplayMode mode = {0};
- if(SDL_GetWindowDisplayMode(_window,&mode)) throw SDL_GetError();
- SDL_Point displaySize = { .x=mode.w, .y=mode.h };
- return displaySize;
- }
- void renderer_class::setFullscreen(int mode){
- if(!_valid) throw "invalid renderer";
- 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;
- }
- void renderer_class::setSize(SDL_Point windowSize, SDL_Point logicalSize){
- if(!_valid) throw "invalid renderer";
- 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);
- if(_texture == nullptr){
- if(SDL_RenderSetLogicalSize(_renderer, logicalSize.x, logicalSize.y)) throw SDL_GetError();
- } else {
- SDL_DestroyTexture(_texture);
- _texture = SDL_CreateTexture(_renderer, 0, SDL_TEXTUREACCESS_TARGET,
- logicalSize.x, logicalSize.y);
- if(_texture == nullptr ||
- SDL_SetRenderTarget(_renderer,_texture))
- {
- throw SDL_GetError();
- }
- }
- _windowSize = windowSize;
- _logicalSize = logicalSize;
- }
- void renderer_class::setTarget(SDL_Texture* target){
- if(!_valid) throw "invalid renderer";
- if(SDL_SetRenderTarget(_renderer,target)) throw SDL_GetError();
- _target = target;
- }
- void renderer_class::setDrawBlendMode(SDL_BlendMode blendMode){
- if(!_valid) throw "invalid renderer";
- if(SDL_SetRenderDrawBlendMode(_renderer,blendMode)) throw SDL_GetError();
- }
- void renderer_class::setDrawColor(SDL_Color newColor){
- if(!_valid) throw "invalid renderer";
- if(SDL_SetRenderDrawColor(_renderer,
- newColor.r, newColor.g,
- newColor.b, newColor.a))
- {
- throw SDL_GetError();
- }
- }
- void renderer_class::present(){
- if(!_valid) throw "invalid renderer";
- if(_texture != nullptr){
- if(SDL_SetRenderTarget(_renderer,_target)) throw SDL_GetError();
- if(SDL_RenderCopy(_renderer,_texture,nullptr,nullptr)) throw SDL_GetError();
- if(SDL_SetRenderTarget(_renderer,_texture)) throw SDL_GetError();
- }
- SDL_RenderPresent(_renderer);
- }
- void renderer_class::clear(){
- if(!_valid) throw "invalid renderer";
- if(SDL_RenderClear(_renderer)) throw SDL_GetError();
- }
- void renderer_class::copy(SDL_Texture* texture,
- const SDL_Rect* srcRect,
- const SDL_Rect* dstRect)
- {
- if(!_valid) throw "invalid renderer";
- if(SDL_RenderCopy(_renderer, texture,
- srcRect, dstRect))
- {
- throw SDL_GetError();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement