New code, WiP.

New Path, Character and other simple classes, but a renderer is missing so now nothing gets displayed anymore.
This commit is contained in:
King_DuckZ 2014-02-12 00:27:55 +01:00
parent 8b11e76835
commit bcc0937726
13 changed files with 179 additions and 27 deletions

View file

@ -29,6 +29,9 @@ add_executable(${PROJECT_NAME}
src/sdlmain.cpp src/sdlmain.cpp
src/game.cpp src/game.cpp
src/gamebase.cpp src/gamebase.cpp
src/character.cpp
src/placeable.cpp
src/path.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}

View file

@ -8,4 +8,6 @@
#define DEF_WIN_WIDTH 800 #define DEF_WIN_WIDTH 800
#define DEF_WIN_HEIGHT 600 #define DEF_WIN_HEIGHT 600
#define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@"
#endif #endif

30
src/character.cpp Normal file
View file

@ -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

26
src/character.hpp Normal file
View file

@ -0,0 +1,26 @@
#ifndef id0CEACFB045ED4C9F8688265AA41E30B0
#define id0CEACFB045ED4C9F8688265AA41E30B0
#include "placeable.hpp"
#include "vector.hpp"
#include <string>
#include <memory>
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<Texture> m_texture;
};
} //unnamed namespace
#endif

View file

@ -1,16 +1,16 @@
#include "game.hpp" #include "game.hpp"
#include "texture.hpp" #include "character.hpp"
namespace cloonel { namespace cloonel {
namespace { namespace {
const char g_characterTexture[] = "resources/graphics/duck.bmp";
} //unnamed namespace } //unnamed namespace
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
Game::Game (SDLMain* parSdlMain) : Game::Game (SDLMain* parSdlMain, const char* parBasePath) :
GameBase(parSdlMain), GameBase(parSdlMain, parBasePath),
m_character(nullptr), m_character(new Character(m_path.GetFullPath(g_characterTexture), parSdlMain))
m_pos(0.0f)
{ {
} }
@ -22,26 +22,26 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void Game::Prepare() { void Game::Prepare() {
m_character = LoadTexture("resources/graphics/duck.bmp"); m_character->Prepare();
} }
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void Game::Destroy() noexcept { void Game::Destroy() noexcept {
m_character = std::move(std::unique_ptr<Texture>(nullptr)); m_character->Destroy();
} }
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void Game::OnRender() { void Game::OnRender() {
const int2 newPos(static_cast<int2>(m_pos + 0.5f)); const int2 newPos(m_character->GetPos() + 0.5f);
m_character->Render(newPos); //m_character->Render(newPos);
} }
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void Game::OnUpdate (float parDelta) { void Game::OnUpdate (float parDelta) {
const float2 increase(parDelta * 15.0f, parDelta * 22.8f); //const float2 increase(parDelta * 15.0f, parDelta * 22.8f);
m_pos += increase; //m_pos += increase;
} }
} //namespace cloonel } //namespace cloonel

View file

@ -6,11 +6,11 @@
#include <memory> #include <memory>
namespace cloonel { namespace cloonel {
class Texture; class Character;
class Game : public GameBase { class Game : public GameBase {
public: public:
Game ( SDLMain* parSdlMain ); Game ( SDLMain* parSdlMain, const char* parBasePath );
virtual ~Game ( void ) noexcept; virtual ~Game ( void ) noexcept;
virtual void Prepare ( void ); virtual void Prepare ( void );
@ -20,8 +20,7 @@ namespace cloonel {
virtual void OnRender ( void ); virtual void OnRender ( void );
virtual void OnUpdate ( float parDelta ); virtual void OnUpdate ( float parDelta );
std::unique_ptr<Texture> m_character; const std::unique_ptr<Character> m_character;
float2 m_pos;
}; };
} //namespace cloonel } //namespace cloonel

View file

@ -23,7 +23,8 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
GameBase::GameBase (SDLMain* parSdlMain) : GameBase::GameBase (SDLMain* parSdlMain, const char* parBasePath) :
m_path(parBasePath),
m_sdlmain(parSdlMain), m_sdlmain(parSdlMain),
m_time0(SDL_GetTicks()), m_time0(SDL_GetTicks()),
m_wantsToQuit(false) m_wantsToQuit(false)
@ -35,14 +36,6 @@ namespace cloonel {
GameBase::~GameBase() { GameBase::~GameBase() {
} }
///------------------------------------------------------------------------
///------------------------------------------------------------------------
std::unique_ptr<Texture> GameBase::LoadTexture (const char* parPath) {
std::ostringstream oss;
oss << "/home/duckz/dev/code/cpp/clooneljump/" << parPath;
return std::move(std::unique_ptr<Texture>(new Texture(oss.str(), m_sdlmain, true)));
}
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
float GameBase::Exec() { float GameBase::Exec() {

View file

@ -1,6 +1,7 @@
#ifndef id8C7FE975525B4329BFBEAF364D934EAD #ifndef id8C7FE975525B4329BFBEAF364D934EAD
#define id8C7FE975525B4329BFBEAF364D934EAD #define id8C7FE975525B4329BFBEAF364D934EAD
#include "path.hpp"
#include <memory> #include <memory>
namespace cloonel { namespace cloonel {
@ -13,13 +14,13 @@ namespace cloonel {
bool WantsToQuit ( void ) const; bool WantsToQuit ( void ) const;
protected: protected:
explicit GameBase ( SDLMain* parSdlMain ); GameBase ( SDLMain* parSdlMain, const char* parBasePath );
virtual ~GameBase ( void ) noexcept; virtual ~GameBase ( void ) noexcept;
virtual void Prepare ( void ) = 0; virtual void Prepare ( void ) = 0;
virtual void Destroy ( void ) noexcept = 0; virtual void Destroy ( void ) noexcept = 0;
std::unique_ptr<Texture> LoadTexture ( const char* parPath ); const Path m_path;
private: private:
virtual void OnRender ( void ) = 0; virtual void OnRender ( void ) = 0;

View file

@ -32,7 +32,7 @@ int main() {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
cloonel::Game game(&sdlmain); cloonel::Game game(&sdlmain, GAME_BASE_PATH);
game.Prepare(); game.Prepare();
RunMainLoop(game); RunMainLoop(game);

49
src/path.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "path.hpp"
#include <cassert>
#include <sstream>
#include <ciso646>
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

20
src/path.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef id6B8B06CEFCED4E48A76E27CE5D65052F
#define id6B8B06CEFCED4E48A76E27CE5D65052F
#include <string>
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

10
src/placeable.cpp Normal file
View file

@ -0,0 +1,10 @@
#include "placeable.hpp"
namespace cloonel {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
Placeable::Placeable (float parX, float parY) :
m_pos(parX, parY)
{
}
} //namespace cloonel

19
src/placeable.hpp Normal file
View file

@ -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