Fix FS#11007: Lua didn't parse negative numbers correct when reading from files
[kugel-rb.git] / apps / plugins / pacbox / pacbox.c
blob5f8ba90a04c722a062d8791d809a1b204dd50feb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Pacbox - a Pacman Emulator for Rockbox
12 * Based on PIE - Pacman Instructional Emulator
14 * Copyright (c) 1997-2003,2004 Alessandro Scotti
15 * http://www.ascotti.org/
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
27 #include "plugin.h"
28 #include "arcade.h"
29 #include "pacbox.h"
30 #include "pacbox_lcd.h"
31 #include "lib/configfile.h"
32 #include "lib/playback_control.h"
34 PLUGIN_HEADER
35 PLUGIN_IRAM_DECLARE
37 struct pacman_settings {
38 int difficulty;
39 int numlives;
40 int bonus;
41 int ghostnames;
42 int showfps;
45 static struct pacman_settings settings;
46 static struct pacman_settings old_settings;
48 #define SETTINGS_VERSION 1
49 #define SETTINGS_MIN_VERSION 1
50 #define SETTINGS_FILENAME "pacbox.cfg"
52 static char* difficulty_options[] = { "Normal", "Hard" };
53 static char* numlives_options[] = { "1", "2", "3", "5" };
54 static char* bonus_options[] = {"10000", "15000", "20000", "No Bonus"};
55 static char* ghostnames_options[] = {"Normal", "Alternate"};
56 static char* showfps_options[] = {"No", "Yes"};
58 static struct configdata config[] =
60 {TYPE_ENUM, 0, 2, { .int_p = &settings.difficulty }, "Difficulty",
61 difficulty_options},
62 {TYPE_ENUM, 0, 4, { .int_p = &settings.numlives }, "Pacmen Per Game",
63 numlives_options},
64 {TYPE_ENUM, 0, 4, { .int_p = &settings.bonus }, "Bonus", bonus_options},
65 {TYPE_ENUM, 0, 2, { .int_p = &settings.ghostnames }, "Ghost Names",
66 ghostnames_options},
67 {TYPE_ENUM, 0, 2, { .int_p = &settings.showfps }, "Show FPS",
68 showfps_options},
71 static bool loadFile( const char * name, unsigned char * buf, int len )
73 char filename[MAX_PATH];
75 rb->snprintf(filename,sizeof(filename), ROCKBOX_DIR "/pacman/%s",name);
77 int fd = rb->open( filename, O_RDONLY);
79 if( fd < 0 ) {
80 return false;
83 int n = rb->read( fd, buf, len);
85 rb->close( fd );
87 if( n != len ) {
88 return false;
91 return true;
94 static bool loadROMS( void )
96 bool romsLoaded = false;
98 romsLoaded = loadFile( "pacman.6e", ram_, 0x1000) &&
99 loadFile( "pacman.6f", ram_+0x1000, 0x1000) &&
100 loadFile( "pacman.6h", ram_+0x2000, 0x1000) &&
101 loadFile( "pacman.6j", ram_+0x3000, 0x1000) &&
102 loadFile( "pacman.5e", charset_rom_, 0x1000) &&
103 loadFile( "pacman.5f", spriteset_rom_, 0x1000);
105 if( romsLoaded ) {
106 decodeROMs();
107 reset_PacmanMachine();
110 return romsLoaded;
113 /* A buffer to render Pacman's 244x288 screen into */
114 static unsigned char video_buffer[ScreenWidth*ScreenHeight] __attribute__ ((aligned (16)));
116 static long start_time;
117 static long video_frames = 0;
119 static int dipDifficulty[] = { DipDifficulty_Normal, DipDifficulty_Hard };
120 static int dipLives[] = { DipLives_1, DipLives_2, DipLives_3, DipLives_5 };
121 static int dipBonus[] = { DipBonus_10000, DipBonus_15000, DipBonus_20000,
122 DipBonus_None };
123 static int dipGhostNames[] = { DipGhostNames_Normal, DipGhostNames_Alternate };
125 static int settings_to_dip(struct pacman_settings settings)
127 return ( DipPlay_OneCoinOneGame |
128 DipCabinet_Upright |
129 DipMode_Play |
130 DipRackAdvance_Off |
132 dipDifficulty[settings.difficulty] |
133 dipLives[settings.numlives] |
134 dipBonus[settings.bonus] |
135 dipGhostNames[settings.ghostnames]
139 static bool pacbox_menu(void)
141 int selected=0;
142 int result;
143 int menu_quit=0;
144 int new_setting;
145 bool need_restart = false;
147 static const struct opt_items noyes[2] = {
148 { "No", -1 },
149 { "Yes", -1 },
152 static const struct opt_items difficulty_options[2] = {
153 { "Normal", -1 },
154 { "Harder", -1 },
157 static const struct opt_items numlives_options[4] = {
158 { "1", -1 },
159 { "2", -1 },
160 { "3", -1 },
161 { "5", -1 },
164 static const struct opt_items bonus_options[4] = {
165 { "10000 points", -1 },
166 { "15000 points", -1 },
167 { "20000 points", -1 },
168 { "No bonus", -1 },
171 static const struct opt_items ghostname_options[2] = {
172 { "Normal", -1 },
173 { "Alternate", -1 },
176 MENUITEM_STRINGLIST(menu, "Pacbox Menu", NULL,
177 "Difficulty", "Pacmen Per Game", "Bonus Life",
178 "Ghost Names", "Display FPS",
179 "Playback Control", "Restart", "Quit");
181 rb->button_clear_queue();
183 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
184 rb->lcd_set_mode(LCD_MODE_RGB565);
185 #endif
187 while (!menu_quit) {
188 result=rb->do_menu(&menu, &selected, NULL, false);
190 switch(result)
192 case 0:
193 new_setting=settings.difficulty;
194 rb->set_option("Difficulty", &new_setting, INT,
195 difficulty_options , 2, NULL);
196 if (new_setting != settings.difficulty) {
197 settings.difficulty=new_setting;
198 need_restart=true;
200 break;
201 case 1:
202 new_setting=settings.numlives;
203 rb->set_option("Pacmen Per Game", &new_setting, INT,
204 numlives_options , 4, NULL);
205 if (new_setting != settings.numlives) {
206 settings.numlives=new_setting;
207 need_restart=true;
209 break;
210 case 2:
211 new_setting=settings.bonus;
212 rb->set_option("Bonus Life", &new_setting, INT,
213 bonus_options , 4, NULL);
214 if (new_setting != settings.bonus) {
215 settings.bonus=new_setting;
216 need_restart=true;
218 break;
219 case 3:
220 new_setting=settings.ghostnames;
221 rb->set_option("Ghost Names", &new_setting, INT,
222 ghostname_options , 2, NULL);
223 if (new_setting != settings.ghostnames) {
224 settings.ghostnames=new_setting;
225 need_restart=true;
227 break;
228 case 4: /* Show FPS */
229 rb->set_option("Display FPS",&settings.showfps,INT,
230 noyes, 2, NULL);
231 break;
232 case 5: /* playback control */
233 playback_control(NULL);
234 break;
235 case 6: /* Restart */
236 need_restart=true;
237 menu_quit=1;
238 break;
239 default:
240 menu_quit=1;
241 break;
245 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
246 rb->lcd_set_mode(LCD_MODE_PAL256);
247 #endif
249 if (need_restart) {
250 init_PacmanMachine(settings_to_dip(settings));
253 /* Possible results:
254 exit game
255 restart game
256 usb connected
258 return (result==7);
263 Runs the game engine for one frame.
265 static int gameProc( void )
267 int x;
268 int fps;
269 char str[80];
270 int status;
271 long end_time;
272 int frame_counter = 0;
273 int yield_counter = 0;
275 while (1)
277 /* Run the machine for one frame (1/60th second) */
278 run();
280 frame_counter++;
282 /* Check the button status */
283 status = rb->button_status();
285 #ifdef HAS_BUTTON_HOLD
286 if (rb->button_hold())
287 status = PACMAN_MENU;
288 #endif
290 if ((status & PACMAN_MENU) == PACMAN_MENU
291 #ifdef PACMAN_RC_MENU
292 || status == PACMAN_RC_MENU
293 #endif
295 end_time = *rb->current_tick;
296 x = pacbox_menu();
297 rb->lcd_clear_display();
298 #ifdef HAVE_REMOTE_LCD
299 rb->lcd_remote_clear_display();
300 rb->lcd_remote_update();
301 #endif
302 if (x == 1) { return 1; }
303 start_time += *rb->current_tick-end_time;
306 #ifdef PACMAN_HAS_REMOTE
307 setDeviceMode( Joy1_Left, (status & PACMAN_LEFT || status == PACMAN_RC_LEFT) ? DeviceOn : DeviceOff);
308 setDeviceMode( Joy1_Right, (status & PACMAN_RIGHT || status == PACMAN_RC_RIGHT) ? DeviceOn : DeviceOff);
309 setDeviceMode( Joy1_Up, (status & PACMAN_UP || status == PACMAN_RC_UP) ? DeviceOn : DeviceOff);
310 setDeviceMode( Joy1_Down, (status & PACMAN_DOWN || status == PACMAN_RC_DOWN) ? DeviceOn : DeviceOff);
311 setDeviceMode( CoinSlot_1, (status & PACMAN_COIN || status == PACMAN_RC_COIN) ? DeviceOn : DeviceOff);
312 setDeviceMode( Key_OnePlayer, (status & PACMAN_1UP || status == PACMAN_RC_1UP) ? DeviceOn : DeviceOff);
313 setDeviceMode( Key_TwoPlayers, (status & PACMAN_2UP || status == PACMAN_RC_2UP) ? DeviceOn : DeviceOff);
314 #else
315 setDeviceMode( Joy1_Left, (status & PACMAN_LEFT) ? DeviceOn : DeviceOff);
316 setDeviceMode( Joy1_Right, (status & PACMAN_RIGHT) ? DeviceOn : DeviceOff);
317 setDeviceMode( Joy1_Up, (status & PACMAN_UP) ? DeviceOn : DeviceOff);
318 setDeviceMode( Joy1_Down, (status & PACMAN_DOWN) ? DeviceOn : DeviceOff);
319 setDeviceMode( CoinSlot_1, (status & PACMAN_COIN) ? DeviceOn : DeviceOff);
320 setDeviceMode( Key_OnePlayer, (status & PACMAN_1UP) ? DeviceOn : DeviceOff);
321 #ifdef PACMAN_2UP
322 setDeviceMode( Key_TwoPlayers, (status & PACMAN_2UP) ? DeviceOn : DeviceOff);
323 #endif
324 #endif
326 /* We only update the screen every third frame - Pacman's native
327 framerate is 60fps, so we are attempting to display 20fps */
328 if (frame_counter == 60 / FPS) {
330 frame_counter = 0;
331 video_frames++;
333 yield_counter ++;
335 if (yield_counter == FPS) {
336 yield_counter = 0;
337 rb->yield ();
340 /* The following functions render the Pacman screen from the
341 contents of the video and color ram. We first update the
342 background, and then draw the Sprites on top.
345 renderBackground( video_buffer );
346 renderSprites( video_buffer );
348 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
349 rb->lcd_blit_pal256( video_buffer, 0, 0, XOFS, YOFS,
350 ScreenWidth, ScreenHeight);
351 #else
352 blit_display(rb->lcd_framebuffer,video_buffer);
353 #endif
355 if (settings.showfps) {
356 fps = (video_frames*HZ*100) / (*rb->current_tick-start_time);
357 rb->snprintf(str,sizeof(str),"%d.%02d / %d fps ",
358 fps/100,fps%100,FPS);
359 rb->lcd_putsxy(0,0,str);
362 #if !defined(HAVE_LCD_MODES) || \
363 defined(HAVE_LCD_MODES) && !(HAVE_LCD_MODES & LCD_MODE_PAL256)
364 rb->lcd_update();
365 #endif
367 /* Keep the framerate at Pacman's 60fps */
368 end_time = start_time + (video_frames*HZ)/FPS;
369 while (TIME_BEFORE(*rb->current_tick,end_time)) {
370 rb->sleep(1);
374 return 0;
377 enum plugin_status plugin_start(const void* parameter)
379 (void)parameter;
381 PLUGIN_IRAM_INIT(rb)
383 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
384 rb->cpu_boost(true);
385 #endif
386 rb->lcd_set_backdrop(NULL);
387 rb->lcd_set_foreground(LCD_WHITE);
388 rb->lcd_set_background(LCD_BLACK);
389 rb->lcd_clear_display();
390 rb->lcd_update();
392 /* Set the default settings */
393 settings.difficulty = 0; /* Normal */
394 settings.numlives = 2; /* 3 lives */
395 settings.bonus = 0; /* 10000 points */
396 settings.ghostnames = 0; /* Normal names */
397 settings.showfps = 0; /* Do not show FPS */
399 if (configfile_load(SETTINGS_FILENAME, config,
400 sizeof(config)/sizeof(*config),
401 SETTINGS_MIN_VERSION
402 ) < 0)
404 /* If the loading failed, save a new config file (as the disk is
405 already spinning) */
406 configfile_save(SETTINGS_FILENAME, config,
407 sizeof(config)/sizeof(*config),
408 SETTINGS_VERSION);
411 /* Keep a copy of the saved version of the settings - so we can check if
412 the settings have changed when we quit */
413 old_settings = settings;
415 /* Initialise the hardware */
416 init_PacmanMachine(settings_to_dip(settings));
418 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
419 rb->lcd_set_mode(LCD_MODE_PAL256);
420 #endif
422 /* Load the romset */
423 if (loadROMS()) {
424 start_time = *rb->current_tick-1;
426 gameProc();
428 /* Save the user settings if they have changed */
429 if (rb->memcmp(&settings,&old_settings,sizeof(settings))!=0) {
430 rb->splash(0, "Saving settings...");
431 configfile_save(SETTINGS_FILENAME, config,
432 sizeof(config)/sizeof(*config),
433 SETTINGS_VERSION);
435 } else {
436 rb->splashf(HZ*2, "No ROMs in %s/pacman/", ROCKBOX_DIR);
439 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
440 rb->lcd_set_mode(LCD_MODE_RGB565);
441 #endif
443 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
444 rb->cpu_boost(false);
445 #endif
447 return PLUGIN_OK;