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:
parent
a8f8d50129
commit
fb92097419
7 changed files with 50 additions and 10 deletions
15
src/game.cpp
15
src/game.cpp
|
@ -9,7 +9,9 @@ namespace cloonel {
|
||||||
///------------------------------------------------------------------------
|
///------------------------------------------------------------------------
|
||||||
Game::Game (SDLMain* parSdlMain) :
|
Game::Game (SDLMain* parSdlMain) :
|
||||||
GameBase(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() {
|
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
|
} //namespace cloonel
|
||||||
|
|
|
@ -17,8 +17,11 @@ namespace cloonel {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void OnRender ( void );
|
virtual void OnRender ( void );
|
||||||
|
virtual void OnUpdate ( float parDelta );
|
||||||
|
|
||||||
std::unique_ptr<Texture> m_character;
|
std::unique_ptr<Texture> m_character;
|
||||||
|
float m_posx;
|
||||||
|
float m_posy;
|
||||||
};
|
};
|
||||||
} //namespace cloonel
|
} //namespace cloonel
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ namespace cloonel {
|
||||||
///------------------------------------------------------------------------
|
///------------------------------------------------------------------------
|
||||||
///------------------------------------------------------------------------
|
///------------------------------------------------------------------------
|
||||||
GameBase::GameBase (SDLMain* parSdlMain) :
|
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_Renderer* const ren = m_sdlmain->GetRenderer();
|
||||||
|
|
||||||
SDL_RenderClear(ren);
|
SDL_RenderClear(ren);
|
||||||
|
OnUpdate(delta);
|
||||||
OnRender();
|
OnRender();
|
||||||
SDL_RenderPresent(ren);
|
SDL_RenderPresent(ren);
|
||||||
SDL_Delay(2000);
|
|
||||||
|
return delta;
|
||||||
}
|
}
|
||||||
} //namespace cloonel
|
} //namespace cloonel
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace cloonel {
|
||||||
|
|
||||||
class GameBase {
|
class GameBase {
|
||||||
public:
|
public:
|
||||||
void Exec ( float parDelta );
|
float Exec ( void );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit GameBase ( SDLMain* parSdlMain );
|
explicit GameBase ( SDLMain* parSdlMain );
|
||||||
|
@ -22,8 +22,10 @@ namespace cloonel {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void OnRender ( void ) = 0;
|
virtual void OnRender ( void ) = 0;
|
||||||
|
virtual void OnUpdate ( float parDelta ) = 0;
|
||||||
|
|
||||||
SDLMain* const m_sdlmain;
|
SDLMain* const m_sdlmain;
|
||||||
|
unsigned int m_time0;
|
||||||
};
|
};
|
||||||
} //namespace cloonel
|
} //namespace cloonel
|
||||||
|
|
||||||
|
|
14
src/main.cpp
14
src/main.cpp
|
@ -4,6 +4,18 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#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
|
///following http://twinklebeardev.blogspot.co.uk/2012/07/lesson-1-hello-world.html
|
||||||
///----------------------------------------------------------------------------
|
///----------------------------------------------------------------------------
|
||||||
|
@ -21,7 +33,7 @@ int main() {
|
||||||
|
|
||||||
cloonel::Game game(&sdlmain);
|
cloonel::Game game(&sdlmain);
|
||||||
game.Prepare();
|
game.Prepare();
|
||||||
game.Exec(0.0f);
|
RunMainLoop(game);
|
||||||
|
|
||||||
std::cout << "Quitting now" << std::endl;
|
std::cout << "Quitting now" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -42,12 +42,15 @@ namespace cloonel {
|
||||||
|
|
||||||
m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf);
|
m_texture = SDL_CreateTextureFromSurface(m_sdlmain->GetRenderer(), surf);
|
||||||
SDL_FreeSurface(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());
|
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
|
} //namespace cloonel
|
||||||
|
|
|
@ -16,12 +16,14 @@ namespace cloonel {
|
||||||
void Reload ( void );
|
void Reload ( void );
|
||||||
void Destroy ( void ) noexcept;
|
void Destroy ( void ) noexcept;
|
||||||
bool IsLoaded ( void ) const { return nullptr != m_texture; }
|
bool IsLoaded ( void ) const { return nullptr != m_texture; }
|
||||||
void Render ( void );
|
void Render ( int parX, int parY );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string m_path;
|
const std::string m_path;
|
||||||
SDL_Texture* m_texture;
|
SDL_Texture* m_texture;
|
||||||
SDLMain* const m_sdlmain;
|
SDLMain* const m_sdlmain;
|
||||||
|
int m_width;
|
||||||
|
int m_height;
|
||||||
};
|
};
|
||||||
} //namespace cloonel
|
} //namespace cloonel
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue