Debian Codegraph @

src/win/wtime.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows time module.
12 *
13 * By Peter Wang.
14 *
15 * See readme.txt for copyright information.
16 */
17
18
19 #include "allegro5/allegro.h"
20 #include "allegro5/internal/aintern_thread.h"
21 #include "allegro5/platform/aintwin.h"
22
23 #include <mmsystem.h>
24
25 ALLEGRO_STATIC_ASSERT(wtime,
26 sizeof(ALLEGRO_TIMEOUT_WIN) <= sizeof(ALLEGRO_TIMEOUT));
27
28
29 #define LARGE_INTEGER_TO_INT64(li) (((int64_t)li.HighPart << 32) | \
30 (int64_t)li.LowPart)
31
32 static int64_t high_res_timer_freq;
33 static int64_t _al_win_prev_time;
34 static double _al_win_total_time;
35
36 static double (*real_get_time_func)(void);
37
38 static _AL_MUTEX time_mutex = _AL_MUTEX_UNINITED;
39
40
41 static double low_res_current_time(void)
42 {
43 int64_t cur_time;
44 double ellapsed_time;
45
46 _al_mutex_lock(&time_mutex);
47
48 cur_time = (int64_t) timeGetTime();
49 ellapsed_time = (double) (cur_time - _al_win_prev_time) / 1000;
50
51 if (cur_time < _al_win_prev_time) {
52 ellapsed_time += 4294967.295;
53 }
54
55 _al_win_total_time += ellapsed_time;
56 _al_win_prev_time = cur_time;
57
58 _al_mutex_unlock(&time_mutex);
59
60 return _al_win_total_time;
61 }
62
63
64 static double high_res_current_time(void)
65 {
66 LARGE_INTEGER count;
67 int64_t cur_time;
68 double ellapsed_time;
69
70 _al_mutex_lock(&time_mutex);
71
72 QueryPerformanceCounter(&count);
73
74 cur_time = LARGE_INTEGER_TO_INT64(count);
75 ellapsed_time = (double)(cur_time - _al_win_prev_time) / (double)high_res_timer_freq;
76
77 _al_win_total_time += ellapsed_time;
78 _al_win_prev_time = cur_time;
79
80 _al_mutex_unlock(&time_mutex);
81
82 return _al_win_total_time;
83 }
84
85
86 double _al_win_get_time(void)
87 {
88 return (*real_get_time_func)();
89 }
90
91
92 void _al_win_init_time(void)
93 {
94 LARGE_INTEGER tmp_freq;
95 _al_win_total_time = 0;
96
97 _al_mutex_init(&time_mutex);
98
99 if (QueryPerformanceFrequency(&tmp_freq) == 0) {
100 real_get_time_func = low_res_current_time;
101 _al_win_prev_time = (int64_t) timeGetTime();
102 }
103 else {
104 LARGE_INTEGER count;
105 high_res_timer_freq = LARGE_INTEGER_TO_INT64(tmp_freq);
106 real_get_time_func = high_res_current_time;
107 QueryPerformanceCounter(&count);
108 _al_win_prev_time = LARGE_INTEGER_TO_INT64(count);
109 }
110 }
111
112
113
114 void _al_win_shutdown_time(void)
115 {
116 _al_mutex_destroy(&time_mutex);
117 }
118
119
120
121 void _al_win_rest(double seconds)
122 {
123 if (seconds <= 0)
124 return;
125
126 Sleep((DWORD)(seconds * 1000.0));
127 }
128
129
130
131 void _al_win_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)
132 {
133 ALLEGRO_TIMEOUT_WIN *wt = (ALLEGRO_TIMEOUT_WIN *) timeout;
134
135 ASSERT(wt);
136 ASSERT(seconds <= INT_MAX/1000.0);
137
138 if (seconds <= 0.0) {
139 wt->abstime = timeGetTime();
140 }
141 else {
142 wt->abstime = timeGetTime() + (DWORD)(seconds * 1000.0);
143 }
144 }
145
146 /* vim: set sts=3 sw=3 et */