From 9961e25c00590ef5c7bea2c8f9769a392248b68a Mon Sep 17 00:00:00 2001
From: Alfred Burgess <alfred.burgess95@gmail.com>
Date: Sun, 10 Dec 2023 08:07:11 +0800
Subject: [PATCH] Added texture spriting ability

---
 assets/texture_test.png       | Bin 0 -> 1136 bytes
 include/components/cTexture.h |   2 +-
 include/window.h              |   3 +++
 src/components/cTexture.cpp   |   9 ++++++--
 src/window.cpp                |  41 ++++++++++++++++++++++++++++++++++
 5 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 assets/texture_test.png

diff --git a/assets/texture_test.png b/assets/texture_test.png
new file mode 100644
index 0000000000000000000000000000000000000000..597523658634a02502ad20caef19789358a0b5c2
GIT binary patch
literal 1136
zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k2}mkgS)K$^jKx9jP7LeL$-D$|SkfJR9T^xl
z_H+M9WCij$3p^r=85sBugD~Uq{1qucHLRX4jv*CsZ!amXn~*NzdeL0KV2M~KTW5w=
z)`YpvlhX@(7>;X1T@DF1eG<tQ5F*G?z`<0Xd)4ld>ih`vYzFn6zn?vBmgg%eF8=&=
z{`ciO!)$;3J$^Z6^}ioqzGt?W3WTYzY209y$lG$2Gh1PeUj%bv=^-hO)xy^tBIa&j
zJM?M@Dk)!E^iJ;G`T6lOFQmSGeE2qVU8w!uUr$Z0uR6Zp)~a`i`U-bE-Q9X>XZ4Q!
z`Tk$-Z&?4{uXt_MuRrgAzOA>fu{`_VzKUb$M=6Tu-(A=+XZQ5#-`6KI#~$9d*X~^D
w{(Ji@&u)En^Yre^9K$6Q!@L~2ch7G=DSglGuyZO=z*2(2)78&qol`;+0GS`pMF0Q*

literal 0
HcmV?d00001

diff --git a/include/components/cTexture.h b/include/components/cTexture.h
index 22c9b23..f86b6c6 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 9c99ba2..7970780 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 c561904..8034caf 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 118b728..810c82c 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 );
-- 
GitLab