| 1 | #include <allegro.h> |
| 2 | #ifdef ALLEGRO_WINDOWS |
| 3 | #include <winalleg.h> |
| 4 | #endif |
| 5 | #include "timer.h" |
| 6 | |
| 7 | bool initialized = false; |
| 8 | |
| 9 | int timer = 0; |
| 10 | int timer_ms = 0; |
| 11 | |
| 12 | #ifdef ALLEGRO_WINDOWS |
| 13 | LARGE_INTEGER last_perf_count_li; |
| 14 | LARGE_INTEGER perf_count_li; |
| 15 | LARGE_INTEGER perf_freq_li; |
| 16 | unsigned long long last_perf_count; |
| 17 | unsigned long long perf_count; |
| 18 | unsigned long long perf_freq; |
| 19 | |
| 20 | unsigned long long li_to_ll(LARGE_INTEGER li) { |
| 21 | unsigned long long ll = li.HighPart; |
| 22 | ll = ll << 32; |
| 23 | ll |= li.LowPart; |
| 24 | return ll; |
| 25 | } |
| 26 | #endif |
| 27 | |
| 28 | bool qpc_mode = false; |
| 29 | |
| 30 | void timer_proc() { |
| 31 | ++timer; |
| 32 | } |
| 33 | END_OF_FUNCTION(timer_proc); |
| 34 | |
| 35 | void init_timer(bool use_qpc) { |
| 36 | #ifdef ALLEGRO_WINDOWS |
| 37 | if (use_qpc) |
| 38 | qpc_mode = QueryPerformanceFrequency(&perf_freq_li); |
| 39 | else |
| 40 | #endif |
| 41 | qpc_mode = false; |
| 42 | |
| 43 | if (qpc_mode) |
| 44 | perf_freq = li_to_ll(perf_freq_li); |
| 45 | |
| 46 | if (!qpc_mode) { |
| 47 | LOCK_VARIABLE(timer); |
| 48 | LOCK_FUNCTION(timer_proc); |
| 49 | install_timer(); |
| 50 | } |
| 51 | |
| 52 | initialized = true; |
| 53 | } |
| 54 | |
| 55 | void stop_timer() { |
| 56 | if (!qpc_mode) |
| 57 | remove_int(timer_proc); |
| 58 | } |
| 59 | |
| 60 | void start_timer(int _timer_ms) { |
| 61 | if (qpc_mode) { |
| 62 | #ifdef ALLEGRO_WINDOWS |
| 63 | QueryPerformanceCounter(&last_perf_count_li); |
| 64 | last_perf_count = li_to_ll(last_perf_count_li); |
| 65 | #endif |
| 66 | } |
| 67 | else { |
| 68 | stop_timer(); |
| 69 | timer_ms = _timer_ms; |
| 70 | install_int(timer_proc, timer_ms); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | float get_timer_delta() { |
| 75 | if (qpc_mode) { |
| 76 | #ifdef ALLEGRO_WINDOWS |
| 77 | QueryPerformanceCounter(&perf_count_li); |
| 78 | perf_count = li_to_ll(perf_count_li); |
| 79 | float result = (float)((double)(perf_count - last_perf_count) / (double)perf_freq); |
| 80 | last_perf_count = perf_count; |
| 81 | return result; |
| 82 | #else |
| 83 | return NULL; |
| 84 | #endif |
| 85 | } |
| 86 | else { |
| 87 | float result = (float)timer * (float)timer_ms * 0.001f; |
| 88 | timer = 0; |
| 89 | return result; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | const char* get_timing_method_string() { |
| 94 | if (!initialized) |
| 95 | return "Not initialized!"; |
| 96 | if (qpc_mode) |
| 97 | #ifdef ALLEGRO_WINDOWS |
| 98 | return "QueryPerformanceCounter"; |
| 99 | #else |
| 100 | return "Something's terribly wrong"; |
| 101 | #endif |
| 102 | return "Allegro timer routines"; |
| 103 | } |
| 104 | |
| 105 | float get_timer_accuracy() { |
| 106 | if (qpc_mode) |
| 107 | #ifdef ALLEGRO_WINDOWS |
| 108 | return (float)perf_freq; |
| 109 | #else |
| 110 | return 0.0f; |
| 111 | #endif |
| 112 | if (timer_ms) |
| 113 | return 1000.0f / (float)timer_ms; |
| 114 | return 0.0f; |
| 115 | } |