mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-28 00:39:30 +00:00
Cleaned initialisation process
This commit is contained in:
parent
8da0e47ae3
commit
0af33d0b3a
6 changed files with 70 additions and 58 deletions
40
source/ctr.c
40
source/ctr.c
|
|
@ -5,13 +5,17 @@
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
int load_gfx_lib(lua_State *L);
|
void load_gfx_lib(lua_State *L);
|
||||||
int load_news_lib(lua_State *L);
|
void load_news_lib(lua_State *L);
|
||||||
int load_ptm_lib(lua_State *L);
|
void load_ptm_lib(lua_State *L);
|
||||||
int load_hid_lib(lua_State *L);
|
void load_hid_lib(lua_State *L);
|
||||||
int load_ir_lib(lua_State *L);
|
void load_ir_lib(lua_State *L);
|
||||||
int load_fs_lib(lua_State *L);
|
void load_fs_lib(lua_State *L);
|
||||||
int load_httpc_lib(lua_State *L);
|
void load_httpc_lib(lua_State *L);
|
||||||
|
|
||||||
|
void unload_gfx_lib(lua_State *L);
|
||||||
|
void unload_hid_lib(lua_State *L);
|
||||||
|
void unload_fs_lib(lua_State *L);
|
||||||
|
|
||||||
static int ctr_run(lua_State *L) {
|
static int ctr_run(lua_State *L) {
|
||||||
lua_pushboolean(L, aptMainLoop());
|
lua_pushboolean(L, aptMainLoop());
|
||||||
|
|
@ -33,14 +37,14 @@ static const struct luaL_Reg ctr_lib[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Subtables
|
// Subtables
|
||||||
struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
|
struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L) } ctr_libs[] = {
|
||||||
{ "gfx", load_gfx_lib },
|
{ "gfx", load_gfx_lib, unload_gfx_lib },
|
||||||
{ "news", load_news_lib },
|
{ "news", load_news_lib, NULL },
|
||||||
{ "ptm", load_ptm_lib },
|
{ "ptm", load_ptm_lib, NULL },
|
||||||
{ "hid", load_hid_lib },
|
{ "hid", load_hid_lib, unload_hid_lib },
|
||||||
{ "ir", load_ir_lib },
|
{ "ir", load_ir_lib, NULL },
|
||||||
{ "fs", load_fs_lib },
|
{ "fs", load_fs_lib, unload_fs_lib },
|
||||||
{ "httpc", load_httpc_lib },
|
{ "httpc", load_httpc_lib, NULL },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -58,3 +62,9 @@ int luaopen_ctr_lib(lua_State *L) {
|
||||||
void load_ctr_lib(lua_State *L) {
|
void load_ctr_lib(lua_State *L) {
|
||||||
luaL_requiref(L, "ctr", luaopen_ctr_lib, 0);
|
luaL_requiref(L, "ctr", luaopen_ctr_lib, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unload_ctr_lib(lua_State *L) {
|
||||||
|
for (int i = 0; ctr_libs[i].name; i++) {
|
||||||
|
if (ctr_libs[i].unload) ctr_libs[i].unload(L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,6 @@ void load_font_lib(lua_State *L) {
|
||||||
luaL_requiref(L, "ctr.gfx.font", luaopen_font_lib, false);
|
luaL_requiref(L, "ctr.gfx.font", luaopen_font_lib, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unload_font_lib() {
|
void unload_font_lib(lua_State *L) {
|
||||||
sftd_free_font(font_default); // Unload current font
|
sftd_free_font(font_default); // Unload current font
|
||||||
}
|
}
|
||||||
|
|
@ -71,7 +71,7 @@ int luaopen_fs_lib(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_fs_lib(lua_State *L) {
|
void load_fs_lib(lua_State *L) {
|
||||||
fsInit();
|
fsInit();
|
||||||
|
|
||||||
fsuHandle = fsGetSessionHandle();
|
fsuHandle = fsGetSessionHandle();
|
||||||
|
|
@ -81,8 +81,6 @@ int load_fs_lib(lua_State *L) {
|
||||||
FSUSER_OpenArchive(fsuHandle, &sdmcArchive);
|
FSUSER_OpenArchive(fsuHandle, &sdmcArchive);
|
||||||
|
|
||||||
luaL_requiref(L, "ctr.fs", luaopen_fs_lib, false);
|
luaL_requiref(L, "ctr.fs", luaopen_fs_lib, false);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void unload_fs_lib(lua_State *L) {
|
void unload_fs_lib(lua_State *L) {
|
||||||
|
|
|
||||||
36
source/gfx.c
36
source/gfx.c
|
|
@ -6,10 +6,14 @@
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
int load_color_lib(lua_State *L);
|
bool isGfxInitialised = false;
|
||||||
int load_font_lib(lua_State *L);
|
|
||||||
int load_texture_lib(lua_State *L);
|
void load_color_lib(lua_State *L);
|
||||||
int load_map_lib(lua_State *L);
|
void load_font_lib(lua_State *L);
|
||||||
|
void load_texture_lib(lua_State *L);
|
||||||
|
void load_map_lib(lua_State *L);
|
||||||
|
|
||||||
|
void unload_font_lib(lua_State *L);
|
||||||
|
|
||||||
u32 color_default;
|
u32 color_default;
|
||||||
sftd_font *font_default;
|
sftd_font *font_default;
|
||||||
|
|
@ -153,11 +157,11 @@ struct { char *name; int value; } gfx_constants[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Subtables
|
// Subtables
|
||||||
struct { char *name; int (*load)(lua_State *L); } gfx_libs[] = {
|
struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } gfx_libs[] = {
|
||||||
{ "color", load_color_lib },
|
{ "color", load_color_lib, NULL },
|
||||||
{ "font", load_font_lib },
|
{ "font", load_font_lib, unload_font_lib },
|
||||||
{ "texture", load_texture_lib },
|
{ "texture", load_texture_lib, NULL },
|
||||||
{ "map", load_map_lib },
|
{ "map", load_map_lib, NULL },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -178,5 +182,19 @@ int luaopen_gfx_lib(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_gfx_lib(lua_State *L) {
|
void load_gfx_lib(lua_State *L) {
|
||||||
|
sf2d_init();
|
||||||
|
sftd_init();
|
||||||
|
|
||||||
|
isGfxInitialised = true;
|
||||||
|
|
||||||
luaL_requiref(L, "ctr.gfx", luaopen_gfx_lib, 0);
|
luaL_requiref(L, "ctr.gfx", luaopen_gfx_lib, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unload_gfx_lib(lua_State *L) {
|
||||||
|
for (int i = 0; gfx_libs[i].name; i++) {
|
||||||
|
if (gfx_libs[i].unload) gfx_libs[i].unload(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
sftd_fini();
|
||||||
|
sf2d_fini();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -156,5 +156,13 @@ int luaopen_hid_lib(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_hid_lib(lua_State *L) {
|
void load_hid_lib(lua_State *L) {
|
||||||
|
HIDUSER_EnableAccelerometer();
|
||||||
|
HIDUSER_EnableGyroscope();
|
||||||
|
|
||||||
luaL_requiref(L, "ctr.hid", luaopen_hid_lib, false);
|
luaL_requiref(L, "ctr.hid", luaopen_hid_lib, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unload_hid_lib(lua_State *L) {
|
||||||
|
HIDUSER_DisableAccelerometer();
|
||||||
|
HIDUSER_DisableGyroscope();
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
#include <sf2d.h>
|
|
||||||
#include <sftd.h>
|
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
@ -8,15 +6,14 @@
|
||||||
|
|
||||||
#define BOOT_FILE "sdmc:/3ds/ctruLua/main.lua"
|
#define BOOT_FILE "sdmc:/3ds/ctruLua/main.lua"
|
||||||
|
|
||||||
int load_ctr_lib(lua_State *L);
|
void load_ctr_lib(lua_State *L);
|
||||||
void unload_font_lib();
|
void unload_ctr_lib(lua_State *L);
|
||||||
void unload_fs_lib();
|
|
||||||
|
|
||||||
bool gfxinit = false;
|
bool isGfxInitialised;
|
||||||
|
|
||||||
// Display an error
|
// Display an error
|
||||||
void error(const char *error) {
|
void error(const char *error) {
|
||||||
if (!gfxinit) gfxInitDefault();
|
if (!isGfxInitialised) gfxInitDefault();
|
||||||
gfxSet3D(false);
|
gfxSet3D(false);
|
||||||
|
|
||||||
consoleInit(GFX_TOP, NULL);
|
consoleInit(GFX_TOP, NULL);
|
||||||
|
|
@ -33,7 +30,7 @@ void error(const char *error) {
|
||||||
gspWaitForVBlank();
|
gspWaitForVBlank();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!gfxinit) gfxExit();
|
if (!isGfxInitialised) gfxExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
|
|
@ -44,16 +41,6 @@ int main() {
|
||||||
error("Memory allocation error while creating a new Lua state");
|
error("Memory allocation error while creating a new Lua state");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init GFX
|
|
||||||
sf2d_init();
|
|
||||||
sftd_init();
|
|
||||||
|
|
||||||
gfxinit = true;
|
|
||||||
|
|
||||||
// Init accel/gyro
|
|
||||||
HIDUSER_EnableAccelerometer();
|
|
||||||
HIDUSER_EnableGyroscope();
|
|
||||||
|
|
||||||
// Load libs
|
// Load libs
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
|
@ -62,20 +49,11 @@ int main() {
|
||||||
// Do the actual thing
|
// Do the actual thing
|
||||||
if (luaL_dofile(L, BOOT_FILE)) error(luaL_checkstring(L, -1));
|
if (luaL_dofile(L, BOOT_FILE)) error(luaL_checkstring(L, -1));
|
||||||
|
|
||||||
|
// Unload libs
|
||||||
|
unload_ctr_lib(L);
|
||||||
|
|
||||||
// Unload Lua
|
// Unload Lua
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
|
|
||||||
// Unload libs
|
|
||||||
unload_font_lib();
|
|
||||||
unload_fs_lib();
|
|
||||||
|
|
||||||
// Disable accel/gyro
|
|
||||||
HIDUSER_DisableAccelerometer();
|
|
||||||
HIDUSER_DisableGyroscope();
|
|
||||||
|
|
||||||
// Uninit GFX
|
|
||||||
sftd_fini();
|
|
||||||
sf2d_fini();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue