Draw and move the texture.

Display the test texture and move it on a line.
The program is unresponsive to events and quits automatically after 15 seconnds.
This commit is contained in:
King_DuckZ 2014-02-09 01:19:40 +01:00
parent a8f8d50129
commit fb92097419
7 changed files with 50 additions and 10 deletions

View file

@ -9,7 +9,9 @@ namespace cloonel {
///------------------------------------------------------------------------
Game::Game (SDLMain* parSdlMain) :
GameBase(parSdlMain),
m_character(nullptr)
m_character(nullptr),
m_posx(0.0f),
m_posy(0.0f)
{
}
@ -33,6 +35,15 @@ namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::OnRender() {
m_character->Render();
const int posx = static_cast<int>(m_posx + 0.5f);
const int posy = static_cast<int>(m_posy + 0.5f);
m_character->Render(posx, posy);
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Game::OnUpdate (float parDelta) {
m_posx += parDelta * 15.0f;
m_posy += parDelta * 22.8f;
}
} //namespace cloonel

View file

@ -17,8 +17,11 @@ namespace cloonel {
private:
virtual void OnRender ( void );
virtual void OnUpdate ( float parDelta );
std::unique_ptr<Texture> m_character;
float m_posx;
float m_posy;
};
} //namespace cloonel

View file

@ -8,7 +8,8 @@ namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
GameBase::GameBase (SDLMain* parSdlMain) :
m_sdlmain(parSdlMain)
m_sdlmain(parSdlMain),
m_time0(SDL_GetTicks())
{
}
@ -27,12 +28,18 @@ namespace cloonel {
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void GameBase::Exec (float parDelta) {
float GameBase::Exec() {
const unsigned int time1 = SDL_GetTicks();
const float delta = static_cast<float>(time1 - m_time0) * 0.001;
m_time0 = time1;
SDL_Renderer* const ren = m_sdlmain->GetRenderer();
SDL_RenderClear(ren);
OnUpdate(delta);
OnRender();
SDL_RenderPresent(ren);
SDL_Delay(2000);
return delta;
}
} //namespace cloonel

View file

@ -9,7 +9,7 @@ namespace cloonel {
class GameBase {
public:
void Exec ( float parDelta );
float Exec ( void );
protected:
explicit GameBase ( SDLMain* parSdlMain );
@ -22,8 +22,10 @@ namespace cloonel {
private:
virtual void OnRender ( void ) = 0;
virtual void OnUpdate ( float parDelta ) = 0;
SDLMain* const m_sdlmain;
unsigned int m_time0;
};
} //namespace cloonel

View file

@ -4,6 +4,18 @@
#include <iostream>
#include <stdexcept>
namespace {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
void RunMainLoop (cloonel::Game& parGame) {
float totalElapsed = 0.0f;
do {
const float delta = parGame.Exec();
totalElapsed += delta;
} while (totalElapsed < 15.0f);
}
} //unnamed namespace
///----------------------------------------------------------------------------
///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html
///----------------------------------------------------------------------------
@ -21,7 +33,7 @@ int main() {
cloonel::Game game(&sdlmain);
game.Prepare();
game.Exec(0.0f);
RunMainLoop(game);
std::cout << "Quitting now" << std::endl;
return 0;

View file

@ -42,12 +42,15 @@ namespace cloonel {
m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf);
SDL_FreeSurface(surf);
if (m_texture)
SDL_QueryTexture(m_texture, nullptr, nullptr, &m_width, &m_height);
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
void Texture::Render() {
void Texture::Render (int parX, int parY) {
assert(IsLoaded());
SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, nullptr);
const SDL_Rect dest = { parX, parY, m_width, m_height };
SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest);
}
} //namespace cloonel

View file

@ -16,12 +16,14 @@ namespace cloonel {
void Reload ( void );
void Destroy ( void ) noexcept;
bool IsLoaded ( void ) const { return nullptr != m_texture; }
void Render ( void );
void Render ( int parX, int parY );
private:
const std::string m_path;
SDL_Texture* m_texture;
SDLMain* const m_sdlmain;
int m_width;
int m_height;
};
} //namespace cloonel