New code and data to get something on screen.

WiP.
This commit is contained in:
King_DuckZ 2014-02-09 00:32:11 +01:00
parent 2429229a95
commit a8f8d50129
10 changed files with 157 additions and 0 deletions

View file

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

BIN
resources/graphics/duck.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

38
src/game.cpp Normal file
View file

@ -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<Texture>(nullptr));
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::OnRender() {
m_character->Render();
}
} //namespace cloonel

25
src/game.hpp Normal file
View file

@ -0,0 +1,25 @@
#ifndef id0E85DAFBFF5D497E8B3699AED806840A
#define id0E85DAFBFF5D497E8B3699AED806840A
#include "gamebase.hpp"
#include <memory>
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<Texture> m_character;
};
} //namespace cloonel
#endif

38
src/gamebase.cpp Normal file
View file

@ -0,0 +1,38 @@
#include "gamebase.hpp"
#include "texture.hpp"
#include "sdlmain.hpp"
#include <sstream>
#include <SDL2/SDL.h>
namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
GameBase::GameBase (SDLMain* parSdlMain) :
m_sdlmain(parSdlMain)
{
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
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)));
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void GameBase::Exec (float parDelta) {
SDL_Renderer* const ren = m_sdlmain->GetRenderer();
SDL_RenderClear(ren);
OnRender();
SDL_RenderPresent(ren);
SDL_Delay(2000);
}
} //namespace cloonel

30
src/gamebase.hpp Normal file
View file

@ -0,0 +1,30 @@
#ifndef id8C7FE975525B4329BFBEAF364D934EAD
#define id8C7FE975525B4329BFBEAF364D934EAD
#include <memory>
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<Texture> LoadTexture ( const char* parPath );
private:
virtual void OnRender ( void ) = 0;
SDLMain* const m_sdlmain;
};
} //namespace cloonel
#endif

View file

@ -1,5 +1,6 @@
#include "CloonelJumpConfig.h"
#include "sdlmain.hpp"
#include "game.hpp"
#include <iostream>
#include <stdexcept>
@ -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;
}

View file

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

View file

@ -3,8 +3,11 @@
#include "sdlmain.hpp"
#include <SDL2/SDL.h>
#include <stdexcept>
#include <cassert>
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

View file

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