diff --git a/CMakeLists.txt b/CMakeLists.txt index 8da09bc..2dea7fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,8 @@ add_executable(${PROJECT_NAME} src/texture.cpp src/sdlerror.cpp src/sdlmain.cpp + src/game.cpp + src/gamebase.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/resources/graphics/duck.bmp b/resources/graphics/duck.bmp new file mode 100644 index 0000000..a0542f4 Binary files /dev/null and b/resources/graphics/duck.bmp differ diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..443fd5d --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,38 @@ +#include "game.hpp" +#include "texture.hpp" + +namespace cloonel { + namespace { + } //unnamed namespace + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + Game::Game (SDLMain* parSdlMain) : + GameBase(parSdlMain), + m_character(nullptr) + { + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + Game::~Game() noexcept { + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void Game::Prepare() { + m_character = LoadTexture("resources/graphics/duck.bmp"); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void Game::Destroy() noexcept { + m_character = std::move(std::unique_ptr(nullptr)); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void Game::OnRender() { + m_character->Render(); + } +} //namespace cloonel diff --git a/src/game.hpp b/src/game.hpp new file mode 100644 index 0000000..2402f28 --- /dev/null +++ b/src/game.hpp @@ -0,0 +1,25 @@ +#ifndef id0E85DAFBFF5D497E8B3699AED806840A +#define id0E85DAFBFF5D497E8B3699AED806840A + +#include "gamebase.hpp" +#include + +namespace cloonel { + class Texture; + + class Game : public GameBase { + public: + Game ( SDLMain* parSdlMain ); + virtual ~Game ( void ) noexcept; + + virtual void Prepare ( void ); + virtual void Destroy ( void ) noexcept; + + private: + virtual void OnRender ( void ); + + std::unique_ptr m_character; + }; +} //namespace cloonel + +#endif diff --git a/src/gamebase.cpp b/src/gamebase.cpp new file mode 100644 index 0000000..af8ab2e --- /dev/null +++ b/src/gamebase.cpp @@ -0,0 +1,38 @@ +#include "gamebase.hpp" +#include "texture.hpp" +#include "sdlmain.hpp" +#include +#include + +namespace cloonel { + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + GameBase::GameBase (SDLMain* parSdlMain) : + m_sdlmain(parSdlMain) + { + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + GameBase::~GameBase() { + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + std::unique_ptr GameBase::LoadTexture (const char* parPath) { + std::ostringstream oss; + oss << "/home/duckz/dev/code/cpp/clooneljump/" << parPath; + return std::move(std::unique_ptr(new Texture(oss.str(), m_sdlmain, true))); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void GameBase::Exec (float parDelta) { + SDL_Renderer* const ren = m_sdlmain->GetRenderer(); + + SDL_RenderClear(ren); + OnRender(); + SDL_RenderPresent(ren); + SDL_Delay(2000); + } +} //namespace cloonel diff --git a/src/gamebase.hpp b/src/gamebase.hpp new file mode 100644 index 0000000..118d690 --- /dev/null +++ b/src/gamebase.hpp @@ -0,0 +1,30 @@ +#ifndef id8C7FE975525B4329BFBEAF364D934EAD +#define id8C7FE975525B4329BFBEAF364D934EAD + +#include + +namespace cloonel { + class SDLMain; + class Texture; + + class GameBase { + public: + void Exec ( float parDelta ); + + protected: + explicit GameBase ( SDLMain* parSdlMain ); + virtual ~GameBase ( void ) noexcept; + + virtual void Prepare ( void ) = 0; + virtual void Destroy ( void ) noexcept = 0; + + std::unique_ptr LoadTexture ( const char* parPath ); + + private: + virtual void OnRender ( void ) = 0; + + SDLMain* const m_sdlmain; + }; +} //namespace cloonel + +#endif diff --git a/src/main.cpp b/src/main.cpp index 81eadf5..4a0f0f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include "CloonelJumpConfig.h" #include "sdlmain.hpp" +#include "game.hpp" #include #include @@ -18,6 +19,10 @@ int main() { std::cerr << e.what() << std::endl; } + cloonel::Game game(&sdlmain); + game.Prepare(); + game.Exec(0.0f); + std::cout << "Quitting now" << std::endl; return 0; } diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index cb67f59..ed2c6b1 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -12,6 +12,7 @@ namespace cloonel { ///------------------------------------------------------------------------ SDLMain::SDLMain (const char* parGameName, int parWidth, int parHeight) : m_gameName(parGameName), + m_localData(new LocalData), m_defWidth(parWidth), m_defHeight(parHeight) { diff --git a/src/texture.cpp b/src/texture.cpp index 11ff4b5..c9f60e9 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -3,8 +3,11 @@ #include "sdlmain.hpp" #include #include +#include namespace cloonel { + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ Texture::Texture (const std::string& parPath, SDLMain* parMain, bool parLoadNow) : m_path(parPath), m_texture(nullptr), @@ -14,10 +17,14 @@ namespace cloonel { Reload(); } + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ Texture::~Texture() noexcept { Destroy(); } + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ void Texture::Destroy() noexcept { if (m_texture) { SDL_DestroyTexture(m_texture); @@ -25,6 +32,8 @@ namespace cloonel { } } + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ void Texture::Reload() { Destroy(); SDL_Surface* surf = SDL_LoadBMP(m_path.c_str()); @@ -32,5 +41,13 @@ namespace cloonel { throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path)); m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf); + SDL_FreeSurface(surf); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + void Texture::Render() { + assert(IsLoaded()); + SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, nullptr); } } //namespace cloonel diff --git a/src/texture.hpp b/src/texture.hpp index ac76c27..3f5e212 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -16,6 +16,7 @@ namespace cloonel { void Reload ( void ); void Destroy ( void ) noexcept; bool IsLoaded ( void ) const { return nullptr != m_texture; } + void Render ( void ); private: const std::string m_path;