diff --git a/assets/background.png b/assets/background.png new file mode 100644 index 0000000000000000000000000000000000000000..fb1f519b0be8d91988ece3fbe1532934ec79c6f6 Binary files /dev/null and b/assets/background.png differ diff --git a/assets/person.png b/assets/person.png new file mode 100644 index 0000000000000000000000000000000000000000..5703a788062c2aef40979cf4879cd1bbe41c4edc Binary files /dev/null and b/assets/person.png differ diff --git a/include/components/cTexture.h b/include/components/cTexture.h new file mode 100644 index 0000000000000000000000000000000000000000..22c9b233b114da97e15f58957c85275cbebbc823 --- /dev/null +++ b/include/components/cTexture.h @@ -0,0 +1,31 @@ +#ifndef __COMPONENTS__TEXTURE_H__ +#define __COMPONENTS__TEXTURE_H__ + +#include <SDL.h> +#include <cstdio> +#include <string> +#include "components/components-generic.h" +#include "window.h" +#include "maths/vec2.h" + + + +class cTexture : public CComponentGeneric +{ + public: + cTexture(); + ~cTexture(); + bool loadFromFile( std::string path ); + void free(); + void render( Vec2 pt ); + void render( Vec2 pt, SDL_Renderer *renderer ); + + int getWidth(){ return this->_width; } + int getHeight(){ return this->_height; } + private: + SDL_Texture* _texture; + uint16_t _width; + uint16_t _height; +}; + +#endif // __COMPONENTS__TEXTURE_H__ diff --git a/include/main.h b/include/main.h index b38e95e5adf4e4a2f431d465c1c6c51db847a099..b1e2153c3c449328bc6e3619b56851bdb9628725 100644 --- a/include/main.h +++ b/include/main.h @@ -21,6 +21,8 @@ enum GenerationMethod #include "entities/entity-generic.h" #include "maths/maths.h" #include "entity-manager.h" +#include "components/cTexture.h" #include "window.h" + #endif // MAIN_H diff --git a/include/window.h b/include/window.h index 3a2bdecb5414d06ca0003bdf547bc3d8f2ff516d..d1acdcf2c80f366325c21f6880017950422f1955 100644 --- a/include/window.h +++ b/include/window.h @@ -1,6 +1,5 @@ #ifndef __WINDOW_H__ #define __WINDOW_H__ -#endif // __WINDOW_H__ #define SDL_MAIN_HANDLED #include <SDL.h> @@ -11,7 +10,9 @@ #include <string> #include "logger.h" #include "maths/vec2.h" +#include "components/cTexture.h" +class cTexture; const uint16_t SCREEN_HEIGHT = 600; const uint16_t SCREEN_WIDTH = 800; @@ -30,11 +31,13 @@ enum KeyPressSurfaces class Window { public: + static SDL_Renderer* renderer; static bool sdlIsInit; Window(); ~Window(); void loadMedia( std::string path ); + void loadMedia(); void update(); void processInput(); @@ -50,6 +53,8 @@ class Window Logger& _logger; SDL_Window* _window; SDL_Texture* _activeTexture; + cTexture* _background; + cTexture* _character; SDL_Renderer* _renderer; SDL_Surface* _screenSurface; SDL_Surface* _renderSurface; @@ -64,4 +69,6 @@ class Window void initialise(); void clean(); -}; \ No newline at end of file +}; + +#endif // __WINDOW_H__ \ No newline at end of file diff --git a/src/components/cTexture.cpp b/src/components/cTexture.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5619049212c7828702825b1a20548af588a3f2e --- /dev/null +++ b/src/components/cTexture.cpp @@ -0,0 +1,61 @@ +#include "components/cTexture.h" + +cTexture::cTexture() + : _texture( NULL ), + _width( 0 ), _height( 0 ) +{} + +cTexture::~cTexture(){ this->free(); } + +bool cTexture::loadFromFile( std::string path ) +{ + this->free(); + SDL_Texture* texture = NULL; + SDL_Surface* surface = IMG_Load( path.c_str() ); + if( surface == NULL ) + { printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() ); } + else + { + SDL_SetColorKey( surface, SDL_TRUE, SDL_MapRGB( surface->format, 0xFF, 0x00, 0xFF ) ); + + texture = SDL_CreateTextureFromSurface( Window::renderer, surface ); + if( texture == NULL ) + { printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() ); } + else + { + this->_width = surface->w; + this->_height = surface->h; + } + + SDL_FreeSurface( surface ); + } + this->_texture = texture; + return texture != NULL; +} + +void cTexture::free() +{ + if( this->_texture != NULL ) + { + _texture = NULL; + _width = 0; + _height = 0; + } +} + +void cTexture::render( Vec2 pt, SDL_Renderer *renderer ) +{ + 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 ); + } +} +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 ); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index adc8d532f1a975544409fa175b3c1ab9717b495e..8f092e1306505b0cb477285c3c2c94a47ed6814f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,6 +44,7 @@ int main( int argc, char* argv[] ) { window->spawn(); window->loadMedia( "assets/hello-world.bmp" ); window->update(); + window->loadMedia(); GameEngine* game = new GameEngine(); // Main Loop diff --git a/src/window.cpp b/src/window.cpp index fb74e8da224e50c7d2018f607725329ba90ef668..944591cf45888a1ca58b1ffeb08711c3d3520b10 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,6 +1,7 @@ #include "window.h" bool Window::sdlIsInit = false; +SDL_Renderer* Window::renderer = NULL; Window::Window() : _logger( Logger::instance() ), _hasInit( false ) @@ -9,9 +10,12 @@ Window::Window() this->_window = nullptr; this->_screenSurface = nullptr; this->_renderSurface = nullptr; + this->_background = new cTexture(); + this->_character = new cTexture(); } Window::~Window() -{} +{ +} void Window::initialise() { @@ -71,6 +75,11 @@ SDL_Surface* Window::loadSurface( std::string path ) return optimizedSurface; } +void Window::loadMedia() +{ + this->_background->loadFromFile( "assets/background.png" ); + this->_character->loadFromFile( "assets/person.png" ); +} void Window::loadMedia( std::string path ) { //Load splash image @@ -81,7 +90,10 @@ void Window::update() { SDL_SetRenderDrawColor( this->_renderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_RenderClear( this->_renderer ); - SDL_Rect core = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); + 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 ); + SDL_Rect core = this->_createViewport( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); // Render Example Primatives this->_drawRect( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); this->_drawRect( 0, 0, 10, 10 ); @@ -91,8 +103,8 @@ void Window::update() { this->_drawPoint( { SCREEN_WIDTH / 2, i } ); } - SDL_Rect overlay = this->_createViewport( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); - SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); + // SDL_Rect overlay = this->_createViewport( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + // SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); SDL_RenderPresent( this->_renderer ); @@ -135,15 +147,21 @@ void Window::spawn() { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); } else { - - this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED ); - if( this->_renderer == NULL ) + if( Window::renderer == NULL ) { - printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); + this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED ); + if( this->_renderer == NULL ) + { + printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); + } else { + SDL_SetRenderDrawColor( this->_renderer, 0xFF, 0x00, 0xFF, 0xFF ); + } + Window::renderer = this->_renderer; } else { - SDL_SetRenderDrawColor( _renderer, 0xFF, 0x00, 0xFF, 0xFF ); + this->_renderer = Window::renderer; } + int imgFlags = IMG_INIT_PNG; if( !( IMG_Init( imgFlags ) & imgFlags ) ) { @@ -188,6 +206,8 @@ void Window::clean() // Clean Texure SDL_DestroyTexture( this->_activeTexture ); this->_activeTexture = nullptr; + delete this->_background; + delete this->_character; //Destroy window SDL_DestroyRenderer( this->_renderer );