From b2f9a29eb0bca107ad4bbe7ce65832d48bcb57f3 Mon Sep 17 00:00:00 2001 From: GenericHeroGuy <86008856+GenericHeroGuy@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:00:23 +0200 Subject: [PATCH] Lua HUD interpolation --- src/lua_hud.h | 3 + src/lua_hudlib.c | 31 +++++++++ src/lua_hudlib_drawlist.c | 142 +++++++++++++++++++++++++++++--------- 3 files changed, 144 insertions(+), 32 deletions(-) diff --git a/src/lua_hud.h b/src/lua_hud.h index ce9f6e7a8..8601192b6 100644 --- a/src/lua_hud.h +++ b/src/lua_hud.h @@ -41,6 +41,9 @@ enum hud { }; extern boolean hud_running; +extern UINT8 hud_interptag; +extern UINT32 hud_interpcounter; +extern boolean hud_interpstring; boolean LUA_HudEnabled(enum hud option); diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 3ec03537b..62aa30c37 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -32,6 +32,9 @@ #define HUDONLY if (!hud_running) return luaL_error(L, "HUD rendering code should not be called outside of rendering hooks!"); boolean hud_running = false; +UINT8 hud_interptag = 0; +UINT32 hud_interpcounter = 0; +boolean hud_interpstring = false; static UINT8 hud_enabled[(hud_MAX/8)+1]; static UINT8 hudAvailable; // hud hooks field @@ -1178,6 +1181,20 @@ static int libd_getDrawInfo(lua_State *L) return 3; } +static int libd_interpolate(lua_State *L) +{ + HUDONLY + hud_interptag = luaL_checkinteger(L, 1); + return 0; +} + +static int libd_interpString(lua_State *L) +{ + HUDONLY + hud_interpstring = luaL_checkboolean(L, 1); + return 0; +} + static luaL_Reg lib_draw[] = { {"patchExists", libd_patchExists}, {"cachePatch", libd_cachePatch}, @@ -1205,6 +1222,8 @@ static luaL_Reg lib_draw[] = { {"getDrawInfo", libd_getDrawInfo}, {"getHudColor", libd_getHudColor}, {"useColorHud", libd_useColorHud}, + {"interpolate", libd_interpolate}, + {"interpString", libd_interpString}, {NULL, NULL} }; @@ -1438,8 +1457,11 @@ void LUAh_GameHUD(huddrawlist_h list) LUA_PushUserdata(gL, &camera[stplyrnum], META_CAMERA); camnum = stplyrnum + 1; + hud_interpcounter = 0; lua_pushnil(gL); while (lua_next(gL, -5) != 0) { + hud_interptag = 0; + hud_interpcounter++; lua_pushvalue(gL, -5); // graphics library (HUD[1]) lua_pushvalue(gL, -5); // stplyr lua_pushvalue(gL, -5); // camera @@ -1474,7 +1496,10 @@ void LUAh_ScoresHUD(huddrawlist_h list) I_Assert(lua_istable(gL, -1)); lua_remove(gL, -3); // pop HUD lua_pushnil(gL); + hud_interpcounter = 0; while (lua_next(gL, -3) != 0) { + hud_interptag = 0; + hud_interpcounter++; lua_pushvalue(gL, -3); // graphics library (HUD[1]) LUA_Call(gL, 1, 0, 1); } @@ -1507,7 +1532,10 @@ void LUAh_IntermissionHUD(huddrawlist_h list) I_Assert(lua_istable(gL, -1)); lua_remove(gL, -3); // pop HUD lua_pushnil(gL); + hud_interpcounter = 0; while (lua_next(gL, -3) != 0) { + hud_interptag = 0; + hud_interpcounter++; lua_pushvalue(gL, -3); // graphics library (HUD[1]) LUA_Call(gL, 1, 0, 1); } @@ -1540,7 +1568,10 @@ void LUAh_VoteHUD(huddrawlist_h list) I_Assert(lua_istable(gL, -1)); lua_remove(gL, -3); // pop HUD lua_pushnil(gL); + hud_interpcounter = 0; while (lua_next(gL, -3) != 0) { + hud_interptag = 0; + hud_interpcounter++; lua_pushvalue(gL, -3); // graphics library (HUD[1]) LUA_Call(gL, 1, 0, 1); } diff --git a/src/lua_hudlib_drawlist.c b/src/lua_hudlib_drawlist.c index 7ec5398e4..14a24d90e 100644 --- a/src/lua_hudlib_drawlist.c +++ b/src/lua_hudlib_drawlist.c @@ -11,11 +11,16 @@ /// \brief a data structure for managing cached drawlists for the Lua hud lib #include "lua_hudlib_drawlist.h" +#include "lua_hud.h" +#include "blua/lstate.h" // shhhhhh +#include "lua_libs.h" #include #include "v_video.h" #include "z_zone.h" +#include "r_main.h" +#include "r_fps.h" enum drawitem_e { DI_Draw = 0, @@ -34,6 +39,7 @@ enum drawitem_e { // A single draw item with all possible arguments needed for a draw call. typedef struct drawitem_s { + UINT64 id; enum drawitem_e type; fixed_t x; fixed_t y; @@ -68,8 +74,10 @@ typedef struct drawitem_s { // The internal structure of a drawlist. struct huddrawlist_s { drawitem_t *items; - size_t items_capacity; size_t items_len; + drawitem_t *olditems; + size_t olditems_len; + size_t capacity; char *strbuf; size_t strbuf_capacity; size_t strbuf_len; @@ -93,8 +101,10 @@ huddrawlist_h LUA_HUD_CreateDrawList(void) drawlist = (huddrawlist_h) Z_Calloc(sizeof(struct huddrawlist_s), PU_STATIC, NULL); drawlist->items = NULL; - drawlist->items_capacity = 0; drawlist->items_len = 0; + drawlist->olditems = NULL; + drawlist->olditems_len = 0; + drawlist->capacity = 0; drawlist->strbuf = NULL; drawlist->strbuf_capacity = 0; drawlist->strbuf_len = 0; @@ -104,15 +114,14 @@ huddrawlist_h LUA_HUD_CreateDrawList(void) void LUA_HUD_ClearDrawList(huddrawlist_h list) { + // swap the old and new lists + void *tmp = list->olditems; + list->olditems = list->items; + list->items = tmp; + // rather than deallocate, we'll just save the existing allocation and empty // it out for reuse - - // this memset probably isn't necessary - if (list->items) - { - memset(list->items, 0, sizeof(drawitem_t) * list->items_capacity); - } - + list->olditems_len = list->items_len; list->items_len = 0; if (list->strbuf) @@ -130,6 +139,10 @@ void LUA_HUD_DestroyDrawList(huddrawlist_h list) { Z_Free(list->items); } + if (list->olditems) + { + Z_Free(list->olditems); + } if (list->strbuf) { Z_Free(list->strbuf); @@ -148,11 +161,11 @@ boolean LUA_HUD_IsDrawListValid(huddrawlist_h list) static size_t AllocateDrawItem(huddrawlist_h list) { if (!list) I_Error("can't allocate draw item: invalid list"); - if (list->items_capacity <= list->items_len + 1) + if (list->capacity <= list->items_len) { - if (list->items_capacity == 0) list->items_capacity = 128; - else list->items_capacity *= 2; - list->items = (drawitem_t *) Z_Realloc(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL); + list->capacity = list->capacity == 0 ? 128 : list->capacity * 2; + list->items = Z_Realloc(list->items, sizeof(struct drawitem_s) * list->capacity, PU_STATIC, NULL); + list->olditems = Z_Realloc(list->olditems, sizeof(struct drawitem_s) * list->capacity, PU_STATIC, NULL); } return list->items_len++; @@ -183,6 +196,14 @@ static size_t CopyString(huddrawlist_h list, const char* str) } } +// leave bit 0 free for the string mode +#define INTERP_LATCH 1 + +#define GETITEMID \ + item->id = hud_interptag \ + ? (((UINT64)gL->savedpc << 32) | (hud_interpcounter << 9) | (hud_interptag << 1) | (hud_interpstring ? INTERP_LATCH : 0)) \ + : 0; + void LUA_HUD_AddDraw( huddrawlist_h list, INT32 x, @@ -194,9 +215,10 @@ void LUA_HUD_AddDraw( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_Draw; - item->x = x; - item->y = y; + item->x = x << FRACBITS; + item->y = y << FRACBITS; item->patch = patch; item->flags = flags; item->colormap = colormap; @@ -214,6 +236,7 @@ void LUA_HUD_AddDrawScaled( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawScaled; item->x = x; item->y = y; @@ -236,6 +259,7 @@ void LUA_HUD_AddDrawStretched( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawStretched; item->x = x; item->y = y; @@ -256,6 +280,7 @@ void LUA_HUD_AddDrawNum( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawNum; item->x = x; item->y = y; @@ -274,6 +299,7 @@ void LUA_HUD_AddDrawPaddedNum( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawPaddedNum; item->x = x; item->y = y; @@ -293,6 +319,7 @@ void LUA_HUD_AddDrawPingNum( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawPingNum; item->x = x; item->y = y; @@ -312,6 +339,7 @@ void LUA_HUD_AddDrawFill( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawFill; item->x = x; item->y = y; @@ -331,6 +359,7 @@ void LUA_HUD_AddDrawString( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawString; item->x = x; item->y = y; @@ -349,6 +378,7 @@ void LUA_HUD_AddDrawKartString( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawKartString; item->x = x; item->y = y; @@ -366,6 +396,7 @@ void LUA_HUD_AddDrawLevelTitle( { size_t i = AllocateDrawItem(list); drawitem_t *item = &list->items[i]; + GETITEMID item->type = DI_DrawLevelTitle; item->x = x; item->y = y; @@ -389,6 +420,11 @@ void LUA_HUD_AddFadeScreen( void LUA_HUD_DrawList(huddrawlist_h list) { size_t i; + size_t j = 0; + fixed_t frac = R_UsingFrameInterpolation() ? rendertimefrac : FRACUNIT; + fixed_t lerpx = 0, lerpy = 0; + drawitem_t *latchitem = NULL; + drawitem_t *oldlatchitem = NULL; if (!list) I_Error("HUD drawlist invalid"); if (list->items_len <= 0) return; @@ -397,68 +433,110 @@ void LUA_HUD_DrawList(huddrawlist_h list) for (i = 0; i < list->items_len; i++) { drawitem_t *item = &list->items[i]; + drawitem_t *olditem = NULL; const char *itemstr = &list->strbuf[item->stroffset]; + if (item->id) { + // find the old one too + // this is kinda cursed... we need to check every item + // but stop when the first-checked item is reached again + size_t stop = j; + while(true) + { + if (j == list->olditems_len) + { + // wrap around + j = 0; + if (j == stop) // i'm dumb + break; + } + drawitem_t *old = &list->olditems[j++]; + if (old->id == item->id) + { + // gotcha! + olditem = old; + if (item->id & INTERP_LATCH) { + if (!latchitem) { + lerpx = FixedMul(frac, item->x - olditem->x); + lerpy = FixedMul(frac, item->y - olditem->y); + latchitem = item; + oldlatchitem = olditem; + } + } else { + lerpx = FixedMul(frac, item->x - olditem->x); + lerpy = FixedMul(frac, item->y - olditem->y); + latchitem = NULL; + } + break; + } + if (j == stop) + break; + } + } + + #define LERP(it) (olditem ? olditem->it + FixedMul(frac, item->it - olditem->it) : item->it) + #define LERPS(it) (olditem ? (latchitem ? (latchitem->it + (item->it - latchitem->it)) - (latchitem->it - oldlatchitem->it) : olditem->it) + lerp##it : item->it) + switch (item->type) { case DI_Draw: - V_DrawFixedPatch(item->x<y<flags, item->patch, item->colormap); + V_DrawFixedPatch(LERPS(x), LERPS(y), FRACUNIT, item->flags, item->patch, item->colormap); break; case DI_DrawScaled: - V_DrawFixedPatch(item->x, item->y, item->scale, item->flags, item->patch, item->colormap); + V_DrawFixedPatch(LERPS(x), LERPS(y), LERP(scale), item->flags, item->patch, item->colormap); break; case DI_DrawStretched: - V_DrawStretchyFixedPatch(item->x, item->y, item->hscale, item->vscale, item->flags, item->patch, item->colormap); + V_DrawStretchyFixedPatch(LERPS(x), LERPS(y), LERP(hscale), LERP(vscale), item->flags, item->patch, item->colormap); break; case DI_DrawNum: - V_DrawTallNum(item->x, item->y, item->flags, item->num); + V_DrawTallNum(LERPS(x), LERPS(y), item->flags, item->num); break; case DI_DrawPaddedNum: - V_DrawPaddedTallNum(item->x, item->y, item->flags, item->num, item->digits); + V_DrawPaddedTallNum(LERPS(x), LERPS(y), item->flags, item->num, item->digits); break; case DI_DrawPingNum: - V_DrawPingNum(item->x, item->y, item->flags, item->num, item->colormap); + V_DrawPingNum(LERPS(x), LERPS(y), item->flags, item->num, item->colormap); break; case DI_DrawFill: - V_DrawFill(item->x, item->y, item->w, item->h, item->c); + V_DrawFill(LERPS(x), LERPS(y), item->w, item->h, item->c); break; case DI_DrawString: switch(item->align) { // hu_font case align_left: - V_DrawString(item->x, item->y, item->flags, itemstr); + V_DrawString(LERPS(x), LERPS(y), item->flags, itemstr); break; case align_center: - V_DrawCenteredString(item->x, item->y, item->flags, itemstr); + V_DrawCenteredString(LERPS(x), LERPS(y), item->flags, itemstr); break; case align_right: - V_DrawRightAlignedString(item->x, item->y, item->flags, itemstr); + V_DrawRightAlignedString(LERPS(x), LERPS(y), item->flags, itemstr); break; case align_fixed: - V_DrawStringAtFixed(item->x, item->y, item->flags, itemstr); + V_DrawStringAtFixed(LERPS(x), LERPS(y), item->flags, itemstr); break; // hu_font, 0.5x scale case align_small: - V_DrawSmallString(item->x, item->y, item->flags, itemstr); + V_DrawSmallString(LERPS(x), LERPS(y), item->flags, itemstr); break; case align_smallright: - V_DrawRightAlignedSmallString(item->x, item->y, item->flags, itemstr); + V_DrawRightAlignedSmallString(LERPS(x), LERPS(y), item->flags, itemstr); break; // tny_font case align_thin: - V_DrawThinString(item->x, item->y, item->flags, itemstr); + V_DrawThinString(LERPS(x), LERPS(y), item->flags, itemstr); break; case align_thinright: - V_DrawRightAlignedThinString(item->x, item->y, item->flags, itemstr); + V_DrawRightAlignedThinString(LERPS(x), LERPS(y), item->flags, itemstr); break; } break; case DI_DrawKartString: - V_DrawKartString(item->x, item->y, item->flags, itemstr); + V_DrawKartString(LERPS(x), LERPS(y), item->flags, itemstr); break; case DI_DrawLevelTitle: - V_DrawLevelTitle(item->x, item->y, item->flags, itemstr); + V_DrawLevelTitle(LERPS(x), LERPS(y), item->flags, itemstr); break; case DI_FadeScreen: V_DrawFadeScreen(item->color, item->strength);