-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
143 lines (125 loc) · 4.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
#include <iostream>
#include <thread>
#include <SFML/Graphics.hpp>
#include <atomic>
#include "Globals.h"
#include "Creature/ObstacleThread.h"
#include "Utils/SoundManager.h"
#include "Utils/TextureManager.h"
#include "UI/GameInterface.h"
#include "Utils/FontManager.h"
auto run() -> void;
auto resetGame() -> void;
std::atomic<bool> isSpawningThreadActive(false);
static sf::Event event;
int main() {
// Setup the textures
TextureManager::setupTextures();
GameInterface::setupUI();
SoundManager::setupSounds();
FontManager::setupFonts();
FontManager::setupTexts();
// The gameloop etc.
setGameState(STATE_PLAY);
dino.setup();
event = sf::Event();
// Variables for delta time
// I learned how to do that from "RyiSnow", video: https://youtu.be/VpH33Uw-_0E?list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&t=1072
// 1000000000 is 1 second (billion nanoseconds)
double drawInterval = 1000000000 / 90; // ≈ 0.0167 seconds ( for 1 / 60 )
double delta = 0;
long long lastTime = std::chrono::high_resolution_clock::now().time_since_epoch().count();
long long currentTime;
long long timer = 0;
while (window.isOpen()){
// Implement delta time (game loop)
// Its REQUIRED when you want app to work the same on different devices
// ...that have different frame rates <-> are faster
currentTime = std::chrono::high_resolution_clock::now().time_since_epoch().count();
delta += (currentTime - lastTime) / drawInterval;
timer += currentTime - lastTime;
lastTime = currentTime;
if (delta >= 1) {
run();
delta--;
}
if (timer >= 1000000000 / 4) { // runs every quarter of a second
if(gameState == STATE_PLAY) {
increaseGameScore();
// std::cout << game_scoreNow << "\n";
}
timer = 0;
}
}
return 0;
}
auto run() -> void {
if(gameState == STATE_PLAY) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
if (dino.isInTheAir(dashFallingValue)) {
dino.sprite.move(0, dashFallingValue);
dino.hitbox.move(0, dashFallingValue);
}
}
GameInterface::drawPlayScreen();
if(!isSpawningThreadActive.load()) {
isSpawningThreadActive.store(true);
std::thread spawnThread([&]() {
ObstacleThread::create();
isSpawningThreadActive.store(false); // Reset the flag when the thread finishes
});
spawnThread.detach();
}
} else if(gameState == STATE_GAMEOVER) {
GameInterface::drawGameOverScreen();
}
while (window.pollEvent(event)){
switch(event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
window.setSize(originalSize);
case sf::Event::LostFocus:
// something
case sf::Event::KeyPressed:
// Restart the game when gameover
if(gameState == STATE_GAMEOVER) {
resetGame();
continue;
}
switch(event.key.code) {
case sf::Keyboard::Up:
case sf::Keyboard::Space: {
if (dino.isInTheAir()) continue;
SoundManager::playSound("jump");
std::thread jumpThread(&Dino::jump, &dino);
jumpThread.detach();
break;
}
case sf::Keyboard::Down:
{
if (dino.getState() == IS_DASHING) continue;
dino.dash();
break;
}
default:
break;
}
default:
break;
//std::cout << "Unknown event type \n";
}
}
}
auto resetGame() -> void {
if(game_scoreNow > game_scoreTop) {
game_scoreTop = game_scoreNow;
text_scoreTop.setString("H " + formatHighScore(game_scoreTop));
}
game_scoreNow = 0;
ObstacleThread::obstacles.clear();
dino.reset();
setGameState(STATE_PLAY);
}