Prepare new maemo release
[maemo-rb.git] / apps / plugins / cube.c
blobb6031af9752b946d20241f739983bc93df08b9e3
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 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
23 #include "plugin.h"
24 #include "lib/playergfx.h"
25 #include "lib/pluginlib_exit.h"
26 #if LCD_DEPTH > 1
27 #include "lib/mylcd.h" /* MYLCD_CFG_RB_XLCD or MYLCD_CFG_PGFX */
28 #include "lib/grey.h"
29 #else
30 #include "lib/grey.h"
31 #include "lib/mylcd.h" /* MYLCD_CFG_GREYLIB or MYLCD_CFG_PGFX */
32 #endif
33 #include "lib/xlcd.h"
34 #include "lib/fixedpoint.h"
36 /* Loops that the values are displayed */
37 #define DISP_TIME 30
39 /* variable button definitions */
40 #if CONFIG_KEYPAD == RECORDER_PAD
41 #define CUBE_QUIT BUTTON_OFF
42 #define CUBE_NEXT BUTTON_RIGHT
43 #define CUBE_PREV BUTTON_LEFT
44 #define CUBE_INC BUTTON_UP
45 #define CUBE_DEC BUTTON_DOWN
46 #define CUBE_MODE BUTTON_F1
47 #define CUBE_PAUSE BUTTON_PLAY
48 #define CUBE_HIGHSPEED BUTTON_ON
50 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
51 #define CUBE_QUIT BUTTON_OFF
52 #define CUBE_NEXT BUTTON_RIGHT
53 #define CUBE_PREV BUTTON_LEFT
54 #define CUBE_INC BUTTON_UP
55 #define CUBE_DEC BUTTON_DOWN
56 #define CUBE_MODE BUTTON_F1
57 #define CUBE_PAUSE BUTTON_SELECT
58 #define CUBE_HIGHSPEED BUTTON_ON
60 #elif CONFIG_KEYPAD == PLAYER_PAD
61 #define CUBE_QUIT BUTTON_STOP
62 #define CUBE_INC BUTTON_RIGHT
63 #define CUBE_DEC BUTTON_LEFT
64 #define CUBE_NEXT (BUTTON_ON | BUTTON_RIGHT)
65 #define CUBE_PREV (BUTTON_ON | BUTTON_LEFT)
66 #define CUBE_MODE BUTTON_MENU
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_NEXT BUTTON_RIGHT
74 #define CUBE_PREV BUTTON_LEFT
75 #define CUBE_INC BUTTON_UP
76 #define CUBE_DEC BUTTON_DOWN
77 #define CUBE_MODE_PRE BUTTON_MENU
78 #define CUBE_MODE (BUTTON_MENU | BUTTON_REL)
79 #define CUBE_PAUSE (BUTTON_MENU | BUTTON_LEFT)
80 #define CUBE_HIGHSPEED (BUTTON_MENU | BUTTON_RIGHT)
82 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
83 (CONFIG_KEYPAD == IRIVER_H300_PAD)
84 #define CUBE_QUIT BUTTON_OFF
85 #define CUBE_NEXT BUTTON_RIGHT
86 #define CUBE_PREV BUTTON_LEFT
87 #define CUBE_INC BUTTON_UP
88 #define CUBE_DEC BUTTON_DOWN
89 #define CUBE_MODE BUTTON_MODE
90 #define CUBE_PAUSE BUTTON_ON
91 #define CUBE_HIGHSPEED BUTTON_SELECT
93 #define CUBE_RC_QUIT BUTTON_RC_STOP
95 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
96 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
97 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
98 #define CUBE_QUIT (BUTTON_SELECT | BUTTON_MENU)
99 #define CUBE_NEXT BUTTON_RIGHT
100 #define CUBE_PREV BUTTON_LEFT
101 #define CUBE_INC BUTTON_SCROLL_FWD
102 #define CUBE_DEC BUTTON_SCROLL_BACK
103 #define CUBE_MODE BUTTON_MENU
104 #define CUBE_PAUSE BUTTON_PLAY
105 #define CUBE_HIGHSPEED_PRE BUTTON_SELECT
106 #define CUBE_HIGHSPEED (BUTTON_SELECT | BUTTON_REL)
108 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
109 #define CUBE_QUIT BUTTON_PLAY
110 #define CUBE_NEXT BUTTON_RIGHT
111 #define CUBE_PREV BUTTON_LEFT
112 #define CUBE_INC BUTTON_UP
113 #define CUBE_DEC BUTTON_DOWN
114 #define CUBE_MODE BUTTON_MODE
115 #define CUBE_PAUSE BUTTON_SELECT
116 #define CUBE_HIGHSPEED BUTTON_EQ
118 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
119 #define CUBE_QUIT BUTTON_POWER
120 #define CUBE_NEXT BUTTON_RIGHT
121 #define CUBE_PREV BUTTON_LEFT
122 #define CUBE_INC BUTTON_UP
123 #define CUBE_DEC BUTTON_DOWN
124 #define CUBE_MODE BUTTON_REC
125 #define CUBE_PAUSE BUTTON_PLAY
126 #define CUBE_HIGHSPEED BUTTON_SELECT
128 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
129 #define CUBE_QUIT BUTTON_POWER
130 #define CUBE_NEXT BUTTON_RIGHT
131 #define CUBE_PREV BUTTON_LEFT
132 #define CUBE_INC BUTTON_UP
133 #define CUBE_DEC BUTTON_DOWN
134 #define CUBE_MODE BUTTON_MENU
135 #define CUBE_PAUSE BUTTON_SELECT
136 #define CUBE_HIGHSPEED BUTTON_A
138 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
139 #define CUBE_QUIT BUTTON_POWER
140 #define CUBE_NEXT BUTTON_RIGHT
141 #define CUBE_PREV BUTTON_LEFT
142 #define CUBE_INC BUTTON_SCROLL_FWD
143 #define CUBE_DEC BUTTON_SCROLL_BACK
144 #define CUBE_MODE BUTTON_DOWN
145 #define CUBE_PAUSE BUTTON_UP
146 #define CUBE_HIGHSPEED BUTTON_SELECT
148 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
149 #define CUBE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
150 #define CUBE_NEXT BUTTON_RIGHT
151 #define CUBE_PREV BUTTON_LEFT
152 #define CUBE_INC BUTTON_SCROLL_FWD
153 #define CUBE_DEC BUTTON_SCROLL_BACK
154 #define CUBE_MODE BUTTON_DOWN
155 #define CUBE_PAUSE BUTTON_UP
156 #define CUBE_HIGHSPEED BUTTON_SELECT
158 #elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
159 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
160 (CONFIG_KEYPAD == SANSA_M200_PAD)
161 #define CUBE_QUIT BUTTON_POWER
162 #define CUBE_NEXT BUTTON_RIGHT
163 #define CUBE_PREV BUTTON_LEFT
164 #define CUBE_INC BUTTON_VOL_UP
165 #define CUBE_DEC BUTTON_VOL_DOWN
166 #define CUBE_MODE BUTTON_DOWN
167 #define CUBE_PAUSE BUTTON_UP
168 #define CUBE_HIGHSPEED BUTTON_SELECT
171 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
172 #define CUBE_QUIT BUTTON_POWER
173 #define CUBE_NEXT BUTTON_RIGHT
174 #define CUBE_PREV BUTTON_LEFT
175 #define CUBE_INC BUTTON_SCROLL_UP
176 #define CUBE_DEC BUTTON_SCROLL_DOWN
177 #define CUBE_MODE BUTTON_REW
178 #define CUBE_PAUSE BUTTON_PLAY
179 #define CUBE_HIGHSPEED BUTTON_FF
181 #elif CONFIG_KEYPAD == MROBE500_PAD
182 #define CUBE_QUIT BUTTON_POWER
184 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
185 #define CUBE_QUIT BUTTON_BACK
186 #define CUBE_NEXT BUTTON_RIGHT
187 #define CUBE_PREV BUTTON_LEFT
188 #define CUBE_INC BUTTON_UP
189 #define CUBE_DEC BUTTON_DOWN
190 #define CUBE_MODE BUTTON_MENU
191 #define CUBE_PAUSE BUTTON_PLAY
192 #define CUBE_HIGHSPEED BUTTON_SELECT
194 #elif (CONFIG_KEYPAD == MROBE100_PAD)
195 #define CUBE_QUIT BUTTON_POWER
196 #define CUBE_NEXT BUTTON_RIGHT
197 #define CUBE_PREV BUTTON_LEFT
198 #define CUBE_INC BUTTON_UP
199 #define CUBE_DEC BUTTON_DOWN
200 #define CUBE_MODE BUTTON_MENU
201 #define CUBE_PAUSE BUTTON_PLAY
202 #define CUBE_HIGHSPEED BUTTON_SELECT
204 #elif (CONFIG_KEYPAD == IAUDIO_M3_PAD)
205 #define CUBE_QUIT BUTTON_RC_REC
206 #define CUBE_NEXT BUTTON_RC_FF
207 #define CUBE_PREV BUTTON_RC_REW
208 #define CUBE_INC BUTTON_RC_VOL_UP
209 #define CUBE_DEC BUTTON_RC_VOL_DOWN
210 #define CUBE_MODE BUTTON_RC_MODE
211 #define CUBE_PAUSE BUTTON_RC_PLAY
212 #define CUBE_HIGHSPEED BUTTON_RC_MENU
214 #elif CONFIG_KEYPAD == COWON_D2_PAD
215 #define CUBE_QUIT BUTTON_POWER
217 #elif (CONFIG_KEYPAD == IAUDIO67_PAD)
218 #define CUBE_QUIT BUTTON_POWER
219 #define CUBE_NEXT BUTTON_RIGHT
220 #define CUBE_PREV BUTTON_LEFT
221 #define CUBE_INC BUTTON_VOLUP
222 #define CUBE_DEC BUTTON_VOLDOWN
223 #define CUBE_MODE BUTTON_MENU
224 #define CUBE_PAUSE BUTTON_PLAY
225 #define CUBE_HIGHSPEED BUTTON_STOP
227 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
228 #define CUBE_QUIT BUTTON_BACK
229 #define CUBE_NEXT BUTTON_RIGHT
230 #define CUBE_PREV BUTTON_LEFT
231 #define CUBE_INC BUTTON_UP
232 #define CUBE_DEC BUTTON_DOWN
233 #define CUBE_MODE BUTTON_MENU
234 #define CUBE_PAUSE BUTTON_PLAY
235 #define CUBE_HIGHSPEED BUTTON_SELECT
237 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
238 #define CUBE_QUIT BUTTON_POWER
239 #define CUBE_NEXT BUTTON_RIGHT
240 #define CUBE_PREV BUTTON_LEFT
241 #define CUBE_INC BUTTON_UP
242 #define CUBE_DEC BUTTON_DOWN
243 #define CUBE_MODE BUTTON_MENU
244 #define CUBE_PAUSE BUTTON_SELECT
245 #define CUBE_HIGHSPEED BUTTON_VIEW
247 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
248 #define CUBE_QUIT BUTTON_POWER
249 #define CUBE_NEXT BUTTON_NEXT
250 #define CUBE_PREV BUTTON_PREV
251 #define CUBE_INC BUTTON_UP
252 #define CUBE_DEC BUTTON_DOWN
253 #define CUBE_MODE BUTTON_MENU
254 #define CUBE_PAUSE BUTTON_PLAY
255 #define CUBE_HIGHSPEED BUTTON_RIGHT
257 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
258 #define CUBE_QUIT BUTTON_POWER
259 #define CUBE_NEXT BUTTON_NEXT
260 #define CUBE_PREV BUTTON_PREV
261 #define CUBE_INC BUTTON_UP
262 #define CUBE_DEC BUTTON_DOWN
263 #define CUBE_MODE BUTTON_MENU
264 #define CUBE_PAUSE BUTTON_PLAY
265 #define CUBE_HIGHSPEED BUTTON_RIGHT
267 #elif CONFIG_KEYPAD == ONDAVX747_PAD
268 #define CUBE_QUIT BUTTON_POWER
269 #elif CONFIG_KEYPAD == ONDAVX777_PAD
270 #define CUBE_QUIT BUTTON_POWER
272 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
273 #define CUBE_QUIT BUTTON_REC
274 #define CUBE_NEXT BUTTON_RIGHT
275 #define CUBE_PREV BUTTON_LEFT
276 #define CUBE_INC BUTTON_UP
277 #define CUBE_DEC BUTTON_DOWN
278 #define CUBE_MODE BUTTON_REW
279 #define CUBE_PAUSE BUTTON_PLAY
280 #define CUBE_HIGHSPEED BUTTON_FFWD
282 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
283 #define CUBE_QUIT BUTTON_REC
284 #define CUBE_NEXT BUTTON_NEXT
285 #define CUBE_PREV BUTTON_PREV
286 #define CUBE_INC BUTTON_UP
287 #define CUBE_DEC BUTTON_DOWN
288 #define CUBE_MODE BUTTON_MENU
289 #define CUBE_PAUSE BUTTON_PLAY
290 #define CUBE_HIGHSPEED BUTTON_OK
292 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
293 #define CUBE_QUIT (BUTTON_REC | BUTTON_PLAY)
294 #define CUBE_NEXT BUTTON_FF
295 #define CUBE_PREV BUTTON_REW
296 #define CUBE_INC BUTTON_VOL_UP
297 #define CUBE_DEC BUTTON_VOL_DOWN
298 #define CUBE_MODE BUTTON_REC
299 #define CUBE_PAUSE BUTTON_PLAY
300 #define CUBE_HIGHSPEED BUTTON_FUNC
302 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
303 #define CUBE_QUIT (BUTTON_MENU | BUTTON_REPEAT)
304 #define CUBE_NEXT BUTTON_FF
305 #define CUBE_PREV BUTTON_REW
306 #define CUBE_INC BUTTON_UP
307 #define CUBE_DEC BUTTON_DOWN
308 #define CUBE_MODE BUTTON_MENU
309 #define CUBE_PAUSE BUTTON_PLAY
310 #define CUBE_HIGHSPEED BUTTON_ENTER
312 #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
313 #define CUBE_QUIT BUTTON_POWER
314 #define CUBE_NEXT BUTTON_RIGHT
315 #define CUBE_PREV BUTTON_LEFT
316 #define CUBE_INC BUTTON_UP
317 #define CUBE_DEC BUTTON_DOWN
318 #define CUBE_MODE BUTTON_SELECT
319 #define CUBE_PAUSE BUTTON_PLAYPAUSE
320 #define CUBE_HIGHSPEED BUTTON_BACK
322 #elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
323 #define CUBE_QUIT BUTTON_POWER
324 #define CUBE_NEXT BUTTON_NEXT
325 #define CUBE_PREV BUTTON_PREV
326 #define CUBE_INC BUTTON_VOL_UP
327 #define CUBE_DEC BUTTON_VOL_DOWN
328 #define CUBE_MODE BUTTON_SELECT
329 #define CUBE_PAUSE BUTTON_DOWN
330 #define CUBE_HIGHSPEED BUTTON_LEFT
332 #elif (CONFIG_KEYPAD == SAMSUNG_YPR0_PAD)
333 #define CUBE_QUIT BUTTON_BACK
334 #define CUBE_NEXT BUTTON_RIGHT
335 #define CUBE_PREV BUTTON_LEFT
336 #define CUBE_INC BUTTON_UP
337 #define CUBE_DEC BUTTON_DOWN
338 #define CUBE_MODE BUTTON_MENU
339 #define CUBE_PAUSE BUTTON_USER
340 #define CUBE_HIGHSPEED BUTTON_SELECT
342 #elif (CONFIG_KEYPAD == HM60X_PAD)
343 #define CUBE_QUIT BUTTON_POWER
344 #define CUBE_NEXT BUTTON_RIGHT
345 #define CUBE_PREV BUTTON_LEFT
346 #define CUBE_INC BUTTON_UP
347 #define CUBE_DEC BUTTON_DOWN
348 #define CUBE_MODE (BUTTON_SELECT|BUTTON_POWER)
349 #define CUBE_PAUSE BUTTON_SELECT
350 #define CUBE_HIGHSPEED (BUTTON_UP|BUTTON_POWER)
352 #elif (CONFIG_KEYPAD == HM801_PAD)
353 #define CUBE_QUIT BUTTON_POWER
354 #define CUBE_NEXT BUTTON_RIGHT
355 #define CUBE_PREV BUTTON_LEFT
356 #define CUBE_INC BUTTON_UP
357 #define CUBE_DEC BUTTON_DOWN
358 #define CUBE_MODE BUTTON_PREV
359 #define CUBE_PAUSE BUTTON_SELECT
360 #define CUBE_HIGHSPEED BUTTON_NEXT
362 #else
363 #error No keymap defined!
364 #endif
366 #ifdef HAVE_TOUCHSCREEN
367 #ifndef CUBE_QUIT
368 #define CUBE_QUIT BUTTON_TOPLEFT
369 #endif
370 #ifndef CUBE_NEXT
371 #define CUBE_NEXT BUTTON_MIDRIGHT
372 #endif
373 #ifndef CUBE_PREV
374 #define CUBE_PREV BUTTON_MIDLEFT
375 #endif
376 #ifndef CUBE_INC
377 #define CUBE_INC BUTTON_TOPMIDDLE
378 #endif
379 #ifndef CUBE_DEC
380 #define CUBE_DEC BUTTON_BOTTOMMIDDLE
381 #endif
382 #ifndef CUBE_MODE
383 #define CUBE_MODE BUTTON_TOPRIGHT
384 #endif
385 #ifndef CUBE_PAUSE
386 #define CUBE_PAUSE BUTTON_CENTER
387 #endif
388 #ifndef CUBE_HIGHSPEED
389 #define CUBE_HIGHSPEED BUTTON_BOTTOMRIGHT
390 #endif
391 #endif
394 #ifdef HAVE_LCD_BITMAP
396 #define DIST (10 * MIN(LCD_HEIGHT, LCD_WIDTH) / 16)
397 static int x_off = LCD_WIDTH/2;
398 static int y_off = LCD_HEIGHT/2;
400 #if LCD_DEPTH == 1
401 #define USEGSLIB
402 GREY_INFO_STRUCT
403 struct my_lcd {
404 void (*update)(void);
405 void (*clear_display)(void);
406 void (*drawline)(int x1, int y1, int x2, int y2);
407 void (*putsxy)(int x, int y, const unsigned char *string);
410 static struct my_lcd greyfuncs = {
411 grey_update, grey_clear_display, grey_drawline, grey_putsxy
413 static struct my_lcd lcdfuncs; /* initialised at runtime */
414 static struct my_lcd *mylcd = &greyfuncs;
416 #define MYLCD(fn) mylcd->fn
418 #else
419 #define MYLCD(fn) rb->lcd_ ## fn
420 #endif
422 #if CONFIG_LCD == LCD_SSD1815
423 #define ASPECT 320 /* = 1.25 (fixed point 24.8) */
424 #else
425 #define ASPECT 256 /* = 1.00 */
426 #endif
428 #else /* !LCD_BITMAP */
430 #define MYLCD(fn) pgfx_ ## fn
431 #define DIST 9
432 static int x_off = 10;
433 static int y_off = 7;
434 #define ASPECT 300 /* = 1.175 */
436 #endif /* !LCD_BITMAP */
438 struct point_3D {
439 long x, y, z;
442 struct point_2D {
443 long x, y;
446 struct line {
447 int start, end;
450 struct face {
451 int corner[4];
452 int line[4];
455 /* initial, unrotated cube corners */
456 static const struct point_3D sommet[8] =
458 {-DIST, -DIST, -DIST},
459 { DIST, -DIST, -DIST},
460 { DIST, DIST, -DIST},
461 {-DIST, DIST, -DIST},
462 {-DIST, -DIST, DIST},
463 { DIST, -DIST, DIST},
464 { DIST, DIST, DIST},
465 {-DIST, DIST, DIST}
468 /* The 12 lines forming the edges */
469 static const struct line lines[12] =
471 {0, 1}, {1, 2}, {2, 3}, {3, 0},
472 {4, 7}, {7, 6}, {6, 5}, {5, 4},
473 {0, 4}, {1, 5}, {2, 6}, {3, 7}
476 static bool lines_drawn[12];
478 /* The 6 faces of the cube; points are in clockwise order when viewed
479 from the outside */
480 static const struct face faces[6] =
482 {{0, 1, 2, 3}, {0, 1, 2, 3}},
483 {{4, 7, 6, 5}, {4, 5, 6, 7}},
484 {{0, 4, 5, 1}, {8, 7, 9, 0}},
485 {{2, 6, 7, 3}, {10, 5, 11, 2}},
486 {{0, 3, 7, 4}, {3, 11, 4, 8}},
487 {{1, 5, 6, 2}, {9, 6, 10, 1}}
490 #if LCD_DEPTH > 1 || defined(USEGSLIB)
491 static const unsigned face_colors[6] =
493 #ifdef HAVE_LCD_COLOR
494 LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(255, 0, 0), LCD_RGBPACK(0, 255, 0),
495 LCD_RGBPACK(0, 255, 0), LCD_RGBPACK(0, 0, 255), LCD_RGBPACK(0, 0, 255)
496 #elif defined(USEGSLIB)
497 #ifdef MROBE_100
498 GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
499 GREY_DARKGRAY, GREY_WHITE, GREY_WHITE
500 #else
501 GREY_LIGHTGRAY, GREY_LIGHTGRAY, GREY_DARKGRAY,
502 GREY_DARKGRAY, GREY_BLACK, GREY_BLACK
503 #endif
504 #else
505 LCD_LIGHTGRAY, LCD_LIGHTGRAY, LCD_DARKGRAY,
506 LCD_DARKGRAY, LCD_BLACK, LCD_BLACK
507 #endif
509 #endif
511 enum {
512 #if LCD_DEPTH > 1 || defined(USEGSLIB)
513 SOLID,
514 #endif
515 HIDDEN_LINES,
516 WIREFRAME,
517 NUM_MODES
520 static int mode = 0;
522 struct counter {
523 const char *label;
524 short speed;
525 short angle;
528 static struct counter axes[] = {
529 {"x-axis", 1, 0},
530 {"y-axis", 3, 0},
531 {"z-axis", 2, 0}
534 static struct point_3D point3D[8];
535 static struct point_2D point2D[8];
536 static long matrice[3][3];
538 static const int nb_points = 8;
539 static long z_off = 600;
541 static void cube_rotate(int xa, int ya, int za)
543 int i;
544 /* Just to prevent unnecessary lookups */
545 long sxa, cxa, sya, cya, sza, cza;
547 sxa = fp14_sin(xa);
548 cxa = fp14_cos(xa);
549 sya = fp14_sin(ya);
550 cya = fp14_cos(ya);
551 sza = fp14_sin(za);
552 cza = fp14_cos(za);
554 /* calculate overall translation matrix */
555 matrice[0][0] = (cza * cya) >> 14;
556 matrice[1][0] = (sza * cya) >> 14;
557 matrice[2][0] = -sya;
559 matrice[0][1] = (((cza * sya) >> 14) * sxa - sza * cxa) >> 14;
560 matrice[1][1] = (((sza * sya) >> 14) * sxa + cxa * cza) >> 14;
561 matrice[2][1] = (sxa * cya) >> 14;
563 matrice[0][2] = (((cza * sya) >> 14) * cxa + sza * sxa) >> 14;
564 matrice[1][2] = (((sza * sya) >> 14) * cxa - cza * sxa) >> 14;
565 matrice[2][2] = (cxa * cya) >> 14;
567 /* apply translation matrix to all points */
568 for (i = 0; i < nb_points; i++)
570 point3D[i].x = matrice[0][0] * sommet[i].x + matrice[1][0] * sommet[i].y
571 + matrice[2][0] * sommet[i].z;
573 point3D[i].y = matrice[0][1] * sommet[i].x + matrice[1][1] * sommet[i].y
574 + matrice[2][1] * sommet[i].z;
576 point3D[i].z = matrice[0][2] * sommet[i].x + matrice[1][2] * sommet[i].y
577 + matrice[2][2] * sommet[i].z;
581 static void cube_viewport(void)
583 int i;
585 /* Do viewport transformation for all points */
586 for (i = 0; i < nb_points; i++)
588 #if ASPECT != 256
589 point2D[i].x = (point3D[i].x * ASPECT) / (point3D[i].z + (z_off << 14))
590 + x_off;
591 #else
592 point2D[i].x = (point3D[i].x << 8) / (point3D[i].z + (z_off << 14))
593 + x_off;
594 #endif
595 point2D[i].y = (point3D[i].y << 8) / (point3D[i].z + (z_off << 14))
596 + y_off;
600 static void cube_draw(void)
602 int i, j, line;
603 #if LCD_DEPTH > 1 || defined(USEGSLIB)
604 unsigned old_foreground;
605 #endif
607 switch (mode)
609 #if LCD_DEPTH > 1 || defined(USEGSLIB)
610 case SOLID:
612 old_foreground = mylcd_get_foreground();
613 for (i = 0; i < 6; i++)
615 /* backface culling; if the shape winds counter-clockwise, we are
616 * looking at the backface, and the (simplified) cross product
617 * is < 0. Do not draw it. */
618 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
619 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
620 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
621 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
622 continue;
624 mylcd_set_foreground(face_colors[i]);
625 mylcd_filltriangle(point2D[faces[i].corner[0]].x,
626 point2D[faces[i].corner[0]].y,
627 point2D[faces[i].corner[1]].x,
628 point2D[faces[i].corner[1]].y,
629 point2D[faces[i].corner[2]].x,
630 point2D[faces[i].corner[2]].y);
631 mylcd_filltriangle(point2D[faces[i].corner[0]].x,
632 point2D[faces[i].corner[0]].y,
633 point2D[faces[i].corner[2]].x,
634 point2D[faces[i].corner[2]].y,
635 point2D[faces[i].corner[3]].x,
636 point2D[faces[i].corner[3]].y);
639 mylcd_set_foreground(old_foreground);
640 break;
641 #endif /* (LCD_DEPTH > 1) || GSLIB */
643 case HIDDEN_LINES:
645 rb->memset(lines_drawn, 0, sizeof(lines_drawn));
646 for (i = 0; i < 6; i++)
648 /* backface culling; if the shape winds counter-clockwise, we are
649 * looking at the backface, and the (simplified) cross product
650 * is < 0. Do not draw it. */
651 if (0 >= (point2D[faces[i].corner[1]].x - point2D[faces[i].corner[0]].x)
652 * (point2D[faces[i].corner[2]].y - point2D[faces[i].corner[1]].y)
653 - (point2D[faces[i].corner[1]].y - point2D[faces[i].corner[0]].y)
654 * (point2D[faces[i].corner[2]].x - point2D[faces[i].corner[1]].x))
655 continue;
657 for (j = 0; j < 4; j++)
659 line = faces[i].line[j];
660 if (!lines_drawn[line])
662 lines_drawn[line] = true;
663 MYLCD(drawline)(point2D[lines[line].start].x,
664 point2D[lines[line].start].y,
665 point2D[lines[line].end].x,
666 point2D[lines[line].end].y);
670 break;
672 case WIREFRAME:
674 for (i = 0; i < 12; i++)
675 MYLCD(drawline)(point2D[lines[i].start].x,
676 point2D[lines[i].start].y,
677 point2D[lines[i].end].x,
678 point2D[lines[i].end].y);
679 break;
683 static void cleanup(void)
685 #ifdef USEGSLIB
686 grey_release();
687 #elif defined HAVE_LCD_CHARCELLS
688 pgfx_release();
689 #endif
692 enum plugin_status plugin_start(const void* parameter)
694 int t_disp = 0;
695 #ifdef USEGSLIB
696 unsigned char *gbuf;
697 size_t gbuf_size = 0;
698 bool mode_switch = true;
699 #endif
701 int button;
702 #if defined(CUBE_MODE_PRE) || \
703 defined(CUBE_PAUSE_PRE) || \
704 defined(CUBE_HIGHSPEED_PRE)
705 int lastbutton = BUTTON_NONE;
706 #endif
707 int curr = 0;
708 bool highspeed = false;
709 bool paused = false;
710 bool redraw = true;
711 bool quit = false;
713 (void)(parameter);
715 #ifdef HAVE_LCD_BITMAP
716 #if defined(USEGSLIB)
717 gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
718 if (!grey_init(gbuf, gbuf_size, GREY_BUFFERED,
719 LCD_WIDTH, LCD_HEIGHT, NULL))
721 rb->splash(HZ, "Couldn't init greyscale display");
722 return PLUGIN_ERROR;
725 /* init lcd_ function pointers */
726 lcdfuncs.update = rb->lcd_update;
727 lcdfuncs.clear_display = rb->lcd_clear_display;
728 lcdfuncs.drawline = rb->lcd_drawline;
729 lcdfuncs.putsxy = rb->lcd_putsxy;
731 #ifdef MROBE_100
732 grey_set_background(GREY_BLACK);
733 #endif
735 grey_setfont(FONT_SYSFIXED);
736 #endif
737 rb->lcd_setfont(FONT_SYSFIXED);
738 #else /* LCD_CHARCELLS */
739 if (!pgfx_init(4, 2))
741 rb->splash(HZ*2, "Old LCD :(");
742 return PLUGIN_OK;
744 pgfx_display(0, 0);
745 #endif
747 atexit(cleanup);
748 while(!quit)
750 if (redraw)
752 MYLCD(clear_display)();
753 cube_rotate(axes[0].angle, axes[1].angle, axes[2].angle);
754 cube_viewport();
755 cube_draw();
756 redraw = false;
759 #ifdef HAVE_LCD_BITMAP
760 if (t_disp > 0)
762 char buffer[30];
763 t_disp--;
764 rb->snprintf(buffer, sizeof(buffer), "%s: %d %s",
765 axes[curr].label,
766 paused ? axes[curr].angle : axes[curr].speed,
767 highspeed ? "(hs)" : "");
768 MYLCD(putsxy)(0, LCD_HEIGHT-8, buffer);
769 if (t_disp == 0)
770 redraw = true;
772 #else
773 if (t_disp > 0)
775 if (t_disp == DISP_TIME)
777 rb->lcd_puts(5, 0, axes[curr].label);
778 rb->lcd_putsf(5, 1, "%d %c",
779 paused ? axes[curr].angle : axes[curr].speed,
780 highspeed ? 'H' : ' ');
782 t_disp--;
783 if (t_disp == 0)
785 rb->lcd_clear_display();
786 pgfx_display(0, 0);
789 #endif
790 #ifdef USEGSLIB
791 if (mode_switch)
793 grey_show(mode == SOLID);
794 mode_switch = false;
796 #endif
797 MYLCD(update)();
799 if (!paused)
801 int i;
803 for (i = 0; i < 3; i++)
805 axes[i].angle += axes[i].speed;
806 if (axes[i].angle > 359)
807 axes[i].angle -= 360;
808 else if (axes[i].angle < 0)
809 axes[i].angle += 360;
811 redraw = true;
813 if (highspeed)
814 rb->yield();
815 else
816 rb->sleep(HZ/25);
817 button = rb->button_get(false);
819 else
821 button = rb->button_get_w_tmo(HZ/25);
824 switch (button)
826 case CUBE_INC:
827 case CUBE_INC|BUTTON_REPEAT:
828 if (!paused)
830 if (axes[curr].speed < 10)
831 axes[curr].speed++;
833 else
835 if (++axes[curr].angle > 359)
836 axes[curr].angle -= 360;
838 t_disp = DISP_TIME;
839 redraw = true;
840 break;
842 case CUBE_DEC:
843 case CUBE_DEC|BUTTON_REPEAT:
844 if (!paused)
846 if (axes[curr].speed > -10)
847 axes[curr].speed--;
849 else
851 if (--axes[curr].angle < 0)
852 axes[curr].angle += 360;
854 t_disp = DISP_TIME;
855 redraw = true;
856 break;
858 case CUBE_NEXT:
859 if (++curr > 2)
860 curr = 0;
861 t_disp = DISP_TIME;
862 break;
864 case CUBE_PREV:
865 if (--curr < 0)
866 curr = 2;
867 t_disp = DISP_TIME;
868 break;
870 case CUBE_MODE:
871 #ifdef CUBE_MODE_PRE
872 if (lastbutton != CUBE_MODE_PRE)
873 break;
874 #endif
875 if (++mode >= NUM_MODES)
876 mode = 0;
877 #ifdef USEGSLIB
878 mylcd = (mode == SOLID) ? &greyfuncs : &lcdfuncs;
879 mode_switch = true;
880 #endif
881 redraw = true;
882 break;
884 case CUBE_PAUSE:
885 #ifdef CUBE_PAUSE_PRE
886 if (lastbutton != CUBE_PAUSE_PRE)
887 break;
888 #endif
889 paused = !paused;
890 break;
892 case CUBE_HIGHSPEED:
893 #ifdef CUBE_HIGHSPEED_PRE
894 if (lastbutton != CUBE_HIGHSPEED_PRE)
895 break;
896 #endif
897 highspeed = !highspeed;
898 t_disp = DISP_TIME;
899 break;
901 #ifdef CUBE_RC_QUIT
902 case CUBE_RC_QUIT:
903 #endif
904 case CUBE_QUIT:
905 exit(EXIT_SUCCESS);
906 break;
908 default:
909 exit_on_usb(button);
910 break;
912 #if defined(CUBE_MODE_PRE) || \
913 defined(CUBE_PAUSE_PRE) || \
914 defined(CUBE_HIGHSPEED_PRE)
915 if (button != BUTTON_NONE)
916 lastbutton = button;
917 #endif
920 return PLUGIN_OK;