typedef & PascalCase for ALL structs and enums
This commit is contained in:
@ -12,26 +12,8 @@
|
||||
#define NUM_DISPLAY_ASCII ((ASCII_END - ASCII_START) + 1)
|
||||
|
||||
|
||||
struct font_data {
|
||||
stbtt_packedchar char_data[NUM_DISPLAY_ASCII];
|
||||
stbtt_fontinfo info;
|
||||
|
||||
const char *file_path;
|
||||
unsigned char *file_bytes;
|
||||
size_t file_bytes_len;
|
||||
|
||||
gpu_texture texture;
|
||||
|
||||
int height_px;
|
||||
float scale_factor;
|
||||
int ascent;
|
||||
int descent;
|
||||
int line_gap;
|
||||
};
|
||||
|
||||
|
||||
static struct font_data *text_load_font_data(const char *path, int height_px) {
|
||||
struct font_data *font_data = ccalloc(1, sizeof *font_data);
|
||||
static FontData *text_load_font_data(const char *path, int height_px) {
|
||||
FontData *font_data = ccalloc(1, sizeof *font_data);
|
||||
font_data->file_path = path;
|
||||
font_data->height_px = height_px;
|
||||
|
||||
@ -77,21 +59,21 @@ static struct font_data *text_load_font_data(const char *path, int height_px) {
|
||||
}
|
||||
|
||||
|
||||
static void text_destroy_font_data(struct font_data *font_data) {
|
||||
static void text_destroy_font_data(FontData *font_data) {
|
||||
free(font_data->file_bytes);
|
||||
delete_gpu_texture(font_data->texture);
|
||||
free(font_data);
|
||||
}
|
||||
|
||||
|
||||
static void text_draw_with(struct font_data* font_data, char* text, t_fvec2 position, t_color color) {
|
||||
static vertex_buffer vertex_array = 0;
|
||||
static void text_draw_with(FontData* font_data, char* text, Vec2 position, Color color) {
|
||||
VertexBuffer vertex_array = 0;
|
||||
if (vertex_array == 0)
|
||||
vertex_array = create_vertex_buffer();
|
||||
|
||||
const size_t len = SDL_strlen(text);
|
||||
|
||||
vertex_buffer_builder payload = build_vertex_buffer(vertex_array, get_text_payload_size() * len);
|
||||
VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_text_payload_size() * len);
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
const char c = text[i];
|
||||
@ -131,21 +113,21 @@ static void ensure_font_cache(const char *font_path, int height_px) {
|
||||
/* HACK: stupid, bad, don't do this */
|
||||
bool is_cached = false;
|
||||
for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) {
|
||||
struct font_data *font_data = ctx.text_cache.data[i];
|
||||
FontData *font_data = ctx.text_cache.data[i];
|
||||
if ((strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) {
|
||||
is_cached = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_cached) {
|
||||
struct font_data *new_font_data = text_load_font_data(font_path, height_px);
|
||||
FontData *new_font_data = text_load_font_data(font_path, height_px);
|
||||
arrput(ctx.text_cache.data, new_font_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct font_data *get_font_data(const char *font_path, int height_px) {
|
||||
struct font_data *font_data = NULL;
|
||||
static FontData *get_font_data(const char *font_path, int height_px) {
|
||||
FontData *font_data = NULL;
|
||||
for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) {
|
||||
font_data = ctx.text_cache.data[i];
|
||||
if ((strcmp(font_path, font_data->file_path) == 0) && height_px == font_data->height_px) {
|
||||
@ -156,18 +138,18 @@ static struct font_data *get_font_data(const char *font_path, int height_px) {
|
||||
}
|
||||
|
||||
|
||||
void render_text(const struct text_primitive *text) {
|
||||
struct font_data *font_data = get_font_data(text->font, text->height_px);
|
||||
void render_text(const TextPrimitive *text) {
|
||||
FontData *font_data = get_font_data(text->font, text->height_px);
|
||||
text_draw_with(font_data, text->text, text->position, text->color);
|
||||
}
|
||||
|
||||
|
||||
void text_cache_init(struct text_cache *cache) {
|
||||
void text_cache_init(TextCache *cache) {
|
||||
arrsetlen(cache->data, 0);
|
||||
}
|
||||
|
||||
|
||||
void text_cache_deinit(struct text_cache *cache) {
|
||||
void text_cache_deinit(TextCache *cache) {
|
||||
for (size_t i = 0; i < arrlenu(ctx.text_cache.data); ++i) {
|
||||
text_destroy_font_data(ctx.text_cache.data[i]);
|
||||
}
|
||||
@ -176,7 +158,7 @@ void text_cache_deinit(struct text_cache *cache) {
|
||||
}
|
||||
|
||||
|
||||
void push_text(char *string, t_fvec2 position, int height_px, t_color color, const char *font_path) {
|
||||
void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path) {
|
||||
ensure_font_cache(font_path, height_px);
|
||||
|
||||
/* the string might not be around by the time it's used, so copy it */
|
||||
@ -185,7 +167,7 @@ void push_text(char *string, t_fvec2 position, int height_px, t_color color, con
|
||||
char *dup_string = cmalloc(strlen(string) + 1);
|
||||
strcpy(dup_string, string);
|
||||
|
||||
struct text_primitive text = {
|
||||
TextPrimitive text = {
|
||||
.color = color,
|
||||
.position = position,
|
||||
.text = dup_string,
|
||||
@ -193,7 +175,7 @@ void push_text(char *string, t_fvec2 position, int height_px, t_color color, con
|
||||
.height_px = height_px,
|
||||
};
|
||||
|
||||
struct primitive_2d primitive = {
|
||||
Primitive2D primitive = {
|
||||
.type = PRIMITIVE_2D_TEXT,
|
||||
.text = text,
|
||||
};
|
||||
@ -204,7 +186,7 @@ void push_text(char *string, t_fvec2 position, int height_px, t_color color, con
|
||||
|
||||
int get_text_width(char *string, int height_px, const char *font_path) {
|
||||
ensure_font_cache(font_path, height_px);
|
||||
struct font_data *font_data = get_font_data(font_path, height_px);
|
||||
FontData *font_data = get_font_data(font_path, height_px);
|
||||
|
||||
int length = 0;
|
||||
for (const char *p = string; *p != '\0'; ++p) {
|
||||
|
Reference in New Issue
Block a user