Skip to content

Commit

Permalink
lua: add lookup of texture by name
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-lysiuk committed Oct 22, 2024
1 parent 610d9c0 commit 7b5ce50
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions Quake/ls_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,26 @@ static int LS_global_text_toascii(lua_State* state)
return 1;
}

// Pushes texture table by the given texture
static void LS_PushTextureTable(lua_State* state, const gltexture_t* const texture)
{
assert(texture);

lua_newtable(state);
lua_pushstring(state, "name");
lua_pushstring(state, texture->name);
lua_rawset(state, -3);
lua_pushstring(state, "width");
lua_pushinteger(state, texture->width);
lua_rawset(state, -3);
lua_pushstring(state, "height");
lua_pushinteger(state, texture->height);
lua_rawset(state, -3);
lua_pushstring(state, "texnum");
lua_pushinteger(state, texture->texnum);
lua_rawset(state, -3);
}

// Pushes sequence of tables with information about loaded textures
static int LS_global_textures_list(lua_State* state)
{
Expand All @@ -702,19 +722,7 @@ static int LS_global_textures_list(lua_State* state)

for (; texture; ++count)
{
lua_newtable(state);
lua_pushstring(state, "name");
lua_pushstring(state, texture->name);
lua_rawset(state, -3);
lua_pushstring(state, "width");
lua_pushinteger(state, texture->width);
lua_rawset(state, -3);
lua_pushstring(state, "height");
lua_pushinteger(state, texture->height);
lua_rawset(state, -3);
lua_pushstring(state, "texnum");
lua_pushinteger(state, texture->texnum);
lua_rawset(state, -3);
LS_PushTextureTable(state, texture);

// Set texture table as sequence value
lua_rawseti(state, -2, count);
Expand All @@ -736,6 +744,23 @@ static int LS_global_textures_list(lua_State* state)
return 1;
}

// Pushes texture table by its name
static int LS_global_textures_index(lua_State* state)
{
const char* const name = luaL_checkstring(state, 2);

for (const gltexture_t* texture = LS_GetTextures(); texture; texture = texture->next)
{
if (strcmp(texture->name, name) == 0)
{
LS_PushTextureTable(state, texture);
return 1;
}
}

return 0;
}

static lua_CFunction ls_loadfunc;

// Calls original load() function with mode explicitly set to text
Expand Down Expand Up @@ -854,6 +879,19 @@ static void LS_InitGlobalTables(lua_State* state)
};

luaL_newlib(state, functions);

if (luaL_newmetatable(state, "textures"))
{
static const luaL_Reg functions[] =
{
{ "__index", LS_global_textures_index },
{ nullptr, nullptr }
};

luaL_setfuncs(state, functions, 0);
}

lua_setmetatable(state, -2);
lua_setglobal(state, "textures");
}

Expand Down

0 comments on commit 7b5ce50

Please sign in to comment.