summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomer Shalev <shalev.tomer@gmail.com>2010-01-19 20:05:19 +0000
committerTomer Shalev <shalev.tomer@gmail.com>2010-01-19 20:05:19 +0000
commit2f78fa114dddcef64db8fe4ffec329178afa4868 (patch)
tree9baaad32de7f95d7a36608ecc95dfd280755abb0
parent31a4dc9caa4c549e729ae8edf1c4648fbc6afc88 (diff)
downloadrockbox-2f78fa114dddcef64db8fe4ffec329178afa4868.tar.gz
rockbox-2f78fa114dddcef64db8fe4ffec329178afa4868.zip
Fractals: No need to have globals under ctx struct
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24285 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/fractals/mandelbrot_set.c107
1 files changed, 52 insertions, 55 deletions
diff --git a/apps/plugins/fractals/mandelbrot_set.c b/apps/plugins/fractals/mandelbrot_set.c
index 338875b8e4..013b462454 100644
--- a/apps/plugins/fractals/mandelbrot_set.c
+++ b/apps/plugins/fractals/mandelbrot_set.c
@@ -69,20 +69,17 @@ static fb_data imgbuffer[LCD_HEIGHT];
69#endif 69#endif
70 70
71/* Fixed point format s5.26: sign, 5 bits integer part, 26 bits fractional part */ 71/* Fixed point format s5.26: sign, 5 bits integer part, 26 bits fractional part */
72struct mandelbrot_ctx 72struct fractal_ops *ops;
73{ 73long x_min;
74 struct fractal_ops *ops; 74long x_max;
75 long x_min; 75long x_step;
76 long x_max; 76long x_delta;
77 long x_step; 77long y_min;
78 long x_delta; 78long y_max;
79 long y_min; 79long y_step;
80 long y_max; 80long y_delta;
81 long y_step; 81int step_log2;
82 long y_delta; 82unsigned max_iter;
83 int step_log2;
84 unsigned max_iter;
85} ctx;
86 83
87static void mandelbrot_init(void); 84static void mandelbrot_init(void);
88 85
@@ -138,19 +135,19 @@ static int ilog2_fp(long value) /* calculate integer log2(value_fp_6.26) */
138 135
139static int recalc_parameters(void) 136static int recalc_parameters(void)
140{ 137{
141 ctx.x_step = (ctx.x_max - ctx.x_min) / LCD_WIDTH; 138 x_step = (x_max - x_min) / LCD_WIDTH;
142 ctx.y_step = (ctx.y_max - ctx.y_min) / LCD_HEIGHT; 139 y_step = (y_max - y_min) / LCD_HEIGHT;
143 ctx.step_log2 = ilog2_fp(MIN(ctx.x_step, ctx.y_step)); 140 step_log2 = ilog2_fp(MIN(x_step, y_step));
144 141
145 if (ctx.step_log2 == LOG2_OUT_OF_BOUNDS) 142 if (step_log2 == LOG2_OUT_OF_BOUNDS)
146 return 1; /* out of bounds */ 143 return 1; /* out of bounds */
147 144
148 ctx.x_delta = X_DELTA(ctx.x_step); 145 x_delta = X_DELTA(x_step);
149 ctx.y_delta = Y_DELTA(ctx.y_step); 146 y_delta = Y_DELTA(y_step);
150 ctx.y_delta = (ctx.y_step * LCD_HEIGHT) / 8; 147 y_delta = (y_step * LCD_HEIGHT) / 8;
151 ctx.max_iter = MAX(15, -15 * ctx.step_log2 - 45); 148 max_iter = MAX(15, -15 * step_log2 - 45);
152 149
153 ctx.ops->calc = (ctx.step_log2 <= -10) ? 150 ops->calc = (step_log2 <= -10) ?
154 mandelbrot_calc_high_prec : mandelbrot_calc_low_prec; 151 mandelbrot_calc_high_prec : mandelbrot_calc_low_prec;
155 152
156 return 0; 153 return 0;
@@ -158,12 +155,12 @@ static int recalc_parameters(void)
158 155
159static void mandelbrot_init(void) 156static void mandelbrot_init(void)
160{ 157{
161 ctx.ops = &mandelbrot_ops; 158 ops = &mandelbrot_ops;
162 159
163 ctx.x_min = MB_XOFS - MB_XFAC; 160 x_min = MB_XOFS - MB_XFAC;
164 ctx.x_max = MB_XOFS + MB_XFAC; 161 x_max = MB_XOFS + MB_XFAC;
165 ctx.y_min = -MB_YFAC; 162 y_min = -MB_YFAC;
166 ctx.y_max = MB_YFAC; 163 y_max = MB_YFAC;
167 164
168 recalc_parameters(); 165 recalc_parameters();
169} 166}
@@ -182,13 +179,13 @@ static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
182 unsigned long last_yield = *rb->current_tick; 179 unsigned long last_yield = *rb->current_tick;
183 unsigned long last_button_yield = *rb->current_tick; 180 unsigned long last_button_yield = *rb->current_tick;
184 181
185 a32 = ctx.x_min + ctx.x_step * rect->px_min; 182 a32 = x_min + x_step * rect->px_min;
186 183
187 for (p_x = rect->px_min; p_x < rect->px_max; p_x++) 184 for (p_x = rect->px_min; p_x < rect->px_max; p_x++)
188 { 185 {
189 a = a32 >> 16; 186 a = a32 >> 16;
190 187
191 b32 = ctx.y_min + ctx.y_step * (LCD_HEIGHT - rect->py_max); 188 b32 = y_min + y_step * (LCD_HEIGHT - rect->py_max);
192 189
193 for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--) 190 for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--)
194 { 191 {
@@ -197,7 +194,7 @@ static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
197 y = b; 194 y = b;
198 n_iter = 0; 195 n_iter = 0;
199 196
200 while (++n_iter <= ctx.max_iter) 197 while (++n_iter <= max_iter)
201 { 198 {
202 x2 = MULS16_ASR10(x, x); 199 x2 = MULS16_ASR10(x, x);
203 y2 = MULS16_ASR10(y, y); 200 y2 = MULS16_ASR10(y, y);
@@ -208,7 +205,7 @@ static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
208 x = x2 - y2 + a; 205 x = x2 - y2 + a;
209 } 206 }
210 207
211 if (n_iter > ctx.max_iter) 208 if (n_iter > max_iter)
212 imgbuffer[p_y] = CONVERGENCE_COLOR; 209 imgbuffer[p_y] = CONVERGENCE_COLOR;
213 else 210 else
214 imgbuffer[p_y] = COLOR(n_iter); 211 imgbuffer[p_y] = COLOR(n_iter);
@@ -238,7 +235,7 @@ static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
238 last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT; 235 last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT;
239 } 236 }
240 237
241 b32 += ctx.y_step; 238 b32 += y_step;
242 } 239 }
243#ifdef USEGSLIB 240#ifdef USEGSLIB
244 grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1, 241 grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1,
@@ -259,7 +256,7 @@ static int mandelbrot_calc_low_prec(struct fractal_rect *rect,
259 } 256 }
260#endif 257#endif
261 258
262 a32 += ctx.x_step; 259 a32 += x_step;
263 } 260 }
264 261
265 rect->valid = 0; 262 rect->valid = 0;
@@ -282,11 +279,11 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
282 279
283 MULS32_INIT(); 280 MULS32_INIT();
284 281
285 a = ctx.x_min + ctx.x_step * rect->px_min; 282 a = x_min + x_step * rect->px_min;
286 283
287 for (p_x = rect->px_min; p_x < rect->px_max; p_x++) 284 for (p_x = rect->px_min; p_x < rect->px_max; p_x++)
288 { 285 {
289 b = ctx.y_min + ctx.y_step * (LCD_HEIGHT - rect->py_max); 286 b = y_min + y_step * (LCD_HEIGHT - rect->py_max);
290 287
291 for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--) 288 for (p_y = rect->py_max - 1; p_y >= rect->py_min; p_y--)
292 { 289 {
@@ -294,7 +291,7 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
294 y = b; 291 y = b;
295 n_iter = 0; 292 n_iter = 0;
296 293
297 while (++n_iter <= ctx.max_iter) 294 while (++n_iter <= max_iter)
298 { 295 {
299 x2 = MULS32_ASR26(x, x); 296 x2 = MULS32_ASR26(x, x);
300 y2 = MULS32_ASR26(y, y); 297 y2 = MULS32_ASR26(y, y);
@@ -305,7 +302,7 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
305 x = x2 - y2 + a; 302 x = x2 - y2 + a;
306 } 303 }
307 304
308 if (n_iter > ctx.max_iter) 305 if (n_iter > max_iter)
309 imgbuffer[p_y] = CONVERGENCE_COLOR; 306 imgbuffer[p_y] = CONVERGENCE_COLOR;
310 else 307 else
311 imgbuffer[p_y] = COLOR(n_iter); 308 imgbuffer[p_y] = COLOR(n_iter);
@@ -335,7 +332,7 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
335 last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT; 332 last_button_yield = *rb->current_tick + BUTTON_YIELD_TIMEOUT;
336 } 333 }
337 334
338 b += ctx.y_step; 335 b += y_step;
339 } 336 }
340#ifdef USEGSLIB 337#ifdef USEGSLIB
341 grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1, 338 grey_ub_gray_bitmap_part(imgbuffer, 0, rect->py_min, 1,
@@ -355,7 +352,7 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
355 last_px = p_x; 352 last_px = p_x;
356 } 353 }
357#endif 354#endif
358 a += ctx.x_step; 355 a += x_step;
359 } 356 }
360 357
361 rect->valid = 0; 358 rect->valid = 0;
@@ -365,25 +362,25 @@ static int mandelbrot_calc_high_prec(struct fractal_rect *rect,
365 362
366static void mandelbrot_move(int dx, int dy) 363static void mandelbrot_move(int dx, int dy)
367{ 364{
368 long d_x = (long)dx * ctx.x_delta; 365 long d_x = (long)dx * x_delta;
369 long d_y = (long)dy * ctx.y_delta; 366 long d_y = (long)dy * y_delta;
370 367
371 ctx.x_min += d_x; 368 x_min += d_x;
372 ctx.x_max += d_x; 369 x_max += d_x;
373 ctx.y_min += d_y; 370 y_min += d_y;
374 ctx.y_max += d_y; 371 y_max += d_y;
375} 372}
376 373
377static int mandelbrot_zoom(int factor) 374static int mandelbrot_zoom(int factor)
378{ 375{
379 int res; 376 int res;
380 long factor_x = (long)factor * ctx.x_delta; 377 long factor_x = (long)factor * x_delta;
381 long factor_y = (long)factor * ctx.y_delta; 378 long factor_y = (long)factor * y_delta;
382 379
383 ctx.x_min += factor_x; 380 x_min += factor_x;
384 ctx.x_max -= factor_x; 381 x_max -= factor_x;
385 ctx.y_min += factor_y; 382 y_min += factor_y;
386 ctx.y_max -= factor_y; 383 y_max -= factor_y;
387 384
388 res = recalc_parameters(); 385 res = recalc_parameters();
389 if (res) /* zoom not possible, revert */ 386 if (res) /* zoom not possible, revert */
@@ -401,14 +398,14 @@ static int mandelbrot_precision(int d)
401 /* Precision increase */ 398 /* Precision increase */
402 for (; d > 0; d--) 399 for (; d > 0; d--)
403 { 400 {
404 ctx.max_iter += ctx.max_iter / 2; 401 max_iter += max_iter / 2;
405 changed = 1; 402 changed = 1;
406 } 403 }
407 404
408 /* Precision decrease */ 405 /* Precision decrease */
409 for (; d < 0 && ctx.max_iter >= 15; d++) 406 for (; d < 0 && max_iter >= 15; d++)
410 { 407 {
411 ctx.max_iter -= ctx.max_iter / 3; 408 max_iter -= max_iter / 3;
412 changed = 1; 409 changed = 1;
413 } 410 }
414 411