mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-27 16:39:29 +00:00
Added some code
This commit is contained in:
parent
5f22a4008a
commit
03baa21c10
80 changed files with 36025 additions and 0 deletions
30
source/ctr.c
Normal file
30
source/ctr.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
int load_gfx_lib(lua_State *L);
|
||||
int load_os_lib(lua_State *L);
|
||||
|
||||
static const struct luaL_Reg ctr_lib[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
|
||||
{ "gfx", load_gfx_lib },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
int luaopen_ctr_lib(lua_State *L) {
|
||||
luaL_newlib(L, ctr_lib);
|
||||
|
||||
for (int i = 0; ctr_libs[i].name; i++) {
|
||||
ctr_libs[i].load(L);
|
||||
lua_setfield(L, -2, ctr_libs[i].name);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void load_ctr_lib(lua_State *L) {
|
||||
load_os_lib(L);
|
||||
luaL_requiref(L, "ctr", luaopen_ctr_lib, 0);
|
||||
}
|
||||
41
source/gfx.c
Normal file
41
source/gfx.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <sf2d.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
u32 defaultColor = RGBA8(255, 255, 255, 255);
|
||||
|
||||
static int gfx_render(lua_State *L) {
|
||||
sf2d_end_frame();
|
||||
sf2d_swapbuffers();
|
||||
sf2d_start_frame(GFX_TOP, GFX_LEFT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int gfx_rectangle(lua_State *L) {
|
||||
int x = luaL_checkinteger(L, 1);
|
||||
int y = luaL_checkinteger(L, 2);
|
||||
int width = luaL_checkinteger(L, 3);
|
||||
int height = luaL_checkinteger(L, 4);
|
||||
u32 color = luaL_optinteger(L, 5, defaultColor);
|
||||
|
||||
sf2d_draw_rectangle(x, y, width, height, color);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg gfx_lib[] = {
|
||||
{ "render", gfx_render },
|
||||
{ "rectangle", gfx_rectangle },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int luaopen_gfx_lib(lua_State *L) {
|
||||
luaL_newlib(L, gfx_lib);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void load_gfx_lib(lua_State *L) {
|
||||
luaL_requiref(L, "ctr.gfx", luaopen_gfx_lib, 0);
|
||||
}
|
||||
53
source/main.c
Normal file
53
source/main.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <3ds.h>
|
||||
#include <sf2d.h>
|
||||
#include <sftd.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#define BOOT_FILE "/ctruLua/main.lua"
|
||||
|
||||
int load_ctr_lib(lua_State *L);
|
||||
|
||||
// Display an error
|
||||
void error(char *error) {
|
||||
gfxInitDefault();
|
||||
|
||||
consoleInit(GFX_TOP, NULL);
|
||||
printf("------------------ FATAL ERROR -------------------");
|
||||
printf(error);
|
||||
printf("\n--------------------------------------------------");
|
||||
printf("Please exit ctruLua by pressing the home button.");
|
||||
|
||||
while (aptMainLoop()) {
|
||||
gfxFlushBuffers();
|
||||
gfxSwapBuffers();
|
||||
gspWaitForVBlank();
|
||||
}
|
||||
}
|
||||
|
||||
// Main loop
|
||||
int main() {
|
||||
// Init GFX
|
||||
sf2d_init();
|
||||
sftd_init();
|
||||
//sf2d_set_3d(true);
|
||||
|
||||
// Init Lua
|
||||
lua_State *L = luaL_newstate();
|
||||
if (L == NULL) error("memory allocation error while creating a new Lua state");
|
||||
luaL_openlibs(L);
|
||||
load_ctr_lib(L);
|
||||
|
||||
// Do the actual thing
|
||||
luaL_dofile(L, BOOT_FILE);
|
||||
|
||||
// Un-init (?)
|
||||
sftd_fini();
|
||||
sf2d_fini();
|
||||
return 0;
|
||||
}
|
||||
22
source/os.c
Normal file
22
source/os.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <3ds.h>
|
||||
|
||||
#include <lua.h>
|
||||
|
||||
static int os_run(lua_State *L) {
|
||||
bool run = aptMainLoop();
|
||||
|
||||
lua_pushboolean(L, run);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void load_os_lib(lua_State *L) {
|
||||
lua_getglobal(L, "os");
|
||||
|
||||
lua_pushcfunction(L, os_run);
|
||||
lua_setfield(L, -2, "run");
|
||||
|
||||
lua_pop(L, 1);
|
||||
|
||||
return;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue