Skip to content
Snippets Groups Projects
Commit 9961e25c authored by Alfred Burgess's avatar Alfred Burgess
Browse files

Added texture spriting ability

parent fcccdc29
Branches
No related merge requests found
assets/texture_test.png

1.11 KiB

......@@ -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; }
......
......@@ -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;
......
......@@ -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 )
......
......@@ -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 );
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment