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

Updated font and fs lib; updated editor; updated sf2dlib

Libs additions: font.load, font:unload, font:getWidth, fs.getDirectory, fs.setDirectory, fs.exists

Editor additions: syntaxic coloring and mono font

sf2dlib update: you will need the latest version of ctrulib.

Also, because of the lib font needs, the sftdlib was modified.
This commit is contained in:
Reuh 2015-09-05 19:00:36 +02:00
parent 3f995629c0
commit 45f3216ed8
14 changed files with 382 additions and 28 deletions

View file

@ -1,27 +1,126 @@
#include <unistd.h>
#include <stdlib.h>
#include <sftd.h>
#include "vera_ttf.h"
#include <lua.h>
#include <lauxlib.h>
sftd_font *font_default;
#include "font.h"
static int font_load(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
font_userdata *font = lua_newuserdata(L, sizeof(*font));
luaL_getmetatable(L, "LFont");
lua_setmetatable(L, -2);
font->font = sftd_load_font_file(path);
// SFTD doesn't actually check if the file exist, so we have to do this ourselves.
if (font->font == NULL || access(path, F_OK) != 0) {
lua_pushnil(L);
lua_pushfstring(L, "No valid font file at %s", path);
return 2;
}
return 1;
}
static int font_setDefault(lua_State *L) {
if (luaL_testudata(L, 1, "LFont") == NULL) {
font_userdata *font = lua_newuserdata(L, sizeof(*font));
luaL_getmetatable(L, "LFont");
lua_setmetatable(L, -2);
font->font = sftd_load_font_mem(vera_ttf, vera_ttf_size);
}
lua_setfield(L, LUA_REGISTRYINDEX, "LFontDefault");
return 0;
}
static int font_getDefault(lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, "LFontDefault");
return 1;
}
static int font_object_width(lua_State *L) {
font_userdata *font = luaL_checkudata(L, 1, "LFont");
if (font->font == NULL) luaL_error(L, "The font object was unloaded");
size_t len;
const char *text = luaL_checklstring(L, 2, &len);
int size = luaL_optinteger(L, 3, 9);
// Wide caracters support. (wchar = UTF32 on 3DS.)
wchar_t wtext[len];
len = mbstowcs(wtext, text, len);
*(wtext+len) = 0x0; // text end
lua_pushinteger(L, sftd_width_wtext(font->font, size, wtext));
return 1;
}
static int font_object_unload(lua_State *L) {
font_userdata *font = luaL_checkudata(L, 1, "LFont");
if (font->font == NULL) return 0;
sftd_free_font(font->font);
font->font = NULL;
return 0;
}
// Font object methods
static const struct luaL_Reg font_object_methods[] = {
{ "width", font_object_width },
{ "unload", font_object_unload },
{ "__gc", font_object_unload },
{ NULL, NULL }
};
// Library functions
static const struct luaL_Reg font_lib[] = {
{ "load", font_load },
{ "setDefault", font_setDefault },
{ "getDefault", font_getDefault },
{ NULL, NULL }
};
int luaopen_font_lib(lua_State *L) {
luaL_newmetatable(L, "LFont");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, font_object_methods, 0);
luaL_newlib(L, font_lib);
return 1;
}
void load_font_lib(lua_State *L) {
font_default = sftd_load_font_mem(vera_ttf, vera_ttf_size); // Load default font
// Load default font
font_userdata *font = lua_newuserdata(L, sizeof(*font));
luaL_getmetatable(L, "LFont");
lua_setmetatable(L, -2);
font->font = sftd_load_font_mem(vera_ttf, vera_ttf_size);
lua_setfield(L, LUA_REGISTRYINDEX, "LFontDefault");
// Load lib
luaL_requiref(L, "ctr.gfx.font", luaopen_font_lib, false);
}
void unload_font_lib(lua_State *L) {
sftd_free_font(font_default); // Unload current font
lua_getfield(L, LUA_REGISTRYINDEX, "LFontDefault");
if (luaL_testudata(L, -1, "LFont") != NULL)
sftd_free_font(((font_userdata *)lua_touserdata(L, -1))->font); // Unload current font
}

8
source/font.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef FONT_H
#define FONT_H
typedef struct {
sftd_font *font;
} font_userdata;
#endif

View file

@ -1,3 +1,5 @@
#include <unistd.h>
#include <3ds/types.h>
#include <3ds/util/utf.h>
#include <3ds/services/fs.h>
@ -8,7 +10,7 @@
Handle *fsuHandle;
FS_archive sdmcArchive;
int fs_list(lua_State *L) {
static int fs_list(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
lua_newtable(L);
@ -60,8 +62,40 @@ int fs_list(lua_State *L) {
return 1;
}
static int fs_exists(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
lua_pushboolean(L, access(path, F_OK) == 0);
return 1;
}
static int fs_getDirectory(lua_State *L) {
char cwd[256];
lua_pushstring(L, getcwd(cwd, 256));
return 1;
}
static int fs_setDirectory(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int result = chdir(path);
if (result == 0)
lua_pushboolean(L, true);
else
lua_pushboolean(L, false);
return 1;
}
static const struct luaL_Reg fs_lib[] = {
{ "list", fs_list },
{ "list", fs_list },
{ "exists", fs_exists },
{ "getDirectory", fs_getDirectory },
{ "setDirectory", fs_setDirectory },
{ NULL, NULL }
};

View file

@ -8,6 +8,8 @@
#include <lua.h>
#include <lauxlib.h>
#include "font.h"
bool isGfxInitialised = false;
void load_color_lib(lua_State *L);
@ -18,7 +20,6 @@ void load_map_lib(lua_State *L);
void unload_font_lib(lua_State *L);
u32 color_default;
sftd_font *font_default;
static int gfx_startFrame(lua_State *L) {
u8 screen = luaL_checkinteger(L, 1);
@ -124,14 +125,20 @@ static int gfx_text(lua_State *L) {
int size = luaL_optinteger(L, 4, 9);
u32 color = luaL_optinteger(L, 5, color_default);
// todo : font selection
font_userdata *font = luaL_testudata(L, 6, "LFont");
if (font == NULL) {
lua_getfield(L, LUA_REGISTRYINDEX, "LFontDefault");
font = luaL_testudata(L, -1, "LFont");
if (font == NULL) luaL_error(L, "No default font set and no font object passed");
}
if (font->font == NULL) luaL_error(L, "The font object was unloaded");
// Wide caracters support. (wchar = UTF32 on 3DS.)
wchar_t wtext[len];
len = mbstowcs(wtext, text, len);
*(wtext+len) = 0x0; // text end
sftd_draw_wtext(font_default, x, y, color, size, wtext);
sftd_draw_wtext(font->font, x, y, color, size, wtext);
return 0;
}