-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
280 lines (252 loc) · 6.97 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <string>
#include <cinttypes>
#include <random>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <chrono>
#include <SDL2/SDL.h>
class Field
{
public:
constexpr static int16_t mMax = 255; // the maximum value a cell can have
constexpr static int mDim = 600; // The dimension of the playing field
constexpr static int mFactor = 1; // the display factor (mFactor*mDim is the resolution of the window=)
constexpr static int16_t r = 3; // effective radius which influences the current cell
double mFac; // factor which influences how much a field follows the average around it
int16_t mField[mDim][mDim]; // the playing field
// calculates the average number around the current cell
int16_t av(const uint32_t x, const uint16_t y)
{
int16_t n = 0; // number of cells counted
int16_t s = 0; // sum of cell values
for (int i = -r; i <= r; i++)
{
int16_t xP = x+i;
if (xP >= 0 && xP < mDim)
{
int16_t dx = sqrt(r * r - i * i);
for (int j = -dx; j <= dx; j++)
{
int16_t yP = y+j;
if (yP >= 0 && yP < mDim && ((!(i == 0) != !(j == 0)) || (i != 0) || (j != 0)))
{
int16_t curr = abs((i!=0?i:1)*(j!=0?j:1));
n += 1/curr;
s += mField[xP][yP]/curr;
}
}
}
}
return s/n;
}
// the sum of values around the current cell
int16_t sum(const uint32_t x, const uint16_t y)
{
int16_t s = 0;
for (int i = -r; i <= r; i++)
{
int16_t xP = x+i;
if (xP >= 0 && xP < mDim)
{
int16_t dx = sqrt(r * r - i * i);
for (int j = -dx; j <= dx; j++)
{
int16_t yP = y+j;
if (yP >= 0 && yP < mDim && ((!(i == 0) != !(j == 0)) || (i != 0) || (j != 0)))
{
s += mField[xP][yP]/abs((i!=0?i:1)*(j!=0?j:1));
}
}
}
}
return s;
}
// returns the value of the current cell
int16_t value(const uint16_t x, const uint16_t y)
{
return mField[x][y];
}
public:
Field()
{
init();
}
// calculates the next value for the current cell.
// the cells value follows the average around it with a factor
void calculateOne(const uint16_t x, const uint16_t y)
{
int16_t v = value(x,y);
v += int16_t(double(av(x,y) - value(x,y)) * mFac);
if (v > mMax)
{
v = mMax;
}
if (v < -mMax)
{
v = -mMax;
}
mField[x][y] = v;
}
void calculateAll()
{
for (int i = 0; i < mDim; i++)
{
for (int j = 0; j < mDim; j++)
{
calculateOne(i,j);
}
}
}
// initialises the field, at this time that's random, but that choice is arbitrary
void init()
{
srand(time(NULL));
for (int i = 0; i < mDim; i++)
{
for (int j = 0; j < mDim; j++)
{
int16_t in = rand()%255;
int16_t p = rand()%2;
if (p%2 == 0)
{
in *= -1;
}
mField[i][j] = in;
}
}
}
// Draws one cell onto the window
void draw(SDL_Renderer *renderer, uint16_t x, uint16_t y)
{
int16_t v = value(x,y);
uint16_t r = 0;
uint16_t g = 0;
uint16_t b = 0;
if (v < 0)
{
r = -v;
}
else
{
b = v;
}
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
if (mFactor == 1)
{
SDL_RenderDrawPoint(renderer, x, y);
}
else
{
SDL_Rect pos;
pos.x = x*mFactor;
pos.y = y*mFactor;
pos.w = mFactor;
pos.h = mFactor;
SDL_RenderFillRect(renderer, &pos);
}
}
// Draws the entire field
void draw(SDL_Renderer *renderer)
{
for (int i = 0; i < mDim; i++)
{
for (int j = 0; j < mDim; j++)
{
draw(renderer, i, j);
}
}
}
// prints the numerical values of the field
void print()
{
for (int i = 0; i < mDim; i++)
{
for (int j = 0; j < mDim; j++)
{
if (abs(mField[i][j]) < 10)
{
std::cout<<" ";
}
if (mField[i][j] >= 0)
{
std::cout<<" ";
}
std::cout<<std::to_string(mField[i][j])<<" ";
}
std::cout<<"\n";
}
std::cout<<"\n";
}
};
int main(int argc, char* argv[])
{
double factor = 1;
if (argc == 2)
{
factor = stod(std::string(argv[1]));
}
std::cout<<"Using a factor of "<<factor<<"\n";
// +++ initialise playing field
Field f;
f.mFac = factor;
int w = f.mDim*f.mFactor;
int h = f.mDim*f.mFactor;
// --- initialise playing field
// +++ initialise SDL2 Window
SDL_Event event;
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(w, h, 0, &window, &renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
// --- initialise SDL2 Window
int ms = 1000; // duration to wait in each loop.
int quit = -1; // set to a positive number in order to quit
while (true)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
f.draw(renderer);
f.calculateAll();
SDL_RenderPresent(renderer);
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q)
{
quit = 1;
}
else if (event.key.keysym.sym == SDLK_UP)
{
ms += 50;
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
ms -= 50;
}
break;
case SDL_QUIT:
quit = 1;
break;
default:
break;
}
}
if (quit >= 0)
{
break;
}
// std::this_thread::sleep_for(std::chrono::milliseconds(ms));
// The delay is disabled since my computer is a bit slow, so that's not necessary.
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
return 0;
}