From 93a7796c5a28eb2fa2478136c57f93a85505f968 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 9 Feb 2014 21:15:43 +0100 Subject: [PATCH] User can quit manually. The program doesn't run for a fixed time anymore. Instead it listens to events and user can quit by closing the window. --- src/gamebase.cpp | 33 ++++++++++++++++++++++++++++++++- src/gamebase.hpp | 3 +++ src/main.cpp | 3 ++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/gamebase.cpp b/src/gamebase.cpp index 3acdde3..3cc3787 100644 --- a/src/gamebase.cpp +++ b/src/gamebase.cpp @@ -3,13 +3,30 @@ #include "sdlmain.hpp" #include #include +#include namespace cloonel { + namespace { + ///--------------------------------------------------------------------- + ///--------------------------------------------------------------------- + bool DoEvents() { + SDL_Event eve; + while (SDL_PollEvent(&eve)) { + switch (eve.type) { + case SDL_QUIT: + return true; + } + } + return false; + } + } //unnamed namespace + ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ GameBase::GameBase (SDLMain* parSdlMain) : m_sdlmain(parSdlMain), - m_time0(SDL_GetTicks()) + m_time0(SDL_GetTicks()), + m_wantsToQuit(false) { } @@ -40,6 +57,20 @@ namespace cloonel { OnRender(); SDL_RenderPresent(ren); + m_wantsToQuit = DoEvents(); + return delta; } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + bool GameBase::WantsToQuit() const { + return m_wantsToQuit or ShouldQuit(); + } + + ///------------------------------------------------------------------------ + ///------------------------------------------------------------------------ + bool GameBase::ShouldQuit() const { + return false; + } } //namespace cloonel diff --git a/src/gamebase.hpp b/src/gamebase.hpp index 6a2553b..ca70571 100644 --- a/src/gamebase.hpp +++ b/src/gamebase.hpp @@ -10,6 +10,7 @@ namespace cloonel { class GameBase { public: float Exec ( void ); + bool WantsToQuit ( void ) const; protected: explicit GameBase ( SDLMain* parSdlMain ); @@ -23,9 +24,11 @@ namespace cloonel { private: virtual void OnRender ( void ) = 0; virtual void OnUpdate ( float parDelta ) = 0; + virtual bool ShouldQuit ( void ) const; SDLMain* const m_sdlmain; unsigned int m_time0; + bool m_wantsToQuit; }; } //namespace cloonel diff --git a/src/main.cpp b/src/main.cpp index b9445eb..17b5309 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "game.hpp" #include #include +#include namespace { ///------------------------------------------------------------------------- @@ -12,7 +13,7 @@ namespace { do { const float delta = parGame.Exec(); totalElapsed += delta; - } while (totalElapsed < 15.0f); + } while (not parGame.WantsToQuit()); } } //unnamed namespace