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:
parent
fb92097419
commit
93a7796c5a
3 changed files with 37 additions and 2 deletions
|
@ -3,13 +3,30 @@
|
|||
#include "sdlmain.hpp"
|
||||
#include <sstream>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <ciso646>
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "game.hpp"
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <ciso646>
|
||||
|
||||
namespace {
|
||||
///-------------------------------------------------------------------------
|
||||
|
@ -12,7 +13,7 @@ namespace {
|
|||
do {
|
||||
const float delta = parGame.Exec();
|
||||
totalElapsed += delta;
|
||||
} while (totalElapsed < 15.0f);
|
||||
} while (not parGame.WantsToQuit());
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
|
|
Loading…
Reference in a new issue