Last active
September 6, 2025 21:20
-
-
Save EngineerSmith/f65e06120c9c746ac745ddc647f35b22 to your computer and use it in GitHub Desktop.
Add custom hitTest SDL via ffi calls - for love2d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| love.conf = function(t) | |
| t.window.borderless = true | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local ffi = require("ffi") | |
| ffi.cdef[[ | |
| typedef struct SDL_Window SDL_Window; // https://wiki.libsdl.org/SDL2/SDL_Window | |
| typedef enum { | |
| SDL_HITTEST_NORMAL, | |
| SDL_HITTEST_DRAGGABLE, | |
| SDL_HITTEST_RESIZE_TOPLEFT, | |
| SDL_HITTEST_RESIZE_TOP, | |
| SDL_HITTEST_RESIZE_TOPRIGHT, | |
| SDL_HITTEST_RESIZE_RIGHT, | |
| SDL_HITTEST_RESIZE_BOTTOMRIGHT, | |
| SDL_HITTEST_RESIZE_BOTTOM, | |
| SDL_HITTEST_RESIZE_BOTTOMLEFT, | |
| SDL_HITTEST_RESIZE_LEFT | |
| } SDL_HitTestResult; // https://wiki.libsdl.org/SDL2/SDL_HitTestResult | |
| typedef struct SDL_Point { | |
| int x; | |
| int y; | |
| } SDL_Point; // https://wiki.libsdl.org/SDL2/SDL_ | |
| typedef SDL_HitTestResult (__cdecl *SDL_HitTest)( | |
| SDL_Window *win, | |
| const SDL_Point *area, | |
| void* data); // https://wiki.libsdl.org/SDL2/SDL_HitTest | |
| int SDL_SetWindowHitTest(SDL_Window *win, SDL_HitTest callback, void *callback_data); // https://wiki.libsdl.org/SDL2/SDL_SetWindowHitTest | |
| SDL_Window* SDL_GL_GetCurrentWindow(void); // https://wiki.libsdl.org/SDL2/SDL_GL_GetCurrentWindow | |
| ]] | |
| local sdl2 = ffi.load("SDL2") | |
| local win = sdl2.SDL_GL_GetCurrentWindow(); | |
| local result = sdl2.SDL_SetWindowHitTest(win, function(win, area, data) | |
| -- Note, this function will be called for EVERY mouse hit, keep it simple. | |
| -- You may want to implement DPI scaling, unless it's a personal project that doesn't need it. | |
| if area.y <= 50 and area.x <= 50 then | |
| return sdl2.SDL_HITTEST_DRAGGABLE | |
| end | |
| return sdl2.SDL_HITTEST_NORMAL | |
| end, nil) | |
| if result ~= 0 then | |
| -- fall back | |
| local w, h, mode = love.window.getMode() | |
| mode.borderless = true | |
| love.window.setMode(w, h, mode) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment