diff --git a/text_rendering/atlas.png b/text_rendering/atlas.png index c1e59c6..754a74d 100644 Binary files a/text_rendering/atlas.png and b/text_rendering/atlas.png differ diff --git a/text_rendering/font.c b/text_rendering/font.c index 0188c43..c50ac54 100644 --- a/text_rendering/font.c +++ b/text_rendering/font.c @@ -123,14 +123,19 @@ const struct font_glyph * font_get_glyph_texture(struct font_atlas *atlas, unsig atlas->priv.scale, 2.0f/glyph_h, &atlas->priv.ctx); - if (!err) - return NULL; + // msdf_genGlyph returns 0 only when there are no contours, so only for + // whitespace and such, for those insert a zero uv map into the cache + // FIXME: this is a waste of space + if (!err) { + atlas->priv.msdf.width = 0; + atlas->priv.msdf.height = 0; + } unsigned int spot = cache_get(); unsigned int oy = (glyph_h * spot) / atlas->width; unsigned int ox = (glyph_h * spot) % atlas->height; - unsigned int w = atlas->width; + unsigned int w = atlas->width; // sum magic shit struct {unsigned char r,g,b;} *a = (void *)atlas->atlas; @@ -180,7 +185,7 @@ const struct font_glyph * font_get_glyph_texture(struct font_atlas *atlas, unsig void font_dump(const struct font_atlas *atlas, const char *path) { stbi_write_png( - path, + path, //atlas->width, //atlas->height, 128, 128, diff --git a/text_rendering/ren.c b/text_rendering/ren.c new file mode 100644 index 0000000..90313c4 --- /dev/null +++ b/text_rendering/ren.c @@ -0,0 +1,53 @@ +#include +#include +#include + + +#define GLSL_VERT_SHADER "vertex.glsl" +#define GLSL_FRAG_SHADER "fragment.glsl" +#define PACKED __attribute__((packed)) + + +struct { + SDL_Window *w; + SDL_GLContext *gl; + GLuint gl_vertbuffer; + GLuint gl_program; + GLuint font_texture; +} ren = {0}; + + +typedef struct PACKED { + union { GLint x, u; }; + union { GLint y, v; }; +} vec2_i; + +typedef struct PACKED { + union { GLint x, r; }; + union { GLint y, g; }; + union { GLint z, b; }; + union { GLint w, a; }; +} vec4_i; + +// textured vertex +struct PACKED v_text { + vec2_i pos; + vec2_i uv; +}; + +// colored vertex +struct PACKED v_col { + vec2_i pos; + vec2_i col; +}; + + +// different stacks +#define _STACK_NAME vtstack +#define _STACK_TYPE struct v_text +#include "stack.h" +#undef _STACK_NAME +#undef _STACK_TYPE +#define _STACK_NAME vcstack +#define _STACK_TYPE struct v_col +#include "stack.h" diff --git a/text_rendering/stack.h b/text_rendering/stack.h new file mode 100644 index 0000000..4074b1f --- /dev/null +++ b/text_rendering/stack.h @@ -0,0 +1,61 @@ +#ifdef _STACK_TYPE +#ifdef _STACK_NAME + + +#ifndef _STACK_REALLOC + #define _STACK_REALLOC(p, s) realloc(p,s) +#endif +#ifndef _STACK_MEMSET + #define _STACK_MEMSET(p, c, s) memset(p,c,s) +#endif +#ifndef _STACK_STEP + #define _STACK_STEP 8 +#endif +#ifndef _STACK_CAT + #define _STACK_CAT_I(a,b) a ## b + #define _STACK_CAT(a,b) _STACK_CAT_I(a,b) +#endif + + +// TODO: add a rolling hash +struct _STACK_NAME { + _STACK_TYPE *items; + int size, idx; +}; + + +int _STACK_CAT(stack_grow_,_STACK_NAME)(struct _STACK_NAME *stack, int step) +{ + if (!stack) + return -1; + stack->items = _STACK_REALLOC(stack->items, (stack->size+step)*sizeof(_STACK_TYPE)); + if(!stack->items) + return -1; + _STACK_MEMSET(&(stack->items[stack->size]), 0, step*sizeof(*(stack->items))); + stack->size += step; + return 0; +} + + +int _STACK_CAT(stack_push_,_STACK_NAME)(struct _STACK_NAME *stack, _STACK_TYPE *e) +{ + if (!stack || !e) + return -1; + if (stack->idx >= stack->size) + if (_STACK_CAT(stack_grow_,_STACK_NAME)(stack, _STACK_STEP)) + return -1; + stack->items[stack->idx++] = *e; + return 0; +} + + +int _STACK_CAT(stack_clear_,_STACK_NAME)(struct _STACK_NAME *stack) +{ + if (!stack) + return -1; + stack->idx = 0; +} + + +#endif +#endif diff --git a/text_rendering/test b/text_rendering/test index 7ba4c45..69d543b 100755 Binary files a/text_rendering/test and b/text_rendering/test differ