very minor code police. also fix a possible but unlikely missed cpu_boost(false)
[Rockbox.git] / apps / plugins / rockblox1d.c
blob7232e365ed22167d0b7e41e220c2ad9288ff8796
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
27 #ifdef HAVE_LCD_BITMAP
29 #if CONFIG_KEYPAD == RECORDER_PAD
30 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
31 #define ONEDROCKBLOX_QUIT BUTTON_OFF
33 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
34 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
35 #define ONEDROCKBLOX_QUIT BUTTON_OFF
37 #elif CONFIG_KEYPAD == ONDIO_PAD
38 #define ONEDROCKBLOX_DOWN BUTTON_RIGHT
39 #define ONEDROCKBLOX_QUIT BUTTON_OFF
41 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
42 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
43 #define ONEDROCKBLOX_QUIT BUTTON_POWER
45 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
46 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
47 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
48 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
49 #define ONEDROCKBLOX_QUIT (BUTTON_SELECT | BUTTON_MENU)
51 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
52 (CONFIG_KEYPAD == IRIVER_H300_PAD)
53 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
54 #define ONEDROCKBLOX_QUIT BUTTON_OFF
56 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
57 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
58 #define ONEDROCKBLOX_QUIT BUTTON_POWER
60 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
61 (CONFIG_KEYPAD == SANSA_C200_PAD)
62 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
63 #define ONEDROCKBLOX_QUIT BUTTON_POWER
65 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
66 #define ONEDROCKBLOX_DOWN BUTTON_PLAY
67 #define ONEDROCKBLOX_QUIT BUTTON_POWER
69 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
70 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
71 #define ONEDROCKBLOX_QUIT BUTTON_BACK
73 #elif (CONFIG_KEYPAD == MROBE100_PAD)
74 #define ONEDROCKBLOX_DOWN BUTTON_SELECT
75 #define ONEDROCKBLOX_QUIT BUTTON_POWER
77 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
78 #define ONEDROCKBLOX_DOWN BUTTON_RC_PLAY
79 #define ONEDROCKBLOX_QUIT BUTTON_RC_REC
81 #elif (CONFIG_KEYPAD == COWOND2_PAD)
82 #define ONEDROCKBLOX_DOWN BUTTON_MENU
83 #define ONEDROCKBLOX_QUIT BUTTON_POWER
85 #else
86 #error No keymap defined!
87 #endif
89 #define mrand(max) (short)(rb->rand()%max)
91 #define TILES 11
93 /**********
94 ** Lots of defines for the drawing stuff :-)
95 ** The most ugly way i could think of...
98 #if (LCD_WIDTH > LCD_HEIGHT)
99 /* Any screens larger than the minis LCD */
100 #if (LCD_WIDTH > 132)
101 /* Max size of one block */
102 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
103 /* Align the playing filed centered */
104 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
105 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
106 /* Score box */
107 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
108 # define SCORE_Y (int)((LCD_HEIGHT/2)-(f_height/2))
109 /* Max. size of bricks is 4 blocks */
110 # define NEXT_H (WIDTH*4+3)
111 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
112 # define NEXT_Y (int)(LCD_HEIGHT/2-NEXT_H/2)
113 #else
114 /* Max size of one block */
115 # define WIDTH (int)((LCD_HEIGHT * 0.85) / TILES)
116 /* Align the playing left centered */
117 # define CENTER_X (int)(LCD_WIDTH*0.2)
118 # define CENTER_Y (int)(LCD_HEIGHT/2-((WIDTH*TILES+TILES)/2))
119 /* Score box */
120 # define SCORE_X (int)(((CENTER_X+WIDTH+4) + (LCD_WIDTH-(CENTER_X+WIDTH+4))/2)-(f_width/2))
121 # define SCORE_Y 16
122 /* Max. size of bricks is 4 blocks */
123 # define NEXT_H (WIDTH*4+3)
124 # define NEXT_X (score_x+f_width+7)
125 # define NEXT_Y (int)(LCD_HEIGHT-((4*WIDTH)+13))
126 #endif
127 #else
128 /* Max size of one block */
129 # define WIDTH (int)((LCD_HEIGHT * 0.8) / TILES)
130 /* Align the playing filed centered */
131 # define CENTER_X (int)(LCD_WIDTH/2-(WIDTH/2))
132 # define CENTER_Y 2
133 /* Score box */
134 # define SCORE_X (int)((LCD_WIDTH/2)-(f_width/2))
135 # define SCORE_Y (LCD_HEIGHT-(f_height+2))
136 /* Max. size of bricks is 4 blocks */
137 # define NEXT_H (WIDTH*4+3)
138 # define NEXT_X (int)(CENTER_X/2-WIDTH/2)
139 # define NEXT_Y (int)((LCD_HEIGHT * 0.8)/2-NEXT_H/2)
140 #endif
142 static const struct plugin_api* rb; /* global api struct pointer */
144 void draw_brick(int pos, int length) {
145 int i = pos;
146 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
147 rb->lcd_fillrect(CENTER_X, CENTER_Y, WIDTH, WIDTH * TILES + TILES);
148 rb->lcd_set_drawmode(DRMODE_SOLID);
150 for (i = pos; i < length + pos; ++i) {
151 if (i >= 0) rb->lcd_fillrect(CENTER_X, CENTER_Y+i+(WIDTH*i), WIDTH, WIDTH);
155 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
157 int i;
158 int f_width, f_height;
159 int score_x;
161 bool quit = false;
162 int button;
164 int cycletime = 300;
165 int end;
167 int pos_cur_brick = 0;
168 int type_cur_brick = 0;
169 int type_next_brick = 0;
171 unsigned long int score = 34126;
172 char score_buf[10];
174 rb = api;
175 (void)parameter;
177 #if LCD_DEPTH > 1
178 rb->lcd_set_backdrop(NULL);
179 rb->lcd_set_background(LCD_BLACK);
180 rb->lcd_set_foreground(LCD_WHITE);
181 #endif
183 rb->lcd_setfont(FONT_SYSFIXED);
185 rb->lcd_getstringsize("100000000", &f_width, &f_height);
187 rb->lcd_clear_display();
189 /***********
190 ** Draw EVERYTHING
193 /* Playing filed box */
194 rb->lcd_vline(CENTER_X-2, CENTER_Y, CENTER_Y + (WIDTH*TILES+TILES));
195 rb->lcd_vline(CENTER_X + WIDTH + 1, CENTER_Y,
196 CENTER_Y + (WIDTH*TILES+TILES));
197 rb->lcd_hline(CENTER_X-2, CENTER_X + WIDTH + 1,
198 CENTER_Y + (WIDTH*TILES+TILES));
200 /* Score box */
201 #if (LCD_WIDTH > LCD_HEIGHT)
202 rb->lcd_drawrect(SCORE_X-4, SCORE_Y-5, f_width+8, f_height+9);
203 rb->lcd_putsxy(SCORE_X-4, SCORE_Y-6-f_height, "score");
204 #else
205 rb->lcd_hline(0, LCD_WIDTH, SCORE_Y-5);
206 rb->lcd_putsxy(2, SCORE_Y-6-f_height, "score");
207 #endif
208 score_x = SCORE_X;
210 /* Next box */
211 rb->lcd_getstringsize("next", &f_width, NULL);
212 #if (LCD_WIDTH > LCD_HEIGHT) && !(LCD_WIDTH > 132)
213 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
214 rb->lcd_putsxy(score_x-4, NEXT_Y-5, "next");
215 #else
216 rb->lcd_drawrect(NEXT_X-5, NEXT_Y-5, WIDTH+10, NEXT_H+10);
217 rb->lcd_putsxy(NEXT_X-5, NEXT_Y-5-f_height-1, "next");
218 #endif
220 /***********
221 ** GAMELOOP
223 rb->srand( *rb->current_tick );
225 type_cur_brick = 2 + mrand(3);
226 type_next_brick = 2 + mrand(3);
228 do {
229 end = *rb->current_tick + (cycletime * HZ) / 1000;
231 draw_brick(pos_cur_brick, type_cur_brick);
233 /* Draw next brick */
234 rb->lcd_set_drawmode(DRMODE_BG|DRMODE_INVERSEVID);
235 rb->lcd_fillrect(NEXT_X, NEXT_Y, WIDTH, WIDTH * 4 + 4);
236 rb->lcd_set_drawmode(DRMODE_SOLID);
238 for (i = 0; i < type_next_brick; ++i) {
239 rb->lcd_fillrect(NEXT_X,
240 NEXT_Y + ((type_next_brick % 2) ? (int)(WIDTH/2) : ((type_next_brick == 2) ? (WIDTH+1) : 0)) + (WIDTH*i) + i,
241 WIDTH, WIDTH);
244 /* Score box */
245 rb->snprintf(score_buf, sizeof(score_buf), "%8ld0", score);
246 rb->lcd_putsxy(score_x, SCORE_Y, score_buf);
248 rb->lcd_update();
250 button = rb->button_status();
252 switch(button) {
253 case ONEDROCKBLOX_DOWN:
254 case (ONEDROCKBLOX_DOWN|BUTTON_REPEAT):
255 cycletime = 100;
256 break;
257 case ONEDROCKBLOX_QUIT:
258 quit = true;
259 break;
260 default:
261 cycletime = 300;
262 if(rb->default_event_handler(button) == SYS_USB_CONNECTED) {
263 quit = true;
267 if ((pos_cur_brick + type_cur_brick) > 10) {
268 type_cur_brick = type_next_brick;
269 type_next_brick = 2 + mrand(3);
270 score += (type_cur_brick - 1) * 2;
271 pos_cur_brick = 1 - type_cur_brick;
272 } else {
273 ++pos_cur_brick;
276 if (end > *rb->current_tick)
277 rb->sleep(end-*rb->current_tick);
278 else
279 rb->yield();
281 } while (!quit);
283 return PLUGIN_OK;
285 #endif