From 7477d4f2432cfe8d7c216da811ba403154f76a8f Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 1 Dec 2024 21:36:24 +0000 Subject: [PATCH] library: targetsize() api for all 2d render events --- src/library/doc.texi | 36 ++++++++++++++++++++++++++++++++- src/library/gl.c | 7 ++++++- src/library/plugin/plugin.h | 4 ++++ src/library/plugin/plugin_api.c | 16 +++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/library/doc.texi b/src/library/doc.texi index a112102..2fbb33d 100644 --- a/src/library/doc.texi +++ b/src/library/doc.texi @@ -2406,6 +2406,23 @@ local red, green, blue, alpha = event:modelvertexcolour(model, vertex) Alias for @ref{renderitemicon-modelvertexcolour} +@node renderitemicon-targetsize +@subsection targetsize + +Returns the width and height of the target area of this render, in +pixels. + +This will be proportional to the size of the inner area of the game +window, given by @ref{functions-gamewindowsize} - that is, if the user +has an interface scaling other than 100%, it will be bigger or smaller +than that area, proportionally. + +@example lua +@verbatim +local width, height = event:targetsize() +@end verbatim +@end example + @node renderitemicon-modelviewmatrix @subsection modelviewmatrix @@ -2523,7 +2540,7 @@ being drawn, in pixels. @example lua @verbatim -local x, y, width, height = event:targetxywh() +local x, y, width, height = event:sourcexywh() @end verbatim @end example @@ -2540,6 +2557,23 @@ local x, y, width, height = event:targetxywh() @end verbatim @end example +@node renderminimap-targetsize +@subsection targetsize + +Returns the width and height of the target area of this render, in +pixels. + +This will be proportional to the size of the inner area of the game +window, given by @ref{functions-gamewindowsize} - that is, if the user +has an interface scaling other than 100%, it will be bigger or smaller +than that area, proportionally. + +@example lua +@verbatim +local width, height = event:targetsize() +@end verbatim +@end example + @node objects-swapbuffers @section SwapBuffers event diff --git a/src/library/gl.c b/src/library/gl.c index eab6f3b..fb093e9 100644 --- a/src/library/gl.c +++ b/src/library/gl.c @@ -1406,13 +1406,16 @@ void _bolt_gl_onDrawElements(GLenum mode, GLsizei count, GLenum type, const void const int16_t x2 = (int16_t)roundf(uv2[0] * tex->width); const int16_t y1 = (int16_t)roundf(uv2[1] * tex->height); const int16_t y2 = (int16_t)roundf(uv0[1] * tex->height); + const float screen_height_float = roundf(2.0 / projection_matrix[5]); const struct RenderMinimapEvent event = { + .screen_width = roundf(2.0 / projection_matrix[0]), + .screen_height = screen_height_float, .source_x = x1, .source_y = y1, .source_w = x2 - x1, .source_h = y2 - y1, .target_x = (int16_t)xy0[0], - .target_y = (int16_t)(roundf(2.0 / projection_matrix[5]) - xy0[1]), + .target_y = (int16_t)(screen_height_float - xy0[1]), .target_w = (uint16_t)(xy2[0] - xy0[0]), .target_h = (uint16_t)(xy0[1] - xy2[1]), }; @@ -1542,6 +1545,8 @@ void _bolt_gl_onDrawElements(GLenum mode, GLsizei count, GLenum type, const void _bolt_get_attr_binding_int(c, vertex_userdata.position, indices[i + 2], 2, xy2); struct RenderItemIconEvent event; event.icon = icon; + event.screen_width = batch.screen_width; + event.screen_height = batch.screen_height; event.target_x = (int16_t)xy2[0]; event.target_y = (int16_t)((int32_t)batch.screen_height - xy2[1]); event.target_w = (uint16_t)(xy0[0] - xy2[0]); diff --git a/src/library/plugin/plugin.h b/src/library/plugin/plugin.h index d45f175..23ad81e 100644 --- a/src/library/plugin/plugin.h +++ b/src/library/plugin/plugin.h @@ -298,6 +298,8 @@ uint64_t _bolt_plugin_itemicon_hash(const void* item, uint64_t seed0, uint64_t s struct RenderItemIconEvent { const struct ItemIcon* icon; + uint32_t screen_width; + uint32_t screen_height; uint16_t target_x; uint16_t target_y; uint16_t target_w; @@ -313,6 +315,8 @@ struct MinimapTerrainEvent { }; struct RenderMinimapEvent { + uint32_t screen_width; + uint32_t screen_height; int16_t source_x; int16_t source_y; uint16_t source_w; diff --git a/src/library/plugin/plugin_api.c b/src/library/plugin/plugin_api.c index 1b0ab84..134bef4 100644 --- a/src/library/plugin/plugin_api.c +++ b/src/library/plugin/plugin_api.c @@ -803,6 +803,13 @@ static int api_renderminimap_targetxywh(lua_State* state) { return 4; } +static int api_renderminimap_targetsize(lua_State* state) { + const struct RenderMinimapEvent* render = require_self_userdata(state, "targetsize"); + lua_pushinteger(state, render->screen_width); + lua_pushinteger(state, render->screen_height); + return 2; +} + static int api_point_transform(lua_State* state) { const struct Point3D* point = require_self_userdata(state, "transform"); const struct Transform3D* transform = require_userdata(state, 2, "transform"); @@ -1265,6 +1272,13 @@ static int api_rendericon_colour(lua_State* state) { return 4; } +static int api_rendericon_targetsize(lua_State* state) { + const struct RenderItemIconEvent* event = require_self_userdata(state, "targetsize"); + lua_pushinteger(state, event->screen_width); + lua_pushinteger(state, event->screen_height); + return 2; +} + static int api_rendericon_modelviewmatrix(lua_State* state) { const struct RenderItemIconEvent* event = require_self_userdata(state, "modelviewmatrix"); const size_t model = luaL_checkinteger(state, 2); @@ -1658,6 +1672,7 @@ static struct ApiFuncTemplate rendericon_functions[] = { BOLTALIAS(modelvertexcolour, modelvertexcolor, rendericon), BOLTFUNC(colour, rendericon), BOLTALIAS(colour, color, rendericon), + BOLTFUNC(targetsize, rendericon), }; static struct ApiFuncTemplate minimapterrain_functions[] = { @@ -1669,6 +1684,7 @@ static struct ApiFuncTemplate minimapterrain_functions[] = { static struct ApiFuncTemplate renderminimap_functions[] = { BOLTFUNC(sourcexywh, renderminimap), BOLTFUNC(targetxywh, renderminimap), + BOLTFUNC(targetsize, renderminimap), }; static struct ApiFuncTemplate point_functions[] = {