base.c
#include <math.h>
#include "inc.h"
static LUAFUNC(new){
const int w = luaL_checkinteger(L, 1);
const int h = luaL_optinteger(L, 2, w);
luaL_argcheck(L, w >= 1, 2, "Width must be >= 1");
luaL_argcheck(L, h >= 1, 3, "Height must be >= 1");
const lil_Image* img = lil_newImageCalloc(L, w, h);
if(!img)
return 0;
lil_pushImage(L, img);
return 1;
}
#ifdef LIL_USE_FREETYPE
static LUAFUNC(font){
const char* nameOrPath = luaL_checkstring(L, 1);
const lil_Colour col = lil_getColour(L, 2, NULL);
const char* path = NULL;
bool isPath = false;
for(int i = 0; nameOrPath[i]; i++){
#ifdef _WIN32
#define CS '\\'
#else
#define CS '/'
#endif
if(nameOrPath[i] == CS){
isPath = true;
break;
}
#undef CS
}
if(isPath){
path = nameOrPath;
} else {
path = lil_findFontPath(nameOrPath);
if(!path)
return luaL_error(L, "Failed to load font");
}
const int w = luaL_checkinteger(L, 3);
const int h = luaL_optinteger(L, 4, w);
int opti = 4;
if(lua_type(L, opti) == LUA_TNUMBER)
opti++;
luaL_argcheck(L, w > 1 && h > 1, 3, "Font size invalid");
lil_Font* font = lil_newFont(path, w, h, col);
if(!isPath)
free((char*)path);
if(!font)
return luaL_error(L, "Failed to load font");
*(lil_Font**)lua_newuserdata(L, sizeof(lil_Font**)) = font;
luaL_getmetatable(L, LIL_FONT_UD_MT);
lua_setmetatable(L, -2);
return 1;
}
static LUAFUNC(findFontPath){
const char* name = luaL_checkstring(L, 1);
const char* path = lil_findFontPath(name);
if(!path)
return 0;
lua_pushstring(L, path);
return 1;
}
#endif
LUAREG(lilLib) = {
{ "new", LUAFUNCD(new) },
#ifdef LIL_USE_FREETYPE
{ "font", LUAFUNCD(font) },
{ "findFontPath", LUAFUNCD(findFontPath) },
#endif
{ NULL, NULL }
};