gfx_cd finished, bugfixes
[libpicofe.git] / gp2x / emu.c
index 632ad18..60907ff 100644 (file)
@@ -54,13 +54,14 @@ extern int crashed_940;
 static short sndBuffer[2*44100/50];\r
 static char noticeMsg[64];                                     // notice msg to draw\r
 static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
-static int reset_timing, osd_fps_x;\r
+static int osd_fps_x;\r
 static int combo_keys = 0, combo_acts = 0;     // keys and actions which need button combos\r
 static int gp2x_old_gamma = 100;\r
 static unsigned char *movie_data = NULL;\r
 static int movie_size = 0;\r
 unsigned char *framebuff = 0;  // temporary buffer for alt renderer\r
 int state_slot = 0;\r
+int reset_timing = 0;\r
 \r
 /*\r
 // tmp\r
@@ -969,13 +970,15 @@ static void updateSound(int len)
 {\r
        if (PicoOpt&8) len<<=1;\r
 \r
-       gp2x_sound_write(PsndOut, len<<1);\r
+       /* avoid writing audio when lagging behind to prevent audio lag */\r
+       if (PicoSkipFrame != 2)\r
+               gp2x_sound_write(PsndOut, len<<1);\r
 }\r
 \r
 \r
-static void SkipFrame(void)\r
+static void SkipFrame(int do_audio)\r
 {\r
-       PicoSkipFrame=1;\r
+       PicoSkipFrame=do_audio ? 1 : 2;\r
        PicoFrame();\r
        PicoSkipFrame=0;\r
 }\r
@@ -987,7 +990,6 @@ void emu_forced_frame(void)
 \r
        PicoOpt |= 0x10;\r
        PicoFrameFull();\r
-       PicoOpt = po_old;\r
 \r
        if (!(Pico.video.reg[12]&1)) {\r
                vidCpyM2 = vidCpyM2_32col;\r
@@ -997,6 +999,8 @@ void emu_forced_frame(void)
        vidCpyM2((unsigned char *)gp2x_screen+320*8, framebuff+328*8);\r
        vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
        gp2x_video_setpalette(localPal, 0x40);\r
+\r
+       PicoOpt = po_old;\r
 }\r
 \r
 static void simpleWait(int thissec, int lim_time)\r
@@ -1156,7 +1160,7 @@ void emu_Loop(void)
                                // when second changes, but we don't want buffer to starve.\r
                                if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
                                        updateKeys();\r
-                                       SkipFrame(); frames_done++;\r
+                                       SkipFrame(1); frames_done++;\r
                                }\r
 \r
                                frames_done  -= target_fps; if (frames_done  < 0) frames_done  = 0;\r
@@ -1169,7 +1173,7 @@ void emu_Loop(void)
                if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
-                               SkipFrame(); frames_done++;\r
+                               SkipFrame(1); frames_done++;\r
                                if (PsndOut) { // do framelimitting if sound is enabled\r
                                        gettimeofday(&tval, 0);\r
                                        if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
@@ -1188,7 +1192,7 @@ void emu_Loop(void)
                                continue;\r
                        }\r
                        updateKeys();\r
-                       SkipFrame(/*tval.tv_usec < lim_time+target_frametime*/); frames_done++;\r
+                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); frames_done++;\r
                        continue;\r
                }\r
 \r