filename | src/gtkui/gtkui.c |
changeset | 622:3554650afb26 |
prev | 618:3ade50e8603c |
next | 630:50addb748df5 |
author | nkeynes |
date | Thu Jan 31 00:19:20 2008 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Tweak gui slice to run 100 times/second instead of 10, but still only update the speed 10/second. Also add speed clamping to <105% (roughly) |
file | annotate | diff | log | raw |
1.1 --- a/src/gtkui/gtkui.c Wed Jan 30 02:39:57 2008 +00001.2 +++ b/src/gtkui/gtkui.c Thu Jan 31 00:19:20 2008 +00001.3 @@ -26,6 +26,8 @@1.4 #include "gdrom/gdrom.h"1.5 #include "gtkui/gtkui.h"1.7 +/* Base GUI clock is 10ms */1.8 +#define GUI_TICK_PERIOD 100000001.10 void gtk_gui_start( void );1.11 void gtk_gui_stop( void );1.12 @@ -56,6 +58,7 @@1.13 * Count of running nanoseconds - used to cut back on the GUI runtime1.14 */1.15 static uint32_t gtk_gui_nanos = 0;1.16 +static uint32_t gtk_gui_ticks = 0;1.17 static struct timeval gtk_gui_lasttv;1.19 static gboolean gtk_gui_init_ok = FALSE;1.20 @@ -366,25 +369,45 @@1.21 }1.23 /**1.24 - * Module run-slice. Because UI processing is fairly expensive, only1.25 - * run the processing about 10 times a second while we're emulating.1.26 + * Module run-slice. Run the event loop 100 times/second (doesn't really need to be1.27 + * any more often than this), and update the speed display 10 times/second.1.28 + *1.29 + * Also detect if we're running too fast here and yield for a bit1.30 */1.31 uint32_t gtk_gui_run_slice( uint32_t nanosecs )1.32 {1.33 gtk_gui_nanos += nanosecs;1.34 - if( gtk_gui_nanos > 100000000 ) {1.35 + if( gtk_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */1.36 + gtk_gui_nanos -= GUI_TICK_PERIOD;1.37 + gtk_gui_ticks ++;1.38 + uint32_t current_period = gtk_gui_ticks * GUI_TICK_PERIOD;1.39 +1.40 + // Run the event loop1.41 + while( gtk_events_pending() )1.42 + gtk_main_iteration();1.43 +1.44 struct timeval tv;1.45 - while( gtk_events_pending() )1.46 - gtk_main_iteration();1.47 -1.48 gettimeofday(&tv,NULL);1.49 - double ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000.0) +1.50 - ((tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000.0);1.51 - double speed = (float)( (double)gtk_gui_nanos * 100.0 / ns );1.52 - gtk_gui_lasttv.tv_sec = tv.tv_sec;1.53 - gtk_gui_lasttv.tv_usec = tv.tv_usec;1.54 - main_window_set_speed( main_win, speed );1.55 - gtk_gui_nanos = 0;1.56 + uint32_t ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000) +1.57 + (tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000;1.58 + if( (ns * 1.05) < current_period ) {1.59 + // We've gotten ahead - sleep for a little bit1.60 + struct timespec tv;1.61 + tv.tv_sec = 0;1.62 + tv.tv_nsec = current_period - ns;1.63 + nanosleep(&tv, &tv);1.64 + }1.65 +1.66 + /* Update the display every 10 ticks (ie 10 times a second) and1.67 + * save the current tv value */1.68 + if( gtk_gui_ticks > 10 ) {1.69 + gtk_gui_ticks -= 10;1.70 +1.71 + double speed = (float)( (double)current_period * 100.0 / ns );1.72 + gtk_gui_lasttv.tv_sec = tv.tv_sec;1.73 + gtk_gui_lasttv.tv_usec = tv.tv_usec;1.74 + main_window_set_speed( main_win, speed );1.75 + }1.76 }1.77 return nanosecs;1.78 }
.