GameKit  0.0.1a
C++ gamedev tools
CoreApplication.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: CoreApplication.cpp
5  *
6  * Description:
7  *
8  * Created: 14/06/2018 02:00:18
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include <ctime>
15 
16 #include "gk/audio/AudioPlayer.hpp"
18 #include "gk/core/Mouse.hpp"
19 #include "gk/core/Exception.hpp"
20 
21 namespace gk {
22 
23 CoreApplication::CoreApplication(int argc, char **argv) : m_argumentParser(argc, argv) {
24 }
25 
27  std::srand(std::time(nullptr));
28 
30 
33 
34  m_argumentParser.addArgument("mute", {"", "--no-sound"});
38  // m_argumentParser.debug();
39 }
40 
41 int CoreApplication::run(bool isProtected) {
42  auto runGame = [&]() {
43  if (m_loadSDL)
44  m_sdlLoader.load();
45 
46  init();
47  mainLoop();
48  };
49 
50  if (isProtected) {
51  try {
52  runGame();
53  }
54  catch(const Exception &e) {
55  std::cerr << "Fatal error " << e.what() << std::endl;
56  return 1;
57  }
58  // catch(const std::exception &e) {
59  // std::cerr << "Exception caught: " << e.what() << std::endl;
60  // return 1;
61  // }
62  // catch(...) {
63  // std::cerr << "Fatal error: Unknown error." << std::endl;
64  // return 1;
65  // }
66  }
67  else {
68  runGame();
69  }
70 
71  return 0;
72 }
73 
74 void CoreApplication::createWindow(u16 screenWidth, u16 screenHeight, const char *windowTitle) {
75  m_window.open(windowTitle, screenWidth, screenHeight);
76 }
77 
78 void CoreApplication::onEvent(const SDL_Event &event) {
79  if (event.type == SDL_QUIT) {
80  m_window.close();
81  }
82 }
83 
85  SDL_Event event;
86  while (SDL_PollEvent(&event)) {
87  onEvent(event);
88 
89  if (!m_stateStack.empty())
90  m_stateStack.top().onEvent(event);
91  }
92 }
93 
95  while(m_window.isOpen() && m_stateStack.size()) {
96  handleEvents();
97 
98  m_clock.updateGame([&] {
99  if (!m_stateStack.empty())
100  m_stateStack.top().update();
101 
103  });
104 
105  m_clock.drawGame([&] {
106  m_window.clear();
107 
108  if(!m_stateStack.empty())
110 
111  m_window.display();
112  });
113  }
114 }
115 
116 } // namespace gk
117 
virtual void onEvent(const SDL_Event &event)
This function is called when a new window event is received.
ResourceHandler m_resourceHandler
Container for all game resources.
static void setWindow(Window *window)
Definition: Mouse.hpp:26
ApplicationState & top() const
Get the top ApplicationState in the stack.
Window m_window
The main window.
CoreApplication(int argc, char **argv)
Constructor.
GameClock m_clock
Simulated time system.
static void setMuteState(bool muteState)
Definition: AudioPlayer.hpp:36
virtual const char * what() const noexcept
Definition: Exception.hpp:36
void createWindow(u16 screenWidth, u16 screenHeight, const char *windowTitle)
Open window.
unsigned short u16
Definition: IntTypes.hpp:22
virtual void update()=0
Execute actions every game tick.
RenderStates m_renderStates
The default render states.
void clearDeletedStates()
Clear the removed states of the stack.
std::size_t size() const
Get the amount of ApplicationState in the stack.
virtual void init()
Initialization function.
int run(bool isProtected=true)
Run the application.
virtual void onEvent(const SDL_Event &)
Do an action in response to an SDL event.
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
void display()
Definition: Window.cpp:65
bool empty() const
Check if the container is empty.
void open(const std::string &caption, u16 width, u16 height)
Definition: Window.cpp:21
void drawGame(std::function< void(void)> drawFunc)
Definition: GameClock.cpp:56
const Argument & getArgument(const std::string &name)
static void setInstance(ApplicationStateStack &instance)
Set the current singleton instance.
bool m_loadSDL
If this flag is set to false, SDL won&#39;t be loaded.
ApplicationStateStack m_stateStack
Stack containing application states.
virtual void handleEvents()
Poll window events and send them to onEvent() and ApplicationStateStack.
void updateGame(std::function< void(void)> updateFunc)
Definition: GameClock.cpp:43
bool isOpen() const
Definition: Window.hpp:38
void addArgument(const std::string &name, const Argument &argument)
void close()
Definition: Window.hpp:37
static void setInstance(ResourceHandler &handler)
virtual void mainLoop()
Game main loop, automatically called by run()
SDLLoader m_sdlLoader
Init and free SDL.
ArgumentParser m_argumentParser
Helper for argument management.
void clear()
Definition: Window.cpp:61