59 lines
1.6 KiB
C
59 lines
1.6 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "moduleasset.h"
|
|
#include "asset/asset.h"
|
|
#include "display/texture.h"
|
|
#include "assert/assert.h"
|
|
#include "util/memory.h"
|
|
|
|
void moduleAsset(scriptcontext_t *context) {
|
|
assertNotNull(context, "Script context cannot be null");
|
|
|
|
lua_State *l = context->luaState;
|
|
assertNotNull(l, "Lua state cannot be null");
|
|
|
|
// Create metatable for texture structure.
|
|
|
|
lua_register(context->luaState, "assetTextureLoad", moduleAssetTextureLoad);
|
|
lua_register(context->luaState, "assetTextureDispose", moduleAssetTextureDispose);
|
|
}
|
|
|
|
int moduleAssetTextureLoad(lua_State *l) {
|
|
assertNotNull(l, "Lua state cannot be NULL.");
|
|
|
|
const char_t *filename = luaL_checkstring(l, 2);
|
|
assertStrLenMin(filename, 1, "Filename cannot be empty.");
|
|
|
|
// Create texture owned to lua
|
|
texture_t *tex = (texture_t *)lua_newuserdata(l, sizeof(texture_t));
|
|
memoryZero(tex, sizeof(texture_t));
|
|
|
|
errorret_t ret = assetLoad(filename, tex);
|
|
if(ret.code != ERROR_OK) {
|
|
errorCatch(errorPrint(ret));
|
|
luaL_error(l, "Failed to load texture asset: %s", filename);
|
|
return 0;
|
|
}
|
|
|
|
// Set metatable
|
|
luaL_getmetatable(l, "texture_mt");
|
|
lua_setmetatable(l, -2);
|
|
|
|
// Return the texture
|
|
return 1;
|
|
}
|
|
|
|
int moduleAssetTextureDispose(lua_State *l) {
|
|
assertNotNull(l, "Lua state cannot be NULL.");
|
|
|
|
texture_t *tex = (texture_t *)luaL_checkudata(l, 1, "texture_mt");
|
|
assertNotNull(tex, "Texture pointer cannot be NULL.");
|
|
|
|
textureDispose(tex);
|
|
return 0;
|
|
} |