Skip to content

Commit

Permalink
library: targetsize() api for all 2d render events
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamcake committed Dec 1, 2024
1 parent 16c02a5 commit 7477d4f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/library/doc.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/library/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
};
Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 4 additions & 0 deletions src/library/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/library/plugin/plugin_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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[] = {
Expand All @@ -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[] = {
Expand Down

0 comments on commit 7477d4f

Please sign in to comment.