-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripple.cpp
113 lines (88 loc) · 2.39 KB
/
ripple.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
//
// ripple.cpp
// visuals
//
// Created by Matthew Eatough on 19/1/2022.
// Copyright © 2022 The Good Goomba. All rights reserved.
//
#include "Headers/ripple.hpp"
#include <array>
#include <iostream>
void ripple::setup(int w, int h,int offX,int offY)
{
width = w;
height = h;
offSetX = offX;
offSetY = offY;
std::vector<float> temp(height,0);
buffer1.clear();
buffer2.clear();
for(int i=0;i<width;i++)
{
buffer1.push_back(temp);
buffer2.push_back(temp);
}
buffer2[width/2][height/2] = 255;
for(int x=1;x<width-1;x++)
{
for(int y=1;y<height-1;y++)
{
sf::Vertex vertex1;
vertex1.position = sf::Vector2f(x+offSetX, y+offSetY);
vertex1.color = sf::Color(buffer2[x][y],buffer2[x][y],buffer2[x][y]);//grayscale
sf::VertexArray pixel;
pixel.append(vertex1);
pixels.push_back(pixel);
}
}
updating = false;
damping = 0.999;
}
void ripple::drop(int mouseX,int mouseY)
{
mouseY -= offSetY;
mouseX -= offSetX;
if (mouseY > 0 && mouseX>0 && mouseY<height && mouseX < width)
{
buffer1.at(mouseX).at(mouseY) = 255;
}
}
void ripple::update()
{
updating = false;
for(int x=1;x<width-1;x++)
{
for(int y=1;y<height-1;y++)
{
buffer2[x][y] = (buffer1[x-1][y ] +
buffer1[x+1][y ] +
buffer1[x ][y+1] +
buffer1[x ][y-1])/2 - buffer2[x][y];
buffer2[x][y] = buffer2[x][y] * damping;
if (buffer2[x][y] < 1)
buffer2[x][y] = 0;
if (buffer1[x][y] > 0)
updating = true;
if (buffer2[x][y] > 255)
buffer2[x][y] = 255;
}
}
pixels.clear();
for(int x=1;x<width-1;x++)
{
for(int y=1;y<height-1;y++)
{
sf::Vertex vertex1;
vertex1.position = sf::Vector2f(x+offSetX, y+offSetY);
vertex1.color = sf::Color(buffer2[x][y], buffer2[x][y], buffer2[x][y]);//grayscale
sf::VertexArray pixel;
pixel.append(vertex1);
pixels.push_back(pixel);
}
}
swapBuf();
}
void ripple::swapBuf()
{
buffer2.swap(buffer1);
}