1919#include <SDL/SDL_mixer.h>
2020#if defined(PANDORA )
2121 #include <unistd.h>
22+ #elif defined(GP2X )
23+ #include <sys/ioctl.h>
24+ #include <unistd.h>
25+ #include <sys/soundcard.h>
26+ #include <fcntl.h>
2227#endif
2328
2429#include "asylum_os.h"
@@ -138,6 +143,7 @@ int game()
138143 frameinc = 1 ;
139144 rate50 = 1 ;
140145 swi_blitz_wait (1 );
146+ uint32_t last_tick = SDL_GetTicks ();
141147 while (!swi_readescapestate ())
142148 {
143149 mainloop :
@@ -183,8 +189,31 @@ int game()
183189 if (cheatpermit == 1 ) cheatread ();
184190 scorewipe ();
185191 plotscore ();
186- frameinc = ((options .gearchange == 0 ) ? 2 : 1 );
187- swi_blitz_wait (frameinc );
192+ if (options .gearchange == 0 )
193+ {
194+ frameinc = 2 ;
195+ swi_blitz_wait (frameinc );
196+ }
197+ else
198+ {
199+ frameinc = 1 ;
200+
201+ uint32_t diff = 20 ;
202+ uint32_t current_tick = SDL_GetTicks ();
203+ if (current_tick - last_tick >= 2 * diff )
204+ {
205+ last_tick = current_tick - diff ;
206+ }
207+ else
208+ {
209+ while (current_tick - last_tick < diff )
210+ {
211+ SDL_Delay (1 );
212+ current_tick = SDL_GetTicks ();
213+ }
214+ last_tick += diff ;
215+ }
216+ }
188217 if ((rate50 != 1 ) && (frameinc < 2 )) //rate 25 but one frame passed
189218 {
190219 swi_blitz_wait (1 );
@@ -250,6 +279,8 @@ void bonus1()
250279const int keydefs [] =
251280#if defined(PANDORA )
252281{ - SDLK_LEFT , - SDLK_RIGHT , - SDLK_UP , - SDLK_DOWN , - SDLK_HOME };
282+ #elif defined(GP2X)
283+ { - SDLK_LEFT , - SDLK_RIGHT , - SDLK_UP , - SDLK_DOWN , - SDLK_a };
253284#else
254285{ - SDLK_z , - SDLK_x , - SDLK_SEMICOLON , - SDLK_PERIOD , - SDLK_RETURN };
255286#endif
@@ -483,6 +514,83 @@ void c_array_initializers()
483514 init_keyboard ();
484515}
485516
517+ #if defined(GP2X )
518+ static int InitialVolume ;
519+
520+ // Set new GP2X mixer level, 0-100
521+ static void Set_GP2X_Volume (int newvol )
522+ {
523+ int soundDev , vol ;
524+
525+ if ((newvol >= 0 ) && (newvol <= 100 ))
526+ {
527+ soundDev = open ("/dev/mixer" , O_RDWR );
528+ if (soundDev != -1 )
529+ {
530+ vol = ((newvol << 8 ) | newvol );
531+ ioctl (soundDev , SOUND_MIXER_WRITE_PCM , & vol );
532+ close (soundDev );
533+ }
534+ }
535+ }
536+
537+ // Returns 0-100, current mixer volume, -1 on error.
538+ static int Get_GP2X_Volume (void )
539+ {
540+ int soundDev , vol ;
541+
542+ vol = -1 ;
543+ soundDev = open ("/dev/mixer" , O_RDONLY );
544+ if (soundDev != -1 )
545+ {
546+ ioctl (soundDev , SOUND_MIXER_READ_PCM , & vol );
547+ close (soundDev );
548+ if (vol != -1 )
549+ {
550+ //just return one channel , not both channels, they're hopefully the same anyways
551+ return (vol & 0xFF );
552+ }
553+ }
554+
555+ return vol ;
556+ }
557+
558+ static void Set_Initial_GP2X_Volume (void )
559+ {
560+ Set_GP2X_Volume (InitialVolume );
561+ }
562+
563+ void Change_HW_Audio_Volume (int amount )
564+ {
565+ int current_volume ;
566+
567+ current_volume = Get_GP2X_Volume ();
568+
569+ if (current_volume == -1 ) current_volume = 68 ;
570+
571+ if ((amount > 1 ) && current_volume < 12 )
572+ {
573+ amount = 1 ;
574+ }
575+ else if ((amount < -1 ) && current_volume <= 12 )
576+ {
577+ amount = -1 ;
578+ }
579+
580+ current_volume += amount ;
581+
582+ if (current_volume > 100 )
583+ {
584+ current_volume = 100 ;
585+ }
586+ else if (current_volume < 0 )
587+ {
588+ current_volume = 0 ;
589+ }
590+ Set_GP2X_Volume (current_volume );
591+ }
592+ #endif
593+
486594int main (int argc , char * * argv )
487595{
488596 find_resources ();
@@ -498,11 +606,19 @@ int main(int argc, char** argv)
498606 open_scores ();
499607 dropprivs ();
500608
609+ #if defined(GP2X )
610+ InitialVolume = Get_GP2X_Volume ();
611+ atexit (Set_Initial_GP2X_Volume );
612+ #endif
613+
501614 SDL_Init (SDL_INIT_VIDEO |SDL_INIT_AUDIO );
502615 SDL_WM_SetCaption ("Asylum" , "Asylum" );
503616 SDL_EnableUNICODE (1 );
504617#ifndef _NO_SOUND
505618 init_audio ();
619+ #endif
620+ #if defined(GP2X )
621+ Set_Initial_GP2X_Volume ();
506622#endif
507623 c_array_initializers ();
508624 swi_stasis_control (8 , 8 );
@@ -718,7 +834,7 @@ void loadconfig()
718834 case 4 : options .firekey = - temp ; break ;
719835 case 5 : options .soundtype = temp ; break ;
720836 //case 6: options.soundquality=temp; break;
721- #if !defined(PANDORA )
837+ #if !( defined(GP2X ) || defined( PANDORA ) )
722838 case 7 : options .fullscreen = temp ; break ;
723839#endif
724840#ifndef DISABLE_OPENGL
@@ -754,7 +870,7 @@ void saveconfig()
754870 config_keywords [5 ], options .soundtype ,
755871 //config_keywords[6], options.soundquality,
756872 config_keywords [7 ],
757- #if defined(PANDORA )
873+ #if defined(GP2X ) || defined ( PANDORA )
758874 1 ,
759875#else
760876 options .fullscreen ,
@@ -785,7 +901,7 @@ void saveconfig()
785901 options .initials [2 ],
786902 ((options .idpermit == 1 ) ? idpermitstring : "" ));
787903 fclose (r0 );
788- #if defined(PANDORA )
904+ #if defined(GP2X ) || defined( PANDORA )
789905 sync ();
790906#endif
791907}
@@ -843,7 +959,7 @@ void savegame()
843959 fwrite (neuronadr -> contents , neuronadr -> width , neuronadr -> height , r0 );
844960 }
845961 fclose (r0 );
846- #if defined(PANDORA )
962+ #if defined(GP2X ) || defined( PANDORA )
847963 sync ();
848964#endif
849965}
@@ -857,7 +973,7 @@ void permitid()
857973 {
858974 fprintf (r0 , "%s" , idpermitstring );
859975 fclose (r0 );
860- #if defined(PANDORA )
976+ #if defined(GP2X ) || defined( PANDORA )
861977 sync ();
862978#endif
863979 }
0 commit comments