diff --git a/assets/animated.png b/assets/animated.png new file mode 100644 index 0000000000000000000000000000000000000000..52206b2bc829ef4384f926d6b508ad7750be6b86 Binary files /dev/null and b/assets/animated.png differ diff --git a/assets/texture_test.png b/assets/texture_test.png index 597523658634a02502ad20caef19789358a0b5c2..52206b2bc829ef4384f926d6b508ad7750be6b86 100644 Binary files a/assets/texture_test.png and b/assets/texture_test.png differ diff --git a/assets/water_animated.png b/assets/water_animated.png new file mode 100644 index 0000000000000000000000000000000000000000..26d2c1cca00109f32fc3874081af9dbffc8a5b7c Binary files /dev/null and b/assets/water_animated.png differ diff --git a/include/colour.h b/include/colour.h index 8f1f23ca34a55ed34dd8408f0357ee9d4a865763..80e875f66bc593955f419b5988ddecb225fcbaea 100644 --- a/include/colour.h +++ b/include/colour.h @@ -13,8 +13,14 @@ struct RGBA{ class Colour { public: - Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a ); + Colour(); + Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255 ); ~Colour(); + + void brighten( uint8_t delta ); + void darken( uint8_t delta ); + + RGBA getRGBA(); private: RGBA _col; diff --git a/include/components/cTexture.h b/include/components/cTexture.h index f86b6c69097527fb8bf9720b2289262aff208fec..0afd96cd69b2b89a8c3887323a8cb58acf4fb3da 100644 --- a/include/components/cTexture.h +++ b/include/components/cTexture.h @@ -6,6 +6,7 @@ #include <string> #include "components/components-generic.h" #include "window.h" +#include "colour.h" #include "maths/vec2.h" @@ -18,10 +19,16 @@ class cTexture : public CComponentGeneric bool loadFromFile( std::string path ); void free(); void render( Vec2 pt ); - void render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip = NULL ); + void render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect *clip = NULL ); int getWidth(){ return this->_width; } int getHeight(){ return this->_height; } + + void setColour( Colour colour ); + void setColour( uint8_t red, uint8_t green, uint8_t blue ); + + void setBlendMode( SDL_BlendMode blending ); + void setAlpha( uint8_t alpha ); private: SDL_Texture* _texture; uint16_t _width; diff --git a/include/window.h b/include/window.h index 75c3b8539cf00c47bb9c57b567530da5546adf7a..bf23aa48b1a02ed840d20ca37a59fd309b348f24 100644 --- a/include/window.h +++ b/include/window.h @@ -56,11 +56,17 @@ class Window Logger& _logger; SDL_Window* _window; SDL_Texture* _activeTexture; + + Colour _screenModulation; cTexture* _background; cTexture* _character; cTexture* _textureSprite; SDL_Rect _sprites[4]; + uint8_t _alpha = 0xff; + cTexture* _water; + const uint8_t _waterFrames = 4; + SDL_Rect _waterTicks[4]; SDL_Renderer* _renderer; SDL_Surface* _screenSurface; @@ -68,7 +74,7 @@ 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 _drawLine( Vec2 from, Vec2 to ); void _drawPoint( Vec2 point ); // void _drawPoint( int16_t x, int16_t y ); void _drawHex( Vec2 point, uint16_t radius ); diff --git a/src/colour.cpp b/src/colour.cpp index 75bf78dcb175b04a623ba259c39120da89710e9a..7a7f0a7bf2932046984ba421d975690563f74041 100644 --- a/src/colour.cpp +++ b/src/colour.cpp @@ -1,7 +1,24 @@ #include "colour.h" -Colour::Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a ) +Colour::Colour( ) + : _col( {0x00, 0x00, 0x00, 0x00} ) +{} +Colour::Colour( uint8_t r, uint8_t g, uint8_t b, uint8_t a ) : _col( {r,g,b,a} ) {} Colour::~Colour() -{} \ No newline at end of file +{} + +RGBA Colour::getRGBA() { return this->_col; } +void Colour::brighten( uint8_t delta ) +{ + if( this->_col.r < 0xFF ) this->_col.r += delta; + if( this->_col.g < 0xFF ) this->_col.g += delta; + if( this->_col.b < 0xFF ) this->_col.b += delta; +} +void Colour::darken( uint8_t delta ) +{ + if( this->_col.r > 0 ) this->_col.r -= delta; + if( this->_col.g > 0 ) this->_col.g -= delta; + if( this->_col.b > 0 ) this->_col.b -= delta; +} \ No newline at end of file diff --git a/src/components/cTexture.cpp b/src/components/cTexture.cpp index 8034cafbb0a32308585976f3ea3f88a7c30dd2f8..499ae0fab7be66decf5fe096a12c9aef173bb34b 100644 --- a/src/components/cTexture.cpp +++ b/src/components/cTexture.cpp @@ -30,6 +30,7 @@ bool cTexture::loadFromFile( std::string path ) SDL_FreeSurface( surface ); } this->_texture = texture; + this->setBlendMode( SDL_BLENDMODE_BLEND ); return texture != NULL; } @@ -43,6 +44,12 @@ void cTexture::free() } } +void cTexture::setColour( Colour colour ) { this->setColour( colour.getRGBA().r, colour.getRGBA().g, colour.getRGBA().b ); } +void cTexture::setColour( uint8_t red, uint8_t green, uint8_t blue ) { SDL_SetTextureColorMod( this->_texture, red, green, blue ); } + +void cTexture::setBlendMode( SDL_BlendMode blending ) { SDL_SetTextureBlendMode( this->_texture, blending ); } +void cTexture::setAlpha( uint8_t alpha ) { SDL_SetTextureAlphaMod( this->_texture, alpha ); } + void cTexture::render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip ) { if( this->_width && this->_height ) diff --git a/src/window.cpp b/src/window.cpp index 82652ec01e72b1c1e2c1d6872767d4a3393120c4..b623414acfbf74033f9978b692e10faff791e8e4 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -14,7 +14,10 @@ Window::Window() this->_renderSurface = nullptr; this->_background = new cTexture(); this->_character = new cTexture(); + this->_water = new cTexture(); this->_textureSprite = new cTexture(); + + this->_screenModulation = Colour( 0x88, 0x88, 0x88, 0xFF ); } Window::~Window() { @@ -82,6 +85,32 @@ void Window::loadMedia() { this->_background->loadFromFile( "assets/background.png" ); this->_character->loadFromFile( "assets/person.png" ); + if( this->_water->loadFromFile( "assets/water_animated .png" ) ) + { + // Set Frame 1 + this->_waterTicks[ 0 ].x = 0; + this->_waterTicks[ 0 ].y = 0; + this->_waterTicks[ 0 ].w = 100; + this->_waterTicks[ 0 ].h = 100; + + //Set top right sprite + this->_waterTicks[ 1 ].x = 100; + this->_waterTicks[ 1 ].y = 0; + this->_waterTicks[ 1 ].w = 100; + this->_waterTicks[ 1 ].h = 100; + + // Set Frame 3 + this->_waterTicks[ 2 ].x = 200; + this->_waterTicks[ 2 ].y = 0; + this->_waterTicks[ 2 ].w = 100; + this->_waterTicks[ 2 ].h = 100; + + // Set Frame 4 + this->_waterTicks[ 3 ].x = 300; + this->_waterTicks[ 3 ].y = 0; + this->_waterTicks[ 3 ].w = 100; + this->_waterTicks[ 3 ].h = 100; + } if( this->_textureSprite->loadFromFile( "assets/texture_test.png" ) ) { //Set top left sprite @@ -126,11 +155,13 @@ void Window::update() { SDL_RenderClear( this->_renderer ); SDL_Rect bg = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); + this->_background->setColour( this->_screenModulation ); 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->setAlpha( this->_alpha ); 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 ] ); @@ -226,7 +257,7 @@ void Window::spawn() } else { if( Window::renderer == NULL ) { - this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED ); + this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ); if( this->_renderer == NULL ) { printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); @@ -272,8 +303,27 @@ void Window::processInput() if( e.type == SDL_QUIT ) this->_toClose = true; else if( e.type == SDL_KEYDOWN ) { - if( e.key.keysym.sym == SDLK_ESCAPE ) this->_toClose = true; - std::cout << "Key: " << e.key.keysym.sym << std::endl; + switch( e.key.keysym.sym ) + { + case SDL_QUIT: this->_toClose = true; break; + case SDLK_ESCAPE: this->_toClose = true; break; + case SDLK_PAGEUP: + this->_screenModulation.brighten( 5 ); + break; + case SDLK_PAGEDOWN: + this->_screenModulation.darken( 5 ); + break; + case SDLK_PLUS: + case SDLK_KP_PLUS: + case SDLK_GREATER: + if( this->_alpha < 0xFF ) this->_alpha += 5; + break; + case SDLK_LESS: + case SDLK_MINUS: + case SDLK_KP_MINUS: + if( this->_alpha > 5 ) this->_alpha -= 5; + break; + } } } }