From f644e2dcef8359472b00d8f7591087d8b31b5206 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 25 Nov 2024 15:54:40 +0000 Subject: [PATCH] library: gamewindowsize + gameviewxywh --- src/library/doc.texi | 28 ++++++++++++++++++++++++++++ src/library/plugin/plugin.c | 20 +++++++++++++++++--- src/library/plugin/plugin.h | 4 ++++ src/library/plugin/plugin_api.c | 21 +++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/library/doc.texi b/src/library/doc.texi index 5daf0ff..a9445c0 100644 --- a/src/library/doc.texi +++ b/src/library/doc.texi @@ -297,6 +297,34 @@ end @end verbatim @end example +@node functions-gamewindowsize +@section gamewindowsize + +Returns the last known width and height of the game window, in pixels. +Specifically this refers to the inner "graphical" area of the window, +excluding any OS-level decorations like borders or title bars. + +@example lua +@verbatim +local width, height = bolt.gamewindowsize() +@end verbatim +@end example + +@node functions-gameviewxywh +@section gameviewxywh + +Returns the last known x, y, width and height of the game view inside +the game window, in pixels. The game view is the area where 3D rendering +of the game world takes place, which by default takes up the entire +window, but its position can be configured by the user in the game's +layout settings. + +@example lua +@verbatim +local x, y, width, height = bolt.gameviewxywh() +@end verbatim +@end example + @node functions-loadfile @section loadfile diff --git a/src/library/plugin/plugin.c b/src/library/plugin/plugin.c index cbe21a4..ccf3e43 100644 --- a/src/library/plugin/plugin.c +++ b/src/library/plugin/plugin.c @@ -113,6 +113,7 @@ static struct hashmap* plugins; #define DEFINE_CALLBACK(APINAME, STRUCTNAME) \ void _bolt_plugin_handle_##APINAME(struct STRUCTNAME* e) { \ + if (!overlay_inited) return; \ size_t iter = 0; \ void* item; \ while (hashmap_iter(plugins, &iter, &item)) { \ @@ -634,10 +635,16 @@ static void _bolt_process_captures(uint32_t window_width, uint32_t window_height } void _bolt_plugin_end_frame(uint32_t window_width, uint32_t window_height) { + const uint8_t wh_valid = window_width > 1 && window_height > 1; if (window_width != overlay_width || window_height != overlay_height) { if (overlay_inited) { - managed_functions.surface_resize_and_clear(overlay.userdata, window_width, window_height); - } else { + if (wh_valid) { + managed_functions.surface_resize_and_clear(overlay.userdata, window_width, window_height); + } else { + managed_functions.surface_destroy(overlay.userdata); + overlay_inited = false; + } + } else if (wh_valid) { managed_functions.surface_init(&overlay, window_width, window_height, NULL); overlay_inited = true; } @@ -645,9 +652,11 @@ void _bolt_plugin_end_frame(uint32_t window_width, uint32_t window_height) { overlay_height = window_height; } + _bolt_plugin_handle_messages(); + if (!overlay_inited) return; + uint8_t need_capture = false; uint8_t capture_ready = true; - _bolt_plugin_handle_messages(); _bolt_process_embedded_windows(window_width, window_height, &need_capture, &capture_ready); _bolt_process_plugins(&need_capture, &capture_ready); @@ -1285,3 +1294,8 @@ void _bolt_plugin_draw_to_overlay(const struct SurfaceFunctions* from, int sx, i from->draw_to_surface(from->userdata, overlay.userdata, sx, sy, sw, sh, dx, dy, dw, dh); } } + +void _bolt_plugin_overlay_size(int* w, int* h) { + *w = overlay_width; + *h = overlay_height; +} diff --git a/src/library/plugin/plugin.h b/src/library/plugin/plugin.h index f3b45d5..1679c6c 100644 --- a/src/library/plugin/plugin.h +++ b/src/library/plugin/plugin.h @@ -391,4 +391,8 @@ BoltSocketType _bolt_plugin_fd(); /// if the overlay is initialised. If not, it does nothing. void _bolt_plugin_draw_to_overlay(const struct SurfaceFunctions*, int, int, int, int, int, int, int, int); +/// Gets the width and height of the internal overlay, which will also be the last known size of +/// the client area of the game window. +void _bolt_plugin_overlay_size(int* w, int* h); + #endif diff --git a/src/library/plugin/plugin_api.c b/src/library/plugin/plugin_api.c index 39f0588..24e2709 100644 --- a/src/library/plugin/plugin_api.c +++ b/src/library/plugin/plugin_api.c @@ -247,6 +247,25 @@ static int api_weekday(lua_State* state) { return 1; } +static int api_gamewindowsize(lua_State* state) { + int w, h; + _bolt_plugin_overlay_size(&w, &h); + lua_pushinteger(state, w); + lua_pushinteger(state, h); + return 2; +} + +static int api_gameviewxywh(lua_State* state) { + int x, y, w, h; + const struct PluginManagedFunctions* managed_functions = _bolt_plugin_managed_functions(); + managed_functions->game_view_rect(&x, &y, &w, &h); + lua_pushinteger(state, x); + lua_pushinteger(state, y); + lua_pushinteger(state, w); + lua_pushinteger(state, h); + return 4; +} + static int api_loadfile(lua_State* state) { lua_getfield(state, LUA_REGISTRYINDEX, PLUGIN_REGISTRYNAME); const struct Plugin* plugin = lua_touserdata(state, -1); @@ -1452,6 +1471,8 @@ static struct ApiFuncTemplate bolt_functions[] = { BOLTFUNC(time), BOLTFUNC(datetime), BOLTFUNC(weekday), + BOLTFUNC(gamewindowsize), + BOLTFUNC(gameviewxywh), BOLTFUNC(loadfile), BOLTFUNC(loadconfig), BOLTFUNC(saveconfig),