From 9ac891fe71fb46e330a3ccfbbaf0728d728a2e80 Mon Sep 17 00:00:00 2001 From: Alfred Burgess <alfred.burgess95@gmail.com> Date: Sat, 9 Dec 2023 15:15:06 +0800 Subject: [PATCH] Added viewport to SDL Window class --- include/window.h | 4 ++++ src/window.cpp | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/window.h b/include/window.h index 258c1ab..3a2bdec 100644 --- a/include/window.h +++ b/include/window.h @@ -56,7 +56,11 @@ class Window SDL_Rect _drawRect( int x, int y, uint16_t width, uint16_t height ); void _drawLine( int16_t x1, int16_t y1, int16_t x2, int16_t y2 ); + // void _drawLine( Vec2 from, Vec2 to ); void _drawPoint( Vec2 point ); + // void _drawPoint( int16_t x, int16_t y ); + + SDL_Rect _createViewport( int16_t x, int16_t y, int16_t w, int16_t h ); void initialise(); void clean(); diff --git a/src/window.cpp b/src/window.cpp index efe1705..fb74e8d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -80,8 +80,8 @@ void Window::loadMedia( std::string path ) void Window::update() { SDL_SetRenderDrawColor( this->_renderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( this->_renderer ); - SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); + SDL_Rect core = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); // Render Example Primatives this->_drawRect( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); this->_drawRect( 0, 0, 10, 10 ); @@ -91,6 +91,9 @@ void Window::update() { this->_drawPoint( { SCREEN_WIDTH / 2, i } ); } + SDL_Rect overlay = this->_createViewport( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); + SDL_RenderPresent( this->_renderer ); } @@ -197,4 +200,15 @@ void Window::clean() SDL_Quit(); this->_logger.info( "Window Closed and clean" ); +} + +SDL_Rect Window::_createViewport( int16_t x, int16_t y, int16_t w, int16_t h ) +{ + SDL_Rect viewport; + viewport.x = x; + viewport.y = y; + viewport.w = w; + viewport.h = h; + SDL_RenderSetViewport( this->_renderer, &viewport ); + return viewport; } \ No newline at end of file -- GitLab