diff --git a/libee/ee.c b/libee/ee.c index 4840d68..7b69f20 100644 --- a/libee/ee.c +++ b/libee/ee.c @@ -190,11 +190,6 @@ int ee_init(void) return 0; } -void ee_set_delay(int usec) -{ - _ee_delay = usec; -} - int ee_get_width(void) { #if defined(USE_SLANG) @@ -217,37 +212,35 @@ int ee_get_height(void) #endif } -#if !defined(USE_CONIO) -static int64_t local_time(void) +void ee_set_delay(int usec) { - struct timeval tv; - int64_t now; - - gettimeofday(&tv, NULL); - now = tv.tv_sec; - now *= 1000000; - now += tv.tv_usec; - return now; + _ee_delay = usec; } -#endif -void ee_refresh(void) +static unsigned int _ee_getticks(void) { -#if !defined(USE_CONIO) - static int64_t local_clock = 0; - int64_t now; + static unsigned int last_sec = 0, last_usec = 0; - if(!local_clock) - { - /* Initialize local_clock */ - local_clock = local_time(); - } + struct timeval tv; + unsigned int ticks = 0; + + gettimeofday(&tv, NULL); - if(local_time() > local_clock + 10000) + if(last_sec != 0) { - /* If we are late, we shouldn't display anything */ + ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec); } -#endif + + last_sec = tv.tv_sec; + last_usec = tv.tv_usec; + + return ticks; +} + +void ee_refresh(void) +{ + static int lastticks = 0; + unsigned int ticks = lastticks + _ee_getticks(); #if defined(USE_SLANG) SLsmg_refresh(); @@ -261,18 +254,15 @@ void ee_refresh(void) # endif #endif -#if !defined(USE_CONIO) - now = local_time(); + /* Wait until _ee_delay + time of last call */ + for(ticks += _ee_getticks(); ticks < _ee_delay; ticks += _ee_getticks()) + usleep(10000); - if(now < local_clock + _ee_delay - 10000) - { - usleep(local_clock + _ee_delay - 10000 - now); - } + lastticks = ticks - _ee_delay; - local_clock += _ee_delay; -#else - delay(5); -#endif + /* If we drifted too much, it's bad, bad, bad. */ + if(lastticks > _ee_delay) + lastticks = 0; } void ee_end(void) diff --git a/src/main.c b/src/main.c index cbe6a7f..0c85088 100644 --- a/src/main.c +++ b/src/main.c @@ -46,6 +46,8 @@ int main (int argc, char **argv) return 1; } + ee_set_delay(100000); + /* Initialize our program */ g->w = ee_get_width(); g->h = ee_get_height(); diff --git a/test/demo.c b/test/demo.c index 1f2f980..910474a 100644 --- a/test/demo.c +++ b/test/demo.c @@ -55,6 +55,8 @@ int main(int argc, char **argv) return 1; } + ee_set_delay(40000); + /* Initialize data */ sprite = ee_load_sprite("data/barboss.txt");