Began separating lua bindings, moved to src/modules dir

This commit is contained in:
rxi
2016-10-15 15:01:36 +01:00
parent 98800f0a73
commit 3de597d997
11 changed files with 321 additions and 303 deletions

View File

@@ -13,7 +13,6 @@
#include "lib/stb/stb_truetype.h"
#include "filesystem.h"
#include "font.h"
#include "luaobj.h"
static const char *initFont(font_t *self, const void *data, int ptsize) {
@@ -144,70 +143,3 @@ void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh,
image_blendMode = oldBlendMode;
image_flip = oldFlip;
}
#define CLASS_TYPE LUAOBJ_TYPE_FONT
#define CLASS_NAME "Font"
int l_font_new(lua_State *L) {
const char *filename;
int ptsize = 12;
if ( lua_isnoneornil(L, 2) ) {
filename = NULL;
ptsize = luaL_optnumber(L, 1, ptsize);
} else {
filename = luaL_checkstring(L, 1);
ptsize = luaL_optnumber(L, 2, ptsize);
}
font_t *self = luaobj_newudata(L, sizeof(*self));
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
if (filename) {
const char *err = font_init(self, filename, ptsize);
if (err) luaL_error(L, err);
} else {
font_initEmbedded(self, ptsize);
}
return 1;
}
int l_font_gc(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
font_deinit(self);
return 0;
}
int l_font_getWidth(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
const char *p = luaL_checkstring(L, 2);
int width = 0;
while (*p) {
width += self->glyphs[(int) (*p++ & 127)].xadvance;
}
lua_pushinteger(L, width);
return 1;
}
int l_font_getHeight(lua_State *L) {
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
lua_pushinteger(L, self->height);
return 1;
}
int luaopen_font(lua_State *L) {
luaL_Reg reg[] = {
{ "new", l_font_new },
{ "__gc", l_font_gc },
{ "getWidth", l_font_getWidth },
{ "getHeight", l_font_getHeight },
{ 0, 0 },
};
luaobj_newclass(L, CLASS_NAME, NULL, l_font_new, reg);
return 1;
}