taisei/src/util/kvparser.c
Andrei Alexeyev 322edd0dce
Text rendering rewrite and optimizations; some refactoring (#129)
* wip font rendering stuff; hashtable monstrosity is temporary

* various text rendering fixes/improvements

* HashTables™ 3.0

* Add some comments to aid navigating the hashtable macro maze

* overhaul text rendering API; add default and example shaders

* text: implement text_render for spellcard effect; misc fixes

* README: update dependencies

Bye SDL_ttf, hello freetype2.

* text_draw: fix resolution/scale-dependent bugs

* make text_draw fallback to the current shader, fix hud and stagetext

* repair the bgm loading

* fix spell practice mode

* fix walloftext

forgot one site of text_draw earlier

* fix wrapped text rendering

* fix and simplify the hud text shader

* dynamic glyph cache

* implement font size change on window resize/quality setting change/etc.

* rename text shaders for consistency

* preloads for fonts and text shaders

* make the stagetext shader look somewhat better

* text_render: attempt to normalize height

* small improvement for stagetext
2018-06-30 00:36:51 +03:00

139 lines
3 KiB
C

/*
* This software is licensed under the terms of the MIT-License
* See COPYING for further information.
* ---
* Copyright (c) 2011-2018, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2018, Andrei Alexeyev <akari@alienslab.net>.
*/
#include "taisei.h"
#include "kvparser.h"
#include "log.h"
#include "stringops.h"
#include "io.h"
#include "vfs/public.h"
bool parse_keyvalue_stream_cb(SDL_RWops *strm, KVCallback callback, void *data) {
static const char separator[] = "= ";
char buffer[256];
int lineno = 0;
int errors = 0;
loopstart: while(SDL_RWgets(strm, buffer, sizeof(buffer))) {
char *ptr = buffer;
char *sep, *key, *val;
++lineno;
while(isspace(*ptr)) {
if(!*(++ptr)) {
// blank line
goto loopstart;
}
}
if(*ptr == '#') {
// comment
continue;
}
sep = strstr(ptr, separator);
if(!sep) {
++errors;
log_warn("Syntax error on line %i: missing separator", lineno);
continue;
}
// split it up
*sep = 0;
key = ptr;
val = sep + sizeof(separator) - 1;
// the separator may be preceeded by any kind of whitespace, so strip it from the key
while(isspace(*(ptr = strchr(key, 0) - 1))) {
*ptr = 0;
}
// strip any kind of line endings from the value
while(strchr("\r\n", *(ptr = strchr(val, 0) - 1))) {
*ptr = 0;
}
if(!callback(key, val, data)) {
++errors;
continue;
}
}
return !errors;
}
bool parse_keyvalue_file_cb(const char *filename, KVCallback callback, void *data) {
SDL_RWops *strm = vfs_open(filename, VFS_MODE_READ);
if(!strm) {
log_warn("VFS error: %s", vfs_get_error());
return false;
}
bool status = parse_keyvalue_stream_cb(strm, callback, data);
SDL_RWclose(strm);
return status;
}
static bool kvcallback_hashtable(const char *key, const char *val, void *data) {
ht_str2ptr_t *ht = data;
ht_set(ht, key, strdup(val));
return true;
}
bool parse_keyvalue_stream(SDL_RWops *strm, ht_str2ptr_t *ht) {
return parse_keyvalue_stream_cb(strm, kvcallback_hashtable, ht);
}
bool parse_keyvalue_file(const char *filename, ht_str2ptr_t *ht) {
return parse_keyvalue_file_cb(filename, kvcallback_hashtable, ht);
}
static bool kvcallback_spec(const char *key, const char *val, void *data) {
KVSpec *spec = data;
for(KVSpec *s = spec; s->name; ++s) {
if(!strcmp(key, s->name)) {
if(s->out_str) {
stralloc(s->out_str, val);
}
if(s->out_int) {
*s->out_int = strtol(val, NULL, 0);
}
if(s->out_long) {
*s->out_long = strtol(val, NULL, 0);
}
if(s->out_float) {
*s->out_float = strtod(val, NULL);
}
if(s->out_double) {
*s->out_double = strtod(val, NULL);
}
return true;
}
}
log_warn("Unknown key '%s' with value '%s' ignored", key, val);
return true;
}
bool parse_keyvalue_stream_with_spec(SDL_RWops *strm, KVSpec *spec) {
return parse_keyvalue_stream_cb(strm, kvcallback_spec, spec);
}
bool parse_keyvalue_file_with_spec(const char *filename, KVSpec *spec) {
return parse_keyvalue_file_cb(filename, kvcallback_spec, spec);
}