Lugdunum  0.1.0
Application.cpp
Go to the documentation of this file.
1 #include <chrono>
3 #include <lug/System/Clock.hpp>
5 
6 namespace lug {
7 namespace Core {
8 
9 Application::Application(const Application::Info& info) : _info{info}, _graphics{info.name, info.version} {}
10 
11 bool Application::init(int argc, char* argv[]) {
12  return beginInit(argc, argv) && finishInit();
13 }
14 
15 bool Application::beginInit(int argc, char* argv[]) {
16  // TODO: Use argc and argv to parse the command line
17  (void)(argc);
18  (void)(argv);
19 
21  return false;
22  }
23 
24  return true;
25 }
26 
28  if (!_graphics.finishInit()) {
29  return false;
30  }
31 
33 
34  if (!_window) {
35  return false;
36  }
37 
38  return true;
39 }
40 
42  float elapsed = 0;
43  uint32_t frames = 0;
44 
45  System::Clock clock;
46 
47  while (!_closed && _window && _window->isOpen()) {
48  const auto elapsedTime = clock.reset();
49 
50  // Poll events
51  {
52  lug::Window::Event event;
53 
54  while (_window->pollEvent(event)) {
55  onEvent(event);
56  }
57  }
58 
59  if (!beginFrame(elapsedTime)) {
60  LUG_LOG.error("Application::run: Can't begin frame");
61  return false;
62  }
63 
64  onFrame(elapsedTime);
65 
66  if (!endFrame()) {
67  LUG_LOG.error("Application::run: Can't end frame");
68  return false;
69  }
70 
71  elapsed += elapsedTime.getSeconds<float>();
72  frames++;
73 
74  if (elapsed >= 1.0f) {
75  LUG_LOG.info("FPS: {}", frames / elapsed);
76  frames = 0;
77  elapsed = 0;
78  }
79  }
80 
81  return true;
82 }
83 
85  _closed = true;
86 }
87 
88 bool Application::beginFrame(const lug::System::Time& elapsedTime) {
89  return _graphics.getRenderer()->beginFrame(elapsedTime);
90 }
91 
93  return _graphics.getRenderer()->endFrame();
94 }
95 
96 } // Core
97 } // lug
Represents an event.
Definition: Event.hpp:89
bool beginFrame(const lug::System::Time &elapsedTime)
Definition: Application.cpp:88
virtual void onEvent(const lug::Window::Event &event)=0
Override this function to handle an event.
Application(const Info &info)
Definition: Application.cpp:9
lug::Graphics::Graphics _graphics
virtual Render::Window * createWindow(Render::Window::InitInfo &initInfo)=0
bool init(int argc, char *argv[])
Init the application with the informations filled in the lug::Graphics::Graphics::InitInfo and lug::G...
Definition: Application.cpp:11
virtual bool endFrame()=0
Renderer * getRenderer() const
Gets the renderer.
Definition: Graphics.inl:16
virtual void onFrame(const System::Time &elapsedTime)=0
Override this function to handle a frame.
bool finishInit()
Finish the initialization of the application with the informations filled in the lug::Graphics::Rende...
Definition: Application.cpp:27
lug::Graphics::Graphics::InitInfo _graphicsInitInfo
bool finishInit()
Finish the initialization of the application with the informations filled in initInfo structure...
Definition: Graphics.cpp:43
bool beginInit(const InitInfo &initInfo)
Begin the initialization of the application with the informations filled in initInfo structure...
Definition: Graphics.cpp:21
lug::Graphics::Render::Window * _window
void close()
Close the application.
Definition: Application.cpp:84
bool run()
Run the application.
Definition: Application.cpp:41
#define LUG_LOG
Definition: Logger.hpp:73
virtual bool beginFrame(const lug::System::Time &elapsedTime)=0
bool isOpen() const
Determines if the window is open.
Definition: Window.cpp:193
virtual bool pollEvent(lug::Window::Event &event)
Checks if an event is available from the window implementation, and fill it in the event parameter...
Definition: Window.cpp:197
bool beginInit(int argc, char *argv[])
Begin the initialization of the application with the informations filled in the lug::Graphics::Graphi...
Definition: Application.cpp:15
lug::Graphics::Render::Window::InitInfo _renderWindowInitInfo