-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudlife.cpp
269 lines (224 loc) · 6.77 KB
/
cloudlife.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
/* cloudlife by Don Marti <[email protected]>
*
* Based on Conway's Life, but with one rule change to make it a better
* screensaver: cells have a max age.
*
* When a cell exceeds the max age, it counts as 3 for populating the next
* generation. This makes long-lived formations explode instead of just
* sitting there burning a hole in your screen.
*
* Cloudlife only draws one pixel of each cell per tick, whether the cell is
* alive or dead. So gliders look like little comets.
* 20 May 2003 -- now includes color cycling and a man page.
* Based on several examples from the hacks directory of:
* xscreensaver, Copyright (c) 1997, 1998, 2002 Jamie Zawinski <[email protected]>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
* Dear ImGui port by Pavel Vasilyev <[email protected]>, Jan 2023
*/
#include <cstdint>
#include <algorithm>
#include "cloudlife.hpp"
#include "imgui_elements.h"
#include "random.h"
void Cloudlife::resize_field(int fw, int fh) {
int s = fw * fh * sizeof(unsigned char);
f->width = fw;
f->height = fh;
f->cells.resize(s);
f->new_cells.resize(s);
std::fill(f->cells.begin(), f->cells.end(), 0);
std::fill(f->new_cells.begin(), f->new_cells.end(), 0);
easel->pal.rescale(f->max_age);
}
unsigned char *Cloudlife::cell_at(unsigned int x, unsigned int y)
{
return (f->cells.data() + x * sizeof(unsigned char) +
y * f->width * sizeof(unsigned char));
}
unsigned char *Cloudlife::new_cell_at(unsigned int x, unsigned int y)
{
return (f->new_cells.data() + x * sizeof(unsigned char) +
y * f->width * sizeof(unsigned char));
}
static unsigned int
random_cell(unsigned int p)
{
int r = xoshiro256plus() & 0xff;
if (r < p) {
return (1);
} else {
return (0);
}
}
void Cloudlife::populate_field(unsigned int p)
{
unsigned int x, y;
for (x = 0; x < f->width; x++) {
for (y = 0; y < f->height; y++) {
*cell_at(x, y) = random_cell(p);
}
}
}
void Cloudlife::resize(int _w, int _h) {
clear();
if (f->height != easel->h / (1 << f->cell_size) + 2 ||
f->width != easel->w / (1 << f->cell_size) + 2) {
refield();
}
}
void Cloudlife::refield() {
resize_field(easel->w / (1 << f->cell_size) + 2,
easel->h / (1 << f->cell_size) + 2);
populate_field(density);
clear();
}
bool Cloudlife::render_gui() {
bool up = false;
up |= ScrollableSliderInt("Initial density", &density, 8, 256, "%d", 8);
up |= ScrollableSliderUInt("Cell size", &f->cell_size, 1, 32, "%d", 1);
up |= ScrollableSliderUInt("Max age", &f->max_age, 4, 256, "%d", 8);
//up |= ScrollableSliderUInt("ncolors", &ncolors, 0, 1024, "%d", 8);
ScrollableSliderUInt("Ticks per frame", &f->ticks_per_frame, 1, 128, "%d", 1);
up |= ImGui::ColorEdit4("Foreground", (float*)&foreground);
up |= ImGui::ColorEdit4("Backgroud", (float*)&background);
up |= ImGui::ColorEdit4("Clear color", (float*)&clear_color);
if (up) {
refield();
}
return up;
}
void Cloudlife::populate_edges(unsigned int p)
{
unsigned int i;
for (i = f->width; i--;) {
*cell_at(i, 0) = random_cell(p);
*cell_at(i, f->height - 1) = random_cell(p);
}
for (i = f->height; i--;) {
*cell_at(f->width - 1, i) = random_cell(p);
*cell_at(0, i) = random_cell(p);
}
}
//--------------------------------------------------------------
uint32_t Cloudlife::get_color_age(int age) {
return easel->pal.get_color(age);
}
void
Cloudlife::draw_field()
{
unsigned int x, y;
unsigned int rx, ry = 0; /* random amount to offset the dot */
unsigned int size = 1 << f->cell_size;
unsigned int mask = size - 1;
unsigned int fg_count, bg_count;
uint32_t fgc, bgc;
fgc = ImGui::GetColorU32(foreground);
//fgc = get_color_age(colorindex);// original color behaviour?
bgc = ImGui::GetColorU32(background);
/* columns 0 and width-1 are off screen and not drawn. */
for (y = 1; y < f->height - 1; y++) {
fg_count = 0;
bg_count = 0;
/* rows 0 and height-1 are off screen and not drawn. */
for (x = 1; x < f->width - 1; x++) {
rx = xoshiro256plus();
ry = rx >> f->cell_size;
rx &= mask;
ry &= mask;
uint8_t age = *cell_at(x, y);
fgc = get_color_age(age);
if (age) {
drawdot((short) x *size - rx - 1,
(short) y *size - ry - 1,
fgc);
} else {
drawdot((short) x *size - rx - 1,
(short) y *size - ry - 1,
bgc);
}
}
}
}
static inline unsigned int
cell_value(unsigned char c, unsigned int age)
{
if (!c) {
return 0;
} else if (c > age) {
return (3);
} else {
return (1);
}
}
unsigned int Cloudlife::is_alive(unsigned int x, unsigned int y)
{
unsigned int count;
unsigned int i, j;
unsigned char *p;
count = 0;
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (y != j || x != i) {
count += cell_value(*cell_at(i, j), f->max_age);
}
}
}
p = cell_at(x, y);
if (*p) {
if (count == 2 || count == 3) {
return ((*p) + 1);
} else {
return (0);
}
} else {
if (count == 3) {
return (1);
} else {
return (0);
}
}
}
unsigned int Cloudlife::do_tick()
{
unsigned int x, y;
unsigned int count = 0;
for (x = 1; x < f->width - 1; x++) {
for (y = 1; y < f->height - 1; y++) {
count += *new_cell_at(x, y) = is_alive(x, y);
}
}
f->cells = f->new_cells;
return count;
}
bool Cloudlife::render(uint32_t *p) {
unsigned int count = 0;
for (int i=0; i<f->ticks_per_frame; ++i)
count = do_tick();
if (count < (f->height + f->width) / 4) {
populate_field(density);
}
if (cycles % (f->max_age / 2) == 0) {
populate_edges(density);
do_tick();
populate_edges(0);
}
/*
if (ncolors) {
if (colortimer) {
colorindex--;
if (colorindex == 0)
colorindex = ncolors;
}
}
*/
draw_field();
cycles++;
return false;
}