SDL

Форк
0
/
SDL_systimer.c 
112 строк · 3.5 Кб
1
/*
2
  Simple DirectMedia Layer
3
  Copyright (C) 1997-2024 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_internal.h"
22

23
#ifdef SDL_TIMER_WINDOWS
24

25
#include "../../core/windows/SDL_windows.h"
26

27

28
#ifdef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
29
static void SDL_CleanupWaitableTimer(void *timer)
30
{
31
    CloseHandle(timer);
32
}
33

34
HANDLE SDL_GetWaitableTimer(void)
35
{
36
    static SDL_TLSID TLS_timer_handle;
37
    HANDLE timer;
38

39
    timer = SDL_GetTLS(&TLS_timer_handle);
40
    if (!timer) {
41
        timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
42
        if (timer) {
43
            SDL_SetTLS(&TLS_timer_handle, timer, SDL_CleanupWaitableTimer);
44
        }
45
    }
46
    return timer;
47
}
48
#endif // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
49

50
Uint64 SDL_GetPerformanceCounter(void)
51
{
52
    LARGE_INTEGER counter;
53
    const BOOL rc = QueryPerformanceCounter(&counter);
54
    SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.
55
    return (Uint64)counter.QuadPart;
56
}
57

58
Uint64 SDL_GetPerformanceFrequency(void)
59
{
60
    LARGE_INTEGER frequency;
61
    const BOOL rc = QueryPerformanceFrequency(&frequency);
62
    SDL_assert(rc != 0); // this should _never_ fail if you're on XP or later.
63
    return (Uint64)frequency.QuadPart;
64
}
65

66
void SDL_SYS_DelayNS(Uint64 ns)
67
{
68
    /* CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag was added in Windows 10 version 1803.
69
     *
70
     * Sleep() is not publicly available to apps in early versions of WinRT.
71
     *
72
     * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
73
     * Windows Phone 8.1.
74
     *
75
     * Use the compiler version to determine availability.
76
     *
77
     * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
78
     * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
79
     *    Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
80
     *    apps and libraries.
81
     */
82
#ifdef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
83
    HANDLE timer = SDL_GetWaitableTimer();
84
    if (timer) {
85
        LARGE_INTEGER due_time;
86
        due_time.QuadPart = -((LONGLONG)ns / 100);
87
        if (SetWaitableTimerEx(timer, &due_time, 0, NULL, NULL, NULL, 0)) {
88
            WaitForSingleObject(timer, INFINITE);
89
        }
90
        return;
91
    }
92
#endif
93

94
    {
95
        const Uint64 max_delay = 0xffffffffLL * SDL_NS_PER_MS;
96
        if (ns > max_delay) {
97
            ns = max_delay;
98
        }
99

100
#if defined(SDL_PLATFORM_WINRT) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
101
        static HANDLE mutex = 0;
102
        if (!mutex) {
103
            mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
104
        }
105
        WaitForSingleObjectEx(mutex, (DWORD)SDL_NS_TO_MS(ns), FALSE);
106
#else
107
        Sleep((DWORD)SDL_NS_TO_MS(ns));
108
#endif
109
    }
110
}
111

112
#endif // SDL_TIMER_WINDOWS
113

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.