-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
130 lines (108 loc) · 3.6 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
#include <iostream>
#include <memory>
#include <exception>
#include <cstdlib>
#include <SDL2/SDL.h>
#include "simulation.hpp"
#include "pconfig.hpp"
static const int default_fps = 100;
static void usage(const char *appname)
{
std::cerr << "Usage: " << appname <<
" <width> <height> <config>" << std::endl;
exit(EXIT_FAILURE);
}
static void print_speed(int speed)
{
std::cout << "Speed: " << speed << "x" << std::endl;
}
int main(int argc, char *argv[])
{
if (argc != 4)
usage(argv[0]);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "Failed to init SDL: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
SDL_DisplayMode dmode;
if (SDL_GetDesktopDisplayMode(0, &dmode) != 0) {
std::cerr << "Failed to get display mode: " << SDL_GetError()
<< std::endl;
exit(EXIT_FAILURE);
}
int width = strtol(argv[1], NULL, 10);
int height = strtol(argv[2], NULL, 10);
if (width < 200 || height < 200) {
std::cerr << "Width/height can not be less than 200" << std::endl;
exit(EXIT_FAILURE);
}
if (width > dmode.w) {
std::cerr << "Width can not be greater than " << dmode.w
<< std::endl;
exit(EXIT_FAILURE);
}
if (height > dmode.h) {
std::cerr << "Height can not be greater than " << dmode.h
<< std::endl;
exit(EXIT_FAILURE);
}
try {
PConfig cfg(argv[3]);
Simulation simulation(width, height, default_fps);
while (true) {
std::unique_ptr<PConfigEntry> entry = cfg.nextEntry();
if (entry == nullptr)
break;
simulation.addParticle(entry->rx, entry->ry, entry->vx, entry->vy,
entry->radius, entry->mass,
entry->r, entry->g, entry->b);
}
while (true) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
SDL_Quit();
return 0;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_SPACE:
if (simulation.paused()) {
std::cout << "Resumed" << std::endl;
simulation.resume();
}
else {
std::cout << "Paused" << std::endl;
simulation.pause();
}
break;
case SDLK_UP:
simulation.incSpeed();
print_speed(simulation.getSpeed());
break;
case SDLK_DOWN:
simulation.decSpeed();
print_speed(simulation.getSpeed());
break;
}
}
}
simulation.tick();
}
}
catch (PConfigError &e) {
std::cerr << "Configuration file error: [l: " << e.getLine()
<< ", c: " << e.getColumn() << "]: " << e.what() << std::endl;
std::cerr << "Format: " << PConfig::formatString() << std::endl;
exit(EXIT_FAILURE);
}
catch (SimulationError &e) {
std::cerr << "Simulation error: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
catch (std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}