-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
77 lines (62 loc) · 1.98 KB
/
Game.cpp
File metadata and controls
77 lines (62 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include "Config.hpp"
#include "GameLoop.hpp"
#include "IObject.hpp"
#include "RenderData.hpp"
#include "Renderer2D.hpp"
#include "Scroller.hpp"
#include "VideoContextSDL.hpp"
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Single_Window.H>
#include <FL/Fl_Window.H>
#include <FL/x.H>
#undef main
int main(int, char **) {
Fl::scheme("gtk+");
int w;
int h;
SDL_Init(SDL_INIT_VIDEO);
VideoContextSDL::getPrimaryDisplayResolution(w, h);
SDL_Quit();
std::cout << w << " " << h << std::endl;
// Create main window
auto window = new Fl_Window(w, h, "RTS");
window->resizable(window);
window->fullscreen();
window->show();
GameLoop game;
// SDL rendering thread
std::thread t([&game, window]() {
VideoContextSDL::Create(fl_xid(window));
VideoContextSDL::GetInstance()->setup();
IRenderer *renderer = new Renderer2D{VideoContextSDL::GetInstance()};
RenderData renderFrame{};
game.Start(renderFrame, *renderer);
while (true) {
const float expectedMS{
(float)(1000.0 / VideoContextSDL::GetInstance()->getFps())};
const auto start{std::chrono::steady_clock::now()};
static Scroller scroller{expectedMS};
scroller.execute();
static std::chrono::time_point<std::chrono::steady_clock> timestamp{};
static std::vector<IObject *> lastRender;
renderFrame.GetRenderData(lastRender, timestamp);
renderer->Render(scroller.GetCameraPos(), scroller.getScale(), lastRender,
timestamp);
const auto end{std::chrono::steady_clock::now()};
const auto elapsedMS{
(float)(std::chrono::duration_cast<std::chrono::milliseconds>(end -
start)
.count())};
if (expectedMS > elapsedMS) {
renderer->Delay((size_t)(expectedMS - elapsedMS));
}
}
});
t.detach();
return Fl::run();
}