diff --git a/assets/MonoRegular-jEmAR.ttf b/assets/MonoRegular-jEmAR.ttf new file mode 100644 index 0000000000000000000000000000000000000000..4a91568d61829b0200e5bd9e7a268d037162a39d Binary files /dev/null and b/assets/MonoRegular-jEmAR.ttf differ diff --git a/assets/arrow.png b/assets/arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..197722bde7a16b1d053fc1b97f34723530d3d7de Binary files /dev/null and b/assets/arrow.png differ diff --git a/include/colour.h b/include/colour.h index 80e875f66bc593955f419b5988ddecb225fcbaea..bf2ad7c62fed100b9ff6ab9f7b475d63ffb9b072 100644 --- a/include/colour.h +++ b/include/colour.h @@ -1,7 +1,7 @@ #ifndef __COLOUR_H__ #define __COLOUR_H__ -#include <cstdint>; +#include <cstdint> struct RGBA{ uint8_t r; diff --git a/include/components/cFontfile.h b/include/components/cFontfile.h new file mode 100644 index 0000000000000000000000000000000000000000..cb06e74cf65a19dcae2f299856a4f74096a86bc6 --- /dev/null +++ b/include/components/cFontfile.h @@ -0,0 +1,28 @@ +#ifndef __COMPONENTS__FONTFILE_H__ +#define __COMPONENTS__FONTFILE_H__ + +#include <SDL_image.h> +#include <SDL_ttf.h> + +#include "components/cTexture.h" +#include "colour.h" + +class cFontfile : public cTexture +{ + static bool ttf_isInit; + public: + cFontfile(); + void renderText( std::string str ); + bool loadFromFile( std::string path ); + + void setColour( Colour colour ); + void setColour( uint8_t red, uint8_t green, uint8_t blue ); + private: + TTF_Font* _font = NULL; + Colour _colour = Colour(0, 0, 0); + SDL_Color _col = { 0xff, 0xff, 0x00 }; + uint8_t _fontsize = 28; +}; + + +#endif // __COMPONENTS__FONTFILE_H__ \ No newline at end of file diff --git a/include/components/cTexture.h b/include/components/cTexture.h index 0afd96cd69b2b89a8c3887323a8cb58acf4fb3da..d76eea8a71d3ef9fbf19f8c897b9896f34cf607b 100644 --- a/include/components/cTexture.h +++ b/include/components/cTexture.h @@ -6,11 +6,10 @@ #include <string> #include "components/components-generic.h" #include "window.h" +#include "logger.h" #include "colour.h" #include "maths/vec2.h" - - class cTexture : public CComponentGeneric { public: @@ -18,8 +17,14 @@ class cTexture : public CComponentGeneric ~cTexture(); bool loadFromFile( std::string path ); void free(); - void render( Vec2 pt ); - void render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect *clip = NULL ); + void render( + SDL_Renderer *renderer, + Vec2 pt, + SDL_Rect *clip = NULL, + double angle = 0, + SDL_Point* center = NULL, + SDL_RendererFlip flip = SDL_FLIP_NONE + ); int getWidth(){ return this->_width; } int getHeight(){ return this->_height; } @@ -29,10 +34,13 @@ class cTexture : public CComponentGeneric void setBlendMode( SDL_BlendMode blending ); void setAlpha( uint8_t alpha ); - private: + protected: + Logger &_logger; SDL_Texture* _texture; uint16_t _width; uint16_t _height; }; +class cFontfile; + #endif // __COMPONENTS__TEXTURE_H__ diff --git a/include/entities/eButton.h b/include/entities/eButton.h new file mode 100644 index 0000000000000000000000000000000000000000..90f3c62af4b9b09aa1b72cbff04d59b0090cac51 --- /dev/null +++ b/include/entities/eButton.h @@ -0,0 +1,21 @@ +#ifndef __ENTITIES__BUTTON +#define __ENTITIES__BUTTON + +#include <SDL.h> + +#include "entities/entity-generic.h" +#include "maths/maths.h" +#include "components/cTexture.h" + +class eButton : public EntityGeneric +{ + public: + void setPosition( Vec2 pos ); + void handleEvent( SDL_Event* evt ); + void render(); + private: + Vec2 _pos; + cTexture _sprite; +}; + +#endif // __ENTITIES__BUTTON diff --git a/include/window.h b/include/window.h index 52f660f25c07623276a1c72139df189a0c75c0d9..c40d6a08391e81ff24eaee84bc26c6ed7cb777c8 100644 --- a/include/window.h +++ b/include/window.h @@ -10,8 +10,13 @@ #include "logger.h" #include "colour.h" #include "maths/vec2.h" -#include "components/cTexture.h" + + +// #include "components/cFontfile.h" +// #include "components/cTexture.h" + +class cFontfile; class cTexture; const uint16_t SCREEN_HEIGHT = 600; @@ -54,12 +59,15 @@ class Window SDL_Surface* loadSurface( std::string path ); Logger& _logger; SDL_Window* _window; + size_t _frame = 0; SDL_Texture* _activeTexture; Colour _screenModulation; cTexture* _background; cTexture* _character; + cTexture* _arrow; cTexture* _textureSprite; + cFontfile* _fontFile; SDL_Rect _sprites[4]; uint8_t _alpha = 0xff; @@ -84,4 +92,7 @@ class Window void clean(); }; + + + #endif // __WINDOW_H__ \ No newline at end of file diff --git a/src/components/cFontfile.cpp b/src/components/cFontfile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519d56b615c1f57f99bcc06e4506f94980bb9bd6 --- /dev/null +++ b/src/components/cFontfile.cpp @@ -0,0 +1,53 @@ +#include "components/cFontfile.h" + +bool cFontfile::ttf_isInit = false; + +cFontfile::cFontfile() + : cTexture() +{ + if( ! cFontfile::ttf_isInit ) + { + if( TTF_Init() == -1 ) + { printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() ); } + else { cFontfile::ttf_isInit = true; } + } +} + + +bool cFontfile::loadFromFile( std::string path ) +{ + this->_logger.info( "Loading Font: "+path ); + this->free(); + SDL_Texture* texture = NULL; + this->_font = TTF_OpenFont( path.c_str(), this->_fontsize ); + if( this->_font == NULL ) + { printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() ); } + else + { + + } + return texture != NULL; +} + +void cFontfile::renderText( std::string str ) +{ + free(); + + SDL_Surface* textSurface = TTF_RenderText_Solid( this->_font, str.c_str(), this->_col ); + + if( textSurface == NULL ) + { /* error */ return; } + this->_texture = SDL_CreateTextureFromSurface( Window::renderer, textSurface ); + if( ! this->_texture ) + { /* error */ return; } + + this->_width = textSurface->w; + this->_height = textSurface->h; + + SDL_FreeSurface( textSurface ); +} + +void cFontfile::setColour( Colour colour ) { this->_colour = colour; } +void cFontfile::setColour( uint8_t red, uint8_t green, uint8_t blue ) { + this->_colour = Colour( red, green, blue ); +} \ No newline at end of file diff --git a/src/components/cTexture.cpp b/src/components/cTexture.cpp index 499ae0fab7be66decf5fe096a12c9aef173bb34b..2562f51efc77077e022bde23d6001b157af5f8ac 100644 --- a/src/components/cTexture.cpp +++ b/src/components/cTexture.cpp @@ -2,6 +2,7 @@ cTexture::cTexture() : _texture( NULL ), + _logger( Logger::instance() ), _width( 0 ), _height( 0 ) {} @@ -50,7 +51,7 @@ void cTexture::setColour( uint8_t red, uint8_t green, uint8_t blue ) { SDL_SetTe 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 ) +void cTexture::render( SDL_Renderer *renderer, Vec2 pt, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip ) { if( this->_width && this->_height ) { @@ -60,14 +61,6 @@ void cTexture::render( Vec2 pt, SDL_Renderer *renderer, SDL_Rect* clip ) quad.w = clip->w; quad.h = clip->h; } - SDL_RenderCopy( renderer, this->_texture, clip, &quad ); - } -} -void cTexture::render( Vec2 pt ) -{ - if( Window::renderer != NULL ) - { - SDL_Rect quad = { (int)pt.x, (int)pt.y, this->_width, this->_height }; - SDL_RenderCopy( Window::renderer, this->_texture, NULL, &quad ); + SDL_RenderCopyEx( renderer, this->_texture, clip, &quad, angle, center, flip ); } } \ No newline at end of file diff --git a/src/entities/eButton.cpp b/src/entities/eButton.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4f5fc3b2b21220a5f76ae1d626a9696d33fc4e1 --- /dev/null +++ b/src/entities/eButton.cpp @@ -0,0 +1,5 @@ +#include "entities/eButton.h" + +void eButton::setPosition( Vec2 pos ){} +void eButton::handleEvent( SDL_Event* evt ){} +void eButton::render(){} \ No newline at end of file diff --git a/src/game-engine.cpp b/src/game-engine.cpp index e24ab037e23abe22760826ee30c1a53dc0195349..2563fa221ae97a14f8616cb35f8d665f061eb575 100644 --- a/src/game-engine.cpp +++ b/src/game-engine.cpp @@ -36,8 +36,8 @@ void GameEngine::run() this->init(); this->isRunning = true; this->_frame = 0; - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - std::time_t _frametime = std::chrono::system_clock::to_time_t(now); + std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now(); + std::time_t _frametime = std::chrono::high_resolution_clock::to_time_t(now); while( this->isRunning ) { this->update(); } } @@ -47,13 +47,15 @@ void GameEngine::update() this->handleInput(); this->_entityManager.update(); - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now(); if ( ++this->_frame % 1 == 0 ) { - std::time_t cTime = std::chrono::system_clock::to_time_t(now); + std::time_t cTime = std::chrono::high_resolution_clock::to_time_t(now); size_t delTime = cTime - this->_frametime; this->_frametime = cTime; - printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime ); + std::cout << cTime << std::endl; + // printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime ); + printf( "Frame: %i at %.2f fps over %i s\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime, delTime ); } this->_window->update(); } @@ -70,22 +72,6 @@ void GameEngine::handleInput() { case SDL_QUIT: this->close(); break; case SDLK_ESCAPE: this->close(); 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; } } } diff --git a/src/window.cpp b/src/window.cpp index abd0c53250e35d6028d798d8a12a8125cf1f6bbe..abe9cd5ca098fdfdeb9f48164a4dffa13e2a367c 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,4 +1,6 @@ #include "window.h" +#include "components/cFontfile.h" +#include "components/cTexture.h" bool Window::sdlIsInit = false; SDL_Renderer* Window::renderer = NULL; @@ -14,7 +16,9 @@ Window::Window() this->_renderSurface = nullptr; this->_background = new cTexture(); this->_character = new cTexture(); + this->_arrow = new cTexture(); this->_water = new cTexture(); + this->_fontFile = new cFontfile(); this->_textureSprite = new cTexture(); this->_screenModulation = Colour( 0x88, 0x88, 0x88, 0xFF ); @@ -38,6 +42,7 @@ void Window::initialise() this->_toClose = false; this->_hasInit = true; + this->_frame = 0; } SDL_Texture* Window::loadTexture( std::string path ) @@ -85,6 +90,9 @@ void Window::loadMedia() { this->_background->loadFromFile( "assets/background.png" ); this->_character->loadFromFile( "assets/person.png" ); + this->_arrow->loadFromFile( "assets/arrow.png" ); + this->_fontFile->loadFromFile( "assets/MonoRegular-jEmAR.ttf" ); + this->_fontFile->setColour( 255, 255, 0 ); if( this->_water->loadFromFile( "assets/water_animated.png" ) ) { // Set Frame 1 @@ -156,20 +164,19 @@ void Window::update() { 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 ); + this->_background->render( this->_renderer, {0,0} ); + this->_character->render( this->_renderer, {SCREEN_HEIGHT/3,SCREEN_HEIGHT/3} ); + // this->_textureSprite->render( this->_renderer, {0,0} ); //Render top left sprite this->_textureSprite->setAlpha( this->_alpha ); - this->_textureSprite->render( {0, 0}, this->_renderer, &this->_sprites[ 0 ] ); + this->_textureSprite->render( this->_renderer, {0, 0}, &this->_sprites[ 0 ] ); //Render top right sprite - this->_textureSprite->render( {SCREEN_WIDTH - this->_sprites[ 1 ].w, 0}, this->_renderer, &this->_sprites[ 1 ] ); + this->_textureSprite->render( this->_renderer, {SCREEN_WIDTH - this->_sprites[ 1 ].w, 0}, &this->_sprites[ 1 ] ); //Render bottom left sprite - this->_textureSprite->render( {0, SCREEN_HEIGHT - this->_sprites[ 2 ].h}, this->_renderer, &this->_sprites[ 2 ] ); + this->_textureSprite->render( this->_renderer, {0, SCREEN_HEIGHT - this->_sprites[ 2 ].h}, &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 ] ); - + this->_textureSprite->render( this->_renderer, {SCREEN_WIDTH - this->_sprites[ 3 ].w, SCREEN_HEIGHT - this->_sprites[ 3 ].h}, &this->_sprites[ 3 ] ); SDL_Rect core = this->_createViewport( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); @@ -183,7 +190,13 @@ void Window::update() { this->_drawPoint( { SCREEN_WIDTH / 2, i } ); } + // Water + this->_water->render( this->_renderer, { 0, 0 }, &this->_waterTicks[ this->_frame % this->_waterFrames ] ); + + SDL_Rect overlay = this->_createViewport( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + this->_arrow->render( this->_renderer, {100, 100}, NULL, this->_frame % 360 ); + const int hexRad = 10; const int hexSpacing = 1; const int drawHexRad = hexRad + hexSpacing; @@ -202,7 +215,11 @@ void Window::update() { // SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); + overlay = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); + this->_fontFile->renderText( std::to_string(this->_frame) ); + this->_fontFile->render( this->_renderer, {50,50} ); + ++this->_frame; SDL_RenderPresent( this->_renderer ); } @@ -296,36 +313,6 @@ void Window::spawn() void Window::processInput() { - SDL_Event e; - while( SDL_PollEvent( &e ) ) - { - // std::cout << "event" << std::endl; - if( e.type == SDL_QUIT ) this->_toClose = true; - else if( e.type == SDL_KEYDOWN ) - { - 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; - } - } - } } void Window::clean()