Fix warning about missing newline at the EOF
[maemo-rb.git] / apps / plugins / rockblox1d.c
blob5b6e812abf143fdfe2f05c34eaabb409489a9c44
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 #if CONFIG_KEYPAD == RECORDER_PAD
26 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
27 #define ONEDROCKBLOX_QUIT BUTTON_OFF
29 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
30 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
31 #define ONEDROCKBLOX_QUIT BUTTON_OFF
33 #elif CONFIG_KEYPAD == ONDIO_PAD
34 #define ONEDROCKBLOX_DOWN BUTTON_RIGHT
35 #define ONEDROCKBLOX_QUIT BUTTON_OFF
37 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
38 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
39 #define ONEDROCKBLOX_QUIT BUTTON_POWER
41 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
42 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
43 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
44 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
45 #define ONEDROCKBLOX_QUIT (BUTTON_SELECT | BUTTON_MENU)
47 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
48 (CONFIG_KEYPAD == IRIVER_H300_PAD)
49 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
50 #define ONEDROCKBLOX_QUIT BUTTON_OFF
52 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
53 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
54 #define ONEDROCKBLOX_QUIT BUTTON_POWER
56 #elif CONFIG_KEYPAD == SANSA_E200_PAD || \
57 CONFIG_KEYPAD == SANSA_C200_PAD || \
58 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
59 CONFIG_KEYPAD == SANSA_M200_PAD || \
60 CONFIG_KEYPAD == SANSA_CONNECT_PAD
61 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
62 #define ONEDROCKBLOX_QUIT BUTTON_POWER
64 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
65 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
66 #define ONEDROCKBLOX_QUIT BUTTON_HOME
68 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
69 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
70 #define ONEDROCKBLOX_QUIT BUTTON_POWER
72 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD) || \
73 (CONFIG_KEYPAD == SAMSUNG_YPR0_PAD)
74 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
75 #define ONEDROCKBLOX_QUIT BUTTON_BACK
77 #elif (CONFIG_KEYPAD == MROBE100_PAD)
78 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
79 #define ONEDROCKBLOX_QUIT BUTTON_POWER
81 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
82 #define ONEDROCKBLOX_DOWN BUTTON_RC_PLAY
83 #define ONEDROCKBLOX_QUIT BUTTON_RC_REC
85 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
86 #define ONEDROCKBLOX_DOWN BUTTON_MENU
87 #define ONEDROCKBLOX_QUIT BUTTON_POWER
89 #elif CONFIG_KEYPAD == IAUDIO67_PAD
90 #define ONEDROCKBLOX_DOWN BUTTON_MENU
91 #define ONEDROCKBLOX_QUIT BUTTON_POWER
93 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
94 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
95 #define ONEDROCKBLOX_QUIT BUTTON_BACK
97 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
98 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
99 #define ONEDROCKBLOX_QUIT BUTTON_POWER
101 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
102 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
103 #define ONEDROCKBLOX_QUIT BUTTON_POWER
105 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
106 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
107 #define ONEDROCKBLOX_QUIT BUTTON_POWER
109 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
110 #define ONEDROCKBLOX_DOWN BUTTON_MENU
111 #define ONEDROCKBLOX_QUIT BUTTON_POWER
113 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
114 #define ONEDROCKBLOX_DOWN BUTTON_DOWN
115 #define ONEDROCKBLOX_QUIT BUTTON_PLAY
117 #elif (CONFIG_KEYPAD == PBELL_VIBE500_PAD)
118 #define ONEDROCKBLOX_DOWN BUTTON_DOWN
119 #define ONEDROCKBLOX_QUIT BUTTON_REC
121 #elif (CONFIG_KEYPAD == MPIO_HD200_PAD)
122 #define ONEDROCKBLOX_DOWN BUTTON_FUNC
123 #define ONEDROCKBLOX_QUIT (BUTTON_REC | BUTTON_PLAY)
125 #elif (CONFIG_KEYPAD == MPIO_HD300_PAD)
126 #define ONEDROCKBLOX_DOWN BUTTON_DOWN
127 #define ONEDROCKBLOX_QUIT (BUTTON_REC | BUTTON_REPEAT)
129 #elif (CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD)
130 #define ONEDROCKBLOX_DOWN BUTTON_DOWN
131 #define ONEDROCKBLOX_QUIT BUTTON_POWER
133 #elif defined(HAVE_TOUCHSCREEN)
135 #define ONEDROCKBLOX_DOWN BUTTON_BOTTOMMIDDLE
136 #define ONEDROCKBLOX_QUIT BUTTON_POWER
138 #else
139 #error No keymap defined!
140 #endif
142 #define mrand(max) (short)(rb->rand()%max)
144 #define TILES 11
146 /**********
147 ** Lots of defines for the drawing stuff :-)
148 ** The most ugly way i could think of...
151 #if (LCD_WIDTH > LCD_HEIGHT)
152 /* Any screens larger than the minis LCD */
153 #if (LCD_WIDTH > 132)
154 /* Max size of one block */
155 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
156 /* Align the playing filed centered */
157 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
158 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
159 /* Score box */
160 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
161 # define SCORE_Y (int)((LCD_HEIGHT/2)-(f_height/2))
162 /* Max. size of bricks is 4 blocks */
163 # define NEXT_H (WIDTH*4+3)
164 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
165 # define NEXT_Y (int)(LCD_HEIGHT/2-NEXT_H/2)
166 #else
167 /* Max size of one block */
168 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
169 /* Align the playing left centered */
170 # define CENTER_X (int)(LCD_WIDTH*0.2)
171 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
172 /* Score box */
173 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
174 # define SCORE_Y 16
175 /* Max. size of bricks is 4 blocks */
176 # define NEXT_H (WIDTH*4+3)
177 # define NEXT_X (score_x+f_width+7)
178 # define NEXT_Y (int)(LCD_HEIGHT-((4*WIDTH)+13))
179 #endif
180 #else
181 /* Max size of one block */
182 # define WIDTH (int)((LCD_HEIGHT * 0.8) / TILES)
183 /* Align the playing filed centered */
184 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
185 # define CENTER_Y 2
186 /* Score box */
187 # define SCORE_X (int)((LCD_WIDTH/2)-(f_width/2))
188 # define SCORE_Y (LCD_HEIGHT-(f_height+2))
189 /* Max. size of bricks is 4 blocks */
190 # define NEXT_H (WIDTH*4+3)
191 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
192 # define NEXT_Y (int)((LCD_HEIGHT * 0.8)/2-NEXT_H/2)
193 #endif
195 static void draw_brick(int pos, int length) {
196 int i = pos;
197 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
198 rb->lcd_fillrect(CENTER_X, CENTER_Y, WIDTH, WIDTH * TILES + TILES);
199 rb->lcd_set_drawmode(DRMODE_SOLID);
201 for (i = pos; i < length + pos; ++i) {
202 if (i >= 0) rb->lcd_fillrect(CENTER_X, CENTER_Y+i+(WIDTH*i), WIDTH, WIDTH);
206 enum plugin_status plugin_start(const void* parameter)
208 int i;
209 int f_width, f_height;
210 int score_x;
212 bool quit = false;
213 int button;
215 int cycletime = 300;
216 int end;
218 int pos_cur_brick = 0;
219 int type_cur_brick = 0;
220 int type_next_brick = 0;
222 unsigned long int score = 34126;
224 (void)parameter;
226 #if LCD_DEPTH > 1
227 rb->lcd_set_backdrop(NULL);
228 rb->lcd_set_background(LCD_BLACK);
229 rb->lcd_set_foreground(LCD_WHITE);
230 #endif
232 rb->lcd_setfont(FONT_SYSFIXED);
234 rb->lcd_getstringsize("100000000", &f_width, &f_height);
236 rb->lcd_clear_display();
238 /***********
239 ** Draw EVERYTHING
242 /* Playing filed box */
243 rb->lcd_vline(CENTER_X-2, CENTER_Y, CENTER_Y + (WIDTH*TILES+TILES));
244 rb->lcd_vline(CENTER_X + WIDTH + 1, CENTER_Y,
245 CENTER_Y + (WIDTH*TILES+TILES));
246 rb->lcd_hline(CENTER_X-2, CENTER_X + WIDTH + 1,
247 CENTER_Y + (WIDTH*TILES+TILES));
249 /* Score box */
250 #if (LCD_WIDTH > LCD_HEIGHT)
251 rb->lcd_drawrect(SCORE_X-4, SCORE_Y-5, f_width+8, f_height+9);
252 rb->lcd_putsxy(SCORE_X-4, SCORE_Y-6-f_height, "score");
253 #else
254 rb->lcd_hline(0, LCD_WIDTH, SCORE_Y-5);
255 rb->lcd_putsxy(2, SCORE_Y-6-f_height, "score");
256 #endif
257 score_x = SCORE_X;
259 /* Next box */
260 rb->lcd_getstringsize("next", &f_width, NULL);
261 #if (LCD_WIDTH > LCD_HEIGHT) && !(LCD_WIDTH > 132)
262 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
263 rb->lcd_putsxy(score_x-4, NEXT_Y-5, "next");
264 #else
265 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
266 rb->lcd_putsxy(NEXT_X-5, NEXT_Y-5-f_height-1, "next");
267 #endif
269 /***********
270 ** GAMELOOP
272 rb->srand( *rb->current_tick );
274 type_cur_brick = 2 + mrand(3);
275 type_next_brick = 2 + mrand(3);
277 do {
278 end = *rb->current_tick + (cycletime * HZ) / 1000;
280 draw_brick(pos_cur_brick, type_cur_brick);
282 /* Draw next brick */
283 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
284 rb->lcd_fillrect(NEXT_X, NEXT_Y, WIDTH, WIDTH * 4 + 4);
285 rb->lcd_set_drawmode(DRMODE_SOLID);
287 for (i = 0; i < type_next_brick; ++i) {
288 rb->lcd_fillrect(NEXT_X,
289 NEXT_Y + ((type_next_brick % 2) ? (int)(WIDTH/2) : ((type_next_brick == 2) ? (WIDTH+1) : 0)) + (WIDTH*i) + i,
290 WIDTH, WIDTH);
293 /* Score box */
294 rb->lcd_putsxyf(score_x, SCORE_Y, "%8ld0", score);
296 rb->lcd_update();
298 button = rb->button_status();
300 switch(button) {
301 case ONEDROCKBLOX_DOWN:
302 case (ONEDROCKBLOX_DOWN|BUTTON_REPEAT):
303 cycletime = 100;
304 break;
305 case ONEDROCKBLOX_QUIT:
306 quit = true;
307 break;
308 default:
309 cycletime = 300;
310 if(rb->default_event_handler(button) == SYS_USB_CONNECTED) {
311 quit = true;
315 if ((pos_cur_brick + type_cur_brick) > 10) {
316 type_cur_brick = type_next_brick;
317 type_next_brick = 2 + mrand(3);
318 score += (type_cur_brick - 1) * 2;
319 pos_cur_brick = 1 - type_cur_brick;
320 } else {
321 ++pos_cur_brick;
324 if (TIME_BEFORE(*rb->current_tick, end))
325 rb->sleep(end-*rb->current_tick);
326 else
327 rb->yield();
329 } while (!quit);
331 return PLUGIN_OK;