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.
This commit is contained in:
King_DuckZ 2014-02-09 21:15:43 +01:00
parent fb92097419
commit 93a7796c5a
3 changed files with 37 additions and 2 deletions

View file

@ -3,13 +3,30 @@
#include "sdlmain.hpp" #include "sdlmain.hpp"
#include <sstream> #include <sstream>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <ciso646>
namespace cloonel { 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) : GameBase::GameBase (SDLMain* parSdlMain) :
m_sdlmain(parSdlMain), m_sdlmain(parSdlMain),
m_time0(SDL_GetTicks()) m_time0(SDL_GetTicks()),
m_wantsToQuit(false)
{ {
} }
@ -40,6 +57,20 @@ namespace cloonel {
OnRender(); OnRender();
SDL_RenderPresent(ren); SDL_RenderPresent(ren);
m_wantsToQuit = DoEvents();
return delta; return delta;
} }
///------------------------------------------------------------------------
///------------------------------------------------------------------------
bool GameBase::WantsToQuit() const {
return m_wantsToQuit or ShouldQuit();
}
///------------------------------------------------------------------------
///------------------------------------------------------------------------
bool GameBase::ShouldQuit() const {
return false;
}
} //namespace cloonel } //namespace cloonel

View file

@ -10,6 +10,7 @@ namespace cloonel {
class GameBase { class GameBase {
public: public:
float Exec ( void ); float Exec ( void );
bool WantsToQuit ( void ) const;
protected: protected:
explicit GameBase ( SDLMain* parSdlMain ); explicit GameBase ( SDLMain* parSdlMain );
@ -23,9 +24,11 @@ namespace cloonel {
private: private:
virtual void OnRender ( void ) = 0; virtual void OnRender ( void ) = 0;
virtual void OnUpdate ( float parDelta ) = 0; virtual void OnUpdate ( float parDelta ) = 0;
virtual bool ShouldQuit ( void ) const;
SDLMain* const m_sdlmain; SDLMain* const m_sdlmain;
unsigned int m_time0; unsigned int m_time0;
bool m_wantsToQuit;
}; };
} //namespace cloonel } //namespace cloonel

View file

@ -3,6 +3,7 @@
#include "game.hpp" #include "game.hpp"
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <ciso646>
namespace { namespace {
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
@ -12,7 +13,7 @@ namespace {
do { do {
const float delta = parGame.Exec(); const float delta = parGame.Exec();
totalElapsed += delta; totalElapsed += delta;
} while (totalElapsed < 15.0f); } while (not parGame.WantsToQuit());
} }
} //unnamed namespace } //unnamed namespace