1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

Added ctr.gfx.getFPS, renamed ctr.hid.read to ctr.hid.keys, unload font from memory when exiting and did some cleaning

This commit is contained in:
Reuh 2015-08-19 17:14:02 +02:00
parent bea69d4e30
commit b772a42d28
8 changed files with 42 additions and 19 deletions

View file

@ -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

View file

@ -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()

View file

@ -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 },

View file

@ -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
}

View file

@ -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 },

View file

@ -4,8 +4,8 @@
#include <lua.h>
#include <lauxlib.h>
// 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 },

View file

@ -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();