1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #include "pacbox_lcd.h"
31 #include "lib/configfile.h"
32 #include "lib/playback_control.h"
37 struct pacman_settings
{
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",
62 {TYPE_ENUM
, 0, 4, { .int_p
= &settings
.numlives
}, "Pacmen Per Game",
64 {TYPE_ENUM
, 0, 4, { .int_p
= &settings
.bonus
}, "Bonus", bonus_options
},
65 {TYPE_ENUM
, 0, 2, { .int_p
= &settings
.ghostnames
}, "Ghost Names",
67 {TYPE_ENUM
, 0, 2, { .int_p
= &settings
.showfps
}, "Show FPS",
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
);
83 int n
= rb
->read( fd
, buf
, len
);
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);
107 reset_PacmanMachine();
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
,
123 static int dipGhostNames
[] = { DipGhostNames_Normal
, DipGhostNames_Alternate
};
125 static int settings_to_dip(struct pacman_settings settings
)
127 return ( DipPlay_OneCoinOneGame
|
132 dipDifficulty
[settings
.difficulty
] |
133 dipLives
[settings
.numlives
] |
134 dipBonus
[settings
.bonus
] |
135 dipGhostNames
[settings
.ghostnames
]
139 static bool pacbox_menu(void)
145 bool need_restart
= false;
147 static const struct opt_items noyes
[2] = {
152 static const struct opt_items difficulty_options
[2] = {
157 static const struct opt_items numlives_options
[4] = {
164 static const struct opt_items bonus_options
[4] = {
165 { "10000 points", -1 },
166 { "15000 points", -1 },
167 { "20000 points", -1 },
171 static const struct opt_items ghostname_options
[2] = {
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();
184 result
=rb
->do_menu(&menu
, &selected
, NULL
, false);
189 new_setting
=settings
.difficulty
;
190 rb
->set_option("Difficulty", &new_setting
, INT
,
191 difficulty_options
, 2, NULL
);
192 if (new_setting
!= settings
.difficulty
) {
193 settings
.difficulty
=new_setting
;
198 new_setting
=settings
.numlives
;
199 rb
->set_option("Pacmen Per Game", &new_setting
, INT
,
200 numlives_options
, 4, NULL
);
201 if (new_setting
!= settings
.numlives
) {
202 settings
.numlives
=new_setting
;
207 new_setting
=settings
.bonus
;
208 rb
->set_option("Bonus Life", &new_setting
, INT
,
209 bonus_options
, 4, NULL
);
210 if (new_setting
!= settings
.bonus
) {
211 settings
.bonus
=new_setting
;
216 new_setting
=settings
.ghostnames
;
217 rb
->set_option("Ghost Names", &new_setting
, INT
,
218 ghostname_options
, 2, NULL
);
219 if (new_setting
!= settings
.ghostnames
) {
220 settings
.ghostnames
=new_setting
;
224 case 4: /* Show FPS */
225 rb
->set_option("Display FPS",&settings
.showfps
,INT
,
228 case 5: /* playback control */
229 playback_control(NULL
);
231 case 6: /* Restart */
242 init_PacmanMachine(settings_to_dip(settings
));
255 Runs the game engine for one frame.
257 static int gameProc( void )
264 int frame_counter
= 0;
265 int yield_counter
= 0;
269 /* Run the machine for one frame (1/60th second) */
274 /* Check the button status */
275 status
= rb
->button_status();
277 #ifdef HAS_BUTTON_HOLD
278 if (rb
->button_hold())
279 status
= PACMAN_MENU
;
282 if ((status
& PACMAN_MENU
) == PACMAN_MENU
283 #ifdef PACMAN_RC_MENU
284 || status
== PACMAN_RC_MENU
287 end_time
= *rb
->current_tick
;
289 rb
->lcd_clear_display();
290 #ifdef HAVE_REMOTE_LCD
291 rb
->lcd_remote_clear_display();
292 rb
->lcd_remote_update();
294 if (x
== 1) { return 1; }
295 start_time
+= *rb
->current_tick
-end_time
;
298 #ifdef PACMAN_HAS_REMOTE
299 setDeviceMode( Joy1_Left
, (status
& PACMAN_LEFT
|| status
== PACMAN_RC_LEFT
) ? DeviceOn
: DeviceOff
);
300 setDeviceMode( Joy1_Right
, (status
& PACMAN_RIGHT
|| status
== PACMAN_RC_RIGHT
) ? DeviceOn
: DeviceOff
);
301 setDeviceMode( Joy1_Up
, (status
& PACMAN_UP
|| status
== PACMAN_RC_UP
) ? DeviceOn
: DeviceOff
);
302 setDeviceMode( Joy1_Down
, (status
& PACMAN_DOWN
|| status
== PACMAN_RC_DOWN
) ? DeviceOn
: DeviceOff
);
303 setDeviceMode( CoinSlot_1
, (status
& PACMAN_COIN
|| status
== PACMAN_RC_COIN
) ? DeviceOn
: DeviceOff
);
304 setDeviceMode( Key_OnePlayer
, (status
& PACMAN_1UP
|| status
== PACMAN_RC_1UP
) ? DeviceOn
: DeviceOff
);
305 setDeviceMode( Key_TwoPlayers
, (status
& PACMAN_2UP
|| status
== PACMAN_RC_2UP
) ? DeviceOn
: DeviceOff
);
307 setDeviceMode( Joy1_Left
, (status
& PACMAN_LEFT
) ? DeviceOn
: DeviceOff
);
308 setDeviceMode( Joy1_Right
, (status
& PACMAN_RIGHT
) ? DeviceOn
: DeviceOff
);
309 setDeviceMode( Joy1_Up
, (status
& PACMAN_UP
) ? DeviceOn
: DeviceOff
);
310 setDeviceMode( Joy1_Down
, (status
& PACMAN_DOWN
) ? DeviceOn
: DeviceOff
);
311 setDeviceMode( CoinSlot_1
, (status
& PACMAN_COIN
) ? DeviceOn
: DeviceOff
);
312 setDeviceMode( Key_OnePlayer
, (status
& PACMAN_1UP
) ? DeviceOn
: DeviceOff
);
314 setDeviceMode( Key_TwoPlayers
, (status
& PACMAN_2UP
) ? DeviceOn
: DeviceOff
);
318 /* We only update the screen every third frame - Pacman's native
319 framerate is 60fps, so we are attempting to display 20fps */
320 if (frame_counter
== 60 / FPS
) {
327 if (yield_counter
== FPS
) {
332 /* The following functions render the Pacman screen from the
333 contents of the video and color ram. We first update the
334 background, and then draw the Sprites on top.
337 renderBackground( video_buffer
);
338 renderSprites( video_buffer
);
340 blit_display(rb
->lcd_framebuffer
,video_buffer
);
342 if (settings
.showfps
) {
343 fps
= (video_frames
*HZ
*100) / (*rb
->current_tick
-start_time
);
344 rb
->snprintf(str
,sizeof(str
),"%d.%02d / %d fps ",
345 fps
/100,fps
%100,FPS
);
346 rb
->lcd_putsxy(0,0,str
);
351 /* Keep the framerate at Pacman's 60fps */
352 end_time
= start_time
+ (video_frames
*HZ
)/FPS
;
353 while (TIME_BEFORE(*rb
->current_tick
,end_time
)) {
361 enum plugin_status
plugin_start(const void* parameter
)
367 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
370 rb
->lcd_set_backdrop(NULL
);
371 rb
->lcd_set_foreground(LCD_WHITE
);
372 rb
->lcd_set_background(LCD_BLACK
);
373 rb
->lcd_clear_display();
376 /* Set the default settings */
377 settings
.difficulty
= 0; /* Normal */
378 settings
.numlives
= 2; /* 3 lives */
379 settings
.bonus
= 0; /* 10000 points */
380 settings
.ghostnames
= 0; /* Normal names */
381 settings
.showfps
= 0; /* Do not show FPS */
383 if (configfile_load(SETTINGS_FILENAME
, config
,
384 sizeof(config
)/sizeof(*config
),
388 /* If the loading failed, save a new config file (as the disk is
390 configfile_save(SETTINGS_FILENAME
, config
,
391 sizeof(config
)/sizeof(*config
),
395 /* Keep a copy of the saved version of the settings - so we can check if
396 the settings have changed when we quit */
397 old_settings
= settings
;
399 /* Initialise the hardware */
400 init_PacmanMachine(settings_to_dip(settings
));
402 /* Load the romset */
404 start_time
= *rb
->current_tick
-1;
408 /* Save the user settings if they have changed */
409 if (rb
->memcmp(&settings
,&old_settings
,sizeof(settings
))!=0) {
410 rb
->splash(0, "Saving settings...");
411 configfile_save(SETTINGS_FILENAME
, config
,
412 sizeof(config
)/sizeof(*config
),
416 rb
->splashf(HZ
*2, "No ROMs in %s/pacman/", ROCKBOX_DIR
);
419 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
420 rb
->cpu_boost(false);