new plugin: FS#10559 - lrcplayer: a plugin to view .lrc file.
[kugel-rb.git] / apps / plugins / fractals / fractal.c
blob971ae506ca766fe7f89e1e411b44a639babf8bc3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Matthias Wientapper
11 * Heavily extended 2005 Jens Arnold
12 * Extended 2009 Tomer Shalev
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "plugin.h"
26 #ifdef HAVE_LCD_BITMAP
28 #include "fractal.h"
29 #include "fractal_rect.h"
30 #include "fractal_sets.h"
31 #include "mandelbrot_set.h"
33 #ifdef USEGSLIB
34 GREY_INFO_STRUCT
35 static unsigned char *gbuf;
36 static size_t gbuf_size = 0;
37 #endif
39 #define REDRAW_NONE 0
40 #define REDRAW_PARTIAL 1
41 #define REDRAW_FULL 2
42 #define REDRAW_FULL_OVERLAY 3
44 PLUGIN_HEADER
46 /* returns 1 if a button has been pressed, 0 otherwise */
47 static int button_yield(void *ctx)
49 long *button = (long *)ctx;
51 *button = rb->button_get(false);
52 switch (*button)
54 case FRACTAL_QUIT:
55 case FRACTAL_UP:
56 case FRACTAL_DOWN:
57 case FRACTAL_LEFT:
58 case FRACTAL_RIGHT:
59 case FRACTAL_ZOOM_IN:
60 case FRACTAL_ZOOM_OUT:
61 case FRACTAL_PRECISION_INC:
62 case FRACTAL_PRECISION_DEC:
63 case FRACTAL_RESET:
64 #ifdef FRACTAL_ZOOM_IN2
65 case FRACTAL_ZOOM_IN2:
66 #endif
67 #ifdef FRACTAL_ZOOM_IN_PRE
68 case FRACTAL_ZOOM_IN_PRE:
69 #endif
70 #if defined(FRACTAL_ZOOM_OUT_PRE) && \
71 (FRACTAL_ZOOM_OUT_PRE != FRACTAL_ZOOM_IN_PRE)
72 case FRACTAL_ZOOM_OUT_PRE:
73 #endif
74 #ifdef FRACTAL_PRECISION_INC_PRE
75 case FRACTAL_PRECISION_INC_PRE:
76 #endif
77 #if defined(FRACTAL_PRECISION_DEC_PRE) && \
78 (FRACTAL_PRECISION_DEC_PRE != FRACTAL_PRECISION_INC_PRE)
79 case FRACTAL_PRECISION_DEC_PRE:
80 #endif
81 return 1;
82 default:
83 *button = BUTTON_NONE;
84 return 0;
88 static void cleanup(void *parameter)
90 (void)parameter;
91 #ifdef USEGSLIB
92 grey_release();
93 #endif
96 enum plugin_status plugin_start(const void* parameter)
98 long lastbutton = BUTTON_NONE;
99 int redraw = REDRAW_FULL;
100 struct fractal_ops *ops = &mandelbrot_ops;
102 (void)parameter;
104 #ifdef USEGSLIB
105 /* get the remainder of the plugin buffer */
106 gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
108 /* initialize the greyscale buffer.*/
109 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
111 rb->splash(HZ, "Couldn't init greyscale display");
112 return 0;
114 grey_show(true); /* switch on greyscale overlay */
115 #endif
117 #if LCD_DEPTH > 1
118 rb->lcd_set_backdrop(NULL);
119 #endif
121 ops->init();
123 /* main loop */
124 while (true)
126 long button = BUTTON_NONE;
128 if (redraw != REDRAW_NONE)
130 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
131 rb->cpu_boost(true);
132 #endif
133 switch (redraw)
135 case REDRAW_FULL:
136 mylcd_ub_clear_display();
137 mylcd_ub_update();
138 /* fall-through */
139 case REDRAW_FULL_OVERLAY:
140 rects_queue_init();
141 break;
142 default:
143 break;
146 /* paint all rects */
147 rects_calc_all(ops->calc, button_yield, (void *)&button);
148 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
149 rb->cpu_boost(false);
150 #endif
151 /* not interrupted by button press - screen is fully drawn */
152 redraw = (button == BUTTON_NONE) ? REDRAW_NONE : REDRAW_PARTIAL;
155 if (button == BUTTON_NONE)
156 button = rb->button_get(true);
158 switch (button)
160 #ifdef FRACTAL_RC_QUIT
161 case FRACTAL_RC_QUIT:
162 #endif
163 case FRACTAL_QUIT:
164 #ifdef USEGSLIB
165 grey_release();
166 #endif
167 return PLUGIN_OK;
169 case FRACTAL_ZOOM_OUT:
170 #ifdef FRACTAL_ZOOM_OUT_PRE
171 if (lastbutton != FRACTAL_ZOOM_OUT_PRE)
172 break;
173 #endif
174 if (!ops->zoom(-1))
175 redraw = REDRAW_FULL;
176 break;
179 case FRACTAL_ZOOM_IN:
180 #ifdef FRACTAL_ZOOM_IN_PRE
181 if (lastbutton != FRACTAL_ZOOM_IN_PRE)
182 break;
183 #endif
184 #ifdef FRACTAL_ZOOM_IN2
185 case FRACTAL_ZOOM_IN2:
186 #endif
187 if (!ops->zoom(1))
188 redraw = REDRAW_FULL;
189 break;
191 case FRACTAL_UP:
192 ops->move(0, +1);
193 mylcd_ub_scroll_down(LCD_SHIFT_Y);
194 mylcd_ub_update();
195 if (redraw != REDRAW_FULL)
196 redraw = rects_move_down() ? REDRAW_FULL : REDRAW_PARTIAL;
197 break;
199 case FRACTAL_DOWN:
200 ops->move(0, -1);
201 mylcd_ub_scroll_up(LCD_SHIFT_Y);
202 mylcd_ub_update();
203 if (redraw != REDRAW_FULL)
204 redraw = rects_move_up() ? REDRAW_FULL : REDRAW_PARTIAL;
205 break;
207 case FRACTAL_LEFT:
208 ops->move(-1, 0);
209 mylcd_ub_scroll_right(LCD_SHIFT_X);
210 mylcd_ub_update();
211 if (redraw != REDRAW_FULL)
212 redraw = rects_move_right() ? REDRAW_FULL : REDRAW_PARTIAL;
213 break;
215 case FRACTAL_RIGHT:
216 ops->move(+1, 0);
217 mylcd_ub_scroll_left(LCD_SHIFT_X);
218 mylcd_ub_update();
219 if (redraw != REDRAW_FULL)
220 redraw = rects_move_left() ? REDRAW_FULL : REDRAW_PARTIAL;
221 break;
223 case FRACTAL_PRECISION_DEC:
224 #ifdef FRACTAL_PRECISION_DEC_PRE
225 if (lastbutton != FRACTAL_PRECISION_DEC_PRE)
226 break;
227 #endif
228 if (ops->precision(-1))
229 redraw = REDRAW_FULL_OVERLAY;
231 break;
233 case FRACTAL_PRECISION_INC:
234 #ifdef FRACTAL_PRECISION_INC_PRE
235 if (lastbutton != FRACTAL_PRECISION_INC_PRE)
236 break;
237 #endif
238 if (ops->precision(+1))
239 redraw = REDRAW_FULL_OVERLAY;
241 break;
243 case FRACTAL_RESET:
244 ops->init();
245 redraw = REDRAW_FULL;
246 break;
248 default:
249 if (rb->default_event_handler_ex(button, cleanup, NULL)
250 == SYS_USB_CONNECTED)
251 return PLUGIN_USB_CONNECTED;
252 break;
255 if (button != BUTTON_NONE)
256 lastbutton = button;
258 #ifdef USEGSLIB
259 grey_release();
260 #endif
261 return PLUGIN_OK;
264 #endif