Read font glyph width from texture

This commit is contained in:
xtreme8000 2023-10-09 16:31:38 +02:00
parent 603ff814c2
commit 73a4dae4e2
3 changed files with 44 additions and 9 deletions

View file

@ -107,14 +107,44 @@ void gutil_bg() {
}
}
static const uint8_t font_char_width[256] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 1, 4, 5, 5, 5, 5, 2, 4, 4, 4, 5,
1, 5, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 4, 5, 4, 5, 6, 5,
5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 3, 5, 3, 5, 5, 2, 5, 5, 5, 5, 5, 4, 5, 5, 1, 5, 4, 2, 5,
5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 4, 1, 4, 6, 5,
};
static uint8_t font_char_width[256];
void gutil_reset_font(struct tex_gfx* tex) {
assert(tex);
int char_width = tex->width / 16;
int char_height = tex->height / 16;
for(int y = 0; y < 16; y++) {
for(int x = 0; x < 16; x++) {
int width = 0;
for(int i = 0; i < char_width; i++) {
bool has_pixel = false;
for(int j = 0; j < char_height; j++) {
uint8_t col[4];
tex_gfx_lookup(tex, x * char_width + i, y * char_height + j,
col);
if(col[0] || col[1] || col[2]) {
has_pixel = true;
break;
}
}
if(!has_pixel)
break;
width++;
}
font_char_width[x + y * 16] = width * 8 / char_width;
}
}
font_char_width[' '] = 4;
}
int gutil_font_width(char* str, int scale) {
int x = 0;

View file

@ -25,6 +25,7 @@
#include "../item/items.h"
#include "../platform/input.h"
#include "../platform/texture.h"
int gutil_control_icon(int x, enum input_button b, char* str);
void gutil_texquad_col(int x, int y, int tx, int ty, int sx, int sy, int width,
@ -36,6 +37,7 @@ void gutil_texquad_rt(int x, int y, int tx, int ty, int sx, int sy, int width,
void gutil_texquad_rt_any(int x, int y, float angle, int tx, int ty, int sx,
int sy, float width, float height);
void gutil_bg(void);
void gutil_reset_font(struct tex_gfx* tex);
int gutil_font_width(char* str, int scale);
void gutil_text(int x, int y, char* str, int scale, bool shadow);
void gutil_draw_item(struct item_data* item, int x, int y, int layer);

View file

@ -23,6 +23,7 @@
#include "../config.h"
#include "../game/game_state.h"
#include "../graphics/gui_util.h"
#include "../lodepng/lodepng.h"
#include "texture.h"
@ -80,6 +81,8 @@ void tex_init() {
tex_gfx_load(&texture_terrain, output, w, h, TEX_FMT_RGBA16, false);
tex_gfx_load_file(&texture_font, "default.png", TEX_FMT_I8, false);
gutil_reset_font(&texture_font);
tex_gfx_load_file(&texture_anim, "anim.png", TEX_FMT_RGBA32, false);
tex_gfx_load_file(&texture_gui_inventory, "gui/inventory.png",
TEX_FMT_RGBA16, false);