You are not logged in.

Read the FAQ and Knowledge Base before posting.
WiFi not emulated and not supported!!
We won't make a 3DS/2DS emulator.



#1 2010-12-01 15:09:50

Spudd86
Member
Registered: 2010-12-01
Posts: 5

Patch: Desmume-cli better FPS limiting

I've got a patch that improves FPS limiting in desmume-cli.

It removes the callback and semaphore, and instead does this:

if ( !my_config.disable_limiter && !ctrls_cfg.boost) {
    int now = SDL_GetTicks();
    int delay =  (limiter_tick0 + limiter_frame_counter*1000/FPS_LIMITER_FPS) - now;
    if (delay > 0) {
        SDL_Delay(delay);
    } else if (delay < -500) { // reset if we fall too far behind don't want to run super fast until we catch up
        limiter_tick0 = now;
        limiter_frame_counter = 0;
    }
}
// always count frames, we'll mess up if the limiter gets turned on later otherwise
limiter_frame_counter += 1 + my_config.frameskip;

I tried it, it took some things that were running at half speed (regardless of what I set frameskip too...) and now runs them at full speed and it gives a MUCH more stable frame rate as long as we're running at full speed. Note that FPS_LIMITER_FPS is 60.

I only did desmume-cli because it didn't look like it would be quite so strait forward for the others, and I wanted to submit this by itself and get some feedback before I did any others, also I don't have a Windows or OSX machine to test...

Last edited by Spudd86 (2011-01-04 04:32:37)

Offline

#2 2010-12-01 19:50:02

xxxbullettxxx
Member
Registered: 2010-11-30
Posts: 5

Re: Patch: Desmume-cli better FPS limiting

How do i download it?

Offline

#3 2010-12-01 21:37:07

Spudd86
Member
Registered: 2010-12-01
Posts: 5

Re: Patch: Desmume-cli better FPS limiting

I dunno... here's the patch:

From 0ba9592a3593af562756d11079bae408845087c0 Mon Sep 17 00:00:00 2001
From: Thomas Jones
Date: Wed, 1 Dec 2010 09:34:14 -0500
Subject: [PATCH] cli: much better FPS limiting

This patch makes the emulator frame rate more stable, and also makes it
run at full speed more often!

Only done for desmume-cli
---
 desmume/src/cli/main.cpp |   72 +++++++--------------------------------------
 1 files changed, 12 insertions(+), 60 deletions(-)

diff --git a/desmume/src/cli/main.cpp b/desmume/src/cli/main.cpp
index 77ae06f..386860e 100644
--- a/desmume/src/cli/main.cpp
+++ b/desmume/src/cli/main.cpp
@@ -73,6 +73,7 @@ static float nds_screen_size_ratio = 1.0f;
 
 
 #define FPS_LIMITER_FRAME_PERIOD 8
+#define FPS_LIMITER_FPS 60
 
 static SDL_Surface * surface;
 
@@ -275,30 +276,6 @@ joinThread_gdb( void *thread_handle) {
 }
 #endif
 
-
-
-/** 
- * A SDL timer callback function. Signals the supplied SDL semaphore
- * if its value is small.
- * 
- * @param interval The interval since the last call (in ms)
- * @param param The pointer to the semaphore.
- * 
- * @return The interval to the next call (required by SDL)
- */
-static Uint32
-fps_limiter_fn( Uint32 interval, void *param) {
-  SDL_sem *sdl_semaphore = (SDL_sem *)param;
-
-  /* signal the semaphore if it is getting low */
-  if ( SDL_SemValue( sdl_semaphore) < 4) {
-    SDL_SemPost( sdl_semaphore);
-  }
-
-  return interval;
-}
-
-
 #ifdef INCLUDE_OPENGL_2D
 /* initialization openGL function */
 static int
@@ -518,8 +495,7 @@ int main(int argc, char ** argv) {
 #endif
 
   int limiter_frame_counter = 0;
-  SDL_sem *fps_limiter_semaphore = NULL;
-  SDL_TimerID limiter_timer = NULL;
+  int limiter_tick0 = 0;
   int error;
 
   GKeyFile *keyfile;
@@ -726,27 +702,6 @@ int main(int argc, char ** argv) {
   /* Since gtk has a different mapping the keys stop to work with the saved configuration :| */
   load_default_config(cli_kb_cfg);
 
-  if ( !my_config.disable_limiter) {
-    /* create the semaphore used for fps limiting */
-    fps_limiter_semaphore = SDL_CreateSemaphore( 1);
-
-    /* start a SDL timer for every FPS_LIMITER_FRAME_PERIOD frames to keep us at 60 fps */
-    if ( fps_limiter_semaphore != NULL) {
-      limiter_timer = SDL_AddTimer( 16 * my_config.fps_limiter_frame_period,
-                                  fps_limiter_fn, fps_limiter_semaphore);
-    }
-
-    if ( limiter_timer == NULL) {
-      fprintf( stderr, "Error trying to start FPS limiter timer: %s\n",
-               SDL_GetError());
-      if ( fps_limiter_semaphore != NULL) {
-        SDL_DestroySemaphore( fps_limiter_semaphore);
-        fps_limiter_semaphore = NULL;
-      }
-      return 1;
-    }
-  }
-
   if(my_config.load_slot != -1){
     loadstate_slot(my_config.load_slot);
   }
@@ -786,14 +741,17 @@ int main(int argc, char ** argv) {
     }
 
     if ( !my_config.disable_limiter && !ctrls_cfg.boost) {
-      limiter_frame_counter += 1 + my_config.frameskip;
-      if ( limiter_frame_counter >= my_config.fps_limiter_frame_period) {
-        limiter_frame_counter = 0;
-
-        /* wait for the timer to expire */
-        SDL_SemWait( fps_limiter_semaphore);
-      }
+        int now = SDL_GetTicks();
+        int delay =  (limiter_tick0 + limiter_frame_counter*1000/FPS_LIMITER_FPS) - now;
+        if (delay > 0) {
+            SDL_Delay(delay);
+        } else if (delay < -500) { // reset if we fall too far behind don't want to run super fast until we catch up
+            limiter_tick0 = now;
+            limiter_frame_counter = 0;
+        }
     }
+    // always count frames, we'll mess up if the limiter gets turned on later otherwise
+    limiter_frame_counter += 1 + my_config.frameskip;
 
 #ifdef DISPLAY_FPS
     fps_frame_counter += 1;
@@ -818,12 +776,6 @@ int main(int argc, char ** argv) {
   /* Unload joystick */
   uninit_joy();
 
-  if ( !my_config.disable_limiter) {
-    /* tidy up the FPS limiter timer and semaphore */
-    SDL_RemoveTimer( limiter_timer);
-    SDL_DestroySemaphore( fps_limiter_semaphore);
-  }
-
   SDL_Quit();
   NDS_DeInit();
 
-- 
1.7.2.2

Last edited by Spudd86 (2010-12-01 21:40:48)

Offline

#4 2010-12-01 21:39:43

shash
Administrator
Registered: 2007-03-17
Posts: 897

Re: Patch: Desmume-cli better FPS limiting

xxxbullettxxx: Don't post on technical forums threads if you can't understand what they're doing and who are they meant for.

Offline

#5 2010-12-01 21:42:07

Spudd86
Member
Registered: 2010-12-01
Posts: 5

Re: Patch: Desmume-cli better FPS limiting

I couldn't find the download link on the sourceforge tracker for the patch either...

Offline

#6 2010-12-01 21:45:58

xxxbullettxxx
Member
Registered: 2010-11-30
Posts: 5

Re: Patch: Desmume-cli better FPS limiting

shash: sorry, I'm a bit of a computer n00b

Offline

Board footer

Powered by FluxBB