lovr.graphics.newFont;

This commit is contained in:
bjorn 2022-06-18 17:43:12 -07:00
parent b89c61a8f4
commit a654cec40f
4 changed files with 157 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#include "graphics/graphics.h"
#include "data/blob.h"
#include "data/image.h"
#include "data/rasterizer.h"
#include "util.h"
#include <lua.h>
#include <lauxlib.h>
@ -1206,6 +1207,42 @@ static int l_lovrGraphicsNewMaterial(lua_State* L) {
return 1;
}
static int l_lovrGraphicsNewFont(lua_State* L) {
FontInfo info = { 0 };
info.rasterizer = luax_totype(L, 1, Rasterizer);
info.padding = 1;
info.spread = 4.;
if (!info.rasterizer) {
Blob* blob = NULL;
float size;
if (lua_type(L, 1) == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
size = luax_optfloat(L, 1, 32.f);
info.padding = luaL_optinteger(L, 2, info.padding);
info.spread = luaL_optnumber(L, 3, info.spread);
} else {
blob = luax_readblob(L, 1, "Font");
size = luax_optfloat(L, 2, 32.f);
info.padding = luaL_optinteger(L, 3, info.padding);
info.spread = luaL_optnumber(L, 4, info.spread);
}
info.rasterizer = lovrRasterizerCreate(blob, size);
lovrRelease(blob, lovrBlobDestroy);
} else {
info.padding = luaL_optinteger(L, 2, info.padding);
info.spread = luaL_optnumber(L, 3, info.spread);
}
Font* font = lovrFontCreate(&info);
luax_pushtype(L, Font, font);
lovrRelease(info.rasterizer, lovrRasterizerDestroy);
lovrRelease(font, lovrFontDestroy);
return 1;
}
static int l_lovrGraphicsGetPass(lua_State* L) {
PassInfo info;
info.type = luax_checkenum(L, 1, PassType, NULL);
@ -1235,6 +1272,7 @@ static const luaL_Reg lovrGraphics[] = {
{ "compileShader", l_lovrGraphicsCompileShader },
{ "newShader", l_lovrGraphicsNewShader },
{ "newMaterial", l_lovrGraphicsNewMaterial },
{ "newFont", l_lovrGraphicsNewFont },
{ "getPass", l_lovrGraphicsGetPass },
{ NULL, NULL }
};
@ -1244,6 +1282,7 @@ extern const luaL_Reg lovrTexture[];
extern const luaL_Reg lovrSampler[];
extern const luaL_Reg lovrShader[];
extern const luaL_Reg lovrMaterial[];
extern const luaL_Reg lovrFont[];
extern const luaL_Reg lovrPass[];
int luaopen_lovr_graphics(lua_State* L) {
@ -1254,6 +1293,7 @@ int luaopen_lovr_graphics(lua_State* L) {
luax_registertype(L, Sampler);
luax_registertype(L, Shader);
luax_registertype(L, Material);
luax_registertype(L, Font);
luax_registertype(L, Pass);
return 1;
}

View File

@ -0,0 +1,7 @@
#include "api.h"
#include <lua.h>
#include <lauxlib.h>
const luaL_Reg lovrFont[] = {
{ NULL, NULL }
};

View File

@ -1,6 +1,7 @@
#include "graphics/graphics.h"
#include "data/blob.h"
#include "data/image.h"
#include "data/rasterizer.h"
#include "headset/headset.h"
#include "math/math.h"
#include "core/gpu.h"
@ -24,6 +25,18 @@ const char** os_vk_get_instance_extensions(uint32_t* count);
#define MAX_SHADER_RESOURCES 32
#define MATERIALS_PER_BLOCK 1024
typedef struct {
struct { float x, y, z; } position;
struct { float x, y, z; } normal;
struct { float u, v; } uv;
} ShapeVertex;
typedef struct {
struct { float x, y; } position;
struct { uint8_t r, g, b, a; } color;
struct { uint16_t u, v; } uv;
} GlyphVertex;
typedef struct {
gpu_phase readPhase;
gpu_phase writePhase;
@ -109,6 +122,31 @@ struct Material {
MaterialInfo info;
};
typedef struct {
uint32_t codepoint;
uint16_t atlas[4];
float width;
float height;
float advance;
float offsetX;
float offsetY;
} Glyph;
struct Font {
uint32_t ref;
FontInfo info;
Material* material;
arr_t(Glyph) glyphs;
map_t glyphLookup;
float pixelDensity;
Texture* atlas;
uint32_t atlasWidth;
uint32_t atlasHeight;
uint32_t rowHeight;
uint32_t atlasX;
uint32_t atlasY;
};
typedef struct {
float view[16];
float projection[16];
@ -139,9 +177,7 @@ typedef struct {
typedef enum {
VERTEX_SHAPE,
VERTEX_POINT,
VERTEX_MODEL,
VERTEX_GLYPH,
VERTEX_EMPTY,
VERTEX_FORMAT_COUNT
} VertexFormat;
@ -206,12 +242,6 @@ struct Pass {
arr_t(Access) access;
};
typedef struct {
struct { float x, y, z; } position;
struct { float x, y, z; } normal;
struct { float u, v; } uv;
} ShapeVertex;
typedef struct {
Material* list;
gpu_buffer* buffer;
@ -421,7 +451,7 @@ bool lovrGraphicsInit(bool debug, bool vsync) {
state.vertexFormats[VERTEX_POINT] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 1,
.attributeCount = 5,
.bufferStrides[1] = 12,
.attributes[0] = { 1, 10, 0, GPU_TYPE_F32x3 },
.attributes[1] = { 0, 11, 0, GPU_TYPE_F32x4 },
@ -430,6 +460,17 @@ bool lovrGraphicsInit(bool debug, bool vsync) {
.attributes[4] = { 0, 14, 0, GPU_TYPE_F32x4 }
};
state.vertexFormats[VERTEX_GLYPH] = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 5,
.bufferStrides[1] = 16,
.attributes[0] = { 1, 10, offsetof(GlyphVertex, position), GPU_TYPE_F32x2 },
.attributes[1] = { 1, 13, offsetof(GlyphVertex, color), GPU_TYPE_UN8x4 },
.attributes[2] = { 1, 12, offsetof(GlyphVertex, uv), GPU_TYPE_UN16x2 },
.attributes[3] = { 0, 11, 0, GPU_TYPE_F32x4 },
.attributes[4] = { 0, 14, 0, GPU_TYPE_F32x4 }
};
state.defaultMaterial = lovrMaterialCreate(&(MaterialInfo) {
.data.color = { 1.f, 1.f, 1.f, 1.f }
});
@ -1643,6 +1684,37 @@ const MaterialInfo* lovrMaterialGetInfo(Material* material) {
return &material->info;
}
// Font
Font* lovrFontCreate(FontInfo* info) {
Font* font = calloc(1, sizeof(Font));
lovrAssert(font, "Out of memory");
font->ref = 1;
font->info = *info;
lovrRetain(info->rasterizer);
map_init(&font->glyphLookup, 36);
arr_init(&font->glyphs, realloc);
return font;
}
void lovrFontDestroy(void* ref) {
Font* font = ref;
lovrRelease(font->info.rasterizer, lovrRasterizerDestroy);
lovrRelease(font->material, lovrMaterialDestroy);
lovrRelease(font->atlas, lovrTextureDestroy);
map_free(&font->glyphLookup);
arr_free(&font->glyphs);
free(font);
}
float lovrFontGetPixelDensity(Font* font) {
return font->pixelDensity;
}
void lovrFontSetPixelDensity(Font* font, float pixelDensity) {
font->pixelDensity = pixelDensity;
}
// Pass
Pass* lovrGraphicsGetPass(PassInfo* info) {
@ -2989,6 +3061,7 @@ static void beginFrame(void) {
state.active = true;
state.tick = gpu_begin();
state.stream = gpu_stream_begin("Internal uploads");
state.allocator.cursor = 0;
arr_clear(&state.passes);
}

View File

@ -6,12 +6,14 @@
struct Blob;
struct Image;
struct Rasterizer;
typedef struct Buffer Buffer;
typedef struct Texture Texture;
typedef struct Sampler Sampler;
typedef struct Shader Shader;
typedef struct Material Material;
typedef struct Font Font;
typedef struct Pass Pass;
typedef struct {
@ -316,6 +318,20 @@ Material* lovrMaterialCreate(MaterialInfo* info);
void lovrMaterialDestroy(void* ref);
const MaterialInfo* lovrMaterialGetInfo(Material* material);
// Font
typedef struct {
struct Rasterizer* rasterizer;
uint32_t padding;
double spread;
} FontInfo;
Font* lovrFontCreate(FontInfo* info);
void lovrFontDestroy(void* ref);
const FontInfo* lovrFontGetInfo(Font* font);
float lovrFontGetPixelDensity(Font* font);
void lovrFontSetPixelDensity(Font* font, float pixelDensity);
// Pass
typedef enum {
@ -351,6 +367,12 @@ typedef enum {
CULL_BACK
} CullMode;
typedef enum {
ALIGN_LEFT,
ALIGN_CENTER,
ALIGN_RIGHT
} HorizontalAlign;
typedef enum {
STENCIL_KEEP,
STENCIL_ZERO,
@ -368,6 +390,12 @@ typedef enum {
VERTEX_TRIANGLES
} VertexMode;
typedef enum {
ALIGN_TOP,
ALIGN_MIDDLE,
ALIGN_BOTTOM
} VerticalAlign;
typedef enum {
WINDING_COUNTERCLOCKWISE,
WINDING_CLOCKWISE