-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (108 loc) · 3.96 KB
/
Copy pathmain.cpp
File metadata and controls
132 lines (108 loc) · 3.96 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
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
#include "entities.hpp"
#include "manager.hpp"
#include <chrono>
#include <iostream>
#include <random>
#include <set>
#define WIN_X 1400
#define WIN_Y 900
#define MAX_FPS 120
void print_v(const v3f &v) { std::cout << v.x << ", " << v.y << ", " << v.z << std::endl; }
int main() {
// Create the main window
sf::RenderWindow window(sf::VideoMode(WIN_X, WIN_Y, 1), "Space--");
window.setFramerateLimit(MAX_FPS);
window.setKeyRepeatEnabled(false);
Manager man;
for (int i = 0; i < 12000; i++) {
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-2e5, 2e5);
man.AddObj<SpaceShip>("ship" + std::to_string(i),
{static_cast<float>(dist(e2)), static_cast<float>(dist(e2)),
static_cast<float>(dist(e2))});
}
auto player = man.AddObj<SpaceShip>("Player", {0, 0, -1000});
player->SetSpd(100.0f);
std::shared_ptr<Camera> cam = man.AddObj<Camera>("Camera", {0, 0, -200});
cam->Follow(player.get());
std::set<sf::Keyboard::Key> activeKeys;
// Pause
bool zaWardo = false;
std::chrono::high_resolution_clock::time_point last;
std::chrono::high_resolution_clock::time_point curr;
float fps = MAX_FPS;
sf::Font font;
font.loadFromFile("Tecnica-55.ttf");
auto text = sf::Text("FPS", font, 40);
last = std::chrono::high_resolution_clock::now();
// Start the game loop
while (window.isOpen()) {
// Process events
sf::Event event;
while (window.pollEvent(event)) {
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::P) {
zaWardo = !zaWardo;
}
if (event.type == sf::Event::KeyPressed) {
activeKeys.insert(event.key.code);
} else if (event.type == sf::Event::KeyReleased) {
activeKeys.erase(event.key.code);
}
}
// Calculate fps
curr = std::chrono::high_resolution_clock::now();
fps = 1.0e9 / std::chrono::duration_cast<std::chrono::nanoseconds>(curr - last).count();
last = curr;
if (zaWardo) {
continue;
}
for (auto key : activeKeys) {
switch (key) {
case sf::Keyboard::Key::W:
player->AddTorque(-player->GetBasis().as_vec(0));
break;
case sf::Keyboard::Key::S:
player->AddTorque(player->GetBasis().as_vec(0));
break;
case sf::Keyboard::Key::A:
player->AddTorque(-player->GetBasis().as_vec(2));
break;
case sf::Keyboard::Key::D:
player->AddTorque(player->GetBasis().as_vec(2));
break;
case sf::Keyboard::Key::E:
player->AddTorque(player->GetBasis().as_vec(1));
break;
case sf::Keyboard::Key::Q:
player->AddTorque(-player->GetBasis().as_vec(1));
break;
case sf::Keyboard::Key::R:
player->Accelerate();
break;
case sf::Keyboard::Key::F:
player->Decelerate();
break;
default:
break;
}
}
// Clear screen
window.clear();
// Debug info
v3f plr_pos = player->GetPos();
text.setString("FPS: " + std::to_string(fps) +
"\nSpeed: " + std::to_string(std::log(player->GetSpd())) +
"\nX: " + std::to_string(plr_pos.x) + "\nY: " + std::to_string(plr_pos.y) +
"\nZ: " + std::to_string(plr_pos.z));
window.draw(text);
man.Draw(window, *cam);
man.Update(1.0 / fps);
// Update the window
window.display();
}
}