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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include "loopManager.h"
LoopManager::LoopManager() : inputSystem(std::make_unique<InputSystem>()) {
shutdownHandler = [this](const ShutDownEvent & event) { this->onShutdown(event); };
subscribe(shutdownHandler);
}
void LoopManager::processInput() {
inputSystem->processInput();
// SDL_Event event;
// SDL_PollEvent(&event);
// switch (event.type) {
// case SDL_QUIT:
// gameRunning = false;
// break;
// case SDL_KEYDOWN:
// triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
// break;
// case SDL_MOUSEBUTTONDOWN:
// int x, y;
// SDL_GetMouseState(&x, &y);
// triggerEvent(MousePressedEvent(x, y));
// break;
// }
}
void LoopManager::setRunning(bool running) { this->gameRunning = running; }
void LoopManager::fixedUpdate() {
//fprintf(stderr, "fixed update\n");
}
void LoopManager::loop() {
LoopTimer & timer = LoopTimer::getInstance();
timer.start();
while (gameRunning) {
timer.update();
processInput();
while (timer.getLag() >= timer.getFixedDeltaTime()) {
fixedUpdate();
timer.advanceFixedUpdate();
}
update();
render();
timer.enforceFrameRate();
}
window.destroyWindow();
}
void onKey(const KeyPressedEvent & e) {
int keyCode = e.getKeyCode();
std::cout << "keycode pressed: " << keyCode << std::endl;
}
void onMouse(const MousePressedEvent & e) {
fprintf(
stderr, "mouse Position X: %d Y: %d\n", e.getMousePosition().first,
e.getMousePosition().second
);
}
void LoopManager::setup() {
gameRunning = window.initWindow();
LoopTimer::getInstance().start();
LoopTimer::getInstance().setFPS(50);
EventHandler<KeyPressedEvent> callback = onKey;
subscribe<KeyPressedEvent>(callback, false);
EventHandler<MousePressedEvent> mouseCallback = onMouse;
subscribe<MousePressedEvent>(mouseCallback, false);
EventHandler<KeyPressedEvent> closeWindowCallback = [this](const KeyPressedEvent & e) {
if (e.getKeyCode() == Escape) {
this->setRunning(false);
}
};
subscribe<KeyPressedEvent>(closeWindowCallback, false);
Button * testButton = new Button(200, 200);
testButton->color = {100, 0, 100};
testButton->onClick = []() { std::cout << "Button was clicked" << std::endl; };
testButton->x = 200;
testButton->y = 200;
inputSystem->registerButton(testButton);
window.addUIObject(testButton);
TextInput * testInput = new TextInput(200, 200);
testInput->x = 100;
testInput->y = 100;
testInput->backgroundColor = {20, 50, 80};
inputSystem->registerTextInput(testInput);
window.addUIObject(testInput);
}
void LoopManager::render() {
//fprintf(stderr, "**********render********** \n");
if (gameRunning) {
window.renderUIObjects();
}
}
void LoopManager::onShutdown(const ShutDownEvent & e) { this->gameRunning = false; }
void LoopManager::update() {
//fprintf(stderr, "**********normal update********** \n");
LoopTimer & timer = LoopTimer::getInstance();
float delta = timer.getDeltaTime();
}
|