when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / plugins / cube.c
blobdfc6bb329bb47838d19887ef65e70ff311e489d1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Damien Teney
11 * modified to use int instead of float math by Andreas Zwirtes
12 * heavily extended by Jens Arnold
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ***************************************************************************/
21 #include "plugin.h"
22 #include "grey.h"
23 #include "playergfx.h"
24 #include "xlcd.h"
25 #include "fixedpoint.h"
27 PLUGIN_HEADER
29 /* Loops that the values are displayed */
30 #define DISP_TIME 30
32 /* variable button definitions */
33 #if CONFIG_KEYPAD == RECORDER_PAD
34 #define CUBE_QUIT BUTTON_OFF
35 #define CUBE_X_INC BUTTON_RIGHT
36 #define CUBE_X_DEC BUTTON_LEFT
37 #define CUBE_Y_INC BUTTON_UP
38 #define CUBE_Y_DEC BUTTON_DOWN
39 #define CUBE_Z_INC BUTTON_F2
40 #define CUBE_Z_DEC BUTTON_F1
41 #define CUBE_MODE BUTTON_F3
42 #define CUBE_PAUSE BUTTON_PLAY
43 #define CUBE_HIGHSPEED BUTTON_ON
45 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
46 #define CUBE_QUIT BUTTON_OFF
47 #define CUBE_X_INC BUTTON_RIGHT
48 #define CUBE_X_DEC BUTTON_LEFT
49 #define CUBE_Y_INC BUTTON_UP
50 #define CUBE_Y_DEC BUTTON_DOWN
51 #define CUBE_Z_INC BUTTON_F2
52 #define CUBE_Z_DEC BUTTON_F1
53 #define CUBE_MODE BUTTON_F3
54 #define CUBE_PAUSE BUTTON_SELECT
55 #define CUBE_HIGHSPEED BUTTON_ON
57 #elif CONFIG_KEYPAD == PLAYER_PAD
58 #define CUBE_QUIT BUTTON_STOP
59 #define CUBE_X_INC BUTTON_RIGHT
60 #define CUBE_X_DEC BUTTON_LEFT
61 #define CUBE_Y_INC (BUTTON_ON | BUTTON_RIGHT)
62 #define CUBE_Y_DEC (BUTTON_ON | BUTTON_LEFT)
63 #define CUBE_Z_INC (BUTTON_MENU | BUTTON_RIGHT)
64 #define CUBE_Z_DEC (BUTTON_MENU | BUTTON_LEFT)
65 #define CUBE_MODE_PRE BUTTON_MENU
66 #define CUBE_MODE (BUTTON_MENU | BUTTON_REL)
67 #define CUBE_PAUSE BUTTON_PLAY
68 #define CUBE_HIGHSPEED_PRE BUTTON_ON
69 #define CUBE_HIGHSPEED (BUTTON_ON | BUTTON_REL)
71 #elif CONFIG_KEYPAD == ONDIO_PAD
72 #define CUBE_QUIT BUTTON_OFF
73 #define CUBE_X_INC BUTTON_RIGHT
74 #define CUBE_X_DEC BUTTON_LEFT
75 #define CUBE_Y_INC BUTTON_UP
76 #define CUBE_Y_DEC BUTTON_DOWN
77 #define CUBE_Z_INC (BUTTON_MENU | BUTTON_UP)
78 #define CUBE_Z_DEC (BUTTON_MENU | BUTTON_DOWN)
79 #define CUBE_MODE_PRE BUTTON_MENU
80 #define CUBE_MODE (BUTTON_MENU | BUTTON_REL)
81 #define CUBE_PAUSE (BUTTON_MENU | BUTTON_LEFT)
82 #define CUBE_HIGHSPEED (BUTTON_MENU | BUTTON_RIGHT)
84 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
85 (CONFIG_KEYPAD == IRIVER_H300_PAD)
86 #define CUBE_QUIT BUTTON_OFF
87 #define CUBE_X_INC BUTTON_RIGHT
88 #define CUBE_X_DEC BUTTON_LEFT
89 #define CUBE_Y_INC BUTTON_UP
90 #define CUBE_Y_DEC BUTTON_DOWN
91 #define CUBE_Z_INC (BUTTON_ON | BUTTON_UP)
92 #define CUBE_Z_DEC (BUTTON_ON | BUTTON_DOWN)
93 #define CUBE_MODE BUTTON_MODE
94 #define CUBE_PAUSE_PRE BUTTON_ON
95 #define CUBE_PAUSE (BUTTON_ON | BUTTON_REL)
96 #define CUBE_HIGHSPEED BUTTON_SELECT
98 #define CUBE_RC_QUIT BUTTON_RC_STOP
100 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
101 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
102 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
103 #define CUBE_QUIT BUTTON_MENU
104 #define CUBE_X_INC BUTTON_RIGHT
105 #define CUBE_X_DEC BUTTON_LEFT
106 #define CUBE_Y_INC BUTTON_SCROLL_FWD
107 #define CUBE_Y_DEC BUTTON_SCROLL_BACK
108 #define CUBE_Z_INC (BUTTON_SELECT | BUTTON_RIGHT)
109 #define CUBE_Z_DEC (BUTTON_SELECT | BUTTON_LEFT)
110 #define CUBE_MODE (BUTTON_SELECT | BUTTON_MENU)
111 #define CUBE_PAUSE_PRE BUTTON_PLAY
112 #define CUBE_PAUSE (BUTTON_PLAY | BUTTON_REL)
113 #define CUBE_HIGHSPEED (BUTTON_SELECT | BUTTON_PLAY)
115 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
116 #define CUBE_QUIT BUTTON_PLAY
117 #define CUBE_X_INC BUTTON_RIGHT
118 #define CUBE_X_DEC BUTTON_LEFT
119 #define CUBE_Y_INC BUTTON_UP
120 #define CUBE_Y_DEC BUTTON_DOWN
121 #define CUBE_Z_INC BUTTON_MODE
122 #define CUBE_Z_DEC BUTTON_EQ
123 #define CUBE_MODE (BUTTON_SELECT | BUTTON_REPEAT)
124 #define CUBE_PAUSE (BUTTON_SELECT | BUTTON_REL)
125 #define CUBE_HIGHSPEED (BUTTON_MODE | BUTTON_EQ) /* TODO: this is impossible */
127 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
128 #define CUBE_QUIT BUTTON_POWER
129 #define CUBE_X_INC BUTTON_LEFT
130 #define CUBE_X_DEC BUTTON_RIGHT
131 #define CUBE_Y_INC BUTTON_UP
132 #define CUBE_Y_DEC BUTTON_DOWN
133 #define CUBE_Z_INC (BUTTON_PLAY | BUTTON_UP)
134 #define CUBE_Z_DEC (BUTTON_PLAY | BUTTON_DOWN)
135 #define CUBE_MODE BUTTON_SELECT
136 #define CUBE_PAUSE_PRE BUTTON_PLAY
137 #define CUBE_PAUSE (BUTTON_PLAY | BUTTON_REL)
138 #define CUBE_HIGHSPEED (BUTTON_REC | BUTTON_REL)
140 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
141 #define CUBE_QUIT BUTTON_POWER
142 #define CUBE_X_INC BUTTON_LEFT
143 #define CUBE_X_DEC BUTTON_RIGHT
144 #define CUBE_Y_INC BUTTON_UP
145 #define CUBE_Y_DEC BUTTON_DOWN
146 #define CUBE_Z_INC BUTTON_VOL_UP
147 #define CUBE_Z_DEC BUTTON_VOL_DOWN
148 #define CUBE_MODE BUTTON_MENU
149 #define CUBE_PAUSE BUTTON_SELECT
150 #define CUBE_HIGHSPEED BUTTON_A
152 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
153 #define CUBE_QUIT BUTTON_POWER
154 #define CUBE_X_INC BUTTON_LEFT
155 #define CUBE_X_DEC BUTTON_RIGHT
156 #define CUBE_Y_INC BUTTON_SCROLL_FWD
157 #define CUBE_Y_DEC BUTTON_SCROLL_BACK
158 #define CUBE_Z_INC BUTTON_UP
159 #define CUBE_Z_DEC BUTTON_DOWN
160 #define CUBE_MODE_PRE BUTTON_SELECT
161 #define CUBE_MODE (BUTTON_SELECT|BUTTON_REPEAT)
162 #define CUBE_PAUSE_PRE BUTTON_SELECT
163 #define CUBE_PAUSE (BUTTON_SELECT|BUTTON_REL)
164 #define CUBE_HIGHSPEED BUTTON_REC
166 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
167 #define CUBE_QUIT BUTTON_POWER
168 #define CUBE_X_INC BUTTON_LEFT
169 #define CUBE_X_DEC BUTTON_RIGHT
170 #define CUBE_Y_INC BUTTON_VOL_UP
171 #define CUBE_Y_DEC BUTTON_VOL_DOWN
172 #define CUBE_Z_INC BUTTON_UP
173 #define CUBE_Z_DEC BUTTON_DOWN
174 #define CUBE_MODE_PRE BUTTON_SELECT
175 #define CUBE_MODE (BUTTON_SELECT|BUTTON_REPEAT)
176 #define CUBE_PAUSE_PRE BUTTON_SELECT
177 #define CUBE_PAUSE (BUTTON_SELECT|BUTTON_REL)
178 #define CUBE_HIGHSPEED BUTTON_REC
181 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
182 #define CUBE_QUIT BUTTON_POWER
183 #define CUBE_X_INC BUTTON_LEFT
184 #define CUBE_X_DEC BUTTON_RIGHT
185 #define CUBE_Y_INC BUTTON_SCROLL_UP
186 #define CUBE_Y_DEC BUTTON_SCROLL_DOWN
187 #define CUBE_Z_INC (BUTTON_PLAY | BUTTON_SCROLL_UP)
188 #define CUBE_Z_DEC (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
189 #define CUBE_MODE BUTTON_REW
190 #define CUBE_PAUSE_PRE BUTTON_PLAY
191 #define CUBE_PAUSE (BUTTON_PLAY | BUTTON_REL)
192 #define CUBE_HIGHSPEED (BUTTON_FF | BUTTON_REL)
194 #elif CONFIG_KEYPAD == MROBE500_PAD
195 #define CUBE_QUIT BUTTON_POWER
196 #define CUBE_X_INC BUTTON_LEFT
197 #define CUBE_X_DEC BUTTON_RIGHT
198 #define CUBE_Y_INC BUTTON_RC_PLAY
199 #define CUBE_Y_DEC BUTTON_RC_DOWN
200 #define CUBE_Z_INC BUTTON_RC_VOL_UP
201 #define CUBE_Z_DEC BUTTON_RC_VOL_DOWN
202 #define CUBE_MODE BUTTON_RC_MODE
203 #define CUBE_PAUSE_PRE BUTTON_RC_HEART
204 #define CUBE_PAUSE (BUTTON_RC_HEART | BUTTON_REL)
205 #define CUBE_HIGHSPEED BUTTON_RC_HEART
207 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
208 #define CUBE_QUIT BUTTON_BACK
209 #define CUBE_X_INC BUTTON_LEFT
210 #define CUBE_X_DEC BUTTON_RIGHT
211 #define CUBE_Y_INC BUTTON_UP
212 #define CUBE_Y_DEC BUTTON_DOWN
213 #define CUBE_Z_INC BUTTON_VOL_UP
214 #define CUBE_Z_DEC BUTTON_VOL_DOWN
215 #define CUBE_MODE BUTTON_PREV
216 #define CUBE_PAUSE_PRE BUTTON_PLAY
217 #define CUBE_PAUSE (BUTTON_PLAY | BUTTON_REL)
218 #define CUBE_HIGHSPEED (BUTTON_NEXT | BUTTON_REL)
220 #elif (CONFIG_KEYPAD == MROBE100_PAD)
221 #define CUBE_QUIT BUTTON_POWER
222 #define CUBE_X_INC BUTTON_LEFT
223 #define CUBE_X_DEC BUTTON_RIGHT
224 #define CUBE_Y_INC BUTTON_UP
225 #define CUBE_Y_DEC BUTTON_DOWN
226 #define CUBE_Z_INC (BUTTON_DISPLAY | BUTTON_UP)
227 #define CUBE_Z_DEC (BUTTON_DISPLAY | BUTTON_DOWN)
228 #define CUBE_MODE BUTTON_MENU
229 #define CUBE_PAUSE BUTTON_PLAY
230 #define CUBE_HIGHSPEED BUTTON_SELECT
232 #else
233 #error No keymap defined!
234 #endif
236 #ifdef HAVE_LCD_BITMAP
238 #define DIST (10 * MIN(LCD_HEIGHT, LCD_WIDTH) / 16)
239 static int x_off = LCD_WIDTH/2;
240 static int y_off = LCD_HEIGHT/2;
242 #if LCD_DEPTH == 1
243 #define USE_GSLIB
244 GREY_INFO_STRUCT
245 struct my_lcd {
246 void (*update)(void);
247 void (*clear_display)(void);
248 void (*drawline)(int x1, int y1, int x2, int y2);
249 void (*putsxy)(int x, int y, const unsigned char *string);
252 static struct my_lcd greyfuncs = {
253 grey_update, grey_clear_display, grey_drawline, grey_putsxy
255 static struct my_lcd lcdfuncs; /* initialised at runtime */
256 static struct my_lcd *mylcd = &greyfuncs;
258 #define MYLCD(fn) mylcd->fn
259 #define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) grey_filltriangle(x1, y1, x2, y2, x3, y3)
260 #define MY_SET_FOREGROUND(fg) grey_set_foreground(fg)
261 #define MY_GET_FOREGROUND() grey_get_foreground()
263 #else
264 #define MYLCD(fn) rb->lcd_ ## fn
265 #define MY_FILLTRIANGLE(x1, y1, x2, y2, x3, y3) xlcd_filltriangle(x1, y1, x2, y2, x3, y3)
266 #define MY_SET_FOREGROUND(fg) rb->lcd_set_foreground(fg)
267 #define MY_GET_FOREGROUND() rb->lcd_get_foreground()
268 #endif
270 #if CONFIG_LCD == LCD_SSD1815
271 #define ASPECT 320 /* = 1.25 (fixed point 24.8) */
272 #else
273 #define ASPECT 256 /* = 1.00 */
274 #endif
276 #else /* !LCD_BITMAP */
278 #define MYLCD(fn) pgfx_ ## fn
279 #define DIST 9
280 static int x_off = 10;
281 static int y_off = 7;
282 #define ASPECT 300 /* = 1.175 */
284 #endif /* !LCD_BITMAP */
286 struct point_3D {
287 long x, y, z;
290 struct point_2D {
291 long x, y;
294 struct line {
295 int start, end;
298 struct face {
299 int corner[4];
300 int line[4];
303 /* initial, unrotated cube corners */
304 static const struct point_3D sommet[8] =
306 {-DIST, -DIST, -DIST},
307 { DIST, -DIST, -DIST},
308 { DIST, DIST, -DIST},
309 {-DIST, DIST, -DIST},
310 {-DIST, -DIST, DIST},
311 { DIST, -DIST, DIST},
312 { DIST, DIST, DIST},
313 {-DIST, DIST, DIST}
316 /* The 12 lines forming the edges */
317 static const struct line lines[12] =
319 {0, 1}, {1, 2}, {2, 3}, {3, 0},
320 {4, 7}, {7, 6}, {6, 5}, {5, 4},
321 {0, 4}, {1, 5}, {2, 6}, {3, 7}
324 static bool lines_drawn[12];
326 /* The 6 faces of the cube; points are in clockwise order when viewed
327 from the outside */
328 static const struct face faces[6] =
330 {{0, 1, 2, 3}, {0, 1, 2, 3}},
331 {{4, 7, 6, 5}, {4, 5, 6, 7}},
332 {{0, 4, 5, 1}, {8, 7, 9, 0}},
333 {{2, 6, 7, 3}, {10, 5, 11, 2}},
334 {{0, 3, 7, 4}, {3, 11, 4, 8}},
335 {{1, 5, 6, 2}, {9, 6, 10, 1}}
338 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
339 static const unsigned face_colors[6] =
341 #ifdef HAVE_LCD_COLOR
342 LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 255, 0),
343 LCD_RGBPACK(0, 255, 0), LCD_RGBPACK(0, 0, 255), LCD_RGBPACK(0, 0, 255)
344 #elif defined(USE_GSLIB)
345 GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
346 GREY_DARKGRAY, GREY_BLACK, GREY_BLACK
347 #else
348 LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY,
349 LCD_DARKGRAY, LCD_BLACK, LCD_BLACK
350 #endif
352 #endif
354 enum {
355 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
356 SOLID,
357 #endif
358 HIDDEN_LINES,
359 WIREFRAME,
360 NUM_MODES
363 static int mode = 0;
365 static struct point_3D point3D[8];
366 static struct point_2D point2D[8];
367 static long matrice[3][3];
369 static const int nb_points = 8;
370 static long z_off = 600;
372 static struct plugin_api* rb;
374 static void cube_rotate(int xa, int ya, int za)
376 int i;
377 /* Just to prevent unnecessary lookups */
378 long sxa, cxa, sya, cya, sza, cza;
380 sxa = sin_int(xa);
381 cxa = cos_int(xa);
382 sya = sin_int(ya);
383 cya = cos_int(ya);
384 sza = sin_int(za);
385 cza = cos_int(za);
387 /* calculate overall translation matrix */
388 matrice[0][0] = (cza * cya) >> 14;
389 matrice[1][0] = (sza * cya) >> 14;
390 matrice[2][0] = -sya;
392 matrice[0][1] = (((cza * sya) >> 14) * sxa - sza * cxa) >> 14;
393 matrice[1][1] = (((sza * sya) >> 14) * sxa + cxa * cza) >> 14;
394 matrice[2][1] = (sxa * cya) >> 14;
396 matrice[0][2] = (((cza * sya) >> 14) * cxa + sza * sxa) >> 14;
397 matrice[1][2] = (((sza * sya) >> 14) * cxa - cza * sxa) >> 14;
398 matrice[2][2] = (cxa * cya) >> 14;
400 /* apply translation matrix to all points */
401 for (i = 0; i < nb_points; i++)
403 point3D[i].x = matrice[0][0] * sommet[i].x + matrice[1][0] * sommet[i].y
404 + matrice[2][0] * sommet[i].z;
406 point3D[i].y = matrice[0][1] * sommet[i].x + matrice[1][1] * sommet[i].y
407 + matrice[2][1] * sommet[i].z;
409 point3D[i].z = matrice[0][2] * sommet[i].x + matrice[1][2] * sommet[i].y
410 + matrice[2][2] * sommet[i].z;
414 static void cube_viewport(void)
416 int i;
418 /* Do viewport transformation for all points */
419 for (i = 0; i < nb_points; i++)
421 #if ASPECT != 256
422 point2D[i].x = (point3D[i].x * ASPECT) / (point3D[i].z + (z_off << 14))
423 + x_off;
424 #else
425 point2D[i].x = (point3D[i].x << 8) / (point3D[i].z + (z_off << 14))
426 + x_off;
427 #endif
428 point2D[i].y = (point3D[i].y << 8) / (point3D[i].z + (z_off << 14))
429 + y_off;
433 static void cube_draw(void)
435 int i, j, line;
436 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
437 unsigned old_foreground;
438 #endif
440 switch (mode)
442 #if LCD_DEPTH > 1 || defined(USE_GSLIB)
443 case SOLID:
445 old_foreground = MY_GET_FOREGROUND();
446 for (i = 0; i < 6; i++)
448 /* backface culling; if the shape winds counter-clockwise, we are
449 * looking at the backface, and the (simplified) cross product
450 * is < 0. Do not draw it. */
451 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
452 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
453 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
454 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
455 continue;
457 MY_SET_FOREGROUND(face_colors[i]);
458 MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
459 point2D[faces[i].corner[0]].y,
460 point2D[faces[i].corner[1]].x,
461 point2D[faces[i].corner[1]].y,
462 point2D[faces[i].corner[2]].x,
463 point2D[faces[i].corner[2]].y);
464 MY_FILLTRIANGLE(point2D[faces[i].corner[0]].x,
465 point2D[faces[i].corner[0]].y,
466 point2D[faces[i].corner[2]].x,
467 point2D[faces[i].corner[2]].y,
468 point2D[faces[i].corner[3]].x,
469 point2D[faces[i].corner[3]].y);
472 MY_SET_FOREGROUND(old_foreground);
473 break;
474 #endif /* (LCD_DEPTH > 1) || GSLIB */
476 case HIDDEN_LINES:
478 rb->memset(lines_drawn, 0, sizeof(lines_drawn));
479 for (i = 0; i < 6; i++)
481 /* backface culling; if the shape winds counter-clockwise, we are
482 * looking at the backface, and the (simplified) cross product
483 * is < 0. Do not draw it. */
484 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
485 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
486 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
487 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
488 continue;
490 for (j = 0; j < 4; j++)
492 line = faces[i].line[j];
493 if (!lines_drawn[line])
495 lines_drawn[line] = true;
496 MYLCD(drawline)(point2D[lines[line].start].x,
497 point2D[lines[line].start].y,
498 point2D[lines[line].end].x,
499 point2D[lines[line].end].y);
503 break;
505 case WIREFRAME:
507 for (i = 0; i < 12; i++)
508 MYLCD(drawline)(point2D[lines[i].start].x,
509 point2D[lines[i].start].y,
510 point2D[lines[i].end].x,
511 point2D[lines[i].end].y);
512 break;
516 void cleanup(void *parameter)
518 (void)parameter;
520 #ifdef USE_GSLIB
521 grey_release();
522 #elif defined HAVE_LCD_CHARCELLS
523 pgfx_release();
524 #endif
527 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
529 char buffer[30];
530 int t_disp = 0;
531 #ifdef USE_GSLIB
532 unsigned char *gbuf;
533 size_t gbuf_size = 0;
534 bool mode_switch = true;
535 #endif
537 int button;
538 int lastbutton = BUTTON_NONE;
539 int xa = 0;
540 int ya = 0;
541 int za = 0;
542 int xs = 1;
543 int ys = 3;
544 int zs = 1;
545 bool highspeed = false;
546 bool paused = false;
547 bool redraw = true;
548 bool exit = false;
550 (void)(parameter);
551 rb = api;
553 #ifdef HAVE_LCD_BITMAP
554 #if LCD_DEPTH > 1
555 xlcd_init(rb);
556 #elif defined(USE_GSLIB)
557 gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
558 if (!grey_init(rb, gbuf, gbuf_size, GREY_BUFFERED,
559 LCD_WIDTH, LCD_HEIGHT, NULL))
561 rb->splash(HZ, "Couldn't init greyscale display");
562 return PLUGIN_ERROR;
564 /* init lcd_ function pointers */
565 lcdfuncs.update = rb->lcd_update;
566 lcdfuncs.clear_display = rb->lcd_clear_display;
567 lcdfuncs.drawline = rb->lcd_drawline;
568 lcdfuncs.putsxy = rb->lcd_putsxy;
570 grey_setfont(FONT_SYSFIXED);
571 #endif
572 rb->lcd_setfont(FONT_SYSFIXED);
573 #else /* LCD_CHARCELLS */
574 if (!pgfx_init(rb, 4, 2))
576 rb->splash(HZ*2, "Old LCD :(");
577 return PLUGIN_OK;
579 pgfx_display(3, 0);
580 #endif
582 while(!exit)
584 if (highspeed)
585 rb->yield();
586 else
587 rb->sleep(4);
589 if (redraw)
591 MYLCD(clear_display)();
592 cube_rotate(xa, ya, za);
593 cube_viewport();
594 cube_draw();
595 redraw = false;
598 #ifdef HAVE_LCD_BITMAP
599 if (t_disp > 0)
601 t_disp--;
602 rb->snprintf(buffer, sizeof(buffer), "x:%d y:%d z:%d h:%d",
603 xs, ys, zs, highspeed);
604 MYLCD(putsxy)(0, LCD_HEIGHT-8, buffer);
605 if (t_disp == 0)
606 redraw = true;
608 #else
609 if (t_disp > 0)
611 if (t_disp == DISP_TIME)
613 rb->snprintf(buffer, sizeof(buffer), "x%d", xs);
614 rb->lcd_puts(0, 0, buffer);
615 rb->snprintf(buffer, sizeof(buffer), "y%d", ys);
616 rb->lcd_puts(0, 1, buffer);
617 pgfx_display(3, 0);
618 rb->snprintf(buffer, sizeof(buffer), "z%d", zs);
619 rb->lcd_puts(8, 0, buffer);
620 rb->snprintf(buffer, sizeof(buffer), "h%d", highspeed);
621 rb->lcd_puts(8, 1, buffer);
623 t_disp--;
624 if (t_disp == 0)
626 rb->lcd_clear_display();
627 pgfx_display(3, 0);
630 #endif
631 #ifdef USE_GSLIB
632 if (mode_switch)
634 grey_show(mode == SOLID);
635 mode_switch = false;
637 #endif
638 MYLCD(update)();
640 if (!paused)
642 xa += xs;
643 if (xa > 359)
644 xa -= 360;
645 else if (xa < 0)
646 xa += 360;
648 ya += ys;
649 if (ya > 359)
650 ya -= 360;
651 else if (ya < 0)
652 ya += 360;
654 za += zs;
655 if (za > 359)
656 za -= 360;
657 else if (za < 0)
658 za += 360;
659 redraw = true;
662 button = rb->button_get(false);
663 switch (button)
665 case CUBE_X_INC:
666 case (CUBE_X_INC|BUTTON_REPEAT):
667 if( !paused )
669 if( xs < 10)
670 xs++;
672 else
674 xa++;
675 if( xa > 359 )
676 xa -= 360;
678 t_disp = DISP_TIME;
679 redraw = true;
680 break;
682 case CUBE_X_DEC:
683 case (CUBE_X_DEC|BUTTON_REPEAT):
684 if( !paused )
686 if (xs > -10)
687 xs--;
689 else
691 xa--;
692 if( xa < 0 )
693 xa += 360;
695 t_disp = DISP_TIME;
696 redraw = true;
697 break;
699 case CUBE_Y_INC:
700 case (CUBE_Y_INC|BUTTON_REPEAT):
701 if( !paused )
703 if (ys < 10)
704 ys++;
706 else
708 ya++;
709 if( ya > 359 )
710 ya -= 360;
712 t_disp = DISP_TIME;
713 redraw = true;
714 break;
716 case CUBE_Y_DEC:
717 case (CUBE_Y_DEC|BUTTON_REPEAT):
718 if( !paused )
720 if (ys > -10)
721 ys--;
723 else
725 ya--;
726 if( ya < 0 )
727 ya += 360;
729 t_disp = DISP_TIME;
730 redraw = true;
731 break;
733 case CUBE_Z_INC:
734 case (CUBE_Z_INC|BUTTON_REPEAT):
735 if( !paused )
737 if (zs < 10)
738 zs++;
740 else
742 za++;
743 if( za > 359 )
744 za -= 360;
746 t_disp = DISP_TIME;
747 redraw = true;
748 break;
750 case CUBE_Z_DEC:
751 case (CUBE_Z_DEC|BUTTON_REPEAT):
752 if( !paused )
754 if (zs > -10)
755 zs--;
757 else
759 za--;
760 if( za < 0 )
761 za += 360;
763 t_disp = DISP_TIME;
764 redraw = true;
765 break;
767 case CUBE_MODE:
768 #ifdef CUBE_MODE_PRE
769 if (lastbutton != CUBE_MODE_PRE)
770 break;
771 #endif
772 if (++mode >= NUM_MODES)
773 mode = 0;
774 #ifdef USE_GSLIB
775 mylcd = (mode == SOLID) ? &greyfuncs : &lcdfuncs;
776 mode_switch = true;
777 #endif
778 redraw = true;
779 break;
781 case CUBE_PAUSE:
782 #ifdef CUBE_PAUSE_PRE
783 if (lastbutton != CUBE_PAUSE_PRE)
784 break;
785 #endif
786 paused = !paused;
787 break;
789 case CUBE_HIGHSPEED:
790 #ifdef CUBE_HIGHSPEED_PRE
791 if (lastbutton != CUBE_HIGHSPEED_PRE)
792 break;
793 #endif
794 highspeed = !highspeed;
795 t_disp = DISP_TIME;
796 break;
798 #ifdef CUBE_RC_QUIT
799 case CUBE_RC_QUIT:
800 #endif
801 case CUBE_QUIT:
802 exit = true;
803 break;
805 default:
806 if (rb->default_event_handler_ex(button, cleanup, NULL)
807 == SYS_USB_CONNECTED)
808 return PLUGIN_USB_CONNECTED;
809 break;
811 if (button != BUTTON_NONE)
812 lastbutton = button;
815 #ifdef USE_GSLIB
816 grey_release();
817 #elif defined(HAVE_LCD_CHARCELLS)
818 pgfx_release();
819 #endif
820 return PLUGIN_OK;