-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
85 lines (80 loc) · 2.35 KB
/
main.cpp
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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <functional>
#include <memory>
#include <iostream>
#include <cmath>
#include <random>
#include "game.h"
#include "animation.h"
#include "game-consts.h"
#include "pterodactyl.h"
#include "ground.h"
#include "cloud.h"
#include "cactus.h"
#include "player.h"
using sf::RenderWindow,
sf::VideoMode,
sf::Event;
using Render = std::function<void(RenderWindow&)>;
using Update = std::function<void(float)>;
using KeyPressed = std::function<void(sf::Event::KeyEvent&)>;
using KeyReleased = std::function<void()>;
void loop(RenderWindow& window,
Update update = nullptr,
Render render = nullptr,
KeyPressed keypressed = nullptr,
KeyReleased keyreleased = nullptr) {
Clock clock;
Time elapsed = clock.restart();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::KeyPressed) {
if (keypressed != nullptr) keypressed(event.key);
} else if (event.type == sf::Event::KeyReleased) {
if (keyreleased != nullptr) keyreleased();
}
}
elapsed += clock.restart();
while (elapsed >= kUpdateMs) {
if (update) update(kUpdateMs.asSeconds());
elapsed -= kUpdateMs;
}
window.clear({247, 247, 247, 255});
if (render) render(window);
window.display();
}
}
int main() {
PtrPlayer p1 = PtrPlayer(new Player());
PtrGame game = PtrGame(new Game(p1));
sf::RenderWindow window(sf::VideoMode(kWidth, kHeight), "Dino Game");
Update update = [&](float dt) {
game->update(dt);
p1->update(dt);
};
Render render = [&](RenderWindow& rw) {
game->render(rw);
p1->render(rw);
};
KeyPressed keyp = [&](sf::Event::KeyEvent& k) {
if (game->hasGameOver() &&
k.code == sf::Keyboard::Enter) {
game = nullptr;
p1 = nullptr;
p1 = PtrPlayer(new Player());
game = PtrGame(new Game(p1));
} else {
p1->keyPressed(k);
}
};
KeyReleased keyr = [&]() {
p1->keyReleased();
};
loop(window, update, render, keyp, keyr);
return 0;
}