#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#if defined(_WIN32)
#if !defined(HWND) && !defined(_MSVC_LANG)
#define HWND void*
#elif !defined(HWND) && defined(_MSVC_LANG)
typedef struct HWND__ *HWND;
#endif
#include "../external/win32_clipboard.h"
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_NATIVE_INCLUDE_NONE
#include "GLFW/glfw3native.h"
#if SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
__declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#endif
#endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <sys/time.h>
#if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
#if defined(_GLFW_X11)
#define GLFW_EXPOSE_NATIVE_X11
#define Font X11Font
#endif
#if defined(_GLFW_WAYLAND)
#define GLFW_EXPOSE_NATIVE_WAYLAND
#endif
#include "GLFW/glfw3native.h"
#if defined(_GLFW_X11)
#undef Font
#endif
#endif
#endif
#if defined(__APPLE__)
#include <unistd.h>
void *glfwGetCocoaWindow(GLFWwindow *handle);
#include "GLFW/glfw3native.h"
#endif
#include <stddef.h>
typedef struct {
GLFWwindow *handle; } PlatformData;
extern CoreData CORE;
static PlatformData platform = { 0 };
int InitPlatform(void); void ClosePlatform(void);
static void ErrorCallback(int error, const char *description);
static void WindowSizeCallback(GLFWwindow *window, int width, int height); static void FramebufferSizeCallback(GLFWwindow *window, int width, int height); static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley); static void WindowPosCallback(GLFWwindow *window, int x, int y); static void WindowIconifyCallback(GLFWwindow *window, int iconified); static void WindowMaximizeCallback(GLFWwindow *window, int maximized); static void WindowFocusCallback(GLFWwindow *window, int focused); static void WindowDropCallback(GLFWwindow *window, int count, const char **paths);
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); static void CharCallback(GLFWwindow *window, unsigned int codepoint); static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods); static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset); static void CursorEnterCallback(GLFWwindow *window, int enter); static void JoystickCallback(int jid, int event);
static void *AllocateWrapper(size_t size, void *user); static void *ReallocateWrapper(void *block, size_t size, void *user); static void DeallocateWrapper(void *block, void *user);
bool WindowShouldClose(void)
{
if (CORE.Window.ready) return CORE.Window.shouldClose;
else return true;
}
void ToggleFullscreen(void)
{
if (!FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
{
CORE.Window.previousPosition = CORE.Window.position;
CORE.Window.previousScreen = CORE.Window.screen;
int monitorCount = 0;
int monitorIndex = GetCurrentMonitor();
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
GLFWmonitor *monitor = (monitorIndex < monitorCount)? monitors[monitorIndex] : NULL;
if (monitor != NULL)
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitorIndex]);
CORE.Window.display.width = mode->width;
CORE.Window.display.height = mode->height;
CORE.Window.position = (Point){ 0, 0 };
CORE.Window.screen = CORE.Window.display;
FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
#if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
#endif
glfwSetWindowMonitor(platform.handle, monitor, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to get monitor");
}
else
{
CORE.Window.position = CORE.Window.previousPosition;
CORE.Window.screen = CORE.Window.previousScreen;
FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
#if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
Vector2 scaleDpi = GetWindowScaleDPI();
CORE.Window.screen.width = (unsigned int)(CORE.Window.screen.width*scaleDpi.x);
CORE.Window.screen.height = (unsigned int)(CORE.Window.screen.height*scaleDpi.y);
}
#endif
glfwSetWindowMonitor(platform.handle, NULL, CORE.Window.position.x, CORE.Window.position.y,
CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
#if defined(_GLFW_X11) || defined(_GLFW_WAYLAND)
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
#endif
}
if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) glfwSwapInterval(1);
}
void ToggleBorderlessWindowed(void)
{
if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) ToggleFullscreen();
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
const int monitor = GetCurrentMonitor();
if ((monitor >= 0) && (monitor < monitorCount))
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
if (mode != NULL)
{
if (!FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
{
CORE.Window.previousPosition = CORE.Window.position;
CORE.Window.previousScreen = CORE.Window.screen;
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
glfwGetMonitorPos(monitors[monitor], &CORE.Window.position.x, &CORE.Window.position.y);
CORE.Window.screen.width = mode->width;
CORE.Window.screen.height = mode->height;
glfwSetWindowMonitor(platform.handle, monitors[monitor], CORE.Window.position.x, CORE.Window.position.y,
CORE.Window.screen.width, CORE.Window.screen.height, mode->refreshRate);
glfwFocusWindow(platform.handle);
FLAG_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
}
else
{
CORE.Window.position = CORE.Window.previousPosition;
CORE.Window.screen = CORE.Window.previousScreen;
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
#if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
Vector2 scaleDpi = GetWindowScaleDPI();
CORE.Window.screen.width = (unsigned int)(CORE.Window.screen.width*scaleDpi.x);
CORE.Window.screen.height = (unsigned int)(CORE.Window.screen.height*scaleDpi.y);
}
#endif
glfwSetWindowMonitor(platform.handle, NULL, CORE.Window.position.x, CORE.Window.position.y,
CORE.Window.screen.width, CORE.Window.screen.height, mode->refreshRate);
glfwFocusWindow(platform.handle);
FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
}
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
}
void MaximizeWindow(void)
{
if (glfwGetWindowAttrib(platform.handle, GLFW_RESIZABLE) == GLFW_TRUE)
{
glfwMaximizeWindow(platform.handle);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
}
}
void MinimizeWindow(void)
{
glfwIconifyWindow(platform.handle);
}
void RestoreWindow(void)
{
if (glfwGetWindowAttrib(platform.handle, GLFW_RESIZABLE) == GLFW_TRUE)
{
glfwRestoreWindow(platform.handle);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
}
}
void SetWindowState(unsigned int flags)
{
if (!CORE.Window.ready) TRACELOG(LOG_WARNING, "WINDOW: SetWindowState does nothing before window initialization, Use \"SetConfigFlags\" instead");
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT) != FLAG_IS_SET(flags, FLAG_VSYNC_HINT)) && FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
{
glfwSwapInterval(1);
FLAG_SET(CORE.Window.flags, FLAG_VSYNC_HINT);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE) != FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE)) && FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
{
ToggleBorderlessWindowed(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE) != FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE)) && FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
{
ToggleFullscreen(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE) != FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE)) && FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
{
glfwSetWindowAttrib(platform.handle, GLFW_RESIZABLE, GLFW_TRUE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED) != FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED)) && FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
{
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_FALSE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN) != FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN)) && FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
{
glfwHideWindow(platform.handle);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED) != FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED)) && FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
{
MinimizeWindow(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED) != FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED)) && FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
{
MaximizeWindow(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED) != FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED)) && FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
{
glfwSetWindowAttrib(platform.handle, GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST) != FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST)) && FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
{
glfwSetWindowAttrib(platform.handle, GLFW_FLOATING, GLFW_TRUE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN) != FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN)) && FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN))
{
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT) != FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT)) && FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
{
TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI) != FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI)) && FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
{
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH) != FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) && FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
{
glfwSetWindowAttrib(platform.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT) != FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT)) && FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
{
TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_INTERLACED_HINT) != FLAG_IS_SET(flags, FLAG_INTERLACED_HINT)) && FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
{
TRACELOG(LOG_WARNING, "WINDOW: Interlaced mode can only be configured before window initialization");
}
}
void ClearWindowState(unsigned int flags)
{
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) && (FLAG_IS_SET(flags, FLAG_VSYNC_HINT)))
{
glfwSwapInterval(0);
FLAG_CLEAR(CORE.Window.flags, FLAG_VSYNC_HINT);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE)) && (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE)))
{
ToggleBorderlessWindowed(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) && (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE)))
{
ToggleFullscreen(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) && (FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE)))
{
glfwSetWindowAttrib(platform.handle, GLFW_RESIZABLE, GLFW_FALSE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_RESIZABLE);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN)) && (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN)))
{
glfwShowWindow(platform.handle);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_HIDDEN);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED)))
{
RestoreWindow(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED)))
{
RestoreWindow(); }
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED)))
{
glfwSetWindowAttrib(platform.handle, GLFW_DECORATED, GLFW_TRUE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNDECORATED);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) && (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED)))
{
glfwSetWindowAttrib(platform.handle, GLFW_FOCUS_ON_SHOW, GLFW_TRUE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST)) && (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST)))
{
glfwSetWindowAttrib(platform.handle, GLFW_FLOATING, GLFW_FALSE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_TOPMOST);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN)) && (FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN)))
{
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) && (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT)))
{
TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)) && (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI)))
{
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) && (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)))
{
glfwSetWindowAttrib(platform.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_FALSE);
FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH);
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT)) && (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT)))
{
TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
}
if ((FLAG_IS_SET(CORE.Window.flags, FLAG_INTERLACED_HINT)) && (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT)))
{
TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
}
}
void SetWindowIcon(Image image)
{
if (image.data == NULL)
{
glfwSetWindowIcon(platform.handle, 0, NULL);
}
else
{
if (image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
{
GLFWimage icon[1] = { 0 };
icon[0].width = image.width;
icon[0].height = image.height;
icon[0].pixels = (unsigned char *)image.data;
glfwSetWindowIcon(platform.handle, 1, icon);
}
else TRACELOG(LOG_WARNING, "GLFW: Window icon image must be in R8G8B8A8 pixel format");
}
}
void SetWindowIcons(Image *images, int count)
{
if ((images == NULL) || (count <= 0))
{
glfwSetWindowIcon(platform.handle, 0, NULL);
}
else
{
int valid = 0;
GLFWimage *icons = (GLFWimage *)RL_CALLOC(count, sizeof(GLFWimage));
for (int i = 0; i < count; i++)
{
if (images[i].format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
{
icons[valid].width = images[i].width;
icons[valid].height = images[i].height;
icons[valid].pixels = (unsigned char *)images[i].data;
valid++;
}
else TRACELOG(LOG_WARNING, "GLFW: Window icon image must be in R8G8B8A8 pixel format");
}
glfwSetWindowIcon(platform.handle, valid, icons);
RL_FREE(icons);
}
}
void SetWindowTitle(const char *title)
{
CORE.Window.title = title;
glfwSetWindowTitle(platform.handle, title);
}
void SetWindowPosition(int x, int y)
{
CORE.Window.position.x = x;
CORE.Window.position.y = y;
glfwSetWindowPos(platform.handle, x, y);
}
void SetWindowMonitor(int monitor)
{
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
{
TRACELOG(LOG_INFO, "GLFW: Selected fullscreen monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
glfwSetWindowMonitor(platform.handle, monitors[monitor], 0, 0, mode->width, mode->height, mode->refreshRate);
}
else
{
TRACELOG(LOG_INFO, "GLFW: Selected monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
const int screenWidth = CORE.Window.render.width;
const int screenHeight = CORE.Window.render.height;
int monitorWorkareaX = 0;
int monitorWorkareaY = 0;
int monitorWorkareaWidth = 0;
int monitorWorkareaHeight = 0;
glfwGetMonitorWorkarea(monitors[monitor], &monitorWorkareaX, &monitorWorkareaY, &monitorWorkareaWidth, &monitorWorkareaHeight);
if ((screenWidth >= monitorWorkareaWidth) || (screenHeight >= monitorWorkareaHeight)) glfwSetWindowPos(platform.handle, monitorWorkareaX, monitorWorkareaY);
else
{
const int x = monitorWorkareaX + (monitorWorkareaWidth/2) - (screenWidth/2);
const int y = monitorWorkareaY + (monitorWorkareaHeight/2) - (screenHeight/2);
glfwSetWindowPos(platform.handle, x, y);
}
}
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
}
void SetWindowMinSize(int width, int height)
{
CORE.Window.screenMin.width = width;
CORE.Window.screenMin.height = height;
int minWidth = (CORE.Window.screenMin.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.width;
int minHeight = (CORE.Window.screenMin.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.height;
int maxWidth = (CORE.Window.screenMax.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.width;
int maxHeight = (CORE.Window.screenMax.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.height;
glfwSetWindowSizeLimits(platform.handle, minWidth, minHeight, maxWidth, maxHeight);
}
void SetWindowMaxSize(int width, int height)
{
CORE.Window.screenMax.width = width;
CORE.Window.screenMax.height = height;
int minWidth = (CORE.Window.screenMin.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.width;
int minHeight = (CORE.Window.screenMin.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMin.height;
int maxWidth = (CORE.Window.screenMax.width == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.width;
int maxHeight = (CORE.Window.screenMax.height == 0)? GLFW_DONT_CARE : (int)CORE.Window.screenMax.height;
glfwSetWindowSizeLimits(platform.handle, minWidth, minHeight, maxWidth, maxHeight);
}
void SetWindowSize(int width, int height)
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
glfwSetWindowSize(platform.handle, width, height);
}
void SetWindowOpacity(float opacity)
{
if (opacity >= 1.0f) opacity = 1.0f;
else if (opacity <= 0.0f) opacity = 0.0f;
glfwSetWindowOpacity(platform.handle, opacity);
}
void SetWindowFocused(void)
{
glfwFocusWindow(platform.handle);
}
#if defined(__linux__) && defined(_GLFW_X11)
static XID X11WindowHandle;
#endif
void *GetWindowHandle(void)
{
#if defined(_WIN32)
return glfwGetWin32Window(platform.handle);
#endif
#if defined(__linux__)
#if defined(_GLFW_WAYLAND)
#if defined(_GLFW_X11)
int platformID = glfwGetPlatform();
if (platformID == GLFW_PLATFORM_WAYLAND)
{
return glfwGetWaylandWindow(platform.handle);
}
else
{
X11WindowHandle = glfwGetX11Window(platform.handle);
return &X11WindowHandle;
}
#else
return glfwGetWaylandWindow(platform.handle);
#endif
#elif defined(_GLFW_X11)
X11WindowHandle = glfwGetX11Window(platform.handle);
return &X11WindowHandle;
#endif
#endif
#if defined(__APPLE__)
return (void *)glfwGetCocoaWindow(platform.handle);
#endif
return NULL;
}
int GetMonitorCount(void)
{
int monitorCount = 0;
glfwGetMonitors(&monitorCount);
return monitorCount;
}
int GetCurrentMonitor(void)
{
int index = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
GLFWmonitor *monitor = NULL;
if (monitorCount >= 1)
{
if (IsWindowFullscreen())
{
monitor = glfwGetWindowMonitor(platform.handle);
for (int i = 0; i < monitorCount; i++)
{
if (monitors[i] == monitor)
{
index = i;
break;
}
}
}
else
{
int closestDist = 0x7FFFFFFF;
int wcx = 0;
int wcy = 0;
glfwGetWindowPos(platform.handle, &wcx, &wcy);
wcx += (int)CORE.Window.screen.width/2;
wcy += (int)CORE.Window.screen.height/2;
for (int i = 0; i < monitorCount; i++)
{
int mx = 0;
int my = 0;
monitor = monitors[i];
glfwGetMonitorPos(monitor, &mx, &my);
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
if (mode)
{
const int right = mx + mode->width - 1;
const int bottom = my + mode->height - 1;
if ((wcx >= mx) &&
(wcx <= right) &&
(wcy >= my) &&
(wcy <= bottom))
{
index = i;
break;
}
int xclosest = wcx;
if (wcx < mx) xclosest = mx;
else if (wcx > right) xclosest = right;
int yclosest = wcy;
if (wcy < my) yclosest = my;
else if (wcy > bottom) yclosest = bottom;
int dx = wcx - xclosest;
int dy = wcy - yclosest;
int dist = (dx*dx) + (dy*dy);
if (dist < closestDist)
{
index = i;
closestDist = dist;
}
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}
}
}
return index;
}
Vector2 GetMonitorPosition(int monitor)
{
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
int x = 0;
int y = 0;
glfwGetMonitorPos(monitors[monitor], &x, &y);
return (Vector2){ (float)x, (float)y };
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return (Vector2){ 0, 0 };
}
int GetMonitorWidth(int monitor)
{
int width = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
if (mode) width = mode->width;
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return width;
}
int GetMonitorHeight(int monitor)
{
int height = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
if (mode) height = mode->height;
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return height;
}
int GetMonitorPhysicalWidth(int monitor)
{
int width = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount)) glfwGetMonitorPhysicalSize(monitors[monitor], &width, NULL);
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return width;
}
int GetMonitorPhysicalHeight(int monitor)
{
int height = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount)) glfwGetMonitorPhysicalSize(monitors[monitor], NULL, &height);
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return height;
}
int GetMonitorRefreshRate(int monitor)
{
int refresh = 0;
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
if (mode) refresh = mode->refreshRate;
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return refresh;
}
const char *GetMonitorName(int monitor)
{
int monitorCount = 0;
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if ((monitor >= 0) && (monitor < monitorCount))
{
return glfwGetMonitorName(monitors[monitor]);
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
return "";
}
Vector2 GetWindowPosition(void)
{
return (Vector2){ (float)CORE.Window.position.x, (float)CORE.Window.position.y };
}
Vector2 GetWindowScaleDPI(void)
{
Vector2 scale = { 1.0f, 1.0f };
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI) && !FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
glfwGetWindowContentScale(platform.handle, &scale.x, &scale.y);
return scale;
}
void SetClipboardText(const char *text)
{
glfwSetClipboardString(platform.handle, text);
}
const char *GetClipboardText(void)
{
return glfwGetClipboardString(platform.handle);
}
#if SUPPORT_CLIPBOARD_IMAGE && defined(__linux__) && defined(_GLFW_X11)
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif
Image GetClipboardImage(void)
{
Image image = { 0 };
#if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES
#if defined(_WIN32)
unsigned long long int dataSize = 0;
void *bmpData = NULL;
int width = 0;
int height = 0;
bmpData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize);
if (bmpData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data.");
else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize);
#elif defined(__linux__) && defined(_GLFW_X11)
Display *dpy = XOpenDisplay(NULL);
if (!dpy) return image;
Window root = DefaultRootWindow(dpy);
Window win = XCreateSimpleWindow(
dpy, root, 0, 0, 1, 1, 0, 0, 0 );
Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
Atom targetType = XInternAtom(dpy, "image/png", False); Atom property = XInternAtom(dpy, "RAYLIB_CLIPBOARD_MANAGER", False);
XConvertSelection(dpy, clipboard, targetType, property, win, CurrentTime);
XEvent ev = { 0 };
XNextEvent(dpy, &ev);
Atom actualType = { 0 };
int actualFormat = 0;
unsigned long nitems = 0;
unsigned long bytesAfter = 0;
unsigned char *data = NULL;
XGetWindowProperty(dpy, win, property, 0, ~0L, False, AnyPropertyType,
&actualType, &actualFormat, &nitems, &bytesAfter, &data);
if (data != NULL)
{
image = LoadImageFromMemory(".png", data, (int)nitems);
XFree(data);
}
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
#else
TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
#endif #else
TRACELOG(LOG_WARNING, "Clipboard image: SUPPORT_CLIPBOARD_IMAGE requires SUPPORT_MODULE_RTEXTURES to work properly");
#endif
return image;
}
void ShowCursor(void)
{
glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
CORE.Input.Mouse.cursorHidden = false;
}
void HideCursor(void)
{
glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
CORE.Input.Mouse.cursorHidden = true;
}
void EnableCursor(void)
{
glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
if (glfwRawMouseMotionSupported()) glfwSetInputMode(platform.handle, GLFW_RAW_MOUSE_MOTION, GLFW_FALSE);
CORE.Input.Mouse.cursorHidden = false;
CORE.Input.Mouse.cursorLocked = false;
}
void DisableCursor(void)
{
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
glfwSetInputMode(platform.handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glfwRawMouseMotionSupported()) glfwSetInputMode(platform.handle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
CORE.Input.Mouse.cursorHidden = true;
CORE.Input.Mouse.cursorLocked = true;
}
void SwapScreenBuffer(void)
{
glfwSwapBuffers(platform.handle);
}
double GetTime(void)
{
double time = glfwGetTime();
return time;
}
void OpenURL(const char *url)
{
if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
else
{
char *cmd = (char *)RL_CALLOC(strlen(url) + 32, sizeof(char));
#if defined(_WIN32)
sprintf(cmd, "explorer \"%s\"", url);
#endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
sprintf(cmd, "xdg-open '%s'", url); #endif
#if defined(__APPLE__)
sprintf(cmd, "open '%s'", url);
#endif
int result = system(cmd);
if (result == -1) TRACELOG(LOG_WARNING, "OpenURL() child process could not be created");
RL_FREE(cmd);
}
}
int SetGamepadMappings(const char *mappings)
{
return glfwUpdateGamepadMappings(mappings);
}
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "SetGamepadVibration() not available on target platform");
}
void SetMousePosition(int x, int y)
{
CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y);
}
void SetMouseCursor(int cursor)
{
CORE.Input.Mouse.cursor = cursor;
if (cursor == MOUSE_CURSOR_DEFAULT) glfwSetCursor(platform.handle, NULL);
else
{
glfwSetCursor(platform.handle, glfwCreateStandardCursor(0x00036000 + cursor));
}
}
const char *GetKeyName(int key)
{
return glfwGetKeyName(key, glfwGetKeyScancode(key));
}
void PollInputEvents(void)
{
#if SUPPORT_GESTURES_SYSTEM
UpdateGestures();
#endif
CORE.Input.Keyboard.keyPressedQueueCount = 0;
CORE.Input.Keyboard.charPressedQueueCount = 0;
CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_UNKNOWN;
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
{
CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
}
for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f };
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
for (int i = 0; i < MAX_GAMEPADS; i++)
{
if (glfwJoystickPresent(i)) CORE.Input.Gamepad.ready[i] = true;
else CORE.Input.Gamepad.ready[i] = false;
}
for (int i = 0; i < MAX_GAMEPADS; i++)
{
if (CORE.Input.Gamepad.ready[i]) {
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
GLFWgamepadstate state = { 0 };
int result = glfwGetGamepadState(i, &state); if (result == GLFW_FALSE) {
state.axes[GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
state.axes[GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
}
const unsigned char *buttons = state.buttons;
for (int k = 0; (buttons != NULL) && (k < MAX_GAMEPAD_BUTTONS); k++)
{
int button = -1;
switch (k)
{
case GLFW_GAMEPAD_BUTTON_Y: button = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
case GLFW_GAMEPAD_BUTTON_B: button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_A: button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
case GLFW_GAMEPAD_BUTTON_X: button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: button = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
case GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
case GLFW_GAMEPAD_BUTTON_BACK: button = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_GUIDE: button = GAMEPAD_BUTTON_MIDDLE; break;
case GLFW_GAMEPAD_BUTTON_START: button = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_DPAD_UP: button = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
case GLFW_GAMEPAD_BUTTON_DPAD_RIGHT: button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
case GLFW_GAMEPAD_BUTTON_DPAD_DOWN: button = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
case GLFW_GAMEPAD_BUTTON_DPAD_LEFT: button = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
case GLFW_GAMEPAD_BUTTON_LEFT_THUMB: button = GAMEPAD_BUTTON_LEFT_THUMB; break;
case GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: button = GAMEPAD_BUTTON_RIGHT_THUMB; break;
default: break;
}
if (button != -1) {
if (buttons[k] == GLFW_PRESS)
{
CORE.Input.Gamepad.currentButtonState[i][button] = 1;
CORE.Input.Gamepad.lastButtonPressed = button;
}
else CORE.Input.Gamepad.currentButtonState[i][button] = 0;
}
}
const float *axes = state.axes;
for (int k = 0; (axes != NULL) && (k < GLFW_GAMEPAD_AXIS_LAST + 1); k++)
{
CORE.Input.Gamepad.axisState[i][k] = axes[k];
}
if (CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1f)
{
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = 1;
CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_LEFT_TRIGGER_2;
}
else CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = 0;
if (CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1f)
{
CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = 1;
CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_RIGHT_TRIGGER_2;
}
else CORE.Input.Gamepad.currentButtonState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = 0;
CORE.Input.Gamepad.axisCount[i] = GLFW_GAMEPAD_AXIS_LAST + 1;
}
}
CORE.Window.resizedLastFrame = false;
if ((CORE.Window.eventWaiting) ||
(FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED) && !FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN)))
{
glfwWaitEvents(); CORE.Time.previous = GetTime();
}
else glfwPollEvents();
CORE.Window.shouldClose = glfwWindowShouldClose(platform.handle);
glfwSetWindowShouldClose(platform.handle, GLFW_FALSE);
}
static void *AllocateWrapper(size_t size, void *user)
{
(void)user;
return RL_CALLOC(size, 1);
}
static void *ReallocateWrapper(void *block, size_t size, void *user)
{
(void)user;
return RL_REALLOC(block, size);
}
static void DeallocateWrapper(void *block, void *user)
{
(void)user;
RL_FREE(block);
}
int InitPlatform(void)
{
glfwSetErrorCallback(ErrorCallback);
const GLFWallocator allocator = {
.allocate = AllocateWrapper,
.reallocate = ReallocateWrapper,
.deallocate = DeallocateWrapper,
.user = NULL, };
glfwInitAllocator(&allocator);
#if defined(__APPLE__)
glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
#endif
int result = glfwInit();
if (result == GLFW_FALSE) { TRACELOG(LOG_WARNING, "GLFW: Failed to initialize GLFW"); return -1; }
glfwDefaultWindowHints();
glfwWindowHint(GLFW_AUTO_ICONIFY, 0);
unsigned int requestedWindowFlags = CORE.Window.flags;
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN)) glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); else glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED)) glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); else glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE);
else glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TOPMOST)) glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
else glfwWindowHint(GLFW_FLOATING, GLFW_FALSE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); else glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
#if defined(__APPLE__)
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
#endif
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
#if defined(__APPLE__)
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
#endif
}
else
{
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
#if defined(__APPLE__)
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
#endif
#if defined(_GLFW_WAYLAND) && !defined(_GLFW_X11)
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
#endif
}
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MOUSE_PASSTHROUGH)) glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
else glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_FALSE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT))
{
TRACELOG(LOG_INFO, "DISPLAY: Trying to enable MSAA x4");
glfwWindowHint(GLFW_SAMPLES, 4); }
if (rlGetVersion() == RL_OPENGL_21)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); }
else if (rlGetVersion() == RL_OPENGL_33)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #if defined(__APPLE__)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); #else
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE); #endif
}
else if (rlGetVersion() == RL_OPENGL_43)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE);
#if RLGL_ENABLE_OPENGL_DEBUG_CONTEXT
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); #endif
}
else if (rlGetVersion() == RL_OPENGL_ES_20) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
}
else if (rlGetVersion() == RL_OPENGL_ES_30) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
}
glfwSetJoystickCallback(NULL);
if ((CORE.Window.screen.width == 0) || (CORE.Window.screen.height == 0)) FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
{
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
if (!monitor)
{
TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
return -1;
}
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
CORE.Window.display.width = mode->width;
CORE.Window.display.height = mode->height;
if ((CORE.Window.screen.width == 0) || (CORE.Window.screen.height == 0))
{
CORE.Window.previousScreen.width = 800;
CORE.Window.previousScreen.height = 450;
CORE.Window.previousPosition.x = CORE.Window.display.width/2 - 800/2;
CORE.Window.previousPosition.y = CORE.Window.display.height/2 - 450/2;
if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
}
else
{
CORE.Window.previousScreen = CORE.Window.screen;
CORE.Window.screen = CORE.Window.display;
}
platform.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", monitor, NULL);
if (!platform.handle)
{
glfwTerminate();
TRACELOG(LOG_WARNING, "GLFW: Failed to initialize Window");
return -1;
}
}
else
{
if (CORE.Window.screen.width == 0) CORE.Window.screen.width = 1;
if (CORE.Window.screen.height == 0) CORE.Window.screen.height = 1;
platform.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", NULL, NULL);
if (!platform.handle)
{
glfwTerminate();
TRACELOG(LOG_WARNING, "GLFW: Failed to initialize Window");
return -1;
}
int monitorCount = 0;
int monitorIndex = GetCurrentMonitor();
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
if (monitorIndex < monitorCount)
{
GLFWmonitor *monitor = monitors[monitorIndex];
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
CORE.Window.display.width = mode->width;
CORE.Window.display.height = mode->height;
if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
glfwSetWindowSize(platform.handle, CORE.Window.screen.width, CORE.Window.screen.height);
}
else
{
glfwTerminate();
TRACELOG(LOG_WARNING, "GLFW: Failed to determine Monitor to center Window");
return -1;
}
CORE.Window.render.width = CORE.Window.screen.width;
CORE.Window.render.height = CORE.Window.screen.height;
}
glfwMakeContextCurrent(platform.handle);
result = glfwGetError(NULL);
if ((result != GLFW_NO_WINDOW_CONTEXT) && (result != GLFW_PLATFORM_ERROR)) CORE.Window.ready = true;
if (CORE.Window.ready)
{
glfwSwapInterval(0);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT))
{
glfwSwapInterval(1);
TRACELOG(LOG_INFO, "DISPLAY: Trying to enable VSYNC");
}
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
Vector2 scaleDpi = GetWindowScaleDPI();
CORE.Window.render.width = (int)(CORE.Window.screen.width*scaleDpi.x);
CORE.Window.render.height = (int)(CORE.Window.screen.height*scaleDpi.y);
CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
#if !defined(__APPLE__)
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
{
int fbWidth = 0;
int fbHeight = 0;
glfwGetFramebufferSize(platform.handle, &fbWidth, &fbHeight);
CORE.Window.render.width = fbWidth;
CORE.Window.render.height = fbHeight;
}
else
{
SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
glfwSetWindowSize(platform.handle, CORE.Window.render.width, CORE.Window.render.height);
}
#endif
}
else CORE.Window.render = CORE.Window.screen;
CORE.Window.currentFbo = CORE.Window.render;
TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully %s",
FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)? "(HighDPI)" : "");
TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
int monitorCount = 0;
int monitorIndex = GetCurrentMonitor();
GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
GLFWmonitor *monitor = monitors[monitorIndex];
int monitorX = 0;
int monitorY = 0;
int monitorWidth = 0;
int monitorHeight = 0;
glfwGetMonitorWorkarea(monitor, &monitorX, &monitorY, &monitorWidth, &monitorHeight);
#if defined(__APPLE__)
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.screen.width)/2;
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.screen.height)/2;
#else
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.render.width)/2;
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.render.height)/2;
#endif
SetWindowPosition(CORE.Window.position.x, CORE.Window.position.y);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) MinimizeWindow();
}
else
{
TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
return -1;
}
SetWindowState(requestedWindowFlags);
rlLoadExtensions(glfwGetProcAddress);
glfwSetWindowSizeCallback(platform.handle, WindowSizeCallback); glfwSetFramebufferSizeCallback(platform.handle, FramebufferSizeCallback);
glfwSetWindowPosCallback(platform.handle, WindowPosCallback);
glfwSetWindowMaximizeCallback(platform.handle, WindowMaximizeCallback);
glfwSetWindowIconifyCallback(platform.handle, WindowIconifyCallback);
glfwSetWindowFocusCallback(platform.handle, WindowFocusCallback);
glfwSetDropCallback(platform.handle, WindowDropCallback);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)) glfwSetWindowContentScaleCallback(platform.handle, WindowContentScaleCallback);
glfwSetKeyCallback(platform.handle, KeyCallback);
glfwSetCharCallback(platform.handle, CharCallback);
glfwSetMouseButtonCallback(platform.handle, MouseButtonCallback);
glfwSetCursorPosCallback(platform.handle, MouseCursorPosCallback); glfwSetScrollCallback(platform.handle, MouseScrollCallback);
glfwSetCursorEnterCallback(platform.handle, CursorEnterCallback);
glfwSetJoystickCallback(JoystickCallback);
glfwSetInputMode(platform.handle, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
for (int i = 0; i < MAX_GAMEPADS; i++)
{
if (glfwJoystickPresent(i))
{
CORE.Input.Gamepad.ready[i] = true;
CORE.Input.Gamepad.axisCount[i] = GLFW_GAMEPAD_AXIS_LAST + 1;
strncpy(CORE.Input.Gamepad.name[i], glfwGetJoystickName(i), MAX_GAMEPAD_NAME_LENGTH - 1);
}
}
InitTimer();
CORE.Storage.basePath = GetWorkingDirectory();
#if defined(__NetBSD__)
char *glfwPlatform = "X11 (NetBSD)";
#else
char *glfwPlatform = "";
switch (glfwGetPlatform())
{
case GLFW_PLATFORM_WIN32: glfwPlatform = "Win32"; break;
case GLFW_PLATFORM_COCOA: glfwPlatform = "Cocoa"; break;
case GLFW_PLATFORM_WAYLAND: glfwPlatform = "Wayland"; break;
case GLFW_PLATFORM_X11: glfwPlatform = "X11"; break;
case GLFW_PLATFORM_NULL: glfwPlatform = "Null"; break;
default: break;
}
#endif
TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (GLFW - %s): Initialized successfully", glfwPlatform);
return 0;
}
void ClosePlatform(void)
{
glfwDestroyWindow(platform.handle);
glfwTerminate();
#if defined(_WIN32) && SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
timeEndPeriod(1); #endif
}
static void ErrorCallback(int error, const char *description)
{
TRACELOG(LOG_WARNING, "GLFW: Error: %i Description: %s", error, description);
}
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
{
}
static void FramebufferSizeCallback(GLFWwindow *window, int width, int height)
{
if ((width == 0) || (height == 0)) return;
SetupViewport(width, height);
CORE.Window.render.width = width;
CORE.Window.render.height = height;
CORE.Window.currentFbo = CORE.Window.render;
CORE.Window.resizedLastFrame = true;
if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
CORE.Window.screenScale = MatrixScale(1.0f, 1.0f, 1.0f);
SetMouseScale(1.0f, 1.0f);
#if defined(_GLFW_WAYLAND) && !defined(_GLFW_X11)
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
int winWidth = 0;
int winHeight = 0;
glfwGetWindowSize(platform.handle, &winWidth, &winHeight);
if ((winWidth != width) || (winHeight != height))
{
CORE.Window.screen.width = winWidth;
CORE.Window.screen.height = winHeight;
float scaleX = (float)width/winWidth;
float scaleY = (float)height/winHeight;
CORE.Window.screenScale = MatrixScale(scaleX, scaleY, 1.0f);
}
}
#endif
}
else {
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
{
Vector2 scaleDpi = GetWindowScaleDPI();
CORE.Window.screen.width = (int)((float)width/scaleDpi.x);
CORE.Window.screen.height = (int)((float)height/scaleDpi.y);
CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
#if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
#endif
}
else
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
}
}
}
static void WindowContentScaleCallback(GLFWwindow *window, float scalex, float scaley)
{
CORE.Window.render.width = (int)((float)CORE.Window.screen.width*scalex);
CORE.Window.render.height = (int)((float)CORE.Window.screen.height*scaley);
CORE.Window.currentFbo = CORE.Window.render;
CORE.Window.screenScale = MatrixScale(scalex, scaley, 1.0f);
#if !defined(__APPLE__) && !defined(_GLFW_WAYLAND)
SetMouseScale(1.0f/scalex, 1.0f/scaley);
#endif
}
static void WindowPosCallback(GLFWwindow *window, int x, int y)
{
CORE.Window.position.x = x;
CORE.Window.position.y = y;
}
static void WindowIconifyCallback(GLFWwindow *window, int iconified)
{
if (iconified) FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); else FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); }
static void WindowMaximizeCallback(GLFWwindow *window, int maximized)
{
if (maximized) FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); else FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); }
static void WindowFocusCallback(GLFWwindow *window, int focused)
{
if (focused) FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); else FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED); }
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
{
if (count > 0)
{
if (CORE.Window.dropFileCount > 0)
{
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
RL_FREE(CORE.Window.dropFilepaths);
CORE.Window.dropFileCount = 0;
CORE.Window.dropFilepaths = NULL;
}
CORE.Window.dropFileCount = count;
CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
{
CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1);
}
}
}
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key < 0) return;
if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
else if (action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
else if (action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
if (((key == KEY_CAPS_LOCK) && (FLAG_IS_SET(mods, GLFW_MOD_CAPS_LOCK))) ||
((key == KEY_NUM_LOCK) && (FLAG_IS_SET(mods, GLFW_MOD_NUM_LOCK)))) CORE.Input.Keyboard.currentKeyState[key] = 1;
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
{
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
CORE.Input.Keyboard.keyPressedQueueCount++;
}
if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(platform.handle, GLFW_TRUE);
}
static void CharCallback(GLFWwindow *window, unsigned int codepoint)
{
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
{
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = codepoint;
CORE.Input.Keyboard.charPressedQueueCount++;
}
}
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
CORE.Input.Mouse.currentButtonState[button] = action;
CORE.Input.Touch.currentTouchState[button] = action;
#if SUPPORT_GESTURES_SYSTEM && SUPPORT_MOUSE_GESTURES
GestureEvent gestureEvent = { 0 };
if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
gestureEvent.pointId[0] = 0;
gestureEvent.pointCount = 1;
gestureEvent.position[0] = GetMousePosition();
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
ProcessGestureEvent(gestureEvent);
#endif
}
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{
CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
#if SUPPORT_GESTURES_SYSTEM && SUPPORT_MOUSE_GESTURES
GestureEvent gestureEvent = { 0 };
gestureEvent.touchAction = TOUCH_ACTION_MOVE;
gestureEvent.pointId[0] = 0;
gestureEvent.pointCount = 1;
gestureEvent.position[0] = CORE.Input.Touch.position[0];
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
ProcessGestureEvent(gestureEvent);
#endif
}
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset };
}
static void CursorEnterCallback(GLFWwindow *window, int enter)
{
if (enter) CORE.Input.Mouse.cursorOnScreen = true;
else CORE.Input.Mouse.cursorOnScreen = false;
}
static void JoystickCallback(int jid, int event)
{
if (jid < MAX_GAMEPADS)
{
if (event == GLFW_CONNECTED)
{
memset(CORE.Input.Gamepad.name[jid], 0, MAX_GAMEPAD_NAME_LENGTH);
strncpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid), MAX_GAMEPAD_NAME_LENGTH - 1);
}
else if (event == GLFW_DISCONNECTED)
{
memset(CORE.Input.Gamepad.name[jid], 0, MAX_GAMEPAD_NAME_LENGTH);
}
}
}
#ifdef _WIN32
# define WIN32_CLIPBOARD_IMPLEMENTATION
# include "../external/win32_clipboard.h"
#endif