From bcc0937726bb72c2e4c414fa00dcf595d70ac264 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 12 Feb 2014 00:27:55 +0100 Subject: [PATCH] New code, WiP. New Path, Character and other simple classes, but a renderer is missing so now nothing gets displayed anymore. --- CMakeLists.txt | 3 +++ src/CloonelJumpConfig.h.in | 2 ++ src/character.cpp | 30 +++++++++++++++++++++++ src/character.hpp | 26 ++++++++++++++++++++ src/game.cpp | 22 ++++++++--------- src/game.hpp | 7 +++--- src/gamebase.cpp | 11 ++------- src/gamebase.hpp | 5 ++-- src/main.cpp | 2 +- src/path.cpp | 49 ++++++++++++++++++++++++++++++++++++++ src/path.hpp | 20 ++++++++++++++++ src/placeable.cpp | 10 ++++++++ src/placeable.hpp | 19 +++++++++++++++ 13 files changed, 179 insertions(+), 27 deletions(-) create mode 100644 src/character.cpp create mode 100644 src/character.hpp create mode 100644 src/path.cpp create mode 100644 src/path.hpp create mode 100644 src/placeable.cpp create mode 100644 src/placeable.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f98422..c6e058a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,9 @@ add_executable(${PROJECT_NAME} src/sdlmain.cpp src/game.cpp src/gamebase.cpp + src/character.cpp + src/placeable.cpp + src/path.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/src/CloonelJumpConfig.h.in b/src/CloonelJumpConfig.h.in index 634f127..e94f270 100644 --- a/src/CloonelJumpConfig.h.in +++ b/src/CloonelJumpConfig.h.in @@ -8,4 +8,6 @@ #define DEF_WIN_WIDTH 800 #define DEF_WIN_HEIGHT 600 +#define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@" + #endif diff --git a/src/character.cpp b/src/character.cpp new file mode 100644 index 0000000..52afc89 --- /dev/null +++ b/src/character.cpp @@ -0,0 +1,30 @@ +#include "character.hpp" +#include "sdlmain.hpp" +#include "texture.hpp" + +namespace cloonel { + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + Character::Character (const std::string& parPath, SDLMain* parMain) : + Placeable(0.0f, 0.0f), + m_texture(new Texture(parPath, parMain, false)) + { + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + Character::~Character() noexcept { + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + void Character::Prepare() { + m_texture->Reload(); + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + void Character::Destroy() noexcept { + m_texture->Destroy(); + } +} //namespace cloonel diff --git a/src/character.hpp b/src/character.hpp new file mode 100644 index 0000000..1543152 --- /dev/null +++ b/src/character.hpp @@ -0,0 +1,26 @@ +#ifndef id0CEACFB045ED4C9F8688265AA41E30B0 +#define id0CEACFB045ED4C9F8688265AA41E30B0 + +#include "placeable.hpp" +#include "vector.hpp" +#include +#include + +namespace cloonel { + class SDLMain; + class Texture; + + class Character : public Placeable { + public: + Character ( const std::string& parPath, SDLMain* parMain ); + ~Character ( void ) noexcept; + + void Prepare ( void ); + void Destroy ( void ) noexcept; + + private: + const std::unique_ptr m_texture; + }; +} //unnamed namespace + +#endif diff --git a/src/game.cpp b/src/game.cpp index 83c07a1..4981430 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,16 +1,16 @@ #include "game.hpp" -#include "texture.hpp" +#include "character.hpp" namespace cloonel { namespace { + const char g_characterTexture[] = "resources/graphics/duck.bmp"; } //unnamed namespace ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ - Game::Game (SDLMain* parSdlMain) : - GameBase(parSdlMain), - m_character(nullptr), - m_pos(0.0f) + Game::Game (SDLMain* parSdlMain, const char* parBasePath) : + GameBase(parSdlMain, parBasePath), + m_character(new Character(m_path.GetFullPath(g_characterTexture), parSdlMain)) { } @@ -22,26 +22,26 @@ namespace cloonel { ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ void Game::Prepare() { - m_character = LoadTexture("resources/graphics/duck.bmp"); + m_character->Prepare(); } ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ void Game::Destroy() noexcept { - m_character = std::move(std::unique_ptr(nullptr)); + m_character->Destroy(); } ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ void Game::OnRender() { - const int2 newPos(static_cast(m_pos + 0.5f)); - m_character->Render(newPos); + const int2 newPos(m_character->GetPos() + 0.5f); + //m_character->Render(newPos); } ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ void Game::OnUpdate (float parDelta) { - const float2 increase(parDelta * 15.0f, parDelta * 22.8f); - m_pos += increase; + //const float2 increase(parDelta * 15.0f, parDelta * 22.8f); + //m_pos += increase; } } //namespace cloonel diff --git a/src/game.hpp b/src/game.hpp index bef768f..685b4d5 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -6,11 +6,11 @@ #include namespace cloonel { - class Texture; + class Character; class Game : public GameBase { public: - Game ( SDLMain* parSdlMain ); + Game ( SDLMain* parSdlMain, const char* parBasePath ); virtual ~Game ( void ) noexcept; virtual void Prepare ( void ); @@ -20,8 +20,7 @@ namespace cloonel { virtual void OnRender ( void ); virtual void OnUpdate ( float parDelta ); - std::unique_ptr m_character; - float2 m_pos; + const std::unique_ptr m_character; }; } //namespace cloonel diff --git a/src/gamebase.cpp b/src/gamebase.cpp index be5f36d..510ea75 100644 --- a/src/gamebase.cpp +++ b/src/gamebase.cpp @@ -23,7 +23,8 @@ namespace cloonel { ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ - GameBase::GameBase (SDLMain* parSdlMain) : + GameBase::GameBase (SDLMain* parSdlMain, const char* parBasePath) : + m_path(parBasePath), m_sdlmain(parSdlMain), m_time0(SDL_GetTicks()), m_wantsToQuit(false) @@ -35,14 +36,6 @@ namespace cloonel { 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))); - } - ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ float GameBase::Exec() { diff --git a/src/gamebase.hpp b/src/gamebase.hpp index ca70571..17df130 100644 --- a/src/gamebase.hpp +++ b/src/gamebase.hpp @@ -1,6 +1,7 @@ #ifndef id8C7FE975525B4329BFBEAF364D934EAD #define id8C7FE975525B4329BFBEAF364D934EAD +#include "path.hpp" #include namespace cloonel { @@ -13,13 +14,13 @@ namespace cloonel { bool WantsToQuit ( void ) const; protected: - explicit GameBase ( SDLMain* parSdlMain ); + GameBase ( SDLMain* parSdlMain, const char* parBasePath ); virtual ~GameBase ( void ) noexcept; virtual void Prepare ( void ) = 0; virtual void Destroy ( void ) noexcept = 0; - std::unique_ptr LoadTexture ( const char* parPath ); + const Path m_path; private: virtual void OnRender ( void ) = 0; diff --git a/src/main.cpp b/src/main.cpp index 17b5309..e944d45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int main() { std::cerr << e.what() << std::endl; } - cloonel::Game game(&sdlmain); + cloonel::Game game(&sdlmain, GAME_BASE_PATH); game.Prepare(); RunMainLoop(game); diff --git a/src/path.cpp b/src/path.cpp new file mode 100644 index 0000000..2ba9257 --- /dev/null +++ b/src/path.cpp @@ -0,0 +1,49 @@ +#include "path.hpp" +#include +#include +#include + +namespace cloonel { + namespace { + ///--------------------------------------------------------------------- + ///--------------------------------------------------------------------- + std::string GetCleanBasePath (const std::string&& parPath) { + const size_t pathlen = parPath.size(); + switch (pathlen) { + case 0: + return std::string("/"); + case 1: + return parPath; + default: + if (parPath[pathlen - 1] == '/') + return parPath.substr(0, pathlen - 1); + else + return parPath; + } + } + } //unnamed namespace + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + Path::Path (const char* parBasePath) : + m_basePath(GetCleanBasePath(std::string(parBasePath))) + { + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + std::string Path::GetFullPath (const std::string& parPath) const { + std::ostringstream oss; + oss << m_basePath << '/' << parPath; + return oss.str(); + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + std::string Path::GetFullPath (const char* parPath) const { + assert(parPath); + std::ostringstream oss; + oss << m_basePath << '/' << parPath; + return oss.str(); + } +} // namespace cloonel diff --git a/src/path.hpp b/src/path.hpp new file mode 100644 index 0000000..2bde7a8 --- /dev/null +++ b/src/path.hpp @@ -0,0 +1,20 @@ +#ifndef id6B8B06CEFCED4E48A76E27CE5D65052F +#define id6B8B06CEFCED4E48A76E27CE5D65052F + +#include + +namespace cloonel { + class Path { + public: + explicit Path ( const char* parBasePath ); + ~Path ( void ) noexcept = default; + + std::string GetFullPath ( const std::string& parPath ) const; + std::string GetFullPath ( const char* parPath ) const; + + private: + const std::string m_basePath; + }; +} //namespace cloonel + +#endif diff --git a/src/placeable.cpp b/src/placeable.cpp new file mode 100644 index 0000000..c016222 --- /dev/null +++ b/src/placeable.cpp @@ -0,0 +1,10 @@ +#include "placeable.hpp" + +namespace cloonel { + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + Placeable::Placeable (float parX, float parY) : + m_pos(parX, parY) + { + } +} //namespace cloonel diff --git a/src/placeable.hpp b/src/placeable.hpp new file mode 100644 index 0000000..8993beb --- /dev/null +++ b/src/placeable.hpp @@ -0,0 +1,19 @@ +#ifndef id703E4B8DFFF747DFA97864384B87E9C1 +#define id703E4B8DFFF747DFA97864384B87E9C1 + +#include "vector.hpp" + +namespace cloonel { + class Placeable { + public: + const float2& GetPos ( void ) const noexcept { return m_pos; } + + protected: + Placeable ( float parX, float parY ); + ~Placeable ( void ) noexcept = default; + + float2 m_pos; + }; +} //namespace cloonel + +#endif