perfect vsync, bugfixes
[fceu.git] / drivers / gp2x / main.c
index a399e1e..5926da7 100644 (file)
 #include "../common/unixdsp.h"
 #include "../common/cheat.h"
 
+#include "../../fce.h"
+#include "../../cart.h"
+
 #include "dface.h"
 
-// just for printing some iNES info for user..
-#include "../../fce.h"
-#include "../../ines.h"
+
+void CleanSurface(void);
 
 // internals
 extern char lastLoadedGameName[2048];
 extern uint8 Exit; // exit emu loop flag
+extern int FSkip;
 void CloseGame(void);
 
 FCEUGI *fceugi = NULL;
-static int ntsccol=0,ntschue=-1,ntsctint=-1;
+int ntsccol=0,ntschue=-1,ntsctint=-1;
 int soundvol=70;
 int inited=0;
 
-int srendlinev[2]={0,0};
-int erendlinev[2]={239,239};
+int srendlinev[2]={8,0};
+int erendlinev[2]={231,239};
 int srendline,erendline;
 
 
@@ -83,7 +86,6 @@ static void ParseGI(FCEUGI *gi)
   InputType[1]=gi->input[1];
  if(gi->inputfc>=0)
   InputTypeFC=gi->inputfc;
- FCEUI_GetCurrentVidSystem(&srendline,&erendline);
 }
 
 void FCEUD_PrintError(char *s)
@@ -91,8 +93,8 @@ void FCEUD_PrintError(char *s)
  puts(s);
 }
 
-static char *cpalette=0;
-static void LoadCPalette(void)
+char *cpalette=0;
+void LoadCPalette(void)
 {
  char tmpp[192];
  FILE *fp;
@@ -100,6 +102,8 @@ static void LoadCPalette(void)
  if(!(fp=fopen(cpalette,"rb")))
  {
   printf(" Error loading custom palette from file: %s\n",cpalette);
+  free(cpalette);
+  cpalette=0;
   return;
  }
  fread(tmpp,1,192,fp);
@@ -130,8 +134,7 @@ void SaveConfig(const char *name)
        if (name)
             sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
        else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
-        DriverInterface(DES_GETNTSCTINT,&ntsctint);
-        DriverInterface(DES_GETNTSCHUE,&ntschue);
+       FCEUI_GetNTSCTH(&ntsctint, &ntschue);
         SaveFCEUConfig(tdir,fceuconfig);
 }
 
@@ -141,9 +144,9 @@ static void LoadConfig(const char *name)
        if (name)
             sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
        else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
+       FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
         LoadFCEUConfig(tdir,fceuconfig);
-        if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
-        if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
+       FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
 }
 
 static void LoadLLGN(void)
@@ -164,7 +167,7 @@ static void LoadLLGN(void)
 static void SaveLLGN(void)
 {
  // save last loaded game name
- if (lastLoadedGameName[0])
+ if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
  {
   char tdir[2048];
   FILE *f;
@@ -182,7 +185,7 @@ static void SaveLLGN(void)
 
 static void CreateDirs(void)
 {
- char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
+ char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
  char tdir[2048];
  int x;
 
@@ -213,12 +216,9 @@ static void CloseStuff(int signum)
          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
-        /* So much SIGBUS evil. */
-        #ifdef SIGBUS
         #if(SIGBUS!=SIGSEGV)
          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
         #endif
-        #endif
          case SIGFPE:printf("Those darn floating points.  Ne'er know when they'll bite!\n");break;
          case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
          case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
@@ -251,13 +251,13 @@ static int DoArgs(int argc, char *argv[])
          {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
          {"-fcexp",0,&fcexp,0x4001},
 
-         {"-gg",&docheckie[1],0,0},
-         {"-no8lim",0,&eoptions,0x8001},
-         {"-subase",0,&eoptions,0x8002},
+         {"-gg",0,&eoptions,0x8000|EO_GG},
+         {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
         {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
         {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
+        {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
          {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
          {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
         {0,(void *)DriverArgs,0,0},
@@ -277,10 +277,8 @@ static int DoArgs(int argc, char *argv[])
        }
        if(docheckie[0])
         Settings.region_force=2;
-       if(docheckie[1])
-        FCEUI_SetGameGenie(1);
-        FCEUI_DisableSpriteLimitation(1);
-        FCEUI_SaveExtraDataUnderBase(eoptions&2);
+       FCEUI_SetGameGenie(eoptions&EO_GG);
+        FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
        FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
 
        for(x=0;x<2;x++)
@@ -289,13 +287,8 @@ static int DoArgs(int argc, char *argv[])
          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
        }
 
-       printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
-        printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
-        srendlinev[0]=0;
         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
-        FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
-        FCEUI_SetSoundVolume(soundvol);
-       DriverInterface(DES_NTSCCOL,&ntsccol); // TODO
+        FCEUI_SetSoundVolume(80);
        DoDriverArgs();
 
        if(fcexp)
@@ -362,8 +355,8 @@ int CLImain(int argc, char *argv[])
         LoadConfig(NULL);
         last_arg_parsed=DoArgs(argc-1,&argv[1]);
        gp2x_opt_setup();
-       gp2x_cpuclock_gamma_update();
        LoadLLGN();
+       FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
        if(cpalette)
         LoadCPalette();
        if(InitSound())
@@ -389,22 +382,20 @@ int CLImain(int argc, char *argv[])
           fceugi=FCEUI_LoadGame(lastLoadedGameName);
          if (fceugi)
          {
+          char infostring[32];
           LoadConfig(lastLoadedGameName);
           if (Settings.region_force)
            FCEUI_SetVidSystem(Settings.region_force - 1);
           ParseGI(fceugi);
-          //RefreshThrottleFPS();
           InitOtherInput();
 
-          // additional print for gpfce
-          // TODO: handlers for other formats then iNES
-          {
-         int MapperNo;
-         iNES_HEADER *head = iNESGetHead(); // TODO: ReMake
-          MapperNo = (head->ROM_type>>4);
-          MapperNo|=(head->ROM_type2&0xF0);
-         FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
-          }
+          if ((eoptions&EO_GG) && geniestage == 0) {
+           strcpy(infostring, "gg.rom is missing, GG disabled");
+           eoptions&=~EO_GG;
+           FCEUI_SetGameGenie(0);
+          } else
+           GameInterface(GI_INFOSTRING, infostring);
+          FCEU_DispMessage("%s", infostring);
          }
          else
          {
@@ -427,15 +418,19 @@ int CLImain(int argc, char *argv[])
          }
          }
 
+        gp2x_opt_update();
         PrepareOtherInput();
+        FCEUI_GetCurrentVidSystem(&srendline,&erendline);
         gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
         switch (Settings.scaling & 3) {
-                case 0: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(0); break;
-                case 1: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break;
-                case 2: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break; // TODO
-                case 3: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(32); break;
+                case 0: gp2x_video_set_offs(0);  gp2x_video_RGB_setscaling(320, 240); break;
+                case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(256, 240); break;
+                case 2: gp2x_video_set_offs(32+srendline*320); gp2x_video_RGB_setscaling(256, erendline-srendline); break;
+                case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(320, 240); break;
         }
+        CleanSurface();
         gp2x_start_sound(Settings.sound_rate, 16, 0);
+        RefreshThrottleFPS();
         FCEUI_Emulate();
        }
 
@@ -459,7 +454,10 @@ static int DriverInitialize(void)
 static void DriverKill(void)
 {
  // SaveConfig(NULL); // done explicitly in menu now
- SetSignals(SIG_IGN);
+ SetSignals(SIG_DFL);
+
+ if(cpalette) free(cpalette);
+ cpalette=0;
 
  if(inited&4)
   KillVideo();
@@ -470,11 +468,16 @@ static void DriverKill(void)
 
 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
 {
- if(!Count && !(eoptions&EO_NOTHROTTLE))
+ FCEUD_UpdateInput();  // must update input before blitting because of save confirmation stuff
+ BlitPrepare(xbuf == NULL);
+ if(!(eoptions&EO_NOTHROTTLE))
+ {
+  if(Count)
+   WriteSound(Buffer,Count);
   SpeedThrottle();
- BlitScreen(xbuf);
- if(Count && !(eoptions&EO_NOTHROTTLE))
-  WriteSound(Buffer,Count);
FCEUD_UpdateInput();
+ }
+ BlitScreen(xbuf == NULL);
+ // make sure last frame won't get skipped, because we need it for menu bg
if (Exit) FSkip=0;
 }