// SONIC ROBO BLAST 2 //----------------------------------------------------------------------------- // Copyright (C) 2014 by John "JTE" Muniz. // Copyright (C) 2014 by Sonic Team Junior. // // This program is free software distributed under the // terms of the GNU General Public License, version 2. // See the 'LICENSE' file for more details. //----------------------------------------------------------------------------- /// \file lua_hudlib.c /// \brief custom HUD rendering library for Lua scripting #include "doomdef.h" #ifdef HAVE_BLUA #include "r_defs.h" #include "st_stuff.h" // hudinfo[] #include "g_game.h" #include "v_video.h" #include "w_wad.h" #include "z_zone.h" #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" boolean hud_running = false; static UINT8 hud_enabled[(hud_MAX/8)+1]; // must match enum hud in lua_hud.h static const char *const hud_disable_options[] = { "stagetitle", "textspectator", "score", "time", "rings", "lives", "nightslink", "nightsdrill", "nightsrings", "nightsscore", "nightstime", "nightsrecords", "rankings", "coopemeralds", "tokens", "tabemblems", NULL}; enum hudinfo { hudinfo_x = 0, hudinfo_y }; static const char *const hudinfo_opt[] = { "x", "y", NULL}; enum patch { patch_valid = 0, patch_width, patch_height, patch_leftoffset, patch_topoffset }; static const char *const patch_opt[] = { "valid", "width", "height", "leftoffset", "topoffset", NULL}; enum hudhook { hudhook_game = 0, hudhook_scores }; static const char *const hudhook_opt[] = { "game", "scores", NULL}; enum align { align_left = 0, align_center, align_right, align_fixed }; static const char *const align_opt[] = { "left", "center", "right", "fixed", NULL}; static int lib_getHudInfo(lua_State *L) { // arg 1 is empty table with this metatable set lua_Integer mindex = luaL_checkinteger(L,2); hudinfo_t **userdata = lua_newuserdata(L, sizeof(hudinfo_t *)); luaL_getmetatable(L, META_HUDINFO); lua_setmetatable(L, -2); *userdata = &hudinfo[mindex]; return 1; } static int lib_hudinfolen(lua_State *L) { lua_pushinteger(L, NUMHUDITEMS); return 1; } static int hudinfo_get(lua_State *L) { hudinfo_t *info = *((hudinfo_t **)luaL_checkudata(L, 1, META_HUDINFO)); enum hudinfo field = luaL_checkoption(L, 2, hudinfo_opt[0], hudinfo_opt); I_Assert(info != NULL); // huditems are always valid switch(field) { case hudinfo_x: lua_pushinteger(L, info->x); break; case hudinfo_y: lua_pushinteger(L, info->y); break; } return 1; } static int hudinfo_set(lua_State *L) { hudinfo_t *info = *((hudinfo_t **)luaL_checkudata(L, 1, META_HUDINFO)); enum hudinfo field = luaL_checkoption(L, 2, hudinfo_opt[0], hudinfo_opt); I_Assert(info != NULL); switch(field) { case hudinfo_x: info->x = (INT32)luaL_checkinteger(L, 3); break; case hudinfo_y: info->y = (INT32)luaL_checkinteger(L, 3); break; } return 0; } static int hudinfo_num(lua_State *L) { hudinfo_t *info = *((hudinfo_t **)luaL_checkudata(L, 1, META_HUDINFO)); lua_pushinteger(L, info-hudinfo); return 1; } static int patch_get(lua_State *L) { patch_t *patch = *((patch_t **)luaL_checkudata(L, 1, META_PATCH)); enum patch field = luaL_checkoption(L, 2, NULL, patch_opt); // patches are CURRENTLY always valid, expected to be cached with PU_STATIC // this may change in the future, so patch.valid still exists I_Assert(patch != NULL); switch (field) { case patch_valid: lua_pushboolean(L, patch != NULL); break; case patch_width: lua_pushinteger(L, SHORT(patch->width)); break; case patch_height: lua_pushinteger(L, SHORT(patch->height)); break; case patch_leftoffset: lua_pushinteger(L, SHORT(patch->leftoffset)); break; case patch_topoffset: lua_pushinteger(L, SHORT(patch->topoffset)); break; } return 1; } static int patch_set(lua_State *L) { return luaL_error(L, LUA_QL("patch_t") " struct cannot be edited by Lua."); } // // lib_draw // static int libd_patchExists(lua_State *L) { if (!hud_running) return luaL_error(L, "HUD rendering code should not be called outside of rendering hooks!"); lua_pushboolean(L, W_CheckNumForName(luaL_checkstring(L, 1)) != LUMPERROR); return 1; } static int libd_cachePatch(lua_State *L) { if (!hud_running) return luaL_error(L, "HUD rendering code should not be called outside of rendering hooks!"); LUA_PushUserdata(L, W_CachePatchName(luaL_checkstring(L, 1), PU_STATIC), META_PATCH); return 1; } static int libd_draw(lua_State *L) { INT32 x, y, flags; patch_t *patch; const UINT8 *colormap = NULL; if (!hud_running) return luaL_error(L, "HUD rendering code should not be called outside of rendering hooks!"); x = luaL_checkinteger(L, 1); y = luaL_checkinteger(L, 2); patch = *((patch_t **)luaL_checkudata(L, 3, META_PATCH)); flags = luaL_optinteger(L, 4, 0); if (!lua_isnoneornil(L, 5)) colormap = luaL_checkudata(L, 5, META_COLORMAP); flags &= ~V_PARAMMASK; // Don't let crashes happen. V_DrawFixedPatch(x<