static int lua_loadimg(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 1) return luaL_error(L, "wrong number of arguments");
char* text = (char*)(luaL_checkstring(L, 1));
Handle fileHandle;
u32 bytesRead;
u16 magic;
u64 long_magic;
FS_path filePath=FS_makePath(PATH_CHAR, text);
FS_archive script=(FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
FSUSER_OpenFileDirectly(NULL, &fileHandle, script, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
FSFILE_Read(fileHandle, &bytesRead, 0, &magic, 2);
Bitmap* bitmap;
if (magic == 0x5089){
FSFILE_Read(fileHandle, &bytesRead, 0, &long_magic, 8);
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
if (long_magic == 0x0A1A0A0D474E5089) bitmap = decodePng(text); // same as openPng but without flip and B<->R channel change
}else if (magic == 0x4D42){
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
bitmap = LoadBitmap(text);
}else if (magic == 0xD8FF){
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
bitmap = OpenJPG(text);
}
if(!bitmap) return luaL_error(L, "Error loading image");
sf2d_texture *tex = sf2d_create_texture(bitmap->width, bitmap->height, GPU_RGBA8, SF2D_PLACE_VRAM);
texture_tile32((u32*)bitmap->pixels, (u32 *)tex->data, bitmap->width, bitmap->height);
free(bitmap->pixels);
free(bitmap);
lua_pushinteger(L, (u32)(tex));
return 1;
}
static int lua_drawimg(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 3) return luaL_error(L, "wrong number of arguments");
int x = luaL_checkinteger(L,1);
int y = luaL_checkinteger(L,2);
sf2d_texture* texture = (sf2d_texture*)luaL_checkinteger(L,3);
sf2d_draw_texture(texture, x, y);
return 0;
}