Added some structuring.

The game runs but only a black window is shown.
This commit is contained in:
King_DuckZ 2014-02-21 21:28:34 +01:00
parent f132916a0f
commit d0893cba3a
16 changed files with 296 additions and 88 deletions

View file

@ -8,8 +8,13 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -Wall -Wextra
include(FindPkgConfig) include(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
find_package(PNG REQUIRED)
add_definitions(${PNG_DEFINITIONS})
include_directories(SYSTEM include_directories(SYSTEM
${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIR}
${PNG_INCLUDE_DIRS}
) )
include_directories( include_directories(
src src
@ -30,14 +35,19 @@ add_executable(${PROJECT_NAME}
src/texture.cpp src/texture.cpp
src/sdlerror.cpp src/sdlerror.cpp
src/sdlmain.cpp src/sdlmain.cpp
src/game.cpp
src/gamebase.cpp src/gamebase.cpp
src/character.cpp src/character.cpp
src/placeable.cpp src/placeable.cpp
src/physicsfswrapper.cpp src/physicsfswrapper.cpp
src/gameplayscene.cpp
src/mover.cpp
src/moveroneshot.cpp
src/moversine.cpp
src/gameplaysceneclassic.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
${SDL2_LIBRARIES} ${SDL2_LIBRARIES}
physfs physfs
${PNG_LIBRARIES}
) )

View file

@ -1,47 +0,0 @@
#include "game.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(new Character(g_characterTexture, parSdlMain))
{
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
Game::~Game() noexcept {
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::Prepare() {
m_character->Prepare();
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::Destroy() noexcept {
m_character->Destroy();
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::OnRender() {
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;
}
} //namespace cloonel

View file

@ -1,27 +0,0 @@
#ifndef id0E85DAFBFF5D497E8B3699AED806840A
#define id0E85DAFBFF5D497E8B3699AED806840A
#include "gamebase.hpp"
#include "vector.hpp"
#include <memory>
namespace cloonel {
class Character;
class Game : public GameBase {
public:
explicit Game ( SDLMain* parSdlMain );
virtual ~Game ( void ) noexcept;
virtual void Prepare ( void );
virtual void Destroy ( void ) noexcept;
private:
virtual void OnRender ( void );
virtual void OnUpdate ( float parDelta );
const std::unique_ptr<Character> m_character;
};
} //namespace cloonel
#endif

24
src/gameplayscene.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "gameplayscene.hpp"
#include "mover.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplayScene::GameplayScene (SDLMain* parSdlMain) :
GameBase(parSdlMain)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplayScene::OnUpdate (float parDelta) {
for (auto itMover : m_movers) {
itMover->ApplyMotion(parDelta);
}
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplayScene::OnRender() {
}
} //namespace cloonel

25
src/gameplayscene.hpp Normal file
View file

@ -0,0 +1,25 @@
#ifndef id1DF84BC48C0547D69F79499E3A25BFC5
#define id1DF84BC48C0547D69F79499E3A25BFC5
#include <vector>
#include "gamebase.hpp"
namespace cloonel {
class Mover;
class GameplayScene : public GameBase {
public:
explicit GameplayScene ( SDLMain* parSdlMain );
virtual ~GameplayScene ( void ) noexcept = default;
void AddMover ( Mover* parMover ) { m_movers.push_back(parMover); }
private:
virtual void OnRender ( void );
virtual void OnUpdate ( float parDelta );
std::vector<Mover*> m_movers;
};
} //namespace cloonel
#endif

View file

@ -0,0 +1,26 @@
#include "gameplaysceneclassic.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplaySceneClassic::GameplaySceneClassic (SDLMain* parSdlMain) :
GameplayScene(parSdlMain)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
GameplaySceneClassic::~GameplaySceneClassic() noexcept {
Destroy();
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplaySceneClassic::Prepare() {
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void GameplaySceneClassic::Destroy() noexcept {
}
} //namespace cloonel

View file

@ -0,0 +1,21 @@
#ifndef idF6FF1F57C36842DC9B20E2F55C507C2E
#define idF6FF1F57C36842DC9B20E2F55C507C2E
#include "gameplayscene.hpp"
namespace cloonel {
class SDLMain;
class GameplaySceneClassic : public GameplayScene {
public:
explicit GameplaySceneClassic ( SDLMain* parSdlMain );
~GameplaySceneClassic ( void ) noexcept;
virtual void Prepare ( void );
virtual void Destroy ( void ) noexcept;
private:
};
} //namespace cloonel
#endif

View file

@ -1,7 +1,7 @@
#include "CloonelJumpConfig.h" #include "CloonelJumpConfig.h"
#include "sdlmain.hpp" #include "sdlmain.hpp"
#include "game.hpp"
#include "physicsfswrapper.hpp" #include "physicsfswrapper.hpp"
#include "gameplaysceneclassic.hpp"
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <ciso646> #include <ciso646>
@ -9,11 +9,9 @@
namespace { namespace {
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
void RunMainLoop (cloonel::Game& parGame) { void RunMainLoop (cloonel::GameplaySceneClassic& parGame) {
float totalElapsed = 0.0f;
do { do {
const float delta = parGame.Exec(); parGame.Exec();
totalElapsed += delta;
} while (not parGame.WantsToQuit()); } while (not parGame.WantsToQuit());
} }
} //unnamed namespace } //unnamed namespace
@ -21,7 +19,7 @@ namespace {
///---------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html ///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html
///---------------------------------------------------------------------------- ///----------------------------------------------------------------------------
int main (int, char* parArgv[]) { int main (int, char* []) {
std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl; std::cout << GameName << " v" << GameVersionMajor << "." << GameVersionMinor << std::endl;
int retVal = 0; int retVal = 0;
@ -36,8 +34,7 @@ int main (int, char* parArgv[]) {
sdlmain.Init(); sdlmain.Init();
cloonel::Game game(&sdlmain); cloonel::GameplaySceneClassic game(&sdlmain);
game.Prepare();
RunMainLoop(game); RunMainLoop(game);
} }
catch (const std::runtime_error& e) { catch (const std::runtime_error& e) {

12
src/mover.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "mover.hpp"
#include "placeable.hpp"
#include <cassert>
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void Mover::ApplyOffsetToPlaceable (std::size_t parIndex, const float2& parOffset) {
assert(m_placeables[parIndex]);
m_placeables[parIndex]->AddOffset(parOffset);
}
} //namespace cloonel

28
src/mover.hpp Normal file
View file

@ -0,0 +1,28 @@
#ifndef id20409B43E62B4247A278B413D1114896
#define id20409B43E62B4247A278B413D1114896
#include "vector.hpp"
#include <vector>
namespace cloonel {
class Placeable;
class Mover {
public:
Mover ( void ) = default;
virtual ~Mover ( void ) noexcept = default;
virtual void ApplyMotion ( float parDelta ) = 0;
void AddPlaceable ( Placeable* parPlaceable ) { m_placeables.push_back(parPlaceable); }
protected:
virtual void Update ( float parDelta ) = 0;
std::size_t PlaceableCount ( void ) const { return m_placeables.size(); }
void ApplyOffsetToPlaceable ( std::size_t parIndex, const float2& parOffset );
private:
std::vector<Placeable*> m_placeables;
};
} //namespace cloonel
#endif

14
src/moveroneshot.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "moveroneshot.hpp"
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverOneShot::ApplyMotion (float parDelta) {
Update(parDelta);
const float2 offs(GetOffset());
const std::size_t placeableCount = PlaceableCount();
for (std::size_t z = 0; z < placeableCount; ++z) {
ApplyOffsetToPlaceable(z, offs);
}
}
} //namespace cloonel

20
src/moveroneshot.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef id9CDCF10D483641A2845353C0835F93EC
#define id9CDCF10D483641A2845353C0835F93EC
#include "vector.hpp"
#include "mover.hpp"
namespace cloonel {
class MoverOneShot : public Mover {
public:
MoverOneShot ( void ) = default;
virtual ~MoverOneShot ( void ) noexcept = default;
virtual void ApplyMotion ( float parDelta );
private:
virtual float2 GetOffset ( void ) const = 0;
};
} //namespace cloonel
#endif

25
src/moversine.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "moversine.hpp"
#include <cmath>
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
MoverSine::MoverSine() :
MoverOneShot(),
m_alpha(0.0f),
m_power(1.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
float2 MoverSine::GetOffset() const {
return float2(std::sin(m_alpha) * m_power);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverSine::Update (float parDelta) {
m_alpha += parDelta;
}
} //namespace cloonel

23
src/moversine.hpp Normal file
View file

@ -0,0 +1,23 @@
#ifndef id16A9347A373E4144A9C2B98E7D7A1351
#define id16A9347A373E4144A9C2B98E7D7A1351
#include "moveroneshot.hpp"
namespace cloonel {
class MoverSine : public MoverOneShot {
public:
MoverSine ( void );
~MoverSine ( void ) noexcept = default;
protected:
virtual void Update ( float parDelta );
private:
virtual float2 GetOffset() const;
float m_alpha;
float m_power;
};
} //namespace cloonel
#endif

View file

@ -7,6 +7,7 @@ namespace cloonel {
class Placeable { class Placeable {
public: public:
const float2& GetPos ( void ) const noexcept { return m_pos; } const float2& GetPos ( void ) const noexcept { return m_pos; }
void AddOffset ( const float2& parOffset ) noexcept { m_pos += parOffset; }
protected: protected:
Placeable ( float parX, float parY ); Placeable ( float parX, float parY );

View file

@ -6,13 +6,59 @@
#include <stdexcept> #include <stdexcept>
#include <cassert> #include <cassert>
#include <ciso646> #include <ciso646>
#include <png.h>
namespace cloonel { namespace cloonel {
namespace { namespace {
///--------------------------------------------------------------------- enum GraphicFormat {
///--------------------------------------------------------------------- GraphicFormat_Unknown,
SDL_Surface* LoadNewSurface (const std::string& parPath) { GraphicFormat_Png
assert(false); };
struct GraphicFormatItem {
const char* extension;
size_t length;
GraphicFormat enumvalue;
};
const GraphicFormatItem g_graphicFormatItems[] = {
{".png", 4, GraphicFormat_Png}
};
GraphicFormat GuessGraphicFormatFromName (const std::string& parPath) __attribute__((pure));
///--------------------------------------------------------------------
///--------------------------------------------------------------------
GraphicFormat GuessGraphicFormatFromName (const std::string& parPath) {
const size_t dotPos = parPath.find_last_of('.');
if (parPath.npos == dotPos) {
return GraphicFormat_Unknown;
}
const size_t extLen = parPath.size() - dotPos;
for (size_t z = 0; z < sizeof(g_graphicFormatItems) / sizeof(g_graphicFormatItems[0]); ++z) {
const GraphicFormatItem& currItem = g_graphicFormatItems[z];
if (currItem.length == extLen and parPath.compare(dotPos, currItem.length, currItem.extension) == 0) {
return currItem.enumvalue;
}
}
return GraphicFormat_Unknown;
}
///--------------------------------------------------------------------
///--------------------------------------------------------------------
SDL_Surface* LoadNewPngSurface (const std::string& parPath) {
PhysicsFSFile rawfile(parPath.c_str(), PhysicsFSFile::OpenMode_Read, "graphics");
unsigned char header[8];
assert(rawfile.IsOpen());
rawfile.Read(header, 8, 1);
if (png_sig_cmp(header, 0, 8))
return nullptr;
png_structp pngptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (not pngptr)
return nullptr;
return nullptr; return nullptr;
} }
} //unnamed namespace } //unnamed namespace
@ -46,8 +92,18 @@ namespace cloonel {
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
void Texture::Reload() { void Texture::Reload() {
const GraphicFormat fmt = GuessGraphicFormatFromName(m_path);
Destroy(); Destroy();
SDL_Surface* surf = SDL_LoadBMP(m_path.c_str());
SDL_Surface* surf = nullptr;
switch (fmt) {
case GraphicFormat_Png:
surf = LoadNewPngSurface(m_path.c_str());
default:
throw std::runtime_error(std::string("Unsupported file format for \"") + m_path + "\"");
}
if (nullptr == surf) if (nullptr == surf)
throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path)); throw std::runtime_error(GetFullErrorMessage(__PRETTY_FUNCTION__, m_path));