font: add draw_float

This commit is contained in:
Zack Buhman 2025-07-16 21:40:48 -05:00
parent fc8ac2170e
commit 25817f7780
3 changed files with 58 additions and 0 deletions

View File

@ -4,6 +4,8 @@
#include "demo/ta_parameter_presets.hpp"
#include "demo/texture.hpp"
#include "printf/unparse.h"
namespace font {
static inline vec2 transform_glyph_texture(const face& face, const vec2& t, int char_code)
@ -70,6 +72,40 @@ namespace font {
base_color);
}
static inline int format_float(char * s, float num)
{
int offset = 0;
bool negative = num < 0;
if (negative) {
s[offset++] = '-';
num = -num;
}
int32_t whole = num;
offset += unparse_base10_unsigned(&s[offset], whole, 0, 0);
s[offset++] = '.';
int32_t fraction = (int32_t)((num - (float)whole) * 1000.0);
if (fraction < 0)
fraction = -fraction;
offset += unparse_base10_unsigned(&s[offset], fraction, 3, '0');
return offset;
}
void face::draw_float(ta_parameter_writer& writer,
const vec3& p,
float num,
uint32_t base_color,
int length) const
{
char s[20];
int offset = format_float(s, num);
s[offset] = 0;
float x = p.x;
if (offset < length) {
x += hori_advance * (length - offset);
}
draw_string(writer, {x, p.y, p.z}, s, base_color);
}
const face ter_u12n = {
.texture_size = tsp_instruction_word::texture_u_size::from_int(128)
| tsp_instruction_word::texture_v_size::from_int(64),

View File

@ -22,6 +22,26 @@ namespace font {
const vec3& p,
int c,
uint32_t base_color) const;
inline void draw_string(ta_parameter_writer& writer,
const vec3& p,
const char * s,
uint32_t base_color) const
{
const uint8_t * u8 = (const uint8_t *)s;
float x = p.x;
while (*u8) {
int c = *u8++;
draw_glyph(writer, {x, p.y, p.z}, c, base_color);
x += hori_advance;
}
}
void draw_float(ta_parameter_writer& writer,
const vec3& p,
float num,
uint32_t base_color,
int length = 0) const;
};
extern const face ter_u12n;

View File

@ -96,6 +96,8 @@ namespace graphics {
{
font::ter_u12n.global(writer);
font::ter_u12n.draw_glyph(writer, vec3(10, 10, 10), 'a', 0xffffffff);
font::ter_u12n.draw_float(writer, vec3(10, 22, 10), 1.234, 0xffffffff, 7);
font::ter_u12n.draw_float(writer, vec3(10, 34, 10), -50.234, 0xffffffff, 7);
writer.append<ta_global_parameter::end_of_list>() =
ta_global_parameter::end_of_list(para_control::para_type::end_of_list);