Adapt the remaining plugins to put the greyscale isr on cop. Now they can be used...
[Rockbox.git] / apps / plugins / jackpot.c
blob8dac5e4f7a40c2edf8a27c330a4b8a01231770e7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Copyright Kévin Ferrare based on work by Pierre Delore
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "pluginlib_actions.h"
22 #include "picture.h"
24 PLUGIN_HEADER
26 const struct button_mapping* plugin_contexts[]={generic_actions};
27 #define NB_PICTURES 9
28 #define NB_SLOTS 3
30 #ifdef HAVE_LCD_CHARCELLS
31 #define PICTURE_ROTATION_STEPS 7
32 static unsigned char jackpot_slots_patterns[]={
33 0x00, 0x0A, 0x1F, 0x1F, 0x1F, 0x0e, 0x04, /* (+00)Heart */
34 0x00, 0x04, 0x0E, 0x1F, 0x1F, 0x04, 0x0E, /* (+07)Spade */
35 0x00, 0x04, 0x0E, 0x1F, 0x0E, 0x04, 0x00, /* (+14)Diamond */
36 0x00, 0x15, 0x0E, 0x1F, 0x0E, 0x15, 0x00, /* (+21)Club */
37 0x03, 0x04, 0x0e, 0x1F, 0x1F, 0x1F, 0x0e, /* (+28)Cherry */
38 0x00, 0x04, 0x04, 0x1F, 0x04, 0x0E, 0x1F, /* (+35)Cross */
39 0x04, 0x0E, 0x15, 0x04, 0x0A, 0x0A, 0x11, /* (+42)Man */
40 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, /* (+49)Square */
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* (+56)Empty */
42 0x00, 0x0A, 0x1F, 0x1F, 0x1F, 0x0e, 0x04 /* (+63)Heart */
44 static unsigned long char_patterns[NB_SLOTS];
45 #define SLEEP_TIME (HZ/24)
46 #else /* bitmaps LCDs */
48 #define PICTURE_HEIGHT (BMPHEIGHT_jackpot_slots/(NB_PICTURES+1))
49 #if NB_SCREENS==1
50 #define PICTURE_ROTATION_STEPS PICTURE_HEIGHT
51 #else
52 #define REMOTE_PICTURE_HEIGHT (BMPHEIGHT_jackpot_slots_remote/(NB_PICTURES+1))
53 #define PICTURE_ROTATION_STEPS REMOTE_PICTURE_HEIGHT
54 #endif
56 /* FIXME: would be nice to have better graphics ... */
57 #include "jackpot_slots.h"
58 #if NB_SCREENS==2
59 #include "jackpot_slots_remote.h"
60 #endif
62 const struct picture jackpot_pictures[]={
63 {jackpot_slots, BMPWIDTH_jackpot_slots,PICTURE_HEIGHT},
64 #if NB_SCREENS==2
65 {jackpot_slots_remote,BMPWIDTH_jackpot_slots_remote,REMOTE_PICTURE_HEIGHT}
66 #endif
69 #define SLEEP_TIME (HZ/200)
70 #endif /* HAVE_LCD_CHARCELLS */
72 static struct plugin_api* rb;
74 struct jackpot
76 /* A slot can display "NB_PICTURES" pictures
77 A picture is moving up, it can take PICTURE_ROTATION_STEPS
78 to move a single picture completely.
79 So values in slot_state are comprised between
80 0 and NB_PICTURES*PICTURE_ROTATION_STEPS
82 int slot_state[NB_SLOTS];
84 The real state of the picture in pixels on each screen
85 Different from slot_state because of the synchronised
86 rotation between different sized bitmaps on remote and main screen
88 int state_y[NB_SCREENS][NB_SLOTS];
89 int money;
92 #ifdef HAVE_LCD_CHARCELLS
93 void patterns_init(struct screen* display)
95 int i;
96 for(i=0;i<NB_SLOTS;i++)
97 char_patterns[i]=display->get_locked_pattern();
100 void patterns_deinit(struct screen* display)
102 /* Restore the old pattern */
103 int i;
104 for(i=0;i<NB_SLOTS;i++)
105 display->unlock_pattern(char_patterns[i]);
107 #endif /* HAVE_LCD_CHARCELLS */
109 /*Call when the program exit*/
110 void jackpot_exit(void *parameter)
112 (void)parameter;
113 #ifdef HAVE_LCD_CHARCELLS
114 patterns_deinit(rb->screens[SCREEN_MAIN]);
115 #endif /* HAVE_LCD_CHARCELLS */
118 void jackpot_init(struct jackpot* game)
120 int i,j;
121 game->money=20;
122 for(i=0;i<NB_SLOTS;i++){
123 game->slot_state[i]=(rb->rand()%NB_PICTURES)*PICTURE_ROTATION_STEPS;
124 FOR_NB_SCREENS(j)
125 game->state_y[j][i]=-1;
129 int jackpot_get_result(struct jackpot* game)
131 int i=NB_SLOTS-1;
132 int multiple=1;
133 int result=0;
134 for(;i>=0;i--)
136 result+=game->slot_state[i]*multiple/PICTURE_ROTATION_STEPS;
137 multiple*=10;
139 return(result);
142 int jackpot_get_gain(struct jackpot* game)
144 switch (jackpot_get_result(game))
146 case 111 : return(20);
147 case 000 : return(15);
148 case 333 : return(10);
149 case 222 : return(8);
150 case 555 : return(5);
151 case 777 : return(4);
152 case 251 : return(4);
153 case 510 : return(4);
154 case 686 : return(3);
155 case 585 : return(3);
156 case 282 : return(3);
157 case 484 : return(3);
158 case 787 : return(2);
159 case 383 : return(2);
160 case 80 : return(2);
162 return(0);
165 void jackpot_display_slot_machine(struct jackpot* game, struct screen* display)
167 char str[20];
168 int i;
169 bool changes=false;
170 #ifdef HAVE_LCD_CHARCELLS
171 display->putc(0, 0, '[');
172 #else
173 const struct picture* picture= &(jackpot_pictures[display->screen_type]);
174 int pos_x=(display->width-NB_SLOTS*(picture->width+1))/2;
175 int pos_y=(display->height-(picture->height))/2;
176 #endif /* HAVE_LCD_CHARCELLS */
177 for(i=0;i<NB_SLOTS;i++)
179 #ifdef HAVE_LCD_CHARCELLS
180 /* the only charcell lcd is 7 pixel high */
181 int state_y=(game->slot_state[i]*7)/PICTURE_ROTATION_STEPS;
182 #else
183 int state_y=
184 (picture->height*game->slot_state[i])/PICTURE_ROTATION_STEPS;
185 #endif /* HAVE_LCD_CHARCELLS */
186 int previous_state_y=game->state_y[display->screen_type][i];
187 if(state_y==previous_state_y)
188 continue;/*no need to update the picture
189 as it's the same as previous displayed one*/
190 changes=true;
191 game->state_y[display->screen_type][i]=state_y;
192 #ifdef HAVE_LCD_CHARCELLS
193 char* current_pattern=&(jackpot_slots_patterns[state_y]);
194 display->define_pattern(char_patterns[i],
195 current_pattern);
196 display->putc(i+1, 0, char_patterns[i]);
197 #else
198 vertical_picture_draw_part(display, picture, state_y, pos_x, pos_y);
199 pos_x+=(picture->width+1);
200 #endif
202 if(changes){
203 #ifdef HAVE_LCD_CHARCELLS
204 rb->snprintf(str,sizeof(str),"$%d", game->money);
205 display->putc(++i, 0, ']');
206 display->puts(++i, 0, str);
207 #else
208 rb->snprintf(str,sizeof(str),"money : $%d", game->money);
209 display->puts(0, 0, str);
210 #endif
211 display->update();
216 void jackpot_info_message(struct screen* display, char* message)
218 #ifdef HAVE_LCD_CHARCELLS
219 display->puts_scroll(0,1,message);
220 #else
221 int xpos, ypos;
222 int message_height, message_width;
223 display->getstringsize(message, &message_width, &message_height);
224 xpos=(display->width-message_width)/2;
225 ypos=display->height-message_height;
226 rb->screen_clear_area(display, 0, ypos, display->width, message_height);
227 display->putsxy(xpos,ypos,message);
228 display->update();
229 #endif /* HAVE_LCD_CHARCELLS */
232 void jackpot_print_turn_result(struct jackpot* game,
233 int gain, struct screen* display)
235 char str[20];
236 if (gain==0)
238 jackpot_info_message(display, "None ...");
239 if (game->money<=0)
240 jackpot_info_message(display, "You lose...STOP to quit");
242 else
244 rb->snprintf(str,sizeof(str),"You win %d$",gain);
245 jackpot_info_message(display, str);
247 display->update();
250 void jackpot_play_turn(struct jackpot* game)
252 /* How many pattern? */
253 int nb_turns[NB_SLOTS];
254 int i,d,gain,turns_remaining=0;
255 if(game->money<=0)
256 return;
257 game->money--;
258 for(i=0;i<NB_SLOTS;i++)
260 nb_turns[i]=(rb->rand()%15+5)*PICTURE_ROTATION_STEPS;
261 turns_remaining+=nb_turns[i];
263 FOR_NB_SCREENS(d)
265 rb->screens[d]->clear_display();
266 jackpot_info_message(rb->screens[d],"Good luck");
268 /* Jackpot Animation */
269 while(turns_remaining>0)
271 for(i=0;i<NB_SLOTS;i++)
273 if(nb_turns[i]>0)
275 nb_turns[i]--;
276 game->slot_state[i]++;
277 if(game->slot_state[i]>=PICTURE_ROTATION_STEPS*NB_PICTURES)
278 game->slot_state[i]=0;
279 turns_remaining--;
282 FOR_NB_SCREENS(d)
283 jackpot_display_slot_machine(game, rb->screens[d]);
284 rb->sleep(SLEEP_TIME);
286 gain=jackpot_get_gain(game);
287 if(gain!=0)
288 game->money+=gain;
289 FOR_NB_SCREENS(d)
290 jackpot_print_turn_result(game, gain, rb->screens[d]);
293 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
295 rb = api;
296 int action, i;
297 struct jackpot game;
298 (void)parameter;
299 rb->srand(*rb->current_tick);
300 #ifdef HAVE_LCD_CHARCELLS
301 patterns_init(rb->screens[SCREEN_MAIN]);
302 #endif /* HAVE_LCD_CHARCELLS */
303 jackpot_init(&game);
305 FOR_NB_SCREENS(i){
306 rb->screens[i]->clear_display();
307 jackpot_display_slot_machine(&game, rb->screens[i]);
309 /*Empty the event queue*/
310 rb->button_clear_queue();
311 while (true)
313 action = pluginlib_getaction(rb, TIMEOUT_BLOCK,
314 plugin_contexts, 1);
315 switch ( action )
317 case PLA_QUIT:
318 return PLUGIN_OK;
319 case PLA_FIRE:
320 jackpot_play_turn(&game);
321 break;
323 default:
324 if (rb->default_event_handler_ex(action, jackpot_exit, NULL)
325 == SYS_USB_CONNECTED)
326 return PLUGIN_USB_CONNECTED;
327 break;
330 jackpot_exit(NULL);
331 return PLUGIN_OK;