source: trunk/src/timer.cpp @ 128

Last change on this file since 128 was 128, checked in by sam, 11 years ago

Implement Timer::PollSeconds(), if necessary.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdlib>
11#include <cstdio>
12#include <stdint.h>
13
14#if defined __linux__
15#   include <sys/time.h>
16#   include <unistd.h>
17#elif defined _WIN32
18#   define WIN32_LEAN_AND_MEAN
19#   include <windows.h>
20#   include <SDL.h> // FIXME: this should not be needed
21#else
22#   include <SDL.h>
23#endif
24
25#include "timer.h"
26
27/*
28 * Timer implementation class
29 */
30
31class TimerData
32{
33    friend class Timer;
34
35private:
36    TimerData()
37    {
38#if defined __linux__
39        gettimeofday(&tv0, NULL);
40#elif defined _WIN32
41        LARGE_INTEGER tmp;
42        QueryPerformanceFrequency(&tmp);
43        seconds_per_cycle = 1.0f / tmp.QuadPart;
44        QueryPerformanceCounter(&cycles0);
45#else
46        SDL_Init(SDL_INIT_TIMER);
47        ticks0 = SDL_GetTicks();
48#endif
49    }
50
51    float GetOrWait(float seconds, bool update)
52    {
53        float ret, delta_time;
54#if defined __linux__
55        struct timeval tv;
56        gettimeofday(&tv, NULL);
57        ret = 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec);
58        if (update)
59            tv0 = tv;
60        delta_time = seconds - ret;
61        if (delta_time > 0.0f)
62            usleep((int)(delta_time * 1e6f));
63#elif defined _WIN32
64        LARGE_INTEGER cycles;
65        QueryPerformanceCounter(&cycles);
66        ret = seconds_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
67        if (update)
68            cycles0 = cycles;
69        delta_time = seconds - ret;
70        if (delta_time > 5e-4f) // FIXME: use native Win32 stuff
71            SDL_Delay((int)(delta_time * 1e3f + 0.5f));
72#else
73        /* The crappy SDL fallback */
74        Uint32 ticks = SDL_GetTicks();
75        ret = 1e-3f * (ticks - ticks0);
76        if (update)
77            ticks0 = ticks;
78        delta_time = seconds - ret;
79        if (delta_time > 5e-4f)
80            SDL_Delay((int)(delta_time * 1e3f + 0.5f));
81#endif
82        return ret;
83    }
84
85#if defined __linux__
86    struct timeval tv0;
87#elif defined _WIN32
88    float seconds_per_cycle;
89    LARGE_INTEGER cycles0;
90#else
91    Uint32 ticks0;
92#endif
93};
94
95/*
96 * Timer public class
97 */
98
99Timer::Timer()
100{
101    data = new TimerData();
102}
103
104Timer::~Timer()
105{
106    delete data;
107}
108
109float Timer::GetSeconds()
110{
111    return data->GetOrWait(0.0f, true);
112}
113
114float Timer::PollSeconds()
115{
116    return data->GetOrWait(0.0f, false);
117}
118
119void Timer::WaitSeconds(float seconds)
120{
121    (void)data->GetOrWait(seconds, false);
122}
123
Note: See TracBrowser for help on using the repository browser.