Quellcode durchsuchen

Remade main() to show file lines on screen

Evgeniy Parfenyuk vor 1 Monat
Ursprung
Commit
6f769ad8fd
4 geänderte Dateien mit 94 neuen und 45 gelöschten Zeilen
  1. 29 35
      src/main.c
  2. 18 5
      src/text_utils.h
  3. 26 5
      src/vnrs_parser.c
  4. 21 0
      src/vnrs_parser.h

+ 29 - 35
src/main.c

@@ -22,29 +22,29 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #include <stdio.h>
 
 #include "vnrs_parser.h"
+#include "text_utils.h"
 
 #define MAIN_ENGINE 1
 #define MAIN_PARSER 2
 #define MAIN MAIN_ENGINE
 
-typedef struct {
-    Texture2D img;
-    Vector2 pos; // In percents
-} Layer;
-
-typedef struct {
-    Layer layers[10];
-    int layers_count;
-
-    char *name;
-    Color name_color;
-    int name_size;
-
-    char *text;
-    Color text_color;
-    int text_size;
-} GameState;
-
+void get_cur_state(GameState *cur_state)
+{
+    cur_state->name = "Sumi";
+    cur_state->name_color = RAYWHITE;
+    cur_state->name_size = 40;
+
+    cur_state->text = strdup("Hello, World!");
+    cur_state->text_color = RAYWHITE;
+    cur_state->text_size = 30;
+
+    /*
+    cur_state->layers[0].img = LoadTexture("assets/spiral_atlas/bedroom.jpg");
+    cur_state->layers[0].pos = (Vector2){0.5, 0.5};
+    cur_state->layers[1].img = LoadTexture("assets/sumi/smile.png");
+    cur_state->layers[1].pos = (Vector2){0.5, 0.5}; */
+    cur_state->layers_count = 0;
+}
 
 int main(void)
 {
@@ -59,27 +59,21 @@ int main(void)
 
 
     GameState cur_state = {0};
-
-    cur_state.name = "Sumi";
-    cur_state.name_color = RAYWHITE;
-    cur_state.name_size = 40;
-
-    cur_state.text = "Hello, World!";
-    cur_state.text_color = RAYWHITE;
-    cur_state.text_size = 30;
-
-    cur_state.layers[0].img = LoadTexture("assets/spiral_atlas/bedroom.jpg");
-    cur_state.layers[0].pos = (Vector2){0.5, 0.5};
-    cur_state.layers[1].img = LoadTexture("assets/sumi/smile.png");
-    cur_state.layers[1].pos = (Vector2){0.5, 0.5};
-    cur_state.layers_count += 2;
-
-
+    get_cur_state(&cur_state);
+    FILE *f;
+    f = fopen("assets/test.vnrs", "r");
+    if (!f) {
+        perror("assets/test.vnrs");
+        return 2;
+    }
     
     while (!WindowShouldClose())
     {
+        if (IsKeyPressed(KEY_SPACE))
+            get_next_state(f, &cur_state);
+
         BeginDrawing();
-            ClearBackground(RAYWHITE);
+            ClearBackground(BLACK);
 
             for (int i = 0; i < cur_state.layers_count; i++) {
                 DrawTexture(cur_state.layers[i].img,

+ 18 - 5
src/text_utils.h

@@ -21,6 +21,8 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #define TEXT_UTILS_H
 
 #include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
 
 inline bool is_whitespace(int ch)
 {
@@ -31,12 +33,23 @@ inline bool is_whitespace(int ch)
 
 inline bool is_emptyline(char *line)
 {
-    for (size_t i = 0; i < strlen(line); i++)
-        if (!is_whitespace(line[i]))
-            return false;
-
-    return true;
+    while (*line)
+    {
+        if (!is_whitespace((unsigned char)*line))
+            return 0;
+        line++;
+    }
+    return 1;
 }
 
 
+inline char *strdup(const char *s)
+{
+    size_t len = strlen(s) + 1;
+    char *p = malloc(len);
+    if (p != NULL)
+        memcpy(p, s, len);
+    return p;
+}
+
 #endif

+ 26 - 5
src/vnrs_parser.c

@@ -52,13 +52,22 @@ static void normalise(char *s)
 static char *get_line(FILE *input)
 {
     static char buffer[MAX_LINE_LENGTH];
-
     if (!input) return NULL;
 
-    if (fgets(buffer, MAX_LINE_LENGTH, input) == NULL)
-        return NULL;
-    normalise(buffer); 
-    return buffer;
+    while (fgets(buffer, sizeof buffer, input)) {
+        normalise(buffer);
+        if (is_emptyline(buffer))
+            continue;
+        size_t len = strlen(buffer);
+        char *result = malloc(len + 1);
+        if (!result)
+            return NULL;
+
+        memcpy(result, buffer, len + 1);
+        return result;
+    }
+
+    return NULL;
 }
 
 
@@ -77,3 +86,15 @@ void parse_vnrs(FILE *input, FILE *output)
             break;
     }
 }
+
+void get_next_state(FILE *input, GameState *state)
+{
+    if (!input) return;
+
+    char *s = get_line(input);
+    if (!s) return;
+
+    free(state->text);
+    state->text = s;
+}
+

+ 21 - 0
src/vnrs_parser.h

@@ -1,6 +1,27 @@
 #ifndef VNRS_PARSER_H
 #define VNRS_PARSER_H
 
+#include <raylib.h>
+
+typedef struct {
+    Texture2D img;
+    Vector2 pos; // In percents
+} Layer;
+
+typedef struct {
+    Layer layers[10];
+    int layers_count;
+
+    char *name;
+    Color name_color;
+    int name_size;
+
+    char *text;
+    Color text_color;
+    int text_size;
+} GameState;
+
 void parse_vnrs(FILE *input, FILE *output);
+void get_next_state(FILE *input, GameState *state);
 
 #endif // VNRS_PARSER_H