-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackgroundWater.hpp
92 lines (81 loc) · 2.54 KB
/
BackgroundWater.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
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
#include "PriDrawable.hpp"
#include <iostream>
#include <cmath>
#include <random>
#include <string>
#include <thread>
#include <mutex>
class BackgroundWater : public PriDrawable {
private:
int width, height;
std::string name;
std::mutex mtx;
public:
BackgroundWater(int width, int height) : PriDrawable(0) {
this->width = width;
this->height = height;
name = "water";
ticks = 0;
populateFrames(10);
}
void populateFrames(int frames) {
for(int i = 0; i < frames; i++) {
std::cout << "Generating frame " << i << " of " << frames << std::endl;
std::cout << "texture size is " << textures.size() << std::endl;
std::thread textureThread(&BackgroundWater::addFrameToTextures, this);
textureThread.detach();
}
//resetToDefaultTexture();
}
void addFrameToTextures() {
mtx.lock();
auto newBackground = regenerate();
textures.push_back(newBackground);
mtx.unlock();
}
void animate() {
if(ticks == 10) {
ticks = 0;
if(textures.size() > currentTextureIndex) {
setTexture(*textures[currentTextureIndex]);
currentTextureIndex = (currentTextureIndex + 1) % 10;
}
}
else {
ticks++;
}
}
~BackgroundWater() {
std::cout << "DESTROYED!!!!" << std::endl;
}
sf::Texture * regenerate() {
auto temp = new sf::Texture();
sf::Image img;
img.create(width, height, sf::Color::Black);
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_int_distribution<int> dist(10, 150);
sf::Color currentColor = getNewColor();
for(int y = 0; y < height; y++) {
int x = 0;
while(x < width) {
int iters = dist(e2);
currentColor = getNewColor();
for(int i = 0; i < iters; i++) {
if(x + i < width) {
img.setPixel(x + i, y, currentColor);
}
}
x += iters;
}
}
temp->loadFromImage(img);
return temp;
}
sf::Color getNewColor() {
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<double> distribution(10, 25); // seemed to work well
return sf::Color(0, (int)(220 + distribution(e2)) % 255, (int)(200 + distribution(e2)) % 255);
}
};