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

Last change on this file since 344 was 344, checked in by Sam Hocevar, 18 years ago
  • configure.ac: + Check for sys/times.h and unistd.h. + Added calls to AC_C_CONST and AC_C_INLINE.
  • src/time.c src/graphics.c: + Fixes for MSVC compilation warnings. + Additional sanity checks in the Win32 driver.
  • src/caca.c: + Use raw()/noraw() in the curses driver instead of cbreak().
  • msvc/libcaca.sln: + Added an MSVC solution.
  • examples/aafire.c: + Slightly reduced the maximum cacafire framerate.
  • Property svn:keywords set to Id
File size: 2.6 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 344 2004-01-13 09:55:32Z 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#if defined(HAVE_SYS_TIME_H)
34#   include <sys/time.h>
35#endif
36#include <time.h>
37
38#if defined(USE_WIN32)
39#   include <windows.h>
40#endif
41
42#if defined(HAVE_UNISTD_H)
43#   include <unistd.h>
44#endif
45
46#include "caca.h"
47#include "caca_internals.h"
48
49void _caca_sleep(unsigned int usec)
50{
51#if defined(HAVE_USLEEP)
52        usleep(usec);
53#elif defined(HAVE_SLEEP)
54        Sleep(usec / 1000);
55#else
56        SLEEP
57#endif
58}
59
60unsigned int _caca_getticks(struct caca_timer *timer)
61{
62#if defined(HAVE_GETTIMEOFDAY)
63    struct timeval tv;
64#elif defined(USE_WIN32)
65    static __int64 freq = -1;
66    unsigned __int64 usec;
67#endif
68    unsigned int ticks = 0;
69    int new_sec, new_usec;
70
71#if defined(HAVE_GETTIMEOFDAY)
72    gettimeofday(&tv, NULL);
73    new_sec = tv.tv_sec;
74    new_usec = tv.tv_usec;
75#elif defined(USE_WIN32)
76    if(freq == -1)
77    {
78        if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
79            freq = 0;
80    }
81
82    QueryPerformanceCounter((LARGE_INTEGER *)&usec);
83    new_sec = (int)(usec * 1000000 / freq / 1000000);
84    new_usec = (int)((usec * 1000000 / freq) % 1000000);
85#endif
86
87    if(timer->last_sec != 0)
88    {
89        /* If the delay was greater than 60 seconds, return 10 seconds
90         * otherwise we may overflow our ticks counter. */
91        if(new_sec >= timer->last_sec + 60)
92            ticks = 60 * 1000000;
93        else
94        {
95            ticks = (new_sec - timer->last_sec) * 1000000;
96            ticks += new_usec;
97            ticks -= timer->last_usec;
98        }
99    }
100
101    timer->last_sec = new_sec;
102    timer->last_usec = new_usec;
103
104    return ticks;
105}
106
Note: See TracBrowser for help on using the repository browser.