-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar.cpp
38 lines (34 loc) · 1.4 KB
/
star.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
#include "includes/star.hpp"
#include <cmath>
float sineApprox(const float& v){
return (4 * v * (3.1415 - v)) / (49.345111 - (4 * v * (3.1415 - v)));
}
Star::Star(const sf::Vector2f& position, const float& radius): fRadius(radius), vPosition(position){
sf::Uint8 red_dim = (rand() / (float)RAND_MAX) * 32;
sf::Uint8 red_bright = (rand() / (float)RAND_MAX) * 128 + 64;
sf::Uint8 green_dim = (rand() / (float)RAND_MAX) * 85;
sf::Uint8 green_bright = 255;
sf::Uint8 blue = 255;
cBrightColor = sf::Color(red_bright, green_bright, blue);
cDimColor = sf::Color(red_dim, green_dim, blue);
cColorDiff = cBrightColor - cDimColor;
fOffset = (rand() / (float)RAND_MAX) * 3.1415;
shape = sf::CircleShape(fRadius);
fLifeTime = fOffset;
float sn = sineApprox(fLifeTime);
cCurrentColor = cDimColor + sf::Color(sn * cBrightColor.r, sn * cBrightColor.g, sn * cBrightColor.b);
shape.setFillColor(cCurrentColor);
shape.setPosition(vPosition);
}
void Star::draw(sf::RenderTarget &target, sf::RenderStates states) const{
target.draw(shape, states);
}
void Star::update(sf::Time timeElapsed){
fLifeTime += timeElapsed.asSeconds();
if (fLifeTime > 3.1415){
fLifeTime -= 3.1415;
}
float sn = sineApprox(fLifeTime);
cCurrentColor = cDimColor + sf::Color(sn * cBrightColor.r, sn * cBrightColor.g, sn * cBrightColor.b);
shape.setFillColor(cCurrentColor);
}