From efa47bcd8ff61d4ac1772f5791c5d0357b26aa07 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 6 Apr 2025 21:47:53 +0200 Subject: [PATCH] new: reader --- src/tdef/reader.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/tdef/reader.h | 27 +++++++++++++++++++++++++++ src/tdef/tdef.c | 26 ++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/tdef/reader.c create mode 100644 src/tdef/reader.h diff --git a/src/tdef/reader.c b/src/tdef/reader.c new file mode 100644 index 0000000..73a99d5 --- /dev/null +++ b/src/tdef/reader.c @@ -0,0 +1,45 @@ +/* + * TDef: small reader defense game + * + * Copyright (C) 2025 Niki Roo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "cutils/cstring.h" +#include "reader.h" + + +command read_command(FILE *file) { + static cstring_t *line = NULL; + + if (!line) + line = new_cstring(); + + if (cstring_readline(line, file)) { + if (!strcmp(".", line->string)) { + return TICK; + } else if (!strcmp("=", line->string)) { + return DISPLAY; + } else { + return UNKNOWN; + } + } + + return QUIT; +} + diff --git a/src/tdef/reader.h b/src/tdef/reader.h new file mode 100644 index 0000000..5c6ea60 --- /dev/null +++ b/src/tdef/reader.h @@ -0,0 +1,27 @@ +/** + * @file reader.h + * @author Niki + * @date 2025 + * + * @brief bla + * + * blablabla + * + */ + +#ifndef READER_H +#define READER_H + +#include + +typedef enum { + TICK, + DISPLAY, + UNKNOWN, + QUIT +} command; + +command read_command(FILE *file); + +#endif /* READER_H */ + diff --git a/src/tdef/tdef.c b/src/tdef/tdef.c index 976ed0f..8afc523 100644 --- a/src/tdef/tdef.c +++ b/src/tdef/tdef.c @@ -24,6 +24,7 @@ #include "any.h" #include "map.h" #include "engine.h" +#include "reader.h" void display(map_t *self) { printf("==========\n\n"); @@ -83,12 +84,29 @@ int main(int argc, char **argv) { engine_t *engine = new_engine(map); printf("Paths: %d\n", map->paths->count); - for (int i = 0 ; i < 11 ; i++) { - printf("Tick is: %d\n", engine->current_tick); - engine_tick(engine); - display(map); + + command cmd = read_command(stdin); + while (cmd != QUIT) { + switch(cmd) { + case TICK: + printf("Tick is: %d\n", engine->current_tick); + engine_tick(engine); + break; + case DISPLAY: + display(map); + break; + case UNKNOWN: + printf("Unrecognized command\n"); + break; + case QUIT: + // cannot happen, but kept for warning purposes + break; + } + + cmd = read_command(stdin); } + free_engine(engine); return 0; } -- 2.27.0