From b772a42d28f20eca1ca4e44afcd8dc6f2b6d71cd Mon Sep 17 00:00:00 2001 From: Reuh Date: Wed, 19 Aug 2015 17:14:02 +0200 Subject: [PATCH] Added ctr.gfx.getFPS, renamed ctr.hid.read to ctr.hid.keys, unload font from memory when exiting and did some cleaning --- README.md | 2 +- sdcard/ctruLua/main.lua | 5 +++-- source/ctr.c | 2 ++ source/font.c | 8 ++++++-- source/gfx.c | 13 ++++++++++++- source/hid.c | 17 +++++++++-------- source/main.c | 10 +++++++--- source/news.c | 4 ++-- 8 files changed, 42 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 644ad43..47f45c6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Warning: the 'u' in the repo's name is a 'ยต', not a 'u'. #### Builds ![build status](http://thomas99.no-ip.org:3000/ctrulua.png) * Most recent working build: [here](http://thomas99.no-ip.org:3000/ctrulua/builds/latest/artifacts/ctruLua.3dsx) (warning: only tested with Citra, sometimes on real hardware). -* See http://thomas99.no-ip.org:3000/ctrulua/ for all the builds. +* See http://thomas99.no-ip.org:3000/ctrulua for all the builds. #### Build instructions diff --git a/sdcard/ctruLua/main.lua b/sdcard/ctruLua/main.lua index deca951..4918e1e 100644 --- a/sdcard/ctruLua/main.lua +++ b/sdcard/ctruLua/main.lua @@ -9,7 +9,7 @@ local angle = 0 gfx.color.setBackground(gfx.color.RGBA8(200, 200, 200)) while os.run() do - local keys = hid.read() + local keys = hid.keys() if keys.down.start then return end @@ -37,7 +37,8 @@ while os.run() do gfx.startFrame(gfx.GFX_BOTTOM) gfx.color.setDefault(0, 0, 0) - gfx.text(5, 10, "Hello world, from Lua !", 15) + gfx.text(5, 7, "FPS: "..math.ceil(gfx.getFPS())) + gfx.text(5, 20, "Hello world, from Lua !", 20) gfx.endFrame() diff --git a/source/ctr.c b/source/ctr.c index 55c31b1..c2cbfac 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -7,10 +7,12 @@ int load_news_lib(lua_State *L); int load_ptm_lib(lua_State *L); int load_hid_lib(lua_State *L); +// Functions static const struct luaL_Reg ctr_lib[] = { { NULL, NULL } }; +// Subtables struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = { { "gfx", load_gfx_lib }, { "news", load_news_lib }, diff --git a/source/font.c b/source/font.c index 49b5831..5bc26ea 100644 --- a/source/font.c +++ b/source/font.c @@ -13,11 +13,15 @@ static const struct luaL_Reg font_lib[] = { int luaopen_font_lib(lua_State *L) { luaL_newlib(L, font_lib); - font_default = sftd_load_font_mem(vera_ttf, vera_ttf_size); // Load default font - return 1; } void load_font_lib(lua_State *L) { + font_default = sftd_load_font_mem(vera_ttf, vera_ttf_size); // Load default font + luaL_requiref(L, "ctr.gfx.font", luaopen_font_lib, false); +} + +void unload_font_lib() { + sftd_free_font(font_default); // Unload current font } \ No newline at end of file diff --git a/source/gfx.c b/source/gfx.c index 3f3647e..363bda9 100644 --- a/source/gfx.c +++ b/source/gfx.c @@ -31,6 +31,14 @@ static int gfx_render(lua_State *L) { return 0; } +static int gfx_getFPS(lua_State *L) { + float fps = sf2d_get_fps(); + + lua_pushnumber(L, fps); + + return 1; +} + static int gfx_line(lua_State *L) { int x1 = luaL_checkinteger(L, 1); int y1 = luaL_checkinteger(L, 2); @@ -98,10 +106,12 @@ static int gfx_text(lua_State *L) { return 0; } +// Functions static const struct luaL_Reg gfx_lib[] = { { "startFrame", gfx_startFrame}, { "endFrame", gfx_endFrame }, { "render", gfx_render }, + { "getFPS", gfx_getFPS }, { "line", gfx_line }, { "point", gfx_point }, { "rectangle", gfx_rectangle }, @@ -110,7 +120,7 @@ static const struct luaL_Reg gfx_lib[] = { { NULL, NULL } }; -// constants +// Constants struct { char *name; int value; } gfx_constants[] = { { "GFX_TOP", GFX_TOP }, { "GFX_BOTTOM", GFX_BOTTOM }, @@ -123,6 +133,7 @@ struct { char *name; int value; } gfx_constants[] = { { NULL, 0 } }; +// Subtables struct { char *name; int (*load)(lua_State *L); } gfx_libs[] = { { "color", load_color_lib }, { "font", load_font_lib }, diff --git a/source/hid.c b/source/hid.c index 3af63c0..1d4dabb 100644 --- a/source/hid.c +++ b/source/hid.c @@ -4,8 +4,8 @@ #include #include -// key list based on hid.h from the ctrulib by smealum -struct { PAD_KEY key; char *name; } hid_keys[] = { +// Key list based on hid.h from the ctrulib by smealum +struct { PAD_KEY key; char *name; } hid_keys_name[] = { { KEY_A , "a" }, { KEY_B , "b" }, { KEY_SELECT , "select" }, @@ -34,10 +34,11 @@ struct { PAD_KEY key; char *name; } hid_keys[] = { { KEY_UP , "up" }, { KEY_DOWN , "down" }, { KEY_LEFT , "left" }, - { KEY_RIGHT , "right" } + { KEY_RIGHT , "right" }, + { 0, NULL } }; -static int hid_read(lua_State *L) { +static int hid_keys(lua_State *L) { hidScanInput(); u32 kDown = hidKeysDown(); @@ -49,9 +50,9 @@ static int hid_read(lua_State *L) { lua_newtable(L); // held table lua_newtable(L); // up table - for (int i = 0; hid_keys[i].key; i++) { - PAD_KEY key = hid_keys[i].key; - char *name = hid_keys[i].name; + for (int i = 0; hid_keys_name[i].key; i++) { + PAD_KEY key = hid_keys_name[i].key; + char *name = hid_keys_name[i].name; if (kDown & key) { lua_pushboolean(L, true); @@ -134,7 +135,7 @@ static int hid_volume(lua_State *L) { } static const struct luaL_Reg hid_lib[] = { - { "read", hid_read }, + { "keys", hid_keys }, { "touch", hid_touch }, { "circle", hid_circle }, { "accel", hid_accel }, diff --git a/source/main.c b/source/main.c index 721c6cc..e5a8b4e 100644 --- a/source/main.c +++ b/source/main.c @@ -9,6 +9,7 @@ #define BOOT_FILE "/ctruLua/main.lua" int load_ctr_lib(lua_State *L); +void unload_font_lib(); // Display an error void error(char *error) { @@ -18,7 +19,7 @@ void error(char *error) { printf("------------------ FATAL ERROR -------------------"); printf(error); printf("\n--------------------------------------------------"); - printf("Please exit ctruLua by pressing the home button."); + printf("Please exit ctruLua by rebooting the console."); while (aptMainLoop()) { gfxFlushBuffers(); @@ -40,12 +41,15 @@ int main() { // Init Lua lua_State *L = luaL_newstate(); - if (L == NULL) error("memory allocation error while creating a new Lua state"); + if (L == NULL) error("Memory allocation error while creating a new Lua state"); luaL_openlibs(L); load_ctr_lib(L); // Do the actual thing - if(luaL_dofile(L, BOOT_FILE)) error("Can open "BOOT_FILE); + if (luaL_dofile(L, BOOT_FILE)) error("Can't open the boot file "BOOT_FILE); + + // Unload current font + unload_font_lib(); // Disable accel/gyro HIDUSER_DisableAccelerometer(); diff --git a/source/news.c b/source/news.c index 6c5d496..75a7864 100644 --- a/source/news.c +++ b/source/news.c @@ -18,8 +18,8 @@ static int news_notification(lua_State *L) { bool jpeg = false; if (lua_isboolean(L, 4)) jpeg = lua_toboolean(L, 4); - - const u16* cTitle = 0; + + const u16* cTitle = 0; const u16* cMessage = 0; u32 titleLength, messageLength, imageDataLength = 0;