-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulation.hpp
67 lines (59 loc) · 1.75 KB
/
simulation.hpp
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
#ifndef _SIMULATION_HPP_
#define _SIMULATION_HPP_
#include <queue>
#include <vector>
#include <memory>
#include <exception>
#include <SDL2/SDL.h>
#include "event.hpp"
class SimulationError : public std::runtime_error {
public:
explicit SimulationError(const std::string msg) :
std::runtime_error(msg) {};
virtual ~SimulationError() {};
};
class Simulation {
private:
int width;
int height;
int fps;
int speed;
int delay_ms;
double now;
SDL_Window *window;
SDL_Renderer *renderer;
std::vector<Particle *> particles;
bool is_paused;
// Comparator for priority queue making it a min queue.
class EventsCompare {
public:
bool operator() (const Event *ev1, const Event *ev2) const {
return std::less<double>()(ev2->getTime(), ev1->getTime());
};
};
// priority queue in C++ is so bloody insane
std::priority_queue<Event*, std::vector<Event*>, EventsCompare> events;
void moveParticles(double dt);
void refresh();
void drawLine(int x0, int y0, int x1, int y1);
void drawDisk(int x0, int y0, int radius);
void resetBackgroundColor();
void initializeEvents();
void addWallCollisionEvent(Particle &p, WallType wtype);
void predictCollisions(Particle &p);
int simulationTimeToMS(double sim_time) const;
double MSToSimulationTime(int ms) const;
public:
Simulation(int width, int height, int tick_time_ms);
virtual ~Simulation();
void addParticle(double x, double y, double vx, double vy,
double radius, int mass, int r, int g, int b);
bool paused() const;
void pause();
void resume();
void incSpeed();
void decSpeed();
int getSpeed() const;
void tick();
};
#endif /* _SIMULATION_HPP_ */