This commit is contained in:
AzaezelX 2022-03-23 01:43:08 -05:00
parent ee4253c982
commit 2614274639
1225 changed files with 148950 additions and 51674 deletions

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -28,6 +28,8 @@
/* #define DEBUG_TIMERS */
#if !defined(__EMSCRIPTEN__) || !SDL_THREADS_DISABLED
typedef struct _SDL_Timer
{
int timerID;
@ -370,4 +372,124 @@ SDL_RemoveTimer(SDL_TimerID id)
return canceled;
}
#else
#include <emscripten/emscripten.h>
typedef struct _SDL_TimerMap
{
int timerID;
int timeoutID;
struct _SDL_TimerMap *next;
} SDL_TimerMap;
typedef struct {
int nextID;
SDL_TimerMap *timermap;
} SDL_TimerData;
static SDL_TimerData SDL_timer_data;
static void
SDL_Emscripten_TimerHelper(SDL_TimerMap *entry, Uint32 interval, SDL_TimerCallback callback, void *param)
{
Uint32 new_timeout;
new_timeout = callback(interval, param);
if (new_timeout != 0) {
entry->timeoutID = EM_ASM_INT({
return Browser.safeSetTimeout(function() {
dynCall('viiii', $0, [$1, $2, $3, $4]);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
}
}
int
SDL_TimerInit(void)
{
return 0;
}
void
SDL_TimerQuit(void)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *entry;
while (data->timermap) {
entry = data->timermap;
data->timermap = entry->next;
SDL_free(entry);
}
}
SDL_TimerID
SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *entry;
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
if (!entry) {
SDL_OutOfMemory();
return 0;
}
entry->timerID = ++data->nextID;
entry->timeoutID = EM_ASM_INT({
return Browser.safeSetTimeout(function() {
dynCall('viiii', $0, [$1, $2, $3, $4]);
}, $2);
}, &SDL_Emscripten_TimerHelper, entry, interval, callback, param);
entry->next = data->timermap;
data->timermap = entry;
return entry->timerID;
}
SDL_bool
SDL_RemoveTimer(SDL_TimerID id)
{
SDL_TimerData *data = &SDL_timer_data;
SDL_TimerMap *prev, *entry;
/* Find the timer */
prev = NULL;
for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
if (entry->timerID == id) {
if (prev) {
prev->next = entry->next;
} else {
data->timermap = entry->next;
}
break;
}
}
if (entry) {
EM_ASM_({
window.clearTimeout($0);
}, entry->timeoutID);
SDL_free(entry);
return SDL_TRUE;
}
return SDL_FALSE;
}
#endif
/* This is a legacy support function; SDL_GetTicks() returns a Uint32,
which wraps back to zero every ~49 days. The newer SDL_GetTicks64()
doesn't have this problem, so we just wrap that function and clamp to
the low 32-bits for binary compatibility. */
Uint32
SDL_GetTicks(void)
{
return (Uint32) (SDL_GetTicks64() & 0xFFFFFFFF);
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -41,8 +41,8 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32
SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
if (!ticks_started) {
SDL_TicksInit();

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -47,14 +47,14 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32
SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
if (!ticks_started) {
SDL_TicksInit();
}
return ((system_time() - start) / 1000);
return (Uint64) ((system_time() - start) / 1000);
}
Uint64

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -40,25 +40,26 @@
typedef unsigned long long ULLONG;
static ULONG ulTmrFreq = 0;
static ULLONG ullTmrStart;
static ULLONG ullTmrStart = 0;
void
SDL_TicksInit(void)
{
ULONG ulRC;
ulRC = DosTmrQueryFreq(&ulTmrFreq);
ULONG ulTmrStart; /* for 32-bit fallback. */
ULONG ulRC = DosTmrQueryFreq(&ulTmrFreq);
if (ulRC != NO_ERROR) {
debug_os2("DosTmrQueryFreq() failed, rc = %u", ulRC);
} else {
ulRC = DosTmrQueryTime((PQWORD)&ullTmrStart);
if (ulRC == NO_ERROR)
if (ulRC == NO_ERROR) {
return;
}
debug_os2("DosTmrQueryTime() failed, rc = %u", ulRC);
}
ulTmrFreq = 0; /* Error - use DosQuerySysInfo() for timer. */
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (PULONG)&ullTmrStart, sizeof(ULONG));
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulTmrStart, sizeof (ULONG));
ullTmrStart = (ULLONG) ulTmrStart;
}
void
@ -66,24 +67,27 @@ SDL_TicksQuit(void)
{
}
Uint32
SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
ULONG ulResult;
ULLONG ullTmrNow;
Uint64 ui64Result;
ULLONG ullTmrNow;
if (ulTmrFreq == 0) /* Was not initialized. */
if (ulTmrFreq == 0) { /* Was not initialized. */
SDL_TicksInit();
}
if (ulTmrFreq != 0) {
DosTmrQueryTime((PQWORD)&ullTmrNow);
ulResult = (ullTmrNow - ullTmrStart) * 1000 / ulTmrFreq;
ui64Result = (ullTmrNow - ullTmrStart) * 1000 / ulTmrFreq;
} else {
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, (PULONG)&ullTmrNow, sizeof(ULONG));
ulResult = (ULONG)ullTmrNow - (ULONG)ullTmrStart;
/* note that this counter rolls over to 0 every ~49 days. Fix your system so DosTmrQueryTime works if you need to avoid this. */
ULONG ulTmrNow;
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulTmrNow, sizeof (ULONG));
ui64Result = (((Uint64) ulTmrNow) - ullTmrStart);
}
return ulResult;
return ui64Result;
}
Uint64
@ -91,9 +95,9 @@ SDL_GetPerformanceCounter(void)
{
QWORD qwTmrNow;
if (ulTmrFreq == 0 || (DosTmrQueryTime(&qwTmrNow) != NO_ERROR))
return SDL_GetTicks();
if (ulTmrFreq == 0 || (DosTmrQueryTime(&qwTmrNow) != NO_ERROR)) {
return SDL_GetTicks64();
}
return *((Uint64 *)&qwTmrNow);
}

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -20,7 +20,7 @@
*/
#include "../../SDL_internal.h"
#ifdef SDL_TIMERS_PSP
#ifdef SDL_TIMER_PSP
#include "SDL_thread.h"
#include "SDL_timer.h"
@ -51,24 +51,23 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32 SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
struct timeval now;
if (!ticks_started) {
SDL_TicksInit();
}
struct timeval now;
Uint32 ticks;
gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks);
return (Uint64)(((Sint64)(now.tv_sec - start.tv_sec) * 1000) + ((now.tv_usec - start.tv_usec) / 1000));
}
Uint64
SDL_GetPerformanceCounter(void)
{
return SDL_GetTicks();
return SDL_GetTicks64();
}
Uint64
@ -85,7 +84,7 @@ void SDL_Delay(Uint32 ms)
sceKernelDelayThreadCB(ms * 1000);
}
#endif /* SDL_TIMERS_PSP */
#endif /* SDL_TIMER_PSP */
/* vim: ts=4 sw=4
*/

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -104,10 +104,9 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32
SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
Uint32 ticks;
if (!ticks_started) {
SDL_TicksInit();
}
@ -116,21 +115,19 @@ SDL_GetTicks(void)
#if HAVE_CLOCK_GETTIME
struct timespec now;
clock_gettime(SDL_MONOTONIC_CLOCK, &now);
ticks = (Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - start_ts.tv_nsec) / 1000000);
return (Uint64)(((Sint64)(now.tv_sec - start_ts.tv_sec) * 1000) + ((now.tv_nsec - start_ts.tv_nsec) / 1000000));
#elif defined(__APPLE__)
uint64_t now = mach_absolute_time();
ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
const uint64_t now = mach_absolute_time();
return ((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
#else
SDL_assert(SDL_FALSE);
ticks = 0;
return 0;
#endif
} else {
struct timeval now;
gettimeofday(&now, NULL);
ticks = (Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
return (Uint64)(((Sint64)(now.tv_sec - start_tv.tv_sec) * 1000) + ((now.tv_usec - start_tv.tv_usec) / 1000));
}
return (ticks);
}
Uint64
@ -190,6 +187,15 @@ SDL_GetPerformanceFrequency(void)
void
SDL_Delay(Uint32 ms)
{
int was_error;
#if HAVE_NANOSLEEP
struct timespec elapsed, tv;
#else
struct timeval tv;
Uint64 then, now, elapsed;
#endif
#ifdef __EMSCRIPTEN__
if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
/* pseudo-synchronous pause, used directly or through e.g. SDL_WaitEvent */
@ -197,21 +203,13 @@ SDL_Delay(Uint32 ms)
return;
}
#endif
int was_error;
#if HAVE_NANOSLEEP
struct timespec elapsed, tv;
#else
struct timeval tv;
Uint32 then, now, elapsed;
#endif
/* Set the timeout interval */
#if HAVE_NANOSLEEP
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
#else
then = SDL_GetTicks();
then = SDL_GetTicks64();
#endif
do {
errno = 0;
@ -222,13 +220,13 @@ SDL_Delay(Uint32 ms)
was_error = nanosleep(&tv, &elapsed);
#else
/* Calculate the time interval left (in case of interrupt) */
now = SDL_GetTicks();
now = SDL_GetTicks64();
elapsed = (now - then);
then = now;
if (elapsed >= ms) {
if (elapsed >= ((Uint64)ms)) {
break;
}
ms -= elapsed;
ms -= (Uint32)elapsed;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;

View file

@ -0,0 +1,89 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_TIMER_VITA
#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_error.h"
#include "../SDL_timer_c.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <psp2/kernel/processmgr.h>
static uint64_t start;
static SDL_bool ticks_started = SDL_FALSE;
void
SDL_TicksInit(void)
{
if (ticks_started) {
return;
}
ticks_started = SDL_TRUE;
start = sceKernelGetProcessTimeWide();
}
void
SDL_TicksQuit(void)
{
ticks_started = SDL_FALSE;
}
Uint64
SDL_GetTicks64(void)
{
uint64_t now;
if (!ticks_started) {
SDL_TicksInit();
}
now = sceKernelGetProcessTimeWide();
return (Uint64) ((now - start) / 1000);
}
Uint64
SDL_GetPerformanceCounter(void)
{
return sceKernelGetProcessTimeWide();
}
Uint64
SDL_GetPerformanceFrequency(void)
{
return 1000000;
}
void SDL_Delay(Uint32 ms)
{
const Uint32 max_delay = 0xffffffffUL / 1000;
if(ms > max_delay)
ms = max_delay;
sceKernelDelayThreadCB(ms * 1000);
}
#endif /* SDL_TIMER_VITA */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -33,12 +33,10 @@
static DWORD start = 0;
static BOOL ticks_started = FALSE;
/* Store if a high-resolution performance counter exists on the system */
static BOOL hires_timer_available;
/* The first high-resolution ticks value of the application */
static LARGE_INTEGER hires_start_ticks;
static LARGE_INTEGER start_ticks;
/* The number of ticks per second of the high-resolution performance counter */
static LARGE_INTEGER hires_ticks_per_second;
static LARGE_INTEGER ticks_per_second;
static void
SDL_SetSystemTimerResolution(const UINT uPeriod)
@ -79,6 +77,8 @@ SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValu
void
SDL_TicksInit(void)
{
BOOL rc;
if (ticks_started) {
return;
}
@ -90,18 +90,12 @@ SDL_TicksInit(void)
SDL_TimerResolutionChanged, NULL);
/* Set first ticks value */
/* QueryPerformanceCounter has had problems in the past, but lots of games
use it, so we'll rely on it here.
/* QueryPerformanceCounter allegedly is always available and reliable as of WinXP,
so we'll rely on it here.
*/
if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
hires_timer_available = TRUE;
QueryPerformanceCounter(&hires_start_ticks);
} else {
hires_timer_available = FALSE;
#ifndef __WINRT__
start = timeGetTime();
#endif /* __WINRT__ */
}
rc = QueryPerformanceFrequency(&ticks_per_second);
SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
QueryPerformanceCounter(&start_ticks);
}
void
@ -116,53 +110,37 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32
SDL_GetTicks(void)
Uint64
SDL_GetTicks64(void)
{
DWORD now = 0;
LARGE_INTEGER hires_now;
LARGE_INTEGER now;
BOOL rc;
if (!ticks_started) {
SDL_TicksInit();
}
if (hires_timer_available) {
QueryPerformanceCounter(&hires_now);
hires_now.QuadPart -= hires_start_ticks.QuadPart;
hires_now.QuadPart *= 1000;
hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
return (DWORD) hires_now.QuadPart;
} else {
#ifndef __WINRT__
now = timeGetTime();
#endif /* __WINRT__ */
}
return (now - start);
rc = QueryPerformanceCounter(&now);
SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
return (Uint64) (((now.QuadPart - start_ticks.QuadPart) * 1000) / ticks_per_second.QuadPart);
}
Uint64
SDL_GetPerformanceCounter(void)
{
LARGE_INTEGER counter;
if (!QueryPerformanceCounter(&counter)) {
return SDL_GetTicks();
}
return counter.QuadPart;
const BOOL rc = QueryPerformanceCounter(&counter);
SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
return (Uint64) counter.QuadPart;
}
Uint64
SDL_GetPerformanceFrequency(void)
{
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency)) {
return 1000;
}
return frequency.QuadPart;
const BOOL rc = QueryPerformanceFrequency(&frequency);
SDL_assert(rc != 0); /* this should _never_ fail if you're on XP or later. */
return (Uint64) frequency.QuadPart;
}
void