zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_systimer.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #ifdef SDL_TIMER_WINDOWS
24 
25 #include "../../core/windows/SDL_windows.h"
26 #include <mmsystem.h>
27 
28 #include "SDL_timer.h"
29 #include "SDL_hints.h"
30 
31 
32 /* The first (low-resolution) ticks value of the application */
33 static DWORD start;
34 static BOOL ticks_started = FALSE;
35 
36 #ifndef USE_GETTICKCOUNT
37 /* Store if a high-resolution performance counter exists on the system */
38 static BOOL hires_timer_available;
39 /* The first high-resolution ticks value of the application */
40 static LARGE_INTEGER hires_start_ticks;
41 /* The number of ticks per second of the high-resolution performance counter */
42 static LARGE_INTEGER hires_ticks_per_second;
43 #endif
44 
45 static void
46 timeSetPeriod(UINT uPeriod)
47 {
48  static UINT timer_period = 0;
49 
50  if (uPeriod != timer_period) {
51  if (timer_period) {
52  timeEndPeriod(timer_period);
53  }
54 
55  timer_period = uPeriod;
56 
57  if (timer_period) {
58  timeBeginPeriod(timer_period);
59  }
60  }
61 }
62 
63 static void
64 SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
65 {
66  UINT uPeriod;
67 
68  /* Unless the hint says otherwise, let's have good sleep precision */
69  if (hint && *hint) {
70  uPeriod = SDL_atoi(hint);
71  } else {
72  uPeriod = 1;
73  }
74  if (uPeriod || oldValue != hint) {
75  timeSetPeriod(uPeriod);
76  }
77 }
78 
79 void
80 SDL_InitTicks(void)
81 {
82  if (ticks_started) {
83  return;
84  }
86 
87  /* Set first ticks value */
88 #ifdef USE_GETTICKCOUNT
89  start = GetTickCount();
90 #else
91  /* QueryPerformanceCounter has had problems in the past, but lots of games
92  use it, so we'll rely on it here.
93  */
94  if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
95  hires_timer_available = TRUE;
96  QueryPerformanceCounter(&hires_start_ticks);
97  } else {
98  hires_timer_available = FALSE;
99  timeSetPeriod(1); /* use 1 ms timer precision */
100  start = timeGetTime();
101  }
102 #endif
103 
105  SDL_TimerResolutionChanged, NULL);
106 }
107 
108 Uint32
109 SDL_GetTicks(void)
110 {
111  DWORD now;
112 #ifndef USE_GETTICKCOUNT
113  LARGE_INTEGER hires_now;
114 #endif
115 
116  if (!ticks_started) {
117  SDL_InitTicks();
118  }
119 
120 #ifdef USE_GETTICKCOUNT
121  now = GetTickCount();
122 #else
123  if (hires_timer_available) {
124  QueryPerformanceCounter(&hires_now);
125 
126  hires_now.QuadPart -= hires_start_ticks.QuadPart;
127  hires_now.QuadPart *= 1000;
128  hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
129 
130  return (DWORD) hires_now.QuadPart;
131  } else {
132  now = timeGetTime();
133  }
134 #endif
135 
136  return (now - start);
137 }
138 
139 Uint64
141 {
142  LARGE_INTEGER counter;
143 
144  if (!QueryPerformanceCounter(&counter)) {
145  return SDL_GetTicks();
146  }
147  return counter.QuadPart;
148 }
149 
150 Uint64
152 {
153  LARGE_INTEGER frequency;
154 
155  if (!QueryPerformanceFrequency(&frequency)) {
156  return 1000;
157  }
158  return frequency.QuadPart;
159 }
160 
161 void
162 SDL_Delay(Uint32 ms)
163 {
164  Sleep(ms);
165 }
166 
167 #endif /* SDL_TIMER_WINDOWS */
168 
169 /* vi: set ts=4 sw=4 expandtab: */
static SDL_bool ticks_started
Definition: SDL_systimer.c:32
#define NULL
Definition: ftobjs.h:61
GLuint start
Definition: glew.h:1239
ALuint timeGetTime(void)
Definition: helpers.c:376
EGLImageKHR EGLint * name
Definition: eglext.h:284
DECLSPEC void SDLCALL SDL_AddHintCallback(const char *name, SDL_HintCallback callback, void *userdata)
Definition: SDL_hints.c:122
GLuint counter
Definition: gl2ext.h:936
#define SDL_HINT_TIMER_RESOLUTION
A variable that controls the timer resolution, in milliseconds.
Definition: SDL_hints.h:268
typedef UINT(WINAPI *PFNWGLGETCONTEXTGPUIDAMDPROC)(HGLRC hglrc)
#define Sleep(x)
Definition: allatency.c:34
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
DECLSPEC Uint32 SDLCALL SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
Definition: SDL_systimer.c:44
uint64_t Uint64
An unsigned 64-bit integer type.
Definition: SDL_stdinc.h:154
DECLSPEC Uint64 SDLCALL SDL_GetPerformanceCounter(void)
Get the current value of the high resolution counter.
Definition: SDL_systimer.c:59
DECLSPEC void SDLCALL SDL_Delay(Uint32 ms)
Wait a specified number of milliseconds before returning.
Definition: SDL_systimer.c:70
#define FALSE
Definition: ftobjs.h:57
void SDL_InitTicks(void)
Definition: SDL_systimer.c:34
DECLSPEC int SDLCALL SDL_atoi(const char *str)
Definition: SDL_string.c:774
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
DECLSPEC Uint64 SDLCALL SDL_GetPerformanceFrequency(void)
Get the count per second of the high resolution counter.
Definition: SDL_systimer.c:65
#define TRUE
Definition: ftobjs.h:53
typedef BOOL(WINAPI *PFNWGLSETSTEREOEMITTERSTATE3DLPROC)(HDC hDC