-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
133 lines (115 loc) · 2.64 KB
/
Copy pathsimulator.cpp
File metadata and controls
133 lines (115 loc) · 2.64 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
133
#include "simulator.h"
#include "logger.h"
#include "graphics.h"
#include "config.h"
#include <omp.h>
#define SHITY_SDL_DEFINE
using namespace std;
using namespace simulator;
static bool running = true;
bool simulator::detailed_logging=false;
int simulator::mousex=0, simulator::mousey=0, simulator::width, simulator::height;
void init()
{
config::read("world.json");
simulator::width=config::cfg.get<int>("dimensions.w");
simulator::height=config::cfg.get<int>("dimensions.h");
if(!graphics::init())
{
cerr<<"graphics init failed\n";
exit(1);
}
if(!physics::init())
{
cerr<<"physics init failed\n";
exit(1);
}
omp_set_num_threads(8);
int x, y;
SDL_PumpEvents();
SDL_GetMouseState(&x, &y);
physics::add_black_hole(physics::black_hole(math::vec(x, y), 10000000));
}
void destroy()
{
graphics::destroy();
physics::destroy();
}
void handle_event(SDL_Event &evt, float dt)
{
if (evt.type == SDL_QUIT)
running = false;
if(evt.type == SDL_KEYDOWN)
{
if(evt.key.keysym.sym==SDLK_ESCAPE)
{
running=false;
}
}
if(evt.type == SDL_MOUSEBUTTONUP)
{
mousex=evt.button.x; mousey=simulator::height-evt.button.y;
logger::log("clicked at %d, %d", mousex, mousey);
if(evt.button.button==SDL_BUTTON_LEFT)
{
physics::blackholes_enabled=!physics::blackholes_enabled;
logger::log("blackholes enabled: %s", physics::blackholes_enabled?"YES":"NO");
}
if(evt.button.button==SDL_BUTTON_RIGHT)
{
detailed_logging=!detailed_logging;
logger::log("logging enabled: %s", detailed_logging?"YES":"NO");
}
}
if(evt.type==SDL_MOUSEMOTION)
{
mousex=evt.button.x; mousey=simulator::height-evt.button.y;
physics::black_holes[0].pos.Set(mousex, mousey);
}
}
void step(double dt)
{
physics::step(dt);
}
void render()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(200, 0, 0);
glScalef(2, 2, 2);
glTranslatef(-200, 0, 0);
physics::draw();
glPopMatrix();
}
int main( int argc, char *argv[] )
{
init();
SDL_Event evt;
uint64_t oldtime=SDL_GetPerformanceCounter();
uint64_t tickcount=0;
while(running)
{
tickcount++;
uint64_t newtime=SDL_GetPerformanceCounter();
double timestep=double(newtime-oldtime)/SDL_GetPerformanceFrequency();
oldtime=newtime;
if(timestep>very_long_time)
{
logger::log("last frame too slow (%lfs)", timestep);
timestep=expected_time;
}
while (SDL_PollEvent(&evt))
handle_event(evt, timestep);
if(timestep>0) //nesto se desilo, jer se za dt=0 ne moze nista desiti...
{
step(timestep);
}
render();
SDL_GL_SwapBuffers();
if(tickcount%50==0) logger::log("%.2lf fps", 1.0/timestep);
//SDL_Delay(1);
}
destroy();
return 0;
}