Skip to content

Commit

Permalink
library: gamewindowsize + gameviewxywh
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamcake committed Nov 25, 2024
1 parent 3546108 commit f644e2d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/library/doc.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions src/library/plugin/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) { \
Expand Down Expand Up @@ -634,20 +635,28 @@ 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;
}
overlay_width = window_width;
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);

Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions src/library/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions src/library/plugin/plugin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit f644e2d

Please sign in to comment.