new: reader
authorNiki Roo <niki@nikiroo.be>
Sun, 6 Apr 2025 19:47:53 +0000 (21:47 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 6 Apr 2025 19:47:53 +0000 (21:47 +0200)
src/tdef/reader.c [new file with mode: 0644]
src/tdef/reader.h [new file with mode: 0644]
src/tdef/tdef.c

diff --git a/src/tdef/reader.c b/src/tdef/reader.c
new file mode 100644 (file)
index 0000000..73a99d5
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#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 (file)
index 0000000..5c6ea60
--- /dev/null
@@ -0,0 +1,27 @@
+/** 
+ * @file reader.h
+ * @author Niki
+ * @date 2025
+ * 
+ * @brief bla
+ *
+ * blablabla
+ *
+ */
+
+#ifndef READER_H
+#define READER_H
+
+#include <stdio.h>
+
+typedef enum {
+       TICK,
+       DISPLAY,
+       UNKNOWN,
+       QUIT
+} command;
+
+command read_command(FILE *file);
+
+#endif /* READER_H */
+
index 976ed0fbf6f1feab9ca5473ea23655b055b96e8c..8afc523a3908e1df4b0c8c13904e9056977f211b 100644 (file)
@@ -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;
 }