src: get farther in to setup

current
Síle Ekaterin Liszka 2023-11-28 00:25:03 -08:00
parent 662d7d0257
commit b6811023ea
6 changed files with 65 additions and 15 deletions

View File

@ -4,7 +4,50 @@
#include "util.h"
#include "font.h"
font_t font_create(char *font_name, char *filename) {
void *fontdb;
int font_compare(const void *a, const void *b) {
font_t *x = (font_t *)a;
font_t *y = (font_t *)b;
return strcmp(x->name, y->name);
}
font_t *load_fonts(toml_table_t *table, const char *base_path) {
font_t *fonts = calloc(32, sizeof(font_t));
font_t *fontptr = fonts;
fontdb = NULL;
// vast majority of projects should have only 1-2 fonts, but let's offer
// some room in case there's more.
for (int i = 0; i < 32; i++) {
const char *key = toml_key_in(table, i);
if (!key) break;
char fullname[200];
toml_datum_t filename = toml_string_in(table, key);
if (!filename.ok) {
error("couldn't load font ", key);
}
snprintf(fullname, 200, "%s/%s", base_path, filename.u.s);
fonts[i] = font_create(key, fullname);
tsearch((void *)fontptr, (void **)fontdb, font_compare);
fontptr++;
}
return fonts;
}
font_t *font_find(const char *key) {
font_t *font = (font_t *)tfind(key, (void **)fontdb, font_compare);
if (!font) {
error("couldn't find requested font ", key);
}
return font;
}
font_t font_create(const char *font_name, const char *filename) {
FILE *fp;
font_t font;
char errbuf[200];

View File

@ -18,7 +18,11 @@ typedef struct {
map_t *map;
} font_t;
font_t font_create(char *name, char *filename);
extern void *fontdb;
font_t *load_fonts(toml_table_t *table, const char *base_path);
font_t *font_find(const char *key);
font_t font_create(const char *name, const char *filename);
void font_destroy(font_t font);
unsigned int map_create(toml_table_t *table, map_t **map);
void map_destroy(map_t *map);

View File

@ -27,6 +27,7 @@ int main(int argc, char **argv) {
snprintf(str, ERRBUFSZ, "can't parse %s: ", argv[1]);
error(str, errbuf);
}
char *base_path = dirname(argv[1]);
toml_datum_t name = toml_string_in(conf, "name");
if (!name.ok) {
@ -34,10 +35,17 @@ int main(int argc, char **argv) {
}
printf("Processing game %s...\n", name.u.s);
puts(" Loading fonts...");
toml_table_t *font = toml_table_in(conf, "font");
if (!font) {
error("couldn't find font table in ", argv[1]);
}
font_t *fonts = load_fonts(font, base_path);
toml_array_t *scripts = toml_array_in(conf, "script");
for (int i = 0; ; i++) {
toml_table_t *toml_script = toml_table_at(scripts, i);
script_t script = script_create(toml_script);
script_t script = script_create(toml_script, base_path, fonts);
}
}

View File

@ -2,11 +2,11 @@
#include <toml.h>
#include "script.h"
#include "font.h"
#include "util.h"
script_t script_create(toml_table_t *table) {
script_t script_create(toml_table_t *table, const char *base_path, font_t *fonts) {
script_t script;
font_t font;
#define TOML_GET_KEY(var, key, func) toml_datum_t (var) = func(table, (key));\
if (!(var).ok) {\
@ -29,17 +29,10 @@ script_t script_create(toml_table_t *table) {
TOML_GET_KEY(little_endian, "little_endian", toml_bool_in)
#undef TOML_GET_KEY
toml_table_t *fonts = toml_table_in(table, "font");
toml_datum_t font_filename = toml_string_in(fonts, font_name.u.s);
if (!font_filename.ok) {
char str[200];
snprintf(str, 200, "failed to obtain data for font %s", font_name.u.s);
error(str, "");
}
font = font_create(font_name.u.s, font_filename.u.s);
font_t *font = font_find(font_name.u.s);
script.filename = filename.u.s;
script.font = font;
script.font = *font;
script.min_tiles_per_line = min_tiles.u.i;
script.max_tiles_per_line = max_tiles.u.i;
script.format = tilemap_format.u.s;

View File

@ -3,6 +3,7 @@
#define _SMEARGLE_SCRIPT_H
#include <stdbool.h>
#include <libgen.h>
#include "font.h"
@ -21,7 +22,7 @@ typedef struct {
bool little_endian;
} script_t;
script_t script_create(toml_table_t *table);
script_t script_create(toml_table_t *table, const char *base_path, font_t *fonts);
void script_destroy(script_t script);
#endif

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <search.h>
static void error(const char *msg, const char *msg1) {
fprintf(stderr, "Error: %s%s\n", msg, msg1 ? msg1 : "");