source: libcaca/trunk/src/time.c @ 341

Last change on this file since 341 was 338, checked in by Sam Hocevar, 16 years ago
  • configure.ac: + Better win32 check for Sleep().
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002, 2003, 2004 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the GNU Lesser General Public
8 *  License as published by the Free Software Foundation; either
9 *  version 2 of the License, or (at your option) any later version.
10 *
11 *  This library is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *  Lesser General Public License for more details.
15 *
16 *  You should have received a copy of the GNU Lesser General Public
17 *  License along with this library; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *  02111-1307  USA
20 */
21
22/** \file time.c
23 *  \version \$Id: time.c 338 2004-01-12 14:01:40Z sam $
24 *  \author Sam Hocevar <sam@zoy.org>
25 *  \brief Timer routines
26 *
27 *  This file contains simple timer routines.
28 */
29
30#include "config.h"
31
32#include <stdlib.h>
33#include <sys/time.h>
34#include <time.h>
35
36#if defined(USE_WIN32)
37#   include <windows.h>
38#endif
39
40#include <unistd.h>
41
42#include "caca.h"
43#include "caca_internals.h"
44
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
56unsigned int _caca_getticks(struct caca_timer *timer)
57{
58#if defined(HAVE_GETTIMEOFDAY)
59    struct timeval tv;
60#elif defined(USE_WIN32)
61    static long long int freq = -1;
62    unsigned long long int usec;
63#endif
64    unsigned int ticks = 0;
65    int new_sec, new_usec;
66
67#if defined(HAVE_GETTIMEOFDAY)
68    gettimeofday(&tv, NULL);
69    new_sec = tv.tv_sec;
70    new_usec = tv.tv_usec;
71#elif defined(USE_WIN32)
72    if(freq == -1)
73    {
74        if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
75            freq = 0;
76    }
77
78    QueryPerformanceCounter((LARGE_INTEGER *)&usec);
79    new_sec = usec * 1000000 / freq / 1000000;
80    new_usec = (usec * 1000000 / freq) % 1000000;
81#endif
82
83    if(timer->last_sec != 0)
84    {
85        /* If the delay was greater than 60 seconds, return 10 seconds
86         * otherwise we may overflow our ticks counter. */
87        if(new_sec >= timer->last_sec + 60)
88            ticks = 60 * 1000000;
89        else
90        {
91            ticks = (new_sec - timer->last_sec) * 1000000;
92            ticks += new_usec;
93            ticks -= timer->last_usec;
94        }
95    }
96
97    timer->last_sec = new_sec;
98    timer->last_usec = new_usec;
99
100    return ticks;
101}
102
Note: See TracBrowser for help on using the repository browser.