Просмотр исходного кода

Update icon struct; add JSON world parsing

jblanked 1 год назад
Родитель
Сommit
797adff1f3
7 измененных файлов с 156 добавлено и 37 удалено
  1. 32 0
      draw/draw.c
  2. 1 0
      draw/draw.h
  3. 103 28
      draw/world.c
  4. 3 1
      draw/world.h
  5. 5 0
      flip_world.h
  6. 4 7
      game.c
  7. 8 1
      game.h

+ 32 - 0
draw/draw.c

@@ -45,4 +45,36 @@ void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon)
             draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
             draw_icon_line(canvas, (Vector){0, i * 19 + 2}, 11, true, icon);
         }
         }
     }
     }
+}
+const Icon *get_icon(char *name)
+{
+    if (strcmp(name, "earth") == 0)
+    {
+        return &I_icon_earth;
+    }
+    if (strcmp(name, "home") == 0)
+    {
+        return &I_icon_home;
+    }
+    if (strcmp(name, "info") == 0)
+    {
+        return &I_icon_info;
+    }
+    if (strcmp(name, "man") == 0)
+    {
+        return &I_icon_man;
+    }
+    if (strcmp(name, "plant") == 0)
+    {
+        return &I_icon_plant;
+    }
+    if (strcmp(name, "tree") == 0)
+    {
+        return &I_icon_tree;
+    }
+    if (strcmp(name, "woman") == 0)
+    {
+        return &I_icon_woman;
+    }
+    return NULL;
 }
 }

+ 1 - 0
draw/draw.h

@@ -28,5 +28,6 @@ extern int camera_y;
 
 
 void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon);
 void draw_icon_line(Canvas *canvas, Vector pos, int amount, bool horizontal, const Icon *icon);
 void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon);
 void draw_icon_half_world(Canvas *canvas, bool right, const Icon *icon);
+const Icon *get_icon(char *name);
 
 
 // create custom icons at https://lopaka.app/sandbox
 // create custom icons at https://lopaka.app/sandbox

+ 103 - 28
draw/world.c

@@ -9,12 +9,87 @@ void draw_bounds(Canvas *canvas)
 
 
 void draw_example_world(Level *level)
 void draw_example_world(Level *level)
 {
 {
-    spawn_icon(level, &I_icon_earth, 112, 56);
-    spawn_icon(level, &I_icon_home, 128, 24);
-    spawn_icon(level, &I_icon_info, 144, 24);
-    spawn_icon(level, &I_icon_man, 160, 56);
-    spawn_icon(level, &I_icon_woman, 168, 56);
-    spawn_icon(level, &I_icon_plant, 168, 32);
+    spawn_icon(level, &I_icon_earth, 112, 56, 15, 16);
+    spawn_icon(level, &I_icon_home, 128, 24, 15, 16);
+    spawn_icon(level, &I_icon_info, 144, 24, 15, 16);
+    spawn_icon(level, &I_icon_man, 160, 56, 7, 16);
+    spawn_icon(level, &I_icon_woman, 168, 56, 9, 16);
+    spawn_icon(level, &I_icon_plant, 168, 32, 16, 16);
+}
+
+/* JSON of the draw_example_world with fields icon, x, y, width, height
+{
+    "name": "Example World",
+    "author": "JBlanked",
+    "json_data": [
+        {
+            "icon": "earth",
+            "x": 112,
+            "y": 56,
+            "width": 15,
+            "height": 16
+        },
+        {
+            "icon": "home",
+            "x": 128,
+            "y": 24,
+            "width": 15,
+            "height": 16
+        },
+        {
+            "icon": "info",
+            "x": 144,
+            "y": 24,
+            "width": 15,
+            "height": 16
+        },
+        {
+            "icon": "man",
+            "x": 160,
+            "y": 56,
+            "width": 7,
+            "height": 16
+        },
+        {
+            "icon": "woman",
+            "x": 168,
+            "y": 56,
+            "width": 9,
+            "height": 16
+        },
+        {
+            "icon": "plant",
+            "x": 168,
+            "y": 32,
+            "width": 16,
+            "height": 16
+        }
+    ]
+}
+
+*/
+
+bool draw_json_world(Level *level, FuriString *json_data)
+{
+    for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
+    {
+        char *data = get_json_array_value("json_data", i, (char *)furi_string_get_cstr(json_data), MAX_WORLD_TOKENS);
+        if (data == NULL)
+        {
+            break;
+        }
+        char *icon = get_json_value("icon", data, 64);
+        char *x = get_json_value("x", data, 64);
+        char *y = get_json_value("y", data, 64);
+        char *width = get_json_value("width", data, 64);
+        char *height = get_json_value("height", data, 64);
+        if (icon == NULL || x == NULL || y == NULL || width == NULL || height == NULL)
+        {
+            return false;
+        }
+        spawn_icon(level, get_icon(icon), atoi(x), atoi(y), atoi(width), atoi(height));
+    }
+    return true;
 }
 }
 
 
 void draw_tree_world(Level *level)
 void draw_tree_world(Level *level)
@@ -24,11 +99,11 @@ void draw_tree_world(Level *level)
     {
     {
         for (int j = 0; j < 22; j++)
         for (int j = 0; j < 22; j++)
         {
         {
-            spawn_icon(level, &I_icon_tree, 5 + j * 17, 2 + i * 17); // Horizontal lines
+            spawn_icon(level, &I_icon_tree, 5 + j * 17, 2 + i * 17, 16, 16); // Horizontal lines
         }
         }
         for (int j = 0; j < 11; j++)
         for (int j = 0; j < 11; j++)
         {
         {
-            spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + j * 17); // Vertical lines
+            spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + j * 17, 16, 16); // Vertical lines
         }
         }
     }
     }
 
 
@@ -37,7 +112,7 @@ void draw_tree_world(Level *level)
     {
     {
         for (int j = 0; j < 22; j++)
         for (int j = 0; j < 22; j++)
         {
         {
-            spawn_icon(level, &I_icon_tree, 5 + j * 17, 2 + i * 17); // Horizontal lines
+            spawn_icon(level, &I_icon_tree, 5 + j * 17, 2 + i * 17, 16, 16); // Horizontal lines
         }
         }
     }
     }
 
 
@@ -46,7 +121,7 @@ void draw_tree_world(Level *level)
     {
     {
         for (int j = 0; j < 8; j++)
         for (int j = 0; j < 8; j++)
         {
         {
-            spawn_icon(level, &I_icon_tree, 5 + i * 17, 50 + j * 17); // Vertical lines
+            spawn_icon(level, &I_icon_tree, 5 + i * 17, 50 + j * 17, 16, 16); // Vertical lines
         }
         }
     }
     }
 
 
@@ -54,86 +129,86 @@ void draw_tree_world(Level *level)
     // Third line (14 left, 3 middle, 0 right) - exit line
     // Third line (14 left, 3 middle, 0 right) - exit line
     for (int i = 0; i < 14; i++)
     for (int i = 0; i < 14; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 2 * 17);
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 2 * 17, 16, 16);
     }
     }
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 16 * 17 + i * 17, 2 + 2 * 17);
+        spawn_icon(level, &I_icon_tree, 5 + 16 * 17 + i * 17, 2 + 2 * 17, 16, 16);
     }
     }
 
 
     // Fourth line (3 left, 6 middle, 4 right)
     // Fourth line (3 left, 6 middle, 4 right)
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 3 * 17); // 3 left
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 3 * 17, 16, 16); // 3 left
     }
     }
     for (int i = 0; i < 6; i++)
     for (int i = 0; i < 6; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 3 * 17); // 6 middle
+        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 3 * 17, 16, 16); // 6 middle
     }
     }
     for (int i = 0; i < 4; i++)
     for (int i = 0; i < 4; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 3 * 17); // 4 right
+        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 3 * 17, 16, 16); // 4 right
     }
     }
 
 
     // Fifth line (6 left, 7 middle, 0 right)
     // Fifth line (6 left, 7 middle, 0 right)
     for (int i = 0; i < 6; i++)
     for (int i = 0; i < 6; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 4 * 17); // 6 left
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 4 * 17, 16, 16); // 6 left
     }
     }
     for (int i = 0; i < 7; i++)
     for (int i = 0; i < 7; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 4 * 17); // 7 middle
+        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 4 * 17, 16, 16); // 7 middle
     }
     }
 
 
     // Sixth line (5 left, 6 middle, 7 right)
     // Sixth line (5 left, 6 middle, 7 right)
     for (int i = 0; i < 5; i++)
     for (int i = 0; i < 5; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 5 * 17); // 5 left
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 5 * 17, 16, 16); // 5 left
     }
     }
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 5 * 17); // 3 middle
+        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 5 * 17, 16, 16); // 3 middle
     }
     }
     for (int i = 0; i < 7; i++)
     for (int i = 0; i < 7; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 5 * 17); // 7 right
+        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 5 * 17, 16, 16); // 7 right
     }
     }
 
 
     // Seventh line (0 left, 7 middle, 4 right)
     // Seventh line (0 left, 7 middle, 4 right)
     for (int i = 0; i < 7; i++)
     for (int i = 0; i < 7; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 6 * 17 + i * 17, 2 + 6 * 17); // 7 middle
+        spawn_icon(level, &I_icon_tree, 5 + 6 * 17 + i * 17, 2 + 6 * 17, 16, 16); // 7 middle
     }
     }
     for (int i = 0; i < 4; i++)
     for (int i = 0; i < 4; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 14 * 17 + i * 17, 2 + 6 * 17); // 4 right
+        spawn_icon(level, &I_icon_tree, 5 + 14 * 17 + i * 17, 2 + 6 * 17, 16, 16); // 4 right
     }
     }
 
 
     // Eighth line (4 left, 3 middle, 4 right)
     // Eighth line (4 left, 3 middle, 4 right)
     for (int i = 0; i < 4; i++)
     for (int i = 0; i < 4; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 7 * 17); // 4 left
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 7 * 17, 16, 16); // 4 left
     }
     }
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 7 * 17); // 3 middle
+        spawn_icon(level, &I_icon_tree, 5 + 7 * 17 + i * 17, 2 + 7 * 17, 16, 16); // 3 middle
     }
     }
     for (int i = 0; i < 4; i++)
     for (int i = 0; i < 4; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 7 * 17); // 4 right
+        spawn_icon(level, &I_icon_tree, 5 + 15 * 17 + i * 17, 2 + 7 * 17, 16, 16); // 4 right
     }
     }
 
 
     // Ninth line (3 left, 2 middle, 3 right)
     // Ninth line (3 left, 2 middle, 3 right)
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 8 * 17); // 3 left
+        spawn_icon(level, &I_icon_tree, 5 + i * 17, 2 + 8 * 17, 16, 16); // 3 left
     }
     }
     for (int i = 0; i < 1; i++) // 2 middle
     for (int i = 0; i < 1; i++) // 2 middle
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 5 * 17 + i * 17, 2 + 8 * 17);
+        spawn_icon(level, &I_icon_tree, 5 + 5 * 17 + i * 17, 2 + 8 * 17, 16, 16);
     }
     }
     for (int i = 0; i < 3; i++)
     for (int i = 0; i < 3; i++)
     {
     {
-        spawn_icon(level, &I_icon_tree, 5 + 11 * 17 + i * 17, 2 + 8 * 17); // 3 right
+        spawn_icon(level, &I_icon_tree, 5 + 11 * 17 + i * 17, 2 + 8 * 17, 16, 16); // 3 right
     }
     }
 }
 }

+ 3 - 1
draw/world.h

@@ -1,6 +1,8 @@
 #pragma once
 #pragma once
 #include <draw/draw.h>
 #include <draw/draw.h>
 #include <game.h>
 #include <game.h>
+#include <flip_world.h>
 void draw_bounds(Canvas *canvas);
 void draw_bounds(Canvas *canvas);
 void draw_example_world(Level *level);
 void draw_example_world(Level *level);
-void draw_tree_world(Level *level);
+void draw_tree_world(Level *level);
+bool draw_json_world(Level *level, FuriString *json_data);

+ 5 - 0
flip_world.h

@@ -22,6 +22,11 @@
 #define WORLD_WIDTH 384
 #define WORLD_WIDTH 384
 #define WORLD_HEIGHT 192
 #define WORLD_HEIGHT 192
 
 
+// Maximum number of world objects
+#define MAX_WORLD_OBJECTS 100
+// Maximum number of world tokens
+#define MAX_WORLD_TOKENS 512
+
 // Define the submenu items for our FlipWorld application
 // Define the submenu items for our FlipWorld application
 typedef enum
 typedef enum
 {
 {

+ 4 - 7
game.c

@@ -145,11 +145,6 @@ static const LevelBehaviour level = {
     .context_size = 0,    // size of level context, will be automatically allocated and freed
     .context_size = 0,    // size of level context, will be automatically allocated and freed
 };
 };
 
 
-typedef struct
-{
-    const Icon *icon;
-} IconContext;
-
 // Forward declaration of icon_desc
 // Forward declaration of icon_desc
 static const EntityDescription icon_desc;
 static const EntityDescription icon_desc;
 
 
@@ -200,11 +195,13 @@ static const EntityDescription icon_desc = {
 };
 };
 
 
 // Helper function to spawn an icon entity at a given position
 // Helper function to spawn an icon entity at a given position
-void spawn_icon(Level *level, const Icon *icon, float x, float y)
+void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height)
 {
 {
     Entity *e = level_add_entity(level, &icon_desc);
     Entity *e = level_add_entity(level, &icon_desc);
     IconContext *icon_ctx = entity_context_get(e);
     IconContext *icon_ctx = entity_context_get(e);
     icon_ctx->icon = icon;
     icon_ctx->icon = icon;
+    icon_ctx->width = width;
+    icon_ctx->height = height;
     // Set the entity position to the center of the icon
     // Set the entity position to the center of the icon
     entity_pos_set(e, (Vector){x + 8, y + 8});
     entity_pos_set(e, (Vector){x + 8, y + 8});
 }
 }
@@ -246,7 +243,7 @@ static void game_stop(void *ctx)
     Your game configuration, do not rename this variable, but you can change its content here.
     Your game configuration, do not rename this variable, but you can change its content here.
 */
 */
 const Game game = {
 const Game game = {
-    .target_fps = 30,                    // target fps, game will try to keep this value
+    .target_fps = 60,                    // target fps, game will try to keep this value
     .show_fps = false,                   // show fps counter on the screen
     .show_fps = false,                   // show fps counter on the screen
     .always_backlight = true,            // keep display backlight always on
     .always_backlight = true,            // keep display backlight always on
     .start = game_start,                 // will be called once, when game starts
     .start = game_start,                 // will be called once, when game starts

+ 8 - 1
game.h

@@ -2,9 +2,16 @@
 #include "engine/engine.h"
 #include "engine/engine.h"
 #include <draw/world.h>
 #include <draw/world.h>
 
 
-void spawn_icon(Level *level, const Icon *icon, float x, float y);
+void spawn_icon(Level *level, const Icon *icon, float x, float y, uint8_t width, uint8_t height);
 
 
 typedef struct
 typedef struct
 {
 {
     uint32_t score;
     uint32_t score;
 } GameContext;
 } GameContext;
+
+typedef struct
+{
+    const Icon *icon;
+    uint8_t width;
+    uint8_t height;
+} IconContext;