fix red.
[kugel-rb/myfork.git] / apps / plugins / rockblox1d.c
blob12f05bb63910e46054848c9e39ef2b070174510e
1 /*****************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// __ \_/ ___\| |/ /| __ \ / __ \ \/ /
5 * Jukebox | | ( (__) ) \___| ( | \_\ ( (__) ) (
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 - 2008 Alexander Papst
11 * Idea from http://www.tetris1d.org
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "plugin.h"
25 PLUGIN_HEADER
28 #ifdef HAVE_LCD_BITMAP
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
32 #define ONEDROCKBLOX_QUIT BUTTON_OFF
34 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
35 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
36 #define ONEDROCKBLOX_QUIT BUTTON_OFF
38 #elif CONFIG_KEYPAD == ONDIO_PAD
39 #define ONEDROCKBLOX_DOWN BUTTON_RIGHT
40 #define ONEDROCKBLOX_QUIT BUTTON_OFF
42 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
43 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
44 #define ONEDROCKBLOX_QUIT BUTTON_POWER
46 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
47 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
48 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
49 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
50 #define ONEDROCKBLOX_QUIT (BUTTON_SELECT | BUTTON_MENU)
52 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
53 (CONFIG_KEYPAD == IRIVER_H300_PAD)
54 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
55 #define ONEDROCKBLOX_QUIT BUTTON_OFF
57 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
58 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
59 #define ONEDROCKBLOX_QUIT BUTTON_POWER
61 #elif CONFIG_KEYPAD == SANSA_E200_PAD || \
62 CONFIG_KEYPAD == SANSA_C200_PAD || \
63 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
64 CONFIG_KEYPAD == SANSA_M200_PAD
65 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
66 #define ONEDROCKBLOX_QUIT BUTTON_POWER
68 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
69 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
70 #define ONEDROCKBLOX_QUIT BUTTON_HOME
72 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
73 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
74 #define ONEDROCKBLOX_QUIT BUTTON_POWER
76 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
77 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
78 #define ONEDROCKBLOX_QUIT BUTTON_BACK
80 #elif (CONFIG_KEYPAD == MROBE100_PAD)
81 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
82 #define ONEDROCKBLOX_QUIT BUTTON_POWER
84 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
85 #define ONEDROCKBLOX_DOWN BUTTON_RC_PLAY
86 #define ONEDROCKBLOX_QUIT BUTTON_RC_REC
88 #elif (CONFIG_KEYPAD == COWOND2_PAD)
89 #define ONEDROCKBLOX_DOWN BUTTON_MENU
90 #define ONEDROCKBLOX_QUIT BUTTON_POWER
92 #elif CONFIG_KEYPAD == IAUDIO67_PAD
93 #define ONEDROCKBLOX_DOWN BUTTON_MENU
94 #define ONEDROCKBLOX_QUIT BUTTON_POWER
96 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
97 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
98 #define ONEDROCKBLOX_QUIT BUTTON_BACK
100 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
101 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
102 #define ONEDROCKBLOX_QUIT BUTTON_POWER
104 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
105 #define ONEDROCKBLOX_DOWN BUTTON_MENU
106 #define ONEDROCKBLOX_QUIT BUTTON_POWER
108 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
109 #define ONEDROCKBLOX_DOWN BUTTON_DOWN
110 #define ONEDROCKBLOX_QUIT BUTTON_PLAY
112 #elif defined(HAVE_TOUCHSCREEN)
114 #define ONEDROCKBLOX_DOWN BUTTON_BOTTOMMIDDLE
115 #define ONEDROCKBLOX_QUIT BUTTON_POWER
117 #else
118 #error No keymap defined!
119 #endif
121 #define mrand(max) (short)(rb->rand()%max)
123 #define TILES 11
125 /**********
126 ** Lots of defines for the drawing stuff :-)
127 ** The most ugly way i could think of...
130 #if (LCD_WIDTH > LCD_HEIGHT)
131 /* Any screens larger than the minis LCD */
132 #if (LCD_WIDTH > 132)
133 /* Max size of one block */
134 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
135 /* Align the playing filed centered */
136 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
137 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
138 /* Score box */
139 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
140 # define SCORE_Y (int)((LCD_HEIGHT/2)-(f_height/2))
141 /* Max. size of bricks is 4 blocks */
142 # define NEXT_H (WIDTH*4+3)
143 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
144 # define NEXT_Y (int)(LCD_HEIGHT/2-NEXT_H/2)
145 #else
146 /* Max size of one block */
147 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
148 /* Align the playing left centered */
149 # define CENTER_X (int)(LCD_WIDTH*0.2)
150 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
151 /* Score box */
152 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
153 # define SCORE_Y 16
154 /* Max. size of bricks is 4 blocks */
155 # define NEXT_H (WIDTH*4+3)
156 # define NEXT_X (score_x+f_width+7)
157 # define NEXT_Y (int)(LCD_HEIGHT-((4*WIDTH)+13))
158 #endif
159 #else
160 /* Max size of one block */
161 # define WIDTH (int)((LCD_HEIGHT * 0.8) / TILES)
162 /* Align the playing filed centered */
163 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
164 # define CENTER_Y 2
165 /* Score box */
166 # define SCORE_X (int)((LCD_WIDTH/2)-(f_width/2))
167 # define SCORE_Y (LCD_HEIGHT-(f_height+2))
168 /* Max. size of bricks is 4 blocks */
169 # define NEXT_H (WIDTH*4+3)
170 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
171 # define NEXT_Y (int)((LCD_HEIGHT * 0.8)/2-NEXT_H/2)
172 #endif
174 void draw_brick(int pos, int length) {
175 int i = pos;
176 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
177 rb->lcd_fillrect(CENTER_X, CENTER_Y, WIDTH, WIDTH * TILES + TILES);
178 rb->lcd_set_drawmode(DRMODE_SOLID);
180 for (i = pos; i < length + pos; ++i) {
181 if (i >= 0) rb->lcd_fillrect(CENTER_X, CENTER_Y+i+(WIDTH*i), WIDTH, WIDTH);
185 enum plugin_status plugin_start(const void* parameter)
187 int i;
188 int f_width, f_height;
189 int score_x;
191 bool quit = false;
192 int button;
194 int cycletime = 300;
195 int end;
197 int pos_cur_brick = 0;
198 int type_cur_brick = 0;
199 int type_next_brick = 0;
201 unsigned long int score = 34126;
202 char score_buf[10];
204 (void)parameter;
206 #if LCD_DEPTH > 1
207 rb->lcd_set_backdrop(NULL);
208 rb->lcd_set_background(LCD_BLACK);
209 rb->lcd_set_foreground(LCD_WHITE);
210 #endif
212 rb->lcd_setfont(FONT_SYSFIXED);
214 rb->lcd_getstringsize("100000000", &f_width, &f_height);
216 rb->lcd_clear_display();
218 /***********
219 ** Draw EVERYTHING
222 /* Playing filed box */
223 rb->lcd_vline(CENTER_X-2, CENTER_Y, CENTER_Y + (WIDTH*TILES+TILES));
224 rb->lcd_vline(CENTER_X + WIDTH + 1, CENTER_Y,
225 CENTER_Y + (WIDTH*TILES+TILES));
226 rb->lcd_hline(CENTER_X-2, CENTER_X + WIDTH + 1,
227 CENTER_Y + (WIDTH*TILES+TILES));
229 /* Score box */
230 #if (LCD_WIDTH > LCD_HEIGHT)
231 rb->lcd_drawrect(SCORE_X-4, SCORE_Y-5, f_width+8, f_height+9);
232 rb->lcd_putsxy(SCORE_X-4, SCORE_Y-6-f_height, "score");
233 #else
234 rb->lcd_hline(0, LCD_WIDTH, SCORE_Y-5);
235 rb->lcd_putsxy(2, SCORE_Y-6-f_height, "score");
236 #endif
237 score_x = SCORE_X;
239 /* Next box */
240 rb->lcd_getstringsize("next", &f_width, NULL);
241 #if (LCD_WIDTH > LCD_HEIGHT) && !(LCD_WIDTH > 132)
242 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
243 rb->lcd_putsxy(score_x-4, NEXT_Y-5, "next");
244 #else
245 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
246 rb->lcd_putsxy(NEXT_X-5, NEXT_Y-5-f_height-1, "next");
247 #endif
249 /***********
250 ** GAMELOOP
252 rb->srand( *rb->current_tick );
254 type_cur_brick = 2 + mrand(3);
255 type_next_brick = 2 + mrand(3);
257 do {
258 end = *rb->current_tick + (cycletime * HZ) / 1000;
260 draw_brick(pos_cur_brick, type_cur_brick);
262 /* Draw next brick */
263 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
264 rb->lcd_fillrect(NEXT_X, NEXT_Y, WIDTH, WIDTH * 4 + 4);
265 rb->lcd_set_drawmode(DRMODE_SOLID);
267 for (i = 0; i < type_next_brick; ++i) {
268 rb->lcd_fillrect(NEXT_X,
269 NEXT_Y + ((type_next_brick % 2) ? (int)(WIDTH/2) : ((type_next_brick == 2) ? (WIDTH+1) : 0)) + (WIDTH*i) + i,
270 WIDTH, WIDTH);
273 /* Score box */
274 rb->snprintf(score_buf, sizeof(score_buf), "%8ld0", score);
275 rb->lcd_putsxy(score_x, SCORE_Y, score_buf);
277 rb->lcd_update();
279 button = rb->button_status();
281 switch(button) {
282 case ONEDROCKBLOX_DOWN:
283 case (ONEDROCKBLOX_DOWN|BUTTON_REPEAT):
284 cycletime = 100;
285 break;
286 case ONEDROCKBLOX_QUIT:
287 quit = true;
288 break;
289 default:
290 cycletime = 300;
291 if(rb->default_event_handler(button) == SYS_USB_CONNECTED) {
292 quit = true;
296 if ((pos_cur_brick + type_cur_brick) > 10) {
297 type_cur_brick = type_next_brick;
298 type_next_brick = 2 + mrand(3);
299 score += (type_cur_brick - 1) * 2;
300 pos_cur_brick = 1 - type_cur_brick;
301 } else {
302 ++pos_cur_brick;
305 if (end > *rb->current_tick)
306 rb->sleep(end-*rb->current_tick);
307 else
308 rb->yield();
310 } while (!quit);
312 return PLUGIN_OK;
314 #endif