Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add more missing implementations to SDL #3436

Merged
merged 6 commits into from Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 69 additions & 18 deletions src/rcore_desktop_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ typedef struct {
SDL_GLContext glContext;

SDL_Joystick *gamepad;
SDL_Cursor *cursor;
} PlatformData;

//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -178,6 +179,22 @@ static const KeyboardKey ScancodeToKey[SCANCODE_MAPPED_NUM] = {
KEY_KP_DECIMAL // SDL_SCANCODE_KP_PERIOD
};

static const int CursorsLUT[] = {
SDL_SYSTEM_CURSOR_ARROW, // 0 MOUSE_CURSOR_DEFAULT
SDL_SYSTEM_CURSOR_ARROW, // 1 MOUSE_CURSOR_ARROW
SDL_SYSTEM_CURSOR_IBEAM, // 2 MOUSE_CURSOR_IBEAM
SDL_SYSTEM_CURSOR_CROSSHAIR, // 3 MOUSE_CURSOR_CROSSHAIR
SDL_SYSTEM_CURSOR_HAND, // 4 MOUSE_CURSOR_POINTING_HAND
SDL_SYSTEM_CURSOR_SIZEWE, // 5 MOUSE_CURSOR_RESIZE_EW
SDL_SYSTEM_CURSOR_SIZENS, // 6 MOUSE_CURSOR_RESIZE_NS
SDL_SYSTEM_CURSOR_SIZENWSE, // 7 MOUSE_CURSOR_RESIZE_NWSE
SDL_SYSTEM_CURSOR_SIZENESW, // 8 MOUSE_CURSOR_RESIZE_NESW
SDL_SYSTEM_CURSOR_SIZEALL, // 9 MOUSE_CURSOR_RESIZE_ALL
SDL_SYSTEM_CURSOR_NO // 10 MOUSE_CURSOR_NOT_ALLOWED
//SDL_SYSTEM_CURSOR_WAIT, // No equivalent implemented on MouseCursor enum on raylib.h
//SDL_SYSTEM_CURSOR_WAITARROW, // No equivalent implemented on MouseCursor enum on raylib.h
};

//----------------------------------------------------------------------------------
// Module Internal Functions Declaration
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -315,14 +332,13 @@ void SetWindowOpacity(float opacity)
// Set window focused
void SetWindowFocused(void)
{
TRACELOG(LOG_WARNING, "SetWindowFocused() not available on target platform");
SDL_RaiseWindow(platform.window);
}

// Get native window handle
void *GetWindowHandle(void)
{
TRACELOG(LOG_WARNING, "GetWindowHandle() not implemented on target platform");
return NULL;
return (void *)platform.window;
}

// Get number of monitors
Expand Down Expand Up @@ -389,15 +405,45 @@ int GetMonitorHeight(int monitor)
// Get selected monitor physical width in millimetres
int GetMonitorPhysicalWidth(int monitor)
{
TRACELOG(LOG_WARNING, "GetMonitorPhysicalWidth() not implemented on target platform");
return 0;
int width = 0;

int monitorCount = 0;
monitorCount = SDL_GetNumVideoDisplays();

if ((monitor >= 0) && (monitor < monitorCount))
{
float vdpi = 0.0f;
SDL_GetDisplayDPI(monitor, NULL, NULL, &vdpi);
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode);
// Calculate size on inches, then convert to millimeter
if (vdpi > 0.0f) width = (mode.w/vdpi)*25.4f;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");

return width;
}

// Get selected monitor physical height in millimetres
int GetMonitorPhysicalHeight(int monitor)
{
TRACELOG(LOG_WARNING, "GetMonitorPhysicalHeight() not implemented on target platform");
return 0;
int height = 0;

int monitorCount = 0;
monitorCount = SDL_GetNumVideoDisplays();

if ((monitor >= 0) && (monitor < monitorCount))
{
float vdpi = 0.0f;
SDL_GetDisplayDPI(monitor, NULL, NULL, &vdpi);
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode);
// Calculate size on inches, then convert to millimeter
if (vdpi > 0.0f) height = (mode.h/vdpi)*25.4f;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");

return height;
}

// Get selected monitor refresh rate
Expand Down Expand Up @@ -452,43 +498,45 @@ Vector2 GetWindowScaleDPI(void)
// Set clipboard text content
void SetClipboardText(const char *text)
{
TRACELOG(LOG_WARNING, "SetClipboardText() not implemented on target platform");
SDL_SetClipboardText(text);
}

// Get clipboard text content
// NOTE: returned string is allocated and freed by GLFW
// NOTE: returned string must be freed with SDL_free()
const char *GetClipboardText(void)
{
TRACELOG(LOG_WARNING, "GetClipboardText() not implemented on target platform");
return NULL;
return SDL_GetClipboardText();
}

// Show mouse cursor
void ShowCursor(void)
{
SDL_ShowCursor(SDL_ENABLE);

CORE.Input.Mouse.cursorHidden = false;
}

// Hides mouse cursor
void HideCursor(void)
{
SDL_ShowCursor(SDL_DISABLE);

CORE.Input.Mouse.cursorHidden = true;
}

// Enables cursor (unlock cursor)
void EnableCursor(void)
{
// Set cursor position in the middle
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_ShowCursor(SDL_ENABLE);

CORE.Input.Mouse.cursorHidden = false;
}

// Disables cursor (lock cursor)
void DisableCursor(void)
{
// Set cursor position in the middle
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
SDL_SetRelativeMouseMode(SDL_TRUE);

CORE.Input.Mouse.cursorHidden = true;
}
Expand Down Expand Up @@ -524,8 +572,7 @@ void OpenURL(const char *url)
// Set internal gamepad mappings
int SetGamepadMappings(const char *mappings)
{
TRACELOG(LOG_WARNING, "SetGamepadMappings() not implemented on target platform");
return 0;
SDL_GameControllerAddMapping(mappings);
}

// Set mouse position XY
Expand All @@ -538,7 +585,10 @@ void SetMousePosition(int x, int y)
// Set mouse cursor
void SetMouseCursor(int cursor)
{
TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform");
platform.cursor = SDL_CreateSystemCursor(CursorsLUT[cursor]);
SDL_SetCursor(platform.cursor);

CORE.Input.Mouse.cursor = cursor;
}

// Register all input events
Expand Down Expand Up @@ -794,6 +844,7 @@ static int InitPlatform(void)

static void ClosePlatform(void)
{
SDL_FreeCursor(platform.cursor); // Free cursor
SDL_GL_DeleteContext(platform.glContext); // Deinitialize OpenGL context
SDL_DestroyWindow(platform.window);
SDL_Quit(); // Deinitialize SDL internal global state
Expand Down