Ignore:
Timestamp:
Jan 11, 2004, 2:45:57 AM (16 years ago)
Author:
Sam Hocevar
Message:
  • configure.ac: + Check for gettimeofday().
  • src/time.c: + Created _caca_time(). + Ported _caca_getticks() to the Win32 API.
  • src/caca.c: + Properly builds on Win32.
  • test/event.c: + Added <stdlib.h> because we use malloc().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/time.c

    r331 r335  
    3434#include <time.h>
    3535
     36#if defined(USE_WIN32)
     37#   include <windows.h>
     38#endif
     39
     40#include <unistd.h>
     41
    3642#include "caca.h"
    3743#include "caca_internals.h"
    3844
     45void _caca_sleep(unsigned int usec)
     46{
     47#if defined(HAVE_USLEEP)
     48        usleep(usec);
     49#elif defined(HAVE_SLEEP)
     50        Sleep(usec / 1000);
     51#else
     52        SLEEP
     53#endif
     54}
     55
    3956unsigned int _caca_getticks(struct caca_timer *timer)
    4057{
     58#if defined(HAVE_GETTIMEOFDAY)
    4159    struct timeval tv;
     60#elif defined(USE_WIN32)
     61    static long long int freq = -1LL;
     62    long long int usec;
     63#endif
    4264    unsigned int ticks = 0;
     65    int new_sec, new_usec;
    4366
     67#if defined(HAVE_GETTIMEOFDAY)
    4468    gettimeofday(&tv, NULL);
     69    new_sec = tv.tv_sec;
     70    new_usec = tv.tv_usec;
     71#elif defined(USE_WIN32)
     72    if(freq == -1LL)
     73    {
     74        if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
     75            freq = 0;
     76    }
     77
     78    QueryPerformanceCounter((LARGE_INTEGER *)&usec);
     79    new_sec = usec / freq;
     80    new_usec = usec % freq;
     81#endif
    4582
    4683    if(timer->last_sec != 0)
     
    4885        /* If the delay was greater than 60 seconds, return 10 seconds
    4986         * otherwise we may overflow our ticks counter. */
    50         if(tv.tv_sec >= timer->last_sec + 60)
     87        if(new_sec >= timer->last_sec + 60)
    5188            ticks = 60 * 1000000;
    5289        else
    5390        {
    54             ticks = (tv.tv_sec - timer->last_sec) * 1000000;
    55             ticks += tv.tv_usec;
     91            ticks = (new_sec - timer->last_sec) * 1000000;
     92            ticks += new_usec;
    5693            ticks -= timer->last_usec;
    5794        }
    5895    }
    5996
    60     timer->last_sec = tv.tv_sec;
    61     timer->last_usec = tv.tv_usec;
     97    timer->last_sec = new_sec;
     98    timer->last_usec = new_usec;
    6299
    63100    return ticks;
Note: See TracChangeset for help on using the changeset viewer.