Cache scaled SDF mipmaps for faster rendering

This commit is contained in:
K. Lange 2018-04-21 17:24:55 +09:00 committed by Kevin Lange
parent 629a4f373f
commit f9196a3181
2 changed files with 17 additions and 7 deletions

View File

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <toaru/graphics.h>
#include <toaru/hashmap.h>
#include <toaru/sdf.h>
#define BASE_WIDTH 50
@ -16,6 +17,8 @@
static sprite_t _font_data_thin;
static sprite_t _font_data_bold;
static hashmap_t * _font_cache;
struct {
char code;
size_t width_bold;
@ -123,6 +126,7 @@ static int loaded = 0;
__attribute__((constructor))
static void _init_sdf(void) {
/* Load the font. */
_font_cache = hashmap_create_int(10);
load_sprite(&_font_data_thin, "/usr/share/sdf_thin.bmp");
load_sprite(&_font_data_bold, "/usr/share/sdf_bold.bmp");
loaded = 1;
@ -198,9 +202,18 @@ int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str,
if (!loaded) return 0;
double scale = (double)size / 50.0;
sprite_t * tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE);
gfx_context_t * t = init_graphics_sprite(tmp);
draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height);
int scale_height = scale * _font_data->height;
sprite_t * tmp;
if (!hashmap_has(_font_cache, (void *)(scale_height | (font << 16)))) {
tmp = create_sprite(scale * _font_data->width, scale * _font_data->height, ALPHA_OPAQUE);
gfx_context_t * t = init_graphics_sprite(tmp);
draw_sprite_scaled(t, _font_data, 0, 0, tmp->width, tmp->height);
free(t);
hashmap_set(_font_cache, (void *)(scale_height | (font << 16)), tmp);
} else {
tmp = hashmap_get(_font_cache, (void *)(scale_height | (font << 16)));
}
int32_t out_width = 0;
while (*str) {
@ -210,9 +223,6 @@ int draw_sdf_string(gfx_context_t * ctx, int32_t x, int32_t y, const char * str,
str++;
}
sprite_free(tmp);
free(t);
return out_width;
}

View File

@ -34,7 +34,7 @@ class Classifier(object):
'<toaru/yutani.h>': (None, '-ltoaru_yutani', ['<toaru/kbd.h>', '<toaru/list.h>', '<toaru/pex.h>', '<toaru/graphics.h>', '<toaru/hashmap.h>']),
'<toaru/decorations.h>': (None, '-ltoaru_decorations', ['<toaru/graphics.h>', '<toaru/yutani.h>','<toaru/dlfcn.h>']),
'<toaru/termemu.h>': (None, '-ltoaru_termemu', ['<toaru/graphics.h>']),
'<toaru/sdf.h>': (None, '-ltoaru_sdf', ['<toaru/graphics.h>']),
'<toaru/sdf.h>': (None, '-ltoaru_sdf', ['<toaru/graphics.h>', '<toaru/hashmap.h>']),
}
def __init__(self, filename):