source: trunk/src/sys/timer.cpp @ 2429

Last change on this file since 2429 was 2429, checked in by Jnat, 7 years ago

Fix missing ;
static float to static void for waitseconds()

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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#   undef near /* Fuck Microsoft */
24#   undef far /* Fuck Microsoft again */
25#   include <xbox.h>
26#elif defined _WIN32
27#   define WIN32_LEAN_AND_MEAN
28#   include <windows.h>
29#elif defined __CELLOS_LV2__
30#   include <sys/sys_time.h>
31#   include <sys/timer.h>
32#   include <sys/time_util.h>
33#elif defined HAVE_SDL_SDL_H
34#   include <SDL/SDL.h>
35#else
36#   include <SDL.h>
37#endif
38
39#include "core.h"
40
41namespace lol
42{
43
44/*
45 * Timer implementation class
46 */
47
48class TimerData
49{
50    friend class Timer;
51
52private:
53    TimerData()
54    {
55        (void)GetSeconds(true);
56    }
57
58#if defined __linux__ || defined __native_client__ || defined __APPLE__
59    float GetSeconds(bool reset)
60    {
61        struct timeval tv, tv0 = m_tv;
62        gettimeofday(&tv, NULL);
63        if (reset)
64            m_tv = tv;
65        return 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec);
66    }
67
68    static void WaitSeconds(float seconds)
69    {
70        if (seconds > 0.0f)
71            usleep((int)(seconds * 1e6f));
72    }
73
74    struct timeval m_tv;
75
76#elif defined _WIN32
77    float GetSeconds(bool reset)
78    {
79        static float secs_per_cycle = GetSecondsPerCycle();
80        LARGE_INTEGER cycles, cycles0 = m_cycles;
81        QueryPerformanceCounter(&cycles);
82        if (reset)
83            m_cycles = cycles;
84        return secs_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
85    }
86
87    static void WaitSeconds(float seconds)
88    {
89        if (seconds > 5e-4f)
90            Sleep((int)(seconds * 1e3f + 0.5f));
91    }
92
93    static float GetSecondsPerCycle()
94    {
95        LARGE_INTEGER tmp;
96        QueryPerformanceFrequency(&tmp);
97        return 1.f / tmp.QuadPart;
98    }
99
100    LARGE_INTEGER m_cycles;
101
102#elif defined __CELLOS_LV2__
103    float GetSeconds(bool reset)
104    {
105        static float secs_per_cycle = GetSecondsPerCycle();
106        uint64_t cycles, cycles0 = m_cycles;
107        SYS_TIMEBASE_GET(cycles);
108        if (reset)
109            m_cycles = cycles;
110        return secs_per_cycle * (cycles - cycles0);
111    }
112
113    static void WaitSeconds(float seconds)
114    {
115        if (seconds > 0.0f)
116            sys_timer_usleep((int)(seconds * 1e6f));
117    }
118
119    static float GetSecondsPerCycle()
120    {
121        return 1.f / sys_time_get_timebase_frequency();
122    }
123
124    uint64_t m_cycles;
125
126#else
127    float GetSeconds(bool reset)
128    {
129        static bool initialised = Init();
130
131        /* The crappy SDL fallback */
132        Uint32 ticks = SDL_GetTicks(), ticks0 = m_ticks;
133        if (reset)
134            m_ticks = ticks;
135        return 1e-3f * (ticks - ticks0);
136    }
137
138    static void WaitSeconds(float seconds)
139    {
140        if (seconds > 5e-4f)
141            SDL_Delay((int)(seconds * 1e3f + 0.5f));
142    }
143
144    static bool Init()
145    {
146        SDL_Init(SDL_INIT_TIMER);
147        return true;
148    }
149
150    Uint32 m_ticks;
151#endif
152};
153
154/*
155 * Timer public class
156 */
157
158Timer::Timer()
159  : data(new TimerData())
160{
161}
162
163Timer::~Timer()
164{
165    delete data;
166}
167
168float Timer::Get()
169{
170    return data->GetSeconds(true);
171}
172
173float Timer::Poll()
174{
175    return data->GetSeconds(false);
176}
177
178void Timer::Wait(float seconds)
179{
180    float secs_elapsed = data->GetSeconds(false);
181    data->WaitSeconds(seconds - secs_elapsed);
182}
183
184} /* namespace lol */
185
Note: See TracBrowser for help on using the repository browser.