source: trunk/src/timer.cpp @ 723

Last change on this file since 723 was 723, checked in by sam, 12 years ago

core: fix a few compilation errors and warnings on OS X.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cstdio>
17#include <stdint.h>
18
19#if defined __linux__ || defined __APPLE__
20#   include <sys/time.h>
21#   include <unistd.h>
22#elif defined _WIN32
23#   define WIN32_LEAN_AND_MEAN
24#   include <windows.h>
25#else
26#   include <SDL.h>
27#endif
28
29#include "core.h"
30
31namespace lol
32{
33
34/*
35 * Timer implementation class
36 */
37
38class TimerData
39{
40    friend class Timer;
41
42private:
43    TimerData()
44    {
45#if defined __linux__ || defined __APPLE__
46        gettimeofday(&tv0, NULL);
47#elif defined _WIN32
48        LARGE_INTEGER tmp;
49        QueryPerformanceFrequency(&tmp);
50        ms_per_cycle = 1e3f / tmp.QuadPart;
51        QueryPerformanceCounter(&cycles0);
52#else
53        SDL_Init(SDL_INIT_TIMER);
54        ticks0 = SDL_GetTicks();
55#endif
56    }
57
58    float GetOrWait(float deltams, bool update)
59    {
60        float ret, towait;
61#if defined __linux__ || defined __APPLE__
62        struct timeval tv;
63        gettimeofday(&tv, NULL);
64        ret = 1e-3f * (tv.tv_usec - tv0.tv_usec)
65            + 1e3f * (tv.tv_sec - tv0.tv_sec);
66        if (update)
67            tv0 = tv;
68        towait = deltams - ret;
69        if (towait > 0.0f)
70            usleep((int)(towait * 1e3f));
71#elif defined _WIN32
72        LARGE_INTEGER cycles;
73        QueryPerformanceCounter(&cycles);
74        ret = ms_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
75        if (update)
76            cycles0 = cycles;
77        towait = deltams - ret;
78        if (towait > 5e-4f)
79            Sleep((int)(towait + 0.5f));
80#else
81        /* The crappy SDL fallback */
82        Uint32 ticks = SDL_GetTicks();
83        ret = ticks - ticks0;
84        if (update)
85            ticks0 = ticks;
86        towait = deltams - ret;
87        if (towait > 0.5f)
88            SDL_Delay((int)(towait + 0.5f));
89#endif
90        return ret;
91    }
92
93#if defined __linux__ || defined __APPLE__
94    struct timeval tv0;
95#elif defined _WIN32
96    float ms_per_cycle;
97    LARGE_INTEGER cycles0;
98#else
99    Uint32 ticks0;
100#endif
101};
102
103/*
104 * Timer public class
105 */
106
107Timer::Timer()
108  : data(new TimerData())
109{
110}
111
112Timer::~Timer()
113{
114    delete data;
115}
116
117float Timer::GetMs()
118{
119    return data->GetOrWait(0.0f, true);
120}
121
122float Timer::PollMs()
123{
124    return data->GetOrWait(0.0f, false);
125}
126
127void Timer::WaitMs(float deltams)
128{
129    (void)data->GetOrWait(deltams, false);
130}
131
132} /* namespace lol */
133
Note: See TracBrowser for help on using the repository browser.