initial commit

current
Síle Ekaterin Liszka 2023-11-27 13:38:00 -08:00
commit 79748bc282
10 changed files with 2761 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
output/

16
meson.build Normal file
View File

@ -0,0 +1,16 @@
project('smeargle', 'c',
version: '0.1'
)
libtoml_dep = dependency(
'libtoml',
fallback: ['libtoml', 'libtoml_dep'],
default_options: ['default_library=static']
)
exe = executable(
'smeargle',
'src/main.c',
dependencies: libtoml_dep,
install: true
)

85
src/font.c Normal file
View File

@ -0,0 +1,85 @@
#include <toml.h>
#include "util.h"
#include "font.h"
font_t font_create(char *name, char *filename) {
FILE *fp;
font_t font;
printf("Processing %s...\n", name);
fp = fopen(filename, "r");
if (!fp) {
char errbuf[200];
snprintf(errbuf, 200, "can't open %s: ", filename);
error(errbuf, strerror(errno));
}
toml_table_t *table = toml_parse_file(fp);
fclose(fp);
#define TOML_GET_KEY((var, key, func)) toml_datum_t *(var) = func(table, key);\
if (!var.ok) {\
error("key '" key "' missing from", filename); \
}
TOML_GET_KEY(name, "name", toml_string_in)
TOML_GET_KEY(image_filename, "filename", toml_string_in)
TOML_GET_KEY(bpp, "bits_per_pixel", toml_int_in)
TOML_GET_KEY(width, "width", toml_int_in)
TOML_GET_KEY(height, "height", toml_int_in)
#undef TOML_GET_KEY
toml_table_t *mapping = toml_table_in(table, "map")
if (!mapping.ok) {
error("map missing for font ", font);
}
font.name = name.u.s;
font.filename = image_filename.u.s;
font.bits_per_pixel = bpp;
font.width = width;
font.height = height;
font.map_entries = map_create(mapping, *map);
return font;
}
unsigned int map_create(toml_table_t *table, map_t **map) {
map_t *node;
map_t *next;
unsigned int nodes;
map_t *root = malloc(sizeof(map_t*));
toml_array_t *space = toml_array_in(table, " ");
toml_datum_t spcidx = toml_int_at(space, 0);
toml_datum_t spcwid = toml_int_at(space, 1);
if (!spcidx.ok) {
error("missing or inappropriate index for glyph: ", key);
}
if (!spcwid.ok) {
error("missing or inappropriate index for glyph: ", key);
}
node = root;
for (nodes = 1; ; nodes++) {
char *key = toml_key_in(table, nodes);
if (!key) break;
toml_array_t *val = toml_array_in(table, key);
toml_datum_t index = toml_int_at(val, 0);
toml_datum_t width = toml_int_at(val, 1);
if (!index.ok) {
error("missing or inappropriate index for glyph: ", key);
}
if (!width.ok) {
error("missing or inappropriate width for glyph: ", key);
}
next = malloc(sizeof(map_t *));
next->glyph = key;
next->index = index.u.i;
next->width = width.u.i;
node->next = next;
node = next;
}
}

17
src/font.h Normal file
View File

@ -0,0 +1,17 @@
typedef struct map_t {
char *glyph;
unsigned int pos;
unsigned int width;
struct map_t *next;
} map_t;
typedef struct {
char *name;
char *toml_filename;
unsigned char bits_per_pixel;
unsigned int width;
unsigned int height;
unsigned int map_entries;
map_t *map;
} font_t;

39
src/main.c Normal file
View File

@ -0,0 +1,39 @@
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <toml.h>
#define ERRBUFSZ 200
int main(int argc, char **argv) {
if (argc < 2) {
error("missing required argument: game.toml", "");
}
FILE *fp;
char errbuf[ERRBUFSZ];
fp = fopen(argv[1], "r");
if (!fp) {
snprintf(errbuf, 200, "can't open %s: ", argv[1]);
error(errbuf, strerror(errno));
}
toml_table_t *conf = toml_parse_file(fp, errbuf, ERRBUFSZ);
fclose(fp);
if (!conf) {
char str[ERRBUFSZ];
snprintf(str, ERRBUFSZ, "can't parse %s: ", argv[1]);
error(str, errbuf);
}
toml_datum_t name = toml_string_in(conf, "name");
if (!name.ok) {
error("missing key: name", "");
}
printf("Preparing to process %s...\n", name.u.s);
}

16
src/script.h Normal file
View File

@ -0,0 +1,16 @@
#include <stdbool.h>
typedef struct {
char *filename;
font_t *font;
char *format;
unsigned int min_tiles_per_line;
unsigned int max_tiles_per_line;
bool leading_zeroes;
unsigned int tile_offset;
char *raw_filename;
char *dedupe_filename;
char *tilemap_filename;
bool little_endian;
} script_t;

7
src/util.h Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
static void error(const char *msg, const char *msg1) {
fprintf(stderr, "Error: %s%s\n", msg, msg1 ? msg1 : "");
exit(1);
}

View File

@ -0,0 +1,13 @@
project('libtoml', 'c',
version: '0.1-git5221b3d'
)
inc = include_directories('.')
lib = library(
'libtoml', 'toml.c',
include_directories: inc
)
libtoml_dep = declare_dependency(
include_directories: '.',
link_with: lib
)

2392
subprojects/libtoml/toml.c Normal file

File diff suppressed because it is too large Load Diff

175
subprojects/libtoml/toml.h Normal file
View File

@ -0,0 +1,175 @@
/*
MIT License
Copyright (c) CK Tan
https://github.com/cktan/tomlc99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef TOML_H
#define TOML_H
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
#define TOML_EXTERN extern "C"
#else
#define TOML_EXTERN extern
#endif
typedef struct toml_timestamp_t toml_timestamp_t;
typedef struct toml_table_t toml_table_t;
typedef struct toml_array_t toml_array_t;
typedef struct toml_datum_t toml_datum_t;
/* Parse a file. Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz);
/* Parse a string containing the full config.
* Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */
char *errbuf, int errbufsz);
/* Free the table returned by toml_parse() or toml_parse_file(). Once
* this function is called, any handles accessed through this tab
* directly or indirectly are no longer valid.
*/
TOML_EXTERN void toml_free(toml_table_t *tab);
/* Timestamp types. The year, month, day, hour, minute, second, z
* fields may be NULL if they are not relevant. e.g. In a DATE
* type, the hour, minute, second and z fields will be NULLs.
*/
struct toml_timestamp_t {
struct { /* internal. do not use. */
int year, month, day;
int hour, minute, second, millisec;
char z[10];
} __buffer;
int *year, *month, *day;
int *hour, *minute, *second, *millisec;
char *z;
};
/*-----------------------------------------------------------------
* Enhanced access methods
*/
struct toml_datum_t {
int ok;
union {
toml_timestamp_t *ts; /* ts must be freed after use */
char *s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
} u;
};
/* on arrays: */
/* ... retrieve size of array. */
TOML_EXTERN int toml_array_nelem(const toml_array_t *arr);
/* ... retrieve values using index. */
TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx);
/* ... retrieve array or table using index. */
TOML_EXTERN toml_array_t *toml_array_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_table_t *toml_table_at(const toml_array_t *arr, int idx);
/* on tables: */
/* ... retrieve the key in table at keyidx. Return 0 if out of range. */
TOML_EXTERN const char *toml_key_in(const toml_table_t *tab, int keyidx);
/* ... returns 1 if key exists in tab, 0 otherwise */
TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key);
/* ... retrieve values using key. */
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr,
const char *key);
TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key);
TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key);
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr,
const char *key);
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr,
const char *key);
/* .. retrieve array or table using key. */
TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab,
const char *key);
TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab,
const char *key);
/*-----------------------------------------------------------------
* lesser used
*/
/* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */
TOML_EXTERN char toml_array_kind(const toml_array_t *arr);
/* For array kind 'v'alue, return the type of values
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed
0 if unknown
*/
TOML_EXTERN char toml_array_type(const toml_array_t *arr);
/* Return the key of an array */
TOML_EXTERN const char *toml_array_key(const toml_array_t *arr);
/* Return the number of key-values in a table */
TOML_EXTERN int toml_table_nkval(const toml_table_t *tab);
/* Return the number of arrays in a table */
TOML_EXTERN int toml_table_narr(const toml_table_t *tab);
/* Return the number of sub-tables in a table */
TOML_EXTERN int toml_table_ntab(const toml_table_t *tab);
/* Return the key of a table*/
TOML_EXTERN const char *toml_table_key(const toml_table_t *tab);
/*--------------------------------------------------------------
* misc
*/
TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret);
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
void (*xxfree)(void *));
/*--------------------------------------------------------------
* deprecated
*/
/* A raw value, must be processed by toml_rto* before using. */
typedef const char *toml_raw_t;
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key);
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx);
TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret);
TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret);
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret);
TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret);
TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen);
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret);
#endif /* TOML_H */