From 575b140a57e2bb86f05b8d18f01a9f2cea1f5034 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Apr 2017 18:53:18 +0200 Subject: more glitch art :) --- src/lib/include/kogata/draw.h | 3 ++- src/lib/libkogata/draw.c | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src/lib') diff --git a/src/lib/include/kogata/draw.h b/src/lib/include/kogata/draw.h index d00036f..d8a2e13 100644 --- a/src/lib/include/kogata/draw.h +++ b/src/lib/include/kogata/draw.h @@ -57,7 +57,8 @@ void g_scroll_up(fb_t *fb, int l); // ---- Text manipulation font_t *g_load_font(const char* fontname); -void g_free_font(font_t *f); +void g_incref_font(font_t *f); +void g_decref_font(font_t *f); int g_text_width(font_t *f, const char* text); int g_text_height(font_t *f, const char* text); diff --git a/src/lib/libkogata/draw.c b/src/lib/libkogata/draw.c index d9a4268..29cae31 100644 --- a/src/lib/libkogata/draw.c +++ b/src/lib/libkogata/draw.c @@ -244,6 +244,7 @@ typedef struct font { uint32_t nchars; } ascii_bitmap; }; + int nrefs; } font_t; font_t *g_load_ascii_bitmap_font(fd_t f) { @@ -272,6 +273,8 @@ font_t *g_load_ascii_bitmap_font(fd_t f) { size_t rd = sc_read(f, sizeof(h), h.ch * h.nchars, (char*)font->ascii_bitmap.data); if (rd != h.ch * h.nchars) goto error; + font->nrefs = 1; + return font; error: @@ -291,12 +294,19 @@ font_t *g_load_font(const char* fontname) { return 0; } -void g_free_font(font_t *f) { - if (f->type == FONT_ASCII_BITMAP) { - free(f->ascii_bitmap.data); - } +void g_incref_font(font_t *f) { + f->nrefs++; +} - free(f); +void g_decref_font(font_t *f) { + f->nrefs--; + + if (f->nrefs == 0) { + if (f->type == FONT_ASCII_BITMAP) { + free(f->ascii_bitmap.data); + } + free(f); + } } int g_text_width(font_t *font, const char* text) { -- cgit v1.2.3