-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
127 lines (103 loc) · 2.74 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
/*
* Particle simulation program using the SDL library.
* Based on examples by John Purcell's (from Cave of Programming).
* The first argument should be a number between 1-4, to choose the mode to
* display (no arguments for flower pattern of polar coordinates particles); the
* second one (any value) will disable screen clearing (enabled by default); and
* the third one (any value) will create a box blur effect.
*
* @author J. Alvarez
*/
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "Screen.hpp"
#include "Swarm.hpp"
#include "SDL2/SDL.h"
#include "RectangularParticle.hpp"
using namespace ParticleSimulation;
int main(int argc, char ** argv) {
srand(time(NULL));
Screen screen;
if (!screen.init()) {
std::cout << "Error initializing screen" << std::endl;
return -1;
}
int swarmType = 0;
int mode = 0;
bool clearScreen = true;
bool boxBlur = false;
if (argc >= 2) {
switch (std::stoi(argv[1])) {
case 1:
swarmType = Swarm::TYPE::POLAR;
mode = Particle::Mode::POLR_FLWR;
break;
case 2:
swarmType = Swarm::TYPE::POLAR;
mode = Particle::Mode::POLR_SPRL;
break;
case 3:
swarmType = Swarm::TYPE::RECT;
mode = Particle::Mode::RECT_RECT;
break;
case 4:
swarmType = Swarm::TYPE::RECT;
mode = Particle::Mode::RECT_CIRC;
break;
default:
swarmType = Swarm::TYPE::RECT;
mode = Particle::Mode::RECT_IMPD;
break;
}
if (argc >= 3)
clearScreen = false;
if (argc >= 4)
boxBlur = true;
}
else {
swarmType = Swarm::TYPE::POLAR;
mode = Particle::Mode::POLR_FLWR;
}
Swarm swarm(swarmType, mode);
bool quit = false;
while (!quit) {
int elapsed = SDL_GetTicks();
Uint8 red = (1 + sin(elapsed * 0.001)) * 128;
Uint8 green = (1 + sin(elapsed * 0.0002)) * 128;
Uint8 blue = (1 + sin(elapsed * 0.0003)) * 128;
swarm.update(elapsed);
Particle * * particles = swarm.getParticles();
if (clearScreen)
screen.clear();
for (int i = 0; i < Swarm::N_PARTICLES; i++) {
Particle * particle = particles[i];
double coord1 = particle->m_coord1;
double coord2 = particle->m_coord2;
int x = 0;
int y = 0;
if (swarmType == Swarm::TYPE::RECT) {
if (mode == Particle::Mode::RECT_RECT || mode
== Particle::Mode::RECT_IMPD)
x = (coord1 + 1) * Screen::S_WIDTH / 2;
else
x = coord1 * Screen::S_HEIGHT / 2 + Screen::S_WIDTH / 2;
y = (coord2 + 1) * Screen::S_HEIGHT / 2;
}
else {
x = (coord1 * cos(coord2)) + Screen::S_WIDTH / 2;
y = (coord1 * sin(coord2)) + Screen::S_HEIGHT / 2;
}
screen.setPixel(x, y, red, green, blue);
}
if (boxBlur)
screen.boxBlur();
screen.update();
if(!screen.processEvents())
quit = true;
}
screen.close();
return 0;
}