-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (53 loc) · 1.91 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
#include <SDL.h>
#include <time.h>
#include <math.h>
#include "Screen.h"
#include "Swarm.h"
#include "Particle.h"
int main(int argc, char *argv[])
{
Screen screen;
Swarm swarm;
srand(time(nullptr));
if (!screen.init())
{
return -1;
}
while(true)
{
int elapsedTime = SDL_GetTicks(); // Time between program start and end or running time
//screen.clear();
swarm.update(elapsedTime);
/*
* sin(x) : returning value between -1 and 1
* 1 + sin(x) : returning value between 0 and 2 (Cause color value cannot be minus)
* 1 + sin(x * 0.0001) : Making the value change more slowly and smoothly between 0-2.
* (1 + sin(x * 0.0001)) * 128 : returnin value between 0-255 (Color Range)
* */
unsigned char red = static_cast<unsigned char>((1 + sin(elapsedTime * 0.0001)) * 128);
unsigned char green = static_cast<unsigned char>((1 + sin(elapsedTime * 0.0002)) * 128);
unsigned char blue = static_cast<unsigned char>((1 + sin(elapsedTime * 0.0003)) * 128);
const Particle* const particles = swarm.getParticles();
if (particles == nullptr)
{
return -2;
}
for (int i = 0; i < Swarm::NPARTICLES; ++i)
{
Particle particle = particles[i];
// Screen borders scaled by 0 and 1. For this reason, 0.5 center of the screen.
// If we want to place the particle on border, we need a value 0 or 1
int x = static_cast<int>((particle.getX() + 0.5) * Screen::SCREEN_WIDTH / 2);
int y = static_cast<int>((particle.getY() - 0.5) * Screen::SCREEN_WIDTH / 2 + Screen::SCREEN_HEIGHT / 2);
screen.setPixel(x, y, red, green, blue);
}
screen.boxBlur();
screen.update();
if (!screen.processEvent())
{
break;
}
}
screen.close();
return 0;
}