fix incorrect assumptions with display

+ clang format
This commit is contained in:
Kylie C 2025-10-18 02:19:31 -04:00
parent 9614c0b97b
commit 98249834ff
3 changed files with 38 additions and 28 deletions

View File

@ -42,14 +42,14 @@ inline float SDL_randf()
// https://wiki.libsdl.org/SDL3/README-migration#sdl_videoh
typedef Uint32 SDL_DisplayID;
typedef int SDL_DisplayID;
inline SDL_DisplayID SDL_GetPrimaryDisplay()
{
return 0;
return 1;
}
// Modified from 83bb0f9105922fd49282f0b931f7873a71877ac8 SDL_video.c#L1331
inline SDL_DisplayMode** SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID, int *count)
inline SDL_DisplayMode** SDL_GetFullscreenDisplayModes(int displayID, int* count)
{
int i;
if (count) *count = 0;
@ -76,11 +76,21 @@ inline SDL_DisplayMode** SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID,
return result;
}
inline SDL_DisplayMode* SDL_GetCurrentDisplayMode(SDL_DisplayID displayID)
inline SDL_DisplayMode g_displayMode;
inline SDL_mutex* g_displayMutex = SDL_CreateMutex();
// This does not handle the differences between the SDL_DisplayMode struct
// https://wiki.libsdl.org/SDL3/SDL_DisplayMode
// https://wiki.libsdl.org/SDL2/SDL_DisplayMode
inline SDL_DisplayMode* SDL_GetCurrentDisplayMode(int displayID)
{
SDL_DisplayMode* mode = nullptr;
SDL_GetCurrentDisplayMode(displayID, mode);
return mode;
SDL_LockMutex(g_displayMutex);
if (SDL_GetCurrentDisplayMode(displayID, &g_displayMode) == 0) {
SDL_UnlockMutex(g_timerMutex);
return &g_displayMode;
}
SDL_UnlockMutex(g_timerMutex);
return NULL;
}
#define SDL_GetWindowSize(...) (SDL_GetWindowSize(__VA_ARGS__), true )

View File

@ -1,11 +1,15 @@
#pragma once
#include "SDL.h"
#include <SDL2/SDL_events.h>
// https://wiki.libsdl.org/SDL3/README-migration#sdl_eventsh
#define SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED SDL_WINDOWEVENT_SIZE_CHANGED
#define SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED (SDL_WINDOWEVENT + 2 + SDL_WINDOWEVENT_SIZE_CHANGED)
#define SDL_EVENT_WINDOW_CLOSE_REQUESTED (SDL_WINDOWEVENT + 2 + SDL_WINDOWEVENT_CLOSE)
#define SDL_EVENT_WINDOW_FOCUS_GAINED (SDL_WINDOWEVENT + 2 + SDL_WINDOWEVENT_FOCUS_GAINED)
#define SDL_EVENT_WINDOW_FOCUS_LOST (SDL_WINDOWEVENT + 2 + SDL_WINDOWEVENT_FOCUS_LOST)
// #define SDL_EVENT_MOUSE_BUTTON_DOWN SDL_MOUSEBUTTONDOWN
// #define SDL_EVENT_MOUSE_BUTTON_UP SDL_MOUSEBUTTONUP
@ -18,12 +22,6 @@
#define SDL_EVENT_QUIT SDL_QUIT
#define SDL_EVENT_WINDOW_CLOSE_REQUESTED SDL_WINDOWEVENT_CLOSE
#define SDL_EVENT_WINDOW_FOCUS_GAINED SDL_WINDOWEVENT_FOCUS_GAINED
#define SDL_EVENT_WINDOW_FOCUS_LOST SDL_WINDOWEVENT_FOCUS_LOST
#define SDL_EVENT_GAMEPAD_AXIS_MOTION SDL_CONTROLLERAXISMOTION
#define SDL_EVENT_GAMEPAD_BUTTON_DOWN SDL_CONTROLLERBUTTONDOWN
#define SDL_EVENT_GAMEPAD_BUTTON_UP SDL_CONTROLLERBUTTONUP

View File

@ -12,22 +12,21 @@
// time is in miliseconds not nanoseconds
#define SDL_NS_TO_MS(MS) (MS)
typedef Uint32 SDL3_TimerCallback (void *userdata, SDL_TimerID timerID, Uint32 interval);
typedef Uint32 SDL3_TimerCallback(void* userdata, SDL_TimerID timerID, Uint32 interval);
struct SDL2TimerShimData {
SDL3_TimerCallback *callback2;
void *userdata2;
SDL3_TimerCallback* callback;
void* userdata;
SDL_TimerID id;
};
static std::map<SDL_TimerID, void*> g_timers;
static SDL_mutex *g_timerMutex = SDL_CreateMutex();
inline std::map<SDL_TimerID, SDL2TimerShimData*> g_timers;
inline SDL_mutex* g_timerMutex = SDL_CreateMutex();
inline Uint32 shim_timer_callback (Uint32 interval, void *param)
inline Uint32 shim_timer_callback(Uint32 interval, void* param)
{
const auto shim = static_cast<SDL2TimerShimData*>(param);
const Uint32 next = shim->callback2(shim->userdata2, shim->id, interval);
auto* shim = static_cast<SDL2TimerShimData*>(param);
const Uint32 next = shim->callback(shim->userdata, shim->id, interval);
if (next == 0) {
SDL_LockMutex(g_timerMutex);
@ -39,13 +38,17 @@ inline Uint32 shim_timer_callback (Uint32 interval, void *param)
return next;
}
inline SDL_TimerID SDL_AddTimer(Uint32 interval, SDL3_TimerCallback callback3, void* userdata)
inline SDL_TimerID SDL_AddTimer(Uint32 interval, SDL3_TimerCallback* callback, void* userdata)
{
const auto shim = static_cast<SDL2TimerShimData*>(SDL_malloc(sizeof(SDL2TimerShimData)));
shim->callback2 = callback3;
shim->userdata2 = userdata;
auto* shim = static_cast<SDL2TimerShimData*>(SDL_malloc(sizeof(SDL2TimerShimData)));
shim->callback = callback;
shim->userdata = userdata;
const SDL_TimerID id = ::SDL_AddTimer(interval, shim_timer_callback, shim);
if (id == 0) {
SDL_free(shim);
return 0;
}
shim->id = id;
SDL_LockMutex(g_timerMutex);
@ -66,5 +69,4 @@ inline SDL_bool SDL_RemoveTimer2(SDL_TimerID id)
return ::SDL_RemoveTimer(id);
}
#define SDL_RemoveTimer SDL_RemoveTimer2