Improve newFont API;

This commit is contained in:
bjorn 2017-02-16 16:41:46 -08:00
parent 078d5290c0
commit ce89900dcc
3 changed files with 23 additions and 9 deletions

View File

@ -5,7 +5,6 @@
#include "math/vec3.h"
#include "util.h"
#include "glfw.h"
#include "Cabin.ttf.h"
#define _USE_MATH_DEFINES
#include <stdlib.h>
#include <math.h>
@ -193,7 +192,7 @@ void lovrGraphicsSetShader(Shader* shader) {
void lovrGraphicsEnsureFont() {
if (!state.activeFont && !state.defaultFont) {
FontData* fontData = lovrFontDataCreate(Cabin_ttf, Cabin_ttf_len, 32);
FontData* fontData = lovrFontDataCreate(NULL, 0, 32);
state.defaultFont = lovrFontCreate(fontData);
lovrRetain(&state.defaultFont->ref);
lovrGraphicsSetFont(state.defaultFont);

View File

@ -1,4 +1,5 @@
#include "loaders/font.h"
#include "loaders/Cabin.ttf.h"
#include "util.h"
#include <ft2build.h>
#include FT_FREETYPE_H
@ -11,6 +12,11 @@ FontData* lovrFontDataCreate(void* data, int size, int height) {
error("Error initializing FreeType");
}
if (!data) {
data = Cabin_ttf;
size = Cabin_ttf_len;
}
FT_Face face = NULL;
FT_Error err = FT_Err_Ok;
err = err || FT_New_Memory_Face(ft, data, size, 0, &face);

View File

@ -634,16 +634,25 @@ int l_lovrGraphicsNewBuffer(lua_State* L) {
}
int l_lovrGraphicsNewFont(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
float fontSize = luaL_optnumber(L, 2, 12);
void* data = NULL;
int size = 0;
float fontSize;
int fileSize;
void* data = lovrFilesystemRead(path, &fileSize);
if (!data) {
luaL_error(L, "Could not load font '%s'", path);
if (lua_type(L, 1) == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
data = NULL;
size = 0;
fontSize = luaL_optnumber(L, 1, 32);
} else {
const char* path = luaL_checkstring(L, 1);
fontSize = luaL_optnumber(L, 2, 32);
data = lovrFilesystemRead(path, &size);
if (!data) {
luaL_error(L, "Could not load font '%s'", path);
}
}
FontData* fontData = lovrFontDataCreate(data, fileSize, fontSize);
FontData* fontData = lovrFontDataCreate(data, size, fontSize);
Font* font = lovrFontCreate(fontData);
luax_pushtype(L, Font, font);
return 1;