|
86 | 86 | #include "GLFW/glfw3native.h" // Required for: glfwGetCocoaWindow()
|
87 | 87 | #endif
|
88 | 88 |
|
| 89 | +#include <stddef.h> // Required for: size_t |
| 90 | + |
89 | 91 | //----------------------------------------------------------------------------------
|
90 | 92 | // Types and Structures Definition
|
91 | 93 | //----------------------------------------------------------------------------------
|
@@ -127,6 +129,11 @@ static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffs
|
127 | 129 | static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
|
128 | 130 | static void JoystickCallback(int jid, int event); // GLFW3 Joystick Connected/Disconnected Callback
|
129 | 131 |
|
| 132 | +// Wrappers used by glfwInitAllocator |
| 133 | +static void* AllocateWrapper(size_t size, void* user); // GLFW3 GLFWallocatefun, wrapps around RL_MALLOC macro |
| 134 | +static void* ReallocateWrapper(void* block, size_t size, void* user); // GLFW3 GLFWreallocatefun, wrapps around RL_MALLOC macro |
| 135 | +static void DeallocateWrapper(void* block, void* user); // GLFW3 GLFWdeallocatefun, wraps around RL_FREE macro |
| 136 | + |
130 | 137 | //----------------------------------------------------------------------------------
|
131 | 138 | // Module Functions Declaration
|
132 | 139 | //----------------------------------------------------------------------------------
|
@@ -1287,21 +1294,38 @@ static void SetDimensionsFromMonitor(GLFWmonitor *monitor)
|
1287 | 1294 | if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
|
1288 | 1295 | }
|
1289 | 1296 |
|
| 1297 | +// Function wrappers around RL_*alloc macros, used by glfwInitAllocator() inside of InitPlatform() |
| 1298 | +// We need to provide these because GLFWallocator expects function pointers with specific signatures. |
| 1299 | +// Similar wrappers exist in utils.c but we cannot reuse them here due to declaration mismatch. |
| 1300 | +// https://www.glfw.org/docs/latest/intro_guide.html#init_allocator |
| 1301 | +static void* AllocateWrapper(size_t size, void* user) |
| 1302 | +{ |
| 1303 | + (void)user; |
| 1304 | + return RL_MALLOC(size); |
| 1305 | +} |
| 1306 | +static void* ReallocateWrapper(void* block, size_t size, void* user) |
| 1307 | +{ |
| 1308 | + (void)user; |
| 1309 | + return RL_REALLOC(block, size); |
| 1310 | +} |
| 1311 | +static void DeallocateWrapper(void* block, void* user) |
| 1312 | +{ |
| 1313 | + (void)user; |
| 1314 | + RL_FREE(block); |
| 1315 | +} |
| 1316 | + |
1290 | 1317 | // Initialize platform: graphics, inputs and more
|
1291 | 1318 | int InitPlatform(void)
|
1292 | 1319 | {
|
1293 | 1320 | glfwSetErrorCallback(ErrorCallback);
|
1294 |
| -/* |
1295 |
| - // TODO: Setup GLFW custom allocators to match raylib ones |
| 1321 | + |
1296 | 1322 | const GLFWallocator allocator = {
|
1297 |
| - .allocate = MemAlloc, |
1298 |
| - .deallocate = MemFree, |
1299 |
| - .reallocate = MemRealloc, |
1300 |
| - .user = NULL |
| 1323 | + .allocate = AllocateWrapper, |
| 1324 | + .deallocate = DeallocateWrapper, |
| 1325 | + .reallocate = ReallocateWrapper, |
| 1326 | + .user = NULL, // RL_*ALLOC macros are not capable of handling user-provided data |
1301 | 1327 | };
|
1302 |
| -
|
1303 | 1328 | glfwInitAllocator(&allocator);
|
1304 |
| -*/ |
1305 | 1329 |
|
1306 | 1330 | #if defined(__APPLE__)
|
1307 | 1331 | glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
|
|
0 commit comments