diff --git a/assets/texture_test.png b/assets/texture_test.png new file mode 100644 index 0000000000000000000000000000000000000000..597523658634a02502ad20caef19789358a0b5c2 Binary files /dev/null and b/assets/texture_test.png differ diff --git a/include/components/cTexture.h b/include/components/cTexture.h index 22c9b233b114da97e15f58957c85275cbebbc823..f86b6c69097527fb8bf9720b2289262aff208fec 100644 --- a/include/components/cTexture.h +++ b/include/components/cTexture.h @@ -18,7 +18,7 @@ class cTexture : public CComponentGeneric bool loadFromFile( std::string path ); void free(); void render( Vec2 pt ); - void render( Vec2 pt, SDL_Renderer *renderer ); + void render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip = NULL ); int getWidth(){ return this->_width; } int getHeight(){ return this->_height; } diff --git a/include/window.h b/include/window.h index 9c99ba2a06a304b3d1b41333396abb58958e3de4..79707801866482a93e65620d1c0f43aee1de44f1 100644 --- a/include/window.h +++ b/include/window.h @@ -55,6 +55,9 @@ class Window SDL_Texture* _activeTexture; cTexture* _background; cTexture* _character; + cTexture* _textureSprite; + SDL_Rect _sprites[4]; + SDL_Renderer* _renderer; SDL_Surface* _screenSurface; SDL_Surface* _renderSurface; diff --git a/src/components/cTexture.cpp b/src/components/cTexture.cpp index c5619049212c7828702825b1a20548af588a3f2e..8034cafbb0a32308585976f3ea3f88a7c30dd2f8 100644 --- a/src/components/cTexture.cpp +++ b/src/components/cTexture.cpp @@ -43,12 +43,17 @@ void cTexture::free() } } -void cTexture::render( Vec2 pt, SDL_Renderer *renderer ) +void cTexture::render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip ) { if( this->_width && this->_height ) { SDL_Rect quad = { (int)pt.x, (int)pt.y, this->_width, this->_height }; - SDL_RenderCopy( renderer, this->_texture, NULL, &quad ); + if( clip != NULL ) + { + quad.w = clip->w; + quad.h = clip->h; + } + SDL_RenderCopy( renderer, this->_texture, clip, &quad ); } } void cTexture::render( Vec2 pt ) diff --git a/src/window.cpp b/src/window.cpp index 118b7284e4c0704c4cd179cd82be9bdc065df369..810c82c41ab2cce93430f72035c7ba756abeb2cd 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -12,6 +12,7 @@ Window::Window() this->_renderSurface = nullptr; this->_background = new cTexture(); this->_character = new cTexture(); + this->_textureSprite = new cTexture(); } Window::~Window() { @@ -79,6 +80,32 @@ void Window::loadMedia() { this->_background->loadFromFile( "assets/background.png" ); this->_character->loadFromFile( "assets/person.png" ); + if( this->_textureSprite->loadFromFile( "assets/texture_test.png" ) ) + { + //Set top left sprite + this->_sprites[ 0 ].x = 0; + this->_sprites[ 0 ].y = 0; + this->_sprites[ 0 ].w = 100; + this->_sprites[ 0 ].h = 100; + + //Set top right sprite + this->_sprites[ 1 ].x = 100; + this->_sprites[ 1 ].y = 0; + this->_sprites[ 1 ].w = 100; + this->_sprites[ 1 ].h = 100; + + //Set bottom left sprite + this->_sprites[ 2 ].x = 0; + this->_sprites[ 2 ].y = 100; + this->_sprites[ 2 ].w = 100; + this->_sprites[ 2 ].h = 100; + + //Set bottom right sprite + this->_sprites[ 3 ].x = 100; + this->_sprites[ 3 ].y = 100; + this->_sprites[ 3 ].w = 100; + this->_sprites[ 3 ].h = 100; + } } void Window::loadMedia( std::string path ) { @@ -93,6 +120,18 @@ void Window::update() { SDL_Rect bg = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); this->_background->render( {0,0}, this->_renderer ); this->_character->render( {SCREEN_HEIGHT/3,SCREEN_HEIGHT/3}, this->_renderer ); + // this->_textureSprite->render( {0,0}, this->_renderer ); + + //Render top left sprite + this->_textureSprite->render( {0, 0}, this->_renderer, &this->_sprites[ 0 ] ); + //Render top right sprite + this->_textureSprite->render( {SCREEN_WIDTH - this->_sprites[ 1 ].w, 0}, this->_renderer, &this->_sprites[ 1 ] ); + //Render bottom left sprite + this->_textureSprite->render( {0, SCREEN_HEIGHT - this->_sprites[ 2 ].h}, this->_renderer, &this->_sprites[ 2 ] ); + //Render bottom right sprite + this->_textureSprite->render( {SCREEN_WIDTH - this->_sprites[ 3 ].w, SCREEN_HEIGHT - this->_sprites[ 3 ].h}, this->_renderer, &this->_sprites[ 3 ] ); + + SDL_Rect core = this->_createViewport( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); @@ -121,6 +160,7 @@ void Window::update() { } } + // SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); @@ -237,6 +277,7 @@ void Window::clean() this->_activeTexture = nullptr; delete this->_background; delete this->_character; + delete this->_textureSprite; //Destroy window SDL_DestroyRenderer( this->_renderer );