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:
parent
bea69d4e30
commit
b772a42d28
8 changed files with 42 additions and 19 deletions
|
|
@ -7,7 +7,7 @@ Warning: the 'u' in the repo's name is a 'µ', not a 'u'.
|
||||||
#### Builds 
|
#### Builds 
|
||||||
|
|
||||||
* 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).
|
* 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
|
#### Build instructions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local angle = 0
|
||||||
gfx.color.setBackground(gfx.color.RGBA8(200, 200, 200))
|
gfx.color.setBackground(gfx.color.RGBA8(200, 200, 200))
|
||||||
|
|
||||||
while os.run() do
|
while os.run() do
|
||||||
local keys = hid.read()
|
local keys = hid.keys()
|
||||||
|
|
||||||
if keys.down.start then return end
|
if keys.down.start then return end
|
||||||
|
|
||||||
|
|
@ -37,7 +37,8 @@ while os.run() do
|
||||||
gfx.startFrame(gfx.GFX_BOTTOM)
|
gfx.startFrame(gfx.GFX_BOTTOM)
|
||||||
|
|
||||||
gfx.color.setDefault(0, 0, 0)
|
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()
|
gfx.endFrame()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,12 @@ int load_news_lib(lua_State *L);
|
||||||
int load_ptm_lib(lua_State *L);
|
int load_ptm_lib(lua_State *L);
|
||||||
int load_hid_lib(lua_State *L);
|
int load_hid_lib(lua_State *L);
|
||||||
|
|
||||||
|
// Functions
|
||||||
static const struct luaL_Reg ctr_lib[] = {
|
static const struct luaL_Reg ctr_lib[] = {
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Subtables
|
||||||
struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
|
struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
|
||||||
{ "gfx", load_gfx_lib },
|
{ "gfx", load_gfx_lib },
|
||||||
{ "news", load_news_lib },
|
{ "news", load_news_lib },
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,15 @@ static const struct luaL_Reg font_lib[] = {
|
||||||
int luaopen_font_lib(lua_State *L) {
|
int luaopen_font_lib(lua_State *L) {
|
||||||
luaL_newlib(L, font_lib);
|
luaL_newlib(L, font_lib);
|
||||||
|
|
||||||
font_default = sftd_load_font_mem(vera_ttf, vera_ttf_size); // Load default font
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_font_lib(lua_State *L) {
|
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);
|
luaL_requiref(L, "ctr.gfx.font", luaopen_font_lib, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unload_font_lib() {
|
||||||
|
sftd_free_font(font_default); // Unload current font
|
||||||
}
|
}
|
||||||
13
source/gfx.c
13
source/gfx.c
|
|
@ -31,6 +31,14 @@ static int gfx_render(lua_State *L) {
|
||||||
return 0;
|
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) {
|
static int gfx_line(lua_State *L) {
|
||||||
int x1 = luaL_checkinteger(L, 1);
|
int x1 = luaL_checkinteger(L, 1);
|
||||||
int y1 = luaL_checkinteger(L, 2);
|
int y1 = luaL_checkinteger(L, 2);
|
||||||
|
|
@ -98,10 +106,12 @@ static int gfx_text(lua_State *L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
static const struct luaL_Reg gfx_lib[] = {
|
static const struct luaL_Reg gfx_lib[] = {
|
||||||
{ "startFrame", gfx_startFrame},
|
{ "startFrame", gfx_startFrame},
|
||||||
{ "endFrame", gfx_endFrame },
|
{ "endFrame", gfx_endFrame },
|
||||||
{ "render", gfx_render },
|
{ "render", gfx_render },
|
||||||
|
{ "getFPS", gfx_getFPS },
|
||||||
{ "line", gfx_line },
|
{ "line", gfx_line },
|
||||||
{ "point", gfx_point },
|
{ "point", gfx_point },
|
||||||
{ "rectangle", gfx_rectangle },
|
{ "rectangle", gfx_rectangle },
|
||||||
|
|
@ -110,7 +120,7 @@ static const struct luaL_Reg gfx_lib[] = {
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
// constants
|
// Constants
|
||||||
struct { char *name; int value; } gfx_constants[] = {
|
struct { char *name; int value; } gfx_constants[] = {
|
||||||
{ "GFX_TOP", GFX_TOP },
|
{ "GFX_TOP", GFX_TOP },
|
||||||
{ "GFX_BOTTOM", GFX_BOTTOM },
|
{ "GFX_BOTTOM", GFX_BOTTOM },
|
||||||
|
|
@ -123,6 +133,7 @@ struct { char *name; int value; } gfx_constants[] = {
|
||||||
{ NULL, 0 }
|
{ NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Subtables
|
||||||
struct { char *name; int (*load)(lua_State *L); } gfx_libs[] = {
|
struct { char *name; int (*load)(lua_State *L); } gfx_libs[] = {
|
||||||
{ "color", load_color_lib },
|
{ "color", load_color_lib },
|
||||||
{ "font", load_font_lib },
|
{ "font", load_font_lib },
|
||||||
|
|
|
||||||
17
source/hid.c
17
source/hid.c
|
|
@ -4,8 +4,8 @@
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
// key list based on hid.h from the ctrulib by smealum
|
// Key list based on hid.h from the ctrulib by smealum
|
||||||
struct { PAD_KEY key; char *name; } hid_keys[] = {
|
struct { PAD_KEY key; char *name; } hid_keys_name[] = {
|
||||||
{ KEY_A , "a" },
|
{ KEY_A , "a" },
|
||||||
{ KEY_B , "b" },
|
{ KEY_B , "b" },
|
||||||
{ KEY_SELECT , "select" },
|
{ KEY_SELECT , "select" },
|
||||||
|
|
@ -34,10 +34,11 @@ struct { PAD_KEY key; char *name; } hid_keys[] = {
|
||||||
{ KEY_UP , "up" },
|
{ KEY_UP , "up" },
|
||||||
{ KEY_DOWN , "down" },
|
{ KEY_DOWN , "down" },
|
||||||
{ KEY_LEFT , "left" },
|
{ 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();
|
hidScanInput();
|
||||||
|
|
||||||
u32 kDown = hidKeysDown();
|
u32 kDown = hidKeysDown();
|
||||||
|
|
@ -49,9 +50,9 @@ static int hid_read(lua_State *L) {
|
||||||
lua_newtable(L); // held table
|
lua_newtable(L); // held table
|
||||||
lua_newtable(L); // up table
|
lua_newtable(L); // up table
|
||||||
|
|
||||||
for (int i = 0; hid_keys[i].key; i++) {
|
for (int i = 0; hid_keys_name[i].key; i++) {
|
||||||
PAD_KEY key = hid_keys[i].key;
|
PAD_KEY key = hid_keys_name[i].key;
|
||||||
char *name = hid_keys[i].name;
|
char *name = hid_keys_name[i].name;
|
||||||
|
|
||||||
if (kDown & key) {
|
if (kDown & key) {
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
|
|
@ -134,7 +135,7 @@ static int hid_volume(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct luaL_Reg hid_lib[] = {
|
static const struct luaL_Reg hid_lib[] = {
|
||||||
{ "read", hid_read },
|
{ "keys", hid_keys },
|
||||||
{ "touch", hid_touch },
|
{ "touch", hid_touch },
|
||||||
{ "circle", hid_circle },
|
{ "circle", hid_circle },
|
||||||
{ "accel", hid_accel },
|
{ "accel", hid_accel },
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#define BOOT_FILE "/ctruLua/main.lua"
|
#define BOOT_FILE "/ctruLua/main.lua"
|
||||||
|
|
||||||
int load_ctr_lib(lua_State *L);
|
int load_ctr_lib(lua_State *L);
|
||||||
|
void unload_font_lib();
|
||||||
|
|
||||||
// Display an error
|
// Display an error
|
||||||
void error(char *error) {
|
void error(char *error) {
|
||||||
|
|
@ -18,7 +19,7 @@ void error(char *error) {
|
||||||
printf("------------------ FATAL ERROR -------------------");
|
printf("------------------ FATAL ERROR -------------------");
|
||||||
printf(error);
|
printf(error);
|
||||||
printf("\n--------------------------------------------------");
|
printf("\n--------------------------------------------------");
|
||||||
printf("Please exit ctruLua by pressing the home button.");
|
printf("Please exit ctruLua by rebooting the console.");
|
||||||
|
|
||||||
while (aptMainLoop()) {
|
while (aptMainLoop()) {
|
||||||
gfxFlushBuffers();
|
gfxFlushBuffers();
|
||||||
|
|
@ -40,12 +41,15 @@ int main() {
|
||||||
|
|
||||||
// Init Lua
|
// Init Lua
|
||||||
lua_State *L = luaL_newstate();
|
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);
|
luaL_openlibs(L);
|
||||||
load_ctr_lib(L);
|
load_ctr_lib(L);
|
||||||
|
|
||||||
// Do the actual thing
|
// 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
|
// Disable accel/gyro
|
||||||
HIDUSER_DisableAccelerometer();
|
HIDUSER_DisableAccelerometer();
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ static int news_notification(lua_State *L) {
|
||||||
bool jpeg = false;
|
bool jpeg = false;
|
||||||
if (lua_isboolean(L, 4))
|
if (lua_isboolean(L, 4))
|
||||||
jpeg = lua_toboolean(L, 4);
|
jpeg = lua_toboolean(L, 4);
|
||||||
|
|
||||||
const u16* cTitle = 0;
|
const u16* cTitle = 0;
|
||||||
const u16* cMessage = 0;
|
const u16* cMessage = 0;
|
||||||
u32 titleLength, messageLength, imageDataLength = 0;
|
u32 titleLength, messageLength, imageDataLength = 0;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue