From 39f27727bf766511935257d6424e1e3f48abdc5d Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 1 Jan 2024 18:06:55 +0100 Subject: don't depend on rxi --- inc/config.h | 2 +- src/config.c | 82 ++--------------- src/main.c | 4 +- src/player.c | 29 +++++- src/world.c | 4 +- vendors/ini.c | 277 ---------------------------------------------------------- vendors/ini.h | 20 ----- 7 files changed, 35 insertions(+), 383 deletions(-) delete mode 100644 vendors/ini.c delete mode 100644 vendors/ini.h diff --git a/inc/config.h b/inc/config.h index e5fe194..7252914 100644 --- a/inc/config.h +++ b/inc/config.h @@ -13,5 +13,5 @@ typedef struct Config { extern Config cfg; -void config_init(const char *path); +void config_init(void); void config_deinit(void); diff --git a/src/config.c b/src/config.c index 9a673ee..6f05e3a 100644 --- a/src/config.c +++ b/src/config.c @@ -9,87 +9,15 @@ Config cfg = {0}; } void -config_init(const char *path) +config_init(void) { - ini_t *ini = NULL; - - ini = ini_load(path); - expect(ini != NULL); - - const char *tileset = ini_get(ini, "tileset", "path"); - if (tileset == NULL) { - pwrn("[tileset] path unspecified, using default"); - tileset = "tileset.png"; - } - cfg.tileset_path = alloc(strlen(tileset) + 1); - expect(cfg.tileset_path != NULL); - strcpy(cfg.tileset_path, tileset); - - const char *world_path = ini_get(ini, "world", "path"); - if (world_path == NULL) { - pwrn("[world] path unspecified, using default"); - world_path = "world.csv"; - } - cfg.world_path = alloc(strlen(world_path) + 1); - expect(cfg.world_path != NULL); - strcpy(cfg.world_path, world_path); - - with (tile_width, ini_get(ini, "tile", "width")) { - cfg.tile_width = atoi(tile_width); - } else { - pwrn("[tile] width unspecified, using default"); - cfg.tile_width = 16; - } - expect(cfg.tile_width > 0); - - with (tile_height, ini_get(ini, "tile", "height")) { - cfg.tile_height = atoi(tile_height); - } else { - pwrn("[tile] height unspecified, using default"); - cfg.tile_height = 16; - } - expect(cfg.tile_height > 0); - - with (cell_width, ini_get(ini, "cell", "width")) { - cfg.cell_width = atoi(cell_width); - } else { - pwrn("[cell] width unspecified, using default"); - cfg.cell_width = 25; - } - expect(cfg.cell_width > 0); - - with (cell_height, ini_get(ini, "cell", "height")) { - cfg.cell_height = atoi(cell_height); - } else { - pwrn("[cell] height unspecified, using default"); - cfg.cell_height = 14; - } - expect(cfg.cell_height > 0); - - with (world_width, ini_get(ini, "world", "width")) { - cfg.world_width = atoi(world_width); - } else { - pwrn("[world] width unspecified, using default"); - cfg.world_width = 16; - } - expect(cfg.world_width > 0); - - with (world_height, ini_get(ini, "world", "height")) { - cfg.world_height = atoi(world_height); - } else { - pwrn("[world] height unspecified, using default"); - cfg.world_height = 16; - } - expect(cfg.world_height > 0); - - ini_free(ini); + cfg.tileset_path = "../tset.bmp"; + cfg.world_path = "world.csv"; + cfg.tile_width = cfg.tile_height = 32; + cfg.cell_width = cfg.cell_height = 20; } void config_deinit(void) { - if (cfg.world_path != NULL) - free(cfg.world_path); - if (cfg.tileset_path != NULL) - free(cfg.tileset_path); } diff --git a/src/main.c b/src/main.c index e2d08b0..9ac5985 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) { srand(time(nullptr)); defer(wdeinit); - config_init("res/world/aancyk.ini"); + config_init(); defer(config_deinit); world_init("res/world/world.csv", "res/world/%d.csv"); defer(world_deinit); @@ -54,7 +54,7 @@ _main_loop([[maybe_unused]] void *udata) TZR_DrawSetColor(0, 0, 0); - if (rand() % 128) { + if (rand() % 4) { TZR_DrawImage(TZR_RES("res/title.bmp"), ___tzr_config.width / 2 + 255 - rand() % 512, ___tzr_config.height / 2 + 255 - rand() % 512, diff --git a/src/player.c b/src/player.c index 71a8c30..5954cf8 100644 --- a/src/player.c +++ b/src/player.c @@ -3,15 +3,30 @@ static int2 pos = I2(0, 0); static int2 dir = I2(1, 0); +static bool collide(int tile, int2 p); + +void +player_init(void) +{ + +} + void player_update(void) { - if (input_pressed("action")) { + // rotate counterclockwise + if (input_pressed("action")) dir = I2(dir.y, -dir.x); - int2_log(log, dir); - } - pos = int2_add(pos, dir); + // deth + if (collide(1, pos)) + pwrn("U DED"); + + // you read mi code?? + const auto next_pos = int2_add(pos, dir); + // thx and sry i guess + if (!collide(0, next_pos)) + pos = next_pos; } void @@ -20,3 +35,9 @@ player_draw(void) TZR_DrawSetColor(0, 0, 0); TZR_DrawRectangle(UNPACK(pos), 3, 4, true, true); } + +static bool +collide(int tile, int2 p) +{ + return world_get(p.x, p.y) == tile; +} diff --git a/src/world.c b/src/world.c index a24c7a6..a0f25fc 100644 --- a/src/world.c +++ b/src/world.c @@ -105,8 +105,8 @@ world_get(int x, int y) const int i = x / cfg.tile_width + (y / cfg.tile_height) * cfg.cell_width; with (cell, g_world.cells[wx + wy * g_world.width]) - return cell->data[i] ? (cell->data[i] - 1) : 0; - return 0; // oob tile + return cell->data[i] - 1; + return -1; // oob tile } int2 diff --git a/vendors/ini.c b/vendors/ini.c deleted file mode 100644 index d6f5b75..0000000 --- a/vendors/ini.c +++ /dev/null @@ -1,277 +0,0 @@ -/** - * Copyright (c) 2016 rxi - * - * 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. - */ - -#include -#include -#include -#include - -#include "ini.h" - -struct ini_t { - char *data; - char *end; -}; - - -/* Case insensitive string compare */ -static int strcmpci(const char *a, const char *b) { - for (;;) { - int d = tolower(*a) - tolower(*b); - if (d != 0 || !*a) { - return d; - } - a++, b++; - } -} - -/* Returns the next string in the split data */ -static char* next(ini_t *ini, char *p) { - p += strlen(p); - while (p < ini->end && *p == '\0') { - p++; - } - return p; -} - -static void trim_back(ini_t *ini, char *p) { - while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) { - *p-- = '\0'; - } -} - -static char* discard_line(ini_t *ini, char *p) { - while (p < ini->end && *p != '\n') { - *p++ = '\0'; - } - return p; -} - - -static char *unescape_quoted_value(ini_t *ini, char *p) { - /* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q` - * as escape sequences are always larger than their resultant data */ - char *q = p; - p++; - while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') { - if (*p == '\\') { - /* Handle escaped char */ - p++; - switch (*p) { - default : *q = *p; break; - case 'r' : *q = '\r'; break; - case 'n' : *q = '\n'; break; - case 't' : *q = '\t'; break; - case '\r' : - case '\n' : - case '\0' : goto end; - } - - } else { - /* Handle normal char */ - *q = *p; - } - q++, p++; - } -end: - return q; -} - - -/* Splits data in place into strings containing section-headers, keys and - * values using one or more '\0' as a delimiter. Unescapes quoted values */ -static void split_data(ini_t *ini) { - char *value_start, *line_start; - char *p = ini->data; - - while (p < ini->end) { - switch (*p) { - case '\r': - case '\n': - case '\t': - case ' ': - *p = '\0'; - /* Fall through */ - - case '\0': - p++; - break; - - case '[': - p += strcspn(p, "]\n"); - *p = '\0'; - break; - - case ';': - p = discard_line(ini, p); - break; - - default: - line_start = p; - p += strcspn(p, "=\n"); - - /* Is line missing a '='? */ - if (*p != '=') { - p = discard_line(ini, line_start); - break; - } - trim_back(ini, p - 1); - - /* Replace '=' and whitespace after it with '\0' */ - do { - *p++ = '\0'; - } while (*p == ' ' || *p == '\r' || *p == '\t'); - - /* Is a value after '=' missing? */ - if (*p == '\n' || *p == '\0') { - p = discard_line(ini, line_start); - break; - } - - if (*p == '"') { - /* Handle quoted string value */ - value_start = p; - p = unescape_quoted_value(ini, p); - - /* Was the string empty? */ - if (p == value_start) { - p = discard_line(ini, line_start); - break; - } - - /* Discard the rest of the line after the string value */ - p = discard_line(ini, p); - - } else { - /* Handle normal value */ - p += strcspn(p, "\n"); - trim_back(ini, p - 1); - } - break; - } - } -} - - - -ini_t* ini_load(const char *filename) { - ini_t *ini = NULL; - FILE *fp = NULL; - int n, sz; - - /* Init ini struct */ - ini = malloc(sizeof(*ini)); - if (!ini) { - goto fail; - } - memset(ini, 0, sizeof(*ini)); - - /* Open file */ - fp = fopen(filename, "rb"); - if (!fp) { - goto fail; - } - - /* Get file size */ - fseek(fp, 0, SEEK_END); - sz = ftell(fp); - rewind(fp); - - /* Load file content into memory, null terminate, init end var */ - ini->data = malloc(sz + 1); - if (!ini->data) { - goto fail; - } - ini->data[sz] = '\0'; - ini->end = ini->data + sz; - n = fread(ini->data, 1, sz, fp); - if (n != sz) { - goto fail; - } - - /* Prepare data */ - split_data(ini); - - /* Clean up and return */ - fclose(fp); - return ini; - -fail: - if (fp) fclose(fp); - if (ini) ini_free(ini); - return NULL; -} - - -void ini_free(ini_t *ini) { - if (ini->data) free(ini->data); - free(ini); -} - - -const char* ini_get(ini_t *ini, const char *section, const char *key) { - char *current_section = ""; - char *val; - char *p = ini->data; - - if (*p == '\0') { - p = next(ini, p); - } - - while (p < ini->end) { - if (*p == '[') { - /* Handle section */ - current_section = p + 1; - - } else { - /* Handle key */ - val = next(ini, p); - if (!section || !strcmpci(section, current_section)) { - if (!strcmpci(p, key)) { - return val; - } - } - p = val; - } - - p = next(ini, p); - } - - return NULL; -} - - -int ini_sget( - ini_t *ini, const char *section, const char *key, - const char *scanfmt, void *dst -) { - const char *val = ini_get(ini, section, key); - if (!val) { - return 0; - } - if (scanfmt) { - sscanf(val, scanfmt, dst); - } else { - *((const char**) dst) = val; - } - return 1; -} diff --git a/vendors/ini.h b/vendors/ini.h deleted file mode 100644 index cd6af9f..0000000 --- a/vendors/ini.h +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) 2016 rxi - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the MIT license. See `ini.c` for details. - */ - -#ifndef INI_H -#define INI_H - -#define INI_VERSION "0.1.1" - -typedef struct ini_t ini_t; - -ini_t* ini_load(const char *filename); -void ini_free(ini_t *ini); -const char* ini_get(ini_t *ini, const char *section, const char *key); -int ini_sget(ini_t *ini, const char *section, const char *key, const char *scanfmt, void *dst); - -#endif -- cgit v1.2.3