From fb92097419ad513ed3fda14f9946bdf2a8bea128 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 9 Feb 2014 01:19:40 +0100 Subject: [PATCH] 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. --- src/game.cpp | 15 +++++++++++++-- src/game.hpp | 3 +++ src/gamebase.cpp | 13 ++++++++++--- src/gamebase.hpp | 4 +++- src/main.cpp | 14 +++++++++++++- src/texture.cpp | 7 +++++-- src/texture.hpp | 4 +++- 7 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 443fd5d..f534be3 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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(m_posx + 0.5f); + const int posy = static_cast(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 diff --git a/src/game.hpp b/src/game.hpp index 2402f28..dd8e0f3 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -17,8 +17,11 @@ namespace cloonel { private: virtual void OnRender ( void ); + virtual void OnUpdate ( float parDelta ); std::unique_ptr m_character; + float m_posx; + float m_posy; }; } //namespace cloonel diff --git a/src/gamebase.cpp b/src/gamebase.cpp index af8ab2e..3acdde3 100644 --- a/src/gamebase.cpp +++ b/src/gamebase.cpp @@ -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(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 diff --git a/src/gamebase.hpp b/src/gamebase.hpp index 118d690..6a2553b 100644 --- a/src/gamebase.hpp +++ b/src/gamebase.hpp @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 4a0f0f4..b9445eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,18 @@ #include #include +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; diff --git a/src/texture.cpp b/src/texture.cpp index c9f60e9..b4597f9 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -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 diff --git a/src/texture.hpp b/src/texture.hpp index 3f5e212..a78d5e7 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -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