Add `getSpritePatch`

This commit is contained in:
Chearii committed 2024-01-16 13:46:30 -05:00
1 parent 8f6cc895a0
commit 68354baf28
1 file changed
+75
+75
View File
@@ -11,6 +11,7 @@
/// \brief custom HUD rendering library for Lua scripting
#include "doomdef.h"
#include "fastcmp.h"
#ifdef HAVE_BLUA
#include "r_defs.h"
#include "r_local.h"
@@ -346,6 +347,79 @@ static int libd_cachePatch(lua_State *L)
return 1;
}
// v.getSpritePatch(sprite, [frame, [angle, [rollangle]]])
static int libd_getSpritePatch(lua_State *L)
{
UINT32 i; // sprite prefix
UINT32 frame = 0; // 'A'
UINT8 angle = 0;
spritedef_t *sprdef;
spriteframe_t *sprframe;
HUDONLY
if (lua_isnumber(L, 1)) // sprite number given, e.g. SPR_THOK
{
i = lua_tonumber(L, 1);
if (i >= NUMSPRITES)
return 0;
}
else if (lua_isstring(L, 1)) // sprite prefix name given, e.g. "THOK"
{
const char *name = lua_tostring(L, 1);
for (i = 0; i < NUMSPRITES; i++)
if (fastcmp(name, sprnames[i]))
break;
if (i >= NUMSPRITES)
return 0;
}
else
return 0;
// TODO: figure out a means of getting playersprites without crashes
if (i == SPR_PLAY) // not for players
return luaL_error(L, "Player sprites are not supported.");
sprdef = &sprites[i];
// set frame number
frame = luaL_optinteger(L, 2, 0);
frame &= FF_FRAMEMASK; // ignore any bits that are not the actual frame, just in case
if (frame >= sprdef->numframes)
return 0;
// set angle number
sprframe = &sprdef->spriteframes[frame];
angle = luaL_optinteger(L, 3, 1);
// convert WAD editor angle numbers (1-8) to internal angle numbers (0-7)
// keep 0 the same since we'll make it default to angle 1 (which is internally 0)
// in case somebody didn't know that angle 0 really just maps all 8 angles to the same patch
if (angle != 0)
angle--;
if (angle >= 8) // out of range?
angle = (angle & 7); // modulus angle by 8
// rotsprite?????
if (lua_isnumber(L, 4) && (cv_spriteroll.value))
{
angle_t rollangle = luaL_checkangle(L, 4);
INT32 rot = R_GetRollAngle(rollangle);
if (rot) {
patch_t *rotsprite = Patch_GetRotatedSprite(sprframe, frame, angle, sprframe->flip & (1<<angle), false, &spriteinfo[i], rot);
LUA_PushUserdata(L, rotsprite, META_PATCH);
lua_pushboolean(L, false);
lua_pushboolean(L, true);
return 3;
}
}
// push both the patch and its "flip" value
LUA_PushUserdata(L, W_CachePatchNum(sprframe->lumppat[angle], PU_STATIC), META_PATCH);
lua_pushboolean(L, (sprframe->flip & (1<<angle)) != 0);
return 2;
}
static int libd_draw(lua_State *L)
{
INT32 x, y, flags;
@@ -954,6 +1028,7 @@ static luaL_Reg lib_draw[] = {
{"drawPaddedNum", libd_drawPaddedNum},
{"drawPingNum", libd_drawPingNum},
{"drawFill", libd_drawFill},
{"getSpritePatch",libd_getSpritePatch},
{"fadeScreen", libd_fadeScreen},
{"drawString", libd_drawString},
{"drawKartString", libd_drawKartString},