source: trunk/src/timer.cpp @ 1082

Last change on this file since 1082 was 1082, checked in by sam, 8 years ago

core: port all code to NativeClient. Nothing runs for now, but it builds.

  • Property svn:keywords set to Id
File size: 3.7 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 <stdint.h>
17
18#if defined __linux__ || defined __native_client__ || defined __APPLE__
19#   include <sys/time.h>
20#   include <unistd.h>
21#elif defined _XBOX
22#   include <xtl.h>
23#   include <xbox.h>
24#elif defined _WIN32
25#   define WIN32_LEAN_AND_MEAN
26#   include <windows.h>
27#elif defined __CELLOS_LV2__
28#   include <sys/sys_time.h>
29#   include <sys/timer.h>
30#   include <sys/time_util.h>
31#else
32#   include <SDL.h>
33#endif
34
35#include "core.h"
36
37namespace lol
38{
39
40/*
41 * Timer implementation class
42 */
43
44class TimerData
45{
46    friend class Timer;
47
48private:
49    TimerData()
50    {
51#if defined __linux__ || defined __native_client__ || defined __APPLE__
52        gettimeofday(&tv0, NULL);
53#elif defined _WIN32
54        QueryPerformanceCounter(&cycles0);
55#elif defined __CELLOS_LV2__
56        SYS_TIMEBASE_GET(cycles0);
57#else
58        SDL_Init(SDL_INIT_TIMER);
59        ticks0 = SDL_GetTicks();
60#endif
61    }
62
63    float GetOrWait(float deltams, bool update)
64    {
65        float ret, towait;
66#if defined __linux__ || defined __native_client__ || defined __APPLE__
67        struct timeval tv;
68        gettimeofday(&tv, NULL);
69        ret = 1e-3f * (tv.tv_usec - tv0.tv_usec)
70            + 1e3f * (tv.tv_sec - tv0.tv_sec);
71        if (update)
72            tv0 = tv;
73        towait = deltams - ret;
74        if (towait > 0.0f)
75            usleep((int)(towait * 1e3f));
76#elif defined _WIN32
77        LARGE_INTEGER cycles;
78        QueryPerformanceCounter(&cycles);
79        static float ms_per_cycle = GetMsPerCycle();
80        ret = ms_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
81        if (update)
82            cycles0 = cycles;
83        towait = deltams - ret;
84        if (towait > 5e-4f)
85            Sleep((int)(towait + 0.5f));
86#elif defined __CELLOS_LV2__
87        uint64_t cycles;
88        SYS_TIMEBASE_GET(cycles);
89        static float ms_per_cycle = GetMsPerCycle();
90        ret = ms_per_cycle * (cycles - cycles0);
91        if (update)
92            cycles0 = cycles;
93        towait = deltams - ret;
94        if (towait > 0.0f)
95            sys_timer_usleep((int)(towait * 1e3f));
96#else
97        /* The crappy SDL fallback */
98        Uint32 ticks = SDL_GetTicks();
99        ret = ticks - ticks0;
100        if (update)
101            ticks0 = ticks;
102        towait = deltams - ret;
103        if (towait > 0.5f)
104            SDL_Delay((int)(towait + 0.5f));
105#endif
106        return ret;
107    }
108
109    static float GetMsPerCycle()
110    {
111#if defined __linux__ || defined __native_client__ || defined __APPLE__
112        return 1.0f;
113#elif defined _WIN32
114        LARGE_INTEGER tmp;
115        QueryPerformanceFrequency(&tmp);
116        return 1e3f / tmp.QuadPart;
117#elif defined __CELLOS_LV2__
118        return 1e3f / sys_time_get_timebase_frequency();
119#else
120        return 1.0f;
121#endif
122    }
123
124#if defined __linux__ || defined __native_client__ || defined __APPLE__
125    struct timeval tv0;
126#elif defined _WIN32
127    LARGE_INTEGER cycles0;
128#elif defined __CELLOS_LV2__
129    uint64_t cycles0;
130#else
131    Uint32 ticks0;
132#endif
133};
134
135/*
136 * Timer public class
137 */
138
139Timer::Timer()
140  : data(new TimerData())
141{
142}
143
144Timer::~Timer()
145{
146    delete data;
147}
148
149float Timer::GetMs()
150{
151    return data->GetOrWait(0.0f, true);
152}
153
154float Timer::PollMs()
155{
156    return data->GetOrWait(0.0f, false);
157}
158
159void Timer::WaitMs(float deltams)
160{
161    (void)data->GetOrWait(deltams, false);
162}
163
164} /* namespace lol */
165
Note: See TracBrowser for help on using the repository browser.