1 | #include <chrono> |
2 | #include <thread> |
3 |
|
4 | #include "Engine.hpp" |
5 | #include "ZydecoCommon.hpp" |
6 |
|
7 | Logger LOGGER("ENGINE"); |
8 |
|
9 | Engine::Engine(): |
10 | m_sdlWindow("Zydeco", SDL_WINDOW_MAXIMIZED), |
11 | m_tickLengthUs(1'000'000 / 60) |
12 | { |
13 | LOGGER.Log(Logger::INFO, "Initializing engine"); |
14 | } |
15 |
|
16 | Engine::~Engine() |
17 | { |
18 |
|
19 | } |
20 |
|
21 | void Engine::Start() |
22 | { |
23 | LOGGER.Log(Logger::INFO, "Entering engine loop"); |
24 |
|
25 | while (1) |
26 | { |
27 | auto current_time = std::chrono::high_resolution_clock::now(); |
28 | std::chrono::duration<double, std::micro> elapsed = current_time - m_tickStartTime; |
29 | m_tickStartTime = current_time; |
30 | if (!Update(elapsed.count())) { break; } |
31 |
|
32 | current_time = std::chrono::high_resolution_clock::now(); |
33 |
|
34 | if ((current_time - m_tickStartTime).count() < m_tickLengthUs) |
35 | { |
36 | std::this_thread::sleep_for(std::chrono::duration<double, std::micro>(16667)); |
37 | } |
38 | } |
39 | } |
40 |
|
41 | void Engine::Kill() |
42 | { |
43 |
|
44 | } |
45 |
|
46 | bool Engine::Update(uint64_t time_since_last_update_us) |
47 | { |
48 | m_tickStartTime = std::chrono::high_resolution_clock::now(); |
49 | // Update members |
50 | bool ret = m_sdlWindow.Update(time_since_last_update_us); |
51 | return ret; |
52 | } |
53 |
|
54 | /* |
55 | class Engine |
56 | { |
57 | public: |
58 | Engine(); |
59 | ~Engine(); |
60 | |
61 | void Start(); |
62 | void Kill(); |
63 | |
64 | void Update(); |
65 | |
66 | protected: |
67 | Window m_sdlWindow; |
68 | Scene m_scene; |
69 | Renderer m_renderer; |
70 | EventHandler m_eventHandler; |
71 | }; |
72 | */ |
73 |
|