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 | #include "mandelbrot_plugin.h"
#include "gate/graphics/fonts.h"
#include "gate/mathematics.h"
#if defined(GATE_SYS_DOS)
typedef float real_t;
#else
typedef double real_t;
#endif
typedef struct mandelbrot
{
real_t xmin;
real_t xmax;
real_t ymin;
real_t ymax;
real_t move_factor;
real_t zoom_factor;
} mandelbrot_t;
static void mandelbrot_init(mandelbrot_t* mb)
{
mb->xmin = -0.3f;
mb->xmax = 0.1f;
mb->ymin = 0.7f;
mb->ymax = 1.1f;
mb->move_factor = 0.125f;
mb->zoom_factor = 0.125f;
}
static void mandelbrot_move(mandelbrot_t* mb, int x, int y)
{
real_t dx = (mb->xmax - mb->xmin) * mb->move_factor;
real_t dy = (mb->ymax - mb->ymin) * mb->move_factor;
mb->xmin += dx * (real_t)x;
mb->xmax += dx * (real_t)x;
mb->ymin += dy * (real_t)y;
mb->ymax += dy * (real_t)y;
}
static void mandelbrot_zoom(mandelbrot_t* mb, real_t x, real_t y, real_t direction)
{
real_t dx = mb->xmax - mb->xmin;
real_t dy = mb->ymax - mb->ymin;
real_t mx = mb->xmin + dx * x;
real_t my = mb->ymin + dy * y;
real_t factor = 0.5 - gate_math_signum_r64(direction) * mb->zoom_factor;
mb->xmin = mx - dx * factor;
mb->xmax = mx + dx * factor;
mb->ymin = my - dy * factor;
mb->ymax = my + dy * factor;
dx = mb->xmax - mb->xmin;
dy = mb->ymax - mb->ymin;
mx = (x - 0.5) * dx;
my = (y - 0.5) * dy;
mb->xmin -= mx;
mb->xmax -= mx;
mb->ymin -= my;
mb->ymax -= my;
}
// http://computer-programming-forum.com/29-pascal/8e85d63a95b3de39.htm
static void mandelbrot_paint(mandelbrot_t* mb, gate_rasterimage_t* image)
{
const int xsize = (int)gate_rasterimage_width(image);
const int ysize = (int)gate_rasterimage_height(image);
const real_t min_x = mb->xmin;
const real_t min_y = mb->ymin;
const real_t f_x = (mb->xmax - mb->xmin) / (real_t)xsize;
const real_t f_y = (mb->ymax - mb->ymin) / (real_t)ysize;
int ix, iy, count;
real_t x, y, qx, qy, temp;
gate_color_t col = GATE_COLOR_BLACK;
for (ix = 0; ix != xsize; ++ix)
{
for (iy = 0; iy != ysize; ++iy)
{
x = (real_t)ix * f_x + min_x;
y = (real_t)iy * f_y + min_y;
qx = x;
qy = y;
for (count = 0; (count != 256) && ((qx * qx + qy * qy) < 4.0f); ++count)
{
temp = 2.0f * qx * qy + y;
qx = qx * qx - qy * qy + x;
qy = temp;
}
col.r = (gate_uint8_t)(256 - (count * 4) % 256);
col.g = (gate_uint8_t)(256 - (count * 16) % 256);
col.b = (gate_uint8_t)(count);
col.a = 255;
gate_rasterimage_set_pixel(image, ix, iy, &col);
}
}
}
static mandelbrot_t mandelbrot;
static gate_rasterimage_t mandelbrot_backbuffer;
static gate_result_t gateframes_mandelbrot_create(gate_framebuffer_t* fb)
{
gate_result_t ret;
gate_framebuffer_info_t fb_info;
gate_uint16_t format_id;
do
{
ret = gate_framebuffer_get_info(fb, &fb_info);
GATE_BREAK_IF_FAILED(ret);
switch (fb_info.bits_per_pixel)
{
case 8: format_id = GATE_IMAGE_PIXELFORMAT_PAL8; break;
case 15: format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
case 16: format_id = GATE_IMAGE_PIXELFORMAT_RGB555; break;
case 24: format_id = GATE_IMAGE_PIXELFORMAT_RGB24; break;
case 32: format_id = GATE_IMAGE_PIXELFORMAT_RGBA; break;
default: format_id = GATE_IMAGE_PIXELFORMAT_DEFAULT; break;
}
if (NULL == gate_rasterimage_create(&mandelbrot_backbuffer, format_id, fb_info.width, fb_info.height, NULL))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
mandelbrot_init(&mandelbrot);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
static gate_result_t gateframes_mandelbrot_destroy(gate_framebuffer_t* fb)
{
GATE_UNUSED_ARG(fb);
gate_rasterimage_release(&mandelbrot_backbuffer);
return GATE_RESULT_OK;
}
static gate_result_t gateframes_mandelbrot_render(gate_framebuffer_t* fb, gate_bool_t redraw_all)
{
GATE_UNUSED_ARG(redraw_all);
mandelbrot_paint(&mandelbrot, &mandelbrot_backbuffer);
return gate_framebuffer_set_image(fb, &mandelbrot_backbuffer, 0, 0);
}
static gate_result_t gateframes_mandelbrot_handle_event(gate_framebuffer_t* fb, gate_framebuffer_event_t* evt,
gate_bool_t* render_frame, gate_uint32_t* out_next_event_timeout)
{
gate_result_t ret = GATE_RESULT_OK;
int x, y;<--- The scope of the variable 'x' can be reduced. [+]The scope of the variable 'x' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level. <--- The scope of the variable 'y' can be reduced. [+]The scope of the variable 'y' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
int width = (int)gate_rasterimage_width(&mandelbrot_backbuffer);
int height = (int)gate_rasterimage_height(&mandelbrot_backbuffer);
GATE_UNUSED_ARG(fb);
switch (evt->event_type)
{
case GATE_FRAMEBUFFER_EVENT_KEY_DOWN:
{
switch (evt->event_data.key.key_code)
{
case GATE_KBD_KEY_LEFT:
case GATE_KBD_KEY_4:
case GATE_KBD_KEY_A:
mandelbrot_move(&mandelbrot, -1, 0);
break;
case GATE_KBD_KEY_RIGHT:
case GATE_KBD_KEY_6:
case GATE_KBD_KEY_D:
mandelbrot_move(&mandelbrot, 1, 0);
break;
case GATE_KBD_KEY_UP:
case GATE_KBD_KEY_8:
case GATE_KBD_KEY_W:
mandelbrot_move(&mandelbrot, 0, -1);
break;
case GATE_KBD_KEY_DOWN:
case GATE_KBD_KEY_2:
case GATE_KBD_KEY_S:
mandelbrot_move(&mandelbrot, 0, 1);
break;
case GATE_KBD_KEY_RETURN:
case GATE_KBD_KEY_PGDOWN:
case GATE_KBD_KEY_ADD:
mandelbrot_zoom(&mandelbrot, 0.5, 0.5, 1.0);
break;
case GATE_KBD_KEY_BACKSPACE:
case GATE_KBD_KEY_PGUP:
case GATE_KBD_KEY_SUBTRACT:
mandelbrot_zoom(&mandelbrot, 0.5, 0.5, -1.0);
break;
default:
break;
}
*render_frame = true;
*out_next_event_timeout = 10;
break;
}
case GATE_FRAMEBUFFER_EVENT_KEY_UP:
{
break;
}
case GATE_FRAMEBUFFER_EVENT_KEY_PRESS:
{
/* evt->event_data.key; */
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_DOWN:
{
x = (int)evt->event_data.pointer.x;
y = (int)evt->event_data.pointer.y;
if (x >= 0 && (x < width) && (y >= 0) && (y < height))
{
mandelbrot_zoom(&mandelbrot, (real_t)x / (real_t)width, (real_t)y / (real_t)height,
evt->event_data.pointer.button == GATE_FRAMEBUFFER_BUTTON_1 ? 1.0 : -1.0);
}
*render_frame = true;
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_BUTTON_UP:
{
/* evt->event_data.pointer; */
break;
}
case GATE_FRAMEBUFFER_EVENT_POINTER_MOVE:
{
/* evt->event_data.pointer; */
break;
}
case GATE_FRAMEBUFFER_EVENT_CANCEL:
{
break;
}
}
return ret;
}
static gateframes_plugin_t mandelbrot_plugin =
{
&gateframes_mandelbrot_create,
&gateframes_mandelbrot_destroy,
&gateframes_mandelbrot_render,
&gateframes_mandelbrot_handle_event
};
gateframes_plugin_t* get_mandelbrot_plugin()
{
return &mandelbrot_plugin;
}
|