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

use enum instead of char (saved about 6k bytes)

jblanked 9 месяцев назад
Родитель
Сommit
c5ad1941e5
2 измененных файлов с 53 добавлено и 86 удалено
  1. 26 83
      game/icon.c
  2. 27 3
      game/icon.h

+ 26 - 83
game/icon.c

@@ -86,7 +86,7 @@ const EntityDescription icon_desc = {
     .context_size = sizeof(IconContext),
 };
 
-static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t width, uint8_t height)
+static IconContext *icon_generic_alloc(IconID id, const Icon *icon, uint8_t width, uint8_t height)
 {
     IconContext *ctx = malloc(sizeof(IconContext));
     if (!ctx)
@@ -94,14 +94,7 @@ static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t
         FURI_LOG_E("Game", "Failed to allocate IconContext");
         return NULL;
     }
-    ctx->id = malloc(strlen(id) + 1);
-    if (!ctx->id)
-    {
-        FURI_LOG_E("Game", "Failed to allocate IconContext id");
-        free(ctx);
-        return NULL;
-    }
-    snprintf(ctx->id, strlen(id) + 1, "%s", id);
+    ctx->id = id;
     ctx->icon = icon;
     ctx->size = (Vector){width, height};
     return ctx;
@@ -110,99 +103,49 @@ static IconContext *icon_generic_alloc(const char *id, const Icon *icon, uint8_t
 IconContext *get_icon_context(const char *name)
 {
     if (is_str(name, "house"))
-        return icon_generic_alloc("house", &I_icon_house_48x32px, 48, 32);
+        return icon_generic_alloc(ICON_ID_HOUSE, &I_icon_house_48x32px, 48, 32);
     else if (is_str(name, "man"))
-        return icon_generic_alloc("man", &I_icon_man_7x16, 7, 16);
+        return icon_generic_alloc(ICON_ID_MAN, &I_icon_man_7x16, 7, 16);
     else if (is_str(name, "plant"))
-        return icon_generic_alloc("plant", &I_icon_plant_16x16, 16, 16);
+        return icon_generic_alloc(ICON_ID_PLANT, &I_icon_plant_16x16, 16, 16);
     else if (is_str(name, "tree"))
-        return icon_generic_alloc("tree", &I_icon_tree_16x16, 16, 16);
+        return icon_generic_alloc(ICON_ID_TREE, &I_icon_tree_16x16, 16, 16);
     else if (is_str(name, "woman"))
-        return icon_generic_alloc("woman", &I_icon_woman_9x16, 9, 16);
+        return icon_generic_alloc(ICON_ID_WOMAN, &I_icon_woman_9x16, 9, 16);
     else if (is_str(name, "fence"))
-        return icon_generic_alloc("fence", &I_icon_fence_16x8px, 16, 8);
+        return icon_generic_alloc(ICON_ID_FENCE, &I_icon_fence_16x8px, 16, 8);
     else if (is_str(name, "fence_end"))
-        return icon_generic_alloc("fence_end", &I_icon_fence_end_16x8px, 16, 8);
-    // else if (is_str(name, "fence_vertical_end") )
-    //     return icon_generic_alloc("fence_vertical_end", &I_icon_fence_vertical_end_6x8px, 6, 8);
-    // else if (is_str(name, "fence_vertical_start") )
-    //     return icon_generic_alloc("fence_vertical_start", &I_icon_fence_vertical_start_6x15px, 6, 15);
+        return icon_generic_alloc(ICON_ID_FENCE_END, &I_icon_fence_end_16x8px, 16, 8);
+    else if (is_str(name, "fence_vertical_end"))
+        return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_END, &I_icon_fence_vertical_end_6x8px, 6, 8);
+    else if (is_str(name, "fence_vertical_start"))
+        return icon_generic_alloc(ICON_ID_FENCE_VERTICAL_START, &I_icon_fence_vertical_start_6x15px, 6, 15);
     else if (is_str(name, "flower"))
-        return icon_generic_alloc("flower", &I_icon_flower_16x16, 16, 16);
+        return icon_generic_alloc(ICON_ID_FLOWER, &I_icon_flower_16x16, 16, 16);
     else if (is_str(name, "lake_bottom"))
-        return icon_generic_alloc("lake_bottom", &I_icon_lake_bottom_31x12px, 31, 12);
+        return icon_generic_alloc(ICON_ID_LAKE_BOTTOM, &I_icon_lake_bottom_31x12px, 31, 12);
     else if (is_str(name, "lake_bottom_left"))
-        return icon_generic_alloc("lake_bottom_left", &I_icon_lake_bottom_left_24x22px, 24, 22);
+        return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_LEFT, &I_icon_lake_bottom_left_24x22px, 24, 22);
     else if (is_str(name, "lake_bottom_right"))
-        return icon_generic_alloc("lake_bottom_right", &I_icon_lake_bottom_right_24x22px, 24, 22);
+        return icon_generic_alloc(ICON_ID_LAKE_BOTTOM_RIGHT, &I_icon_lake_bottom_right_24x22px, 24, 22);
     else if (is_str(name, "lake_left"))
-        return icon_generic_alloc("lake_left", &I_icon_lake_left_11x31px, 11, 31);
+        return icon_generic_alloc(ICON_ID_LAKE_LEFT, &I_icon_lake_left_11x31px, 11, 31);
     else if (is_str(name, "lake_right"))
-        return icon_generic_alloc("lake_right", &I_icon_lake_right_11x31, 11, 31);
+        return icon_generic_alloc(ICON_ID_LAKE_RIGHT, &I_icon_lake_right_11x31, 11, 31);
     else if (is_str(name, "lake_top"))
-        return icon_generic_alloc("lake_top", &I_icon_lake_top_31x12px, 31, 12);
+        return icon_generic_alloc(ICON_ID_LAKE_TOP, &I_icon_lake_top_31x12px, 31, 12);
     else if (is_str(name, "lake_top_left"))
-        return icon_generic_alloc("lake_top_left", &I_icon_lake_top_left_24x22px, 24, 22);
+        return icon_generic_alloc(ICON_ID_LAKE_TOP_LEFT, &I_icon_lake_top_left_24x22px, 24, 22);
     else if (is_str(name, "lake_top_right"))
-        return icon_generic_alloc("lake_top_right", &I_icon_lake_top_right_24x22px, 24, 22);
+        return icon_generic_alloc(ICON_ID_LAKE_TOP_RIGHT, &I_icon_lake_top_right_24x22px, 24, 22);
     else if (is_str(name, "rock_large"))
-        return icon_generic_alloc("rock_large", &I_icon_rock_large_18x19px, 18, 19);
+        return icon_generic_alloc(ICON_ID_ROCK_LARGE, &I_icon_rock_large_18x19px, 18, 19);
     else if (is_str(name, "rock_medium"))
-        return icon_generic_alloc("rock_medium", &I_icon_rock_medium_16x14px, 16, 14);
+        return icon_generic_alloc(ICON_ID_ROCK_MEDIUM, &I_icon_rock_medium_16x14px, 16, 14);
     else if (is_str(name, "rock_small"))
-        return icon_generic_alloc("rock_small", &I_icon_rock_small_10x8px, 10, 8);
+        return icon_generic_alloc(ICON_ID_ROCK_SMALL, &I_icon_rock_small_10x8px, 10, 8);
 
     // If no match is found
     FURI_LOG_E("Game", "Icon not found: %s", name);
     return NULL;
-}
-
-const char *icon_get_id(const Icon *icon)
-{
-    if (icon == &I_icon_house_48x32px)
-        return "house";
-    else if (icon == &I_icon_man_7x16)
-        return "man";
-    else if (icon == &I_icon_plant_16x16)
-        return "plant";
-    else if (icon == &I_icon_tree_16x16)
-        return "tree";
-    else if (icon == &I_icon_woman_9x16)
-        return "woman";
-    else if (icon == &I_icon_fence_16x8px)
-        return "fence";
-    else if (icon == &I_icon_fence_end_16x8px)
-        return "fence_end";
-    // else if (icon == &I_icon_fence_vertical_end_6x8px)
-    //     return "fence_vertical_end";
-    // else if (icon == &I_icon_fence_vertical_start_6x15px)
-    //     return "fence_vertical_start";
-    else if (icon == &I_icon_flower_16x16)
-        return "flower";
-    else if (icon == &I_icon_lake_bottom_31x12px)
-        return "lake_bottom";
-    else if (icon == &I_icon_lake_bottom_left_24x22px)
-        return "lake_bottom_left";
-    else if (icon == &I_icon_lake_bottom_right_24x22px)
-        return "lake_bottom_right";
-    else if (icon == &I_icon_lake_left_11x31px)
-        return "lake_left";
-    else if (icon == &I_icon_lake_right_11x31)
-        return "lake_right";
-    else if (icon == &I_icon_lake_top_31x12px)
-        return "lake_top";
-    else if (icon == &I_icon_lake_top_left_24x22px)
-        return "lake_top_left";
-    else if (icon == &I_icon_lake_top_right_24x22px)
-        return "lake_top_right";
-    else if (icon == &I_icon_rock_large_18x19px)
-        return "rock_large";
-    else if (icon == &I_icon_rock_medium_16x14px)
-        return "rock_medium";
-    else if (icon == &I_icon_rock_small_10x8px)
-        return "rock_small";
-
-    // If no match is found
-    FURI_LOG_E("Game", "Icon ID not found for given icon pointer.");
-    return NULL;
-}
+}

+ 27 - 3
game/icon.h

@@ -2,13 +2,37 @@
 #include "flip_world_icons.h"
 #include "game.h"
 
+typedef enum
+{
+    ICON_ID_HOUSE,                // House
+    ICON_ID_MAN,                  // Man
+    ICON_ID_PLANT,                // Plant
+    ICON_ID_TREE,                 // Tree
+    ICON_ID_WOMAN,                // Woman
+    ICON_ID_FENCE,                // Fence
+    ICON_ID_FENCE_END,            // Fence end
+    ICON_ID_FENCE_VERTICAL_END,   // Vertical fence end
+    ICON_ID_FENCE_VERTICAL_START, // Vertical fence start
+    ICON_ID_FLOWER,               // Flower
+    ICON_ID_LAKE_BOTTOM,          // Lake bottom
+    ICON_ID_LAKE_BOTTOM_LEFT,     // Lake bottom left
+    ICON_ID_LAKE_BOTTOM_RIGHT,    // Lake bottom right
+    ICON_ID_LAKE_LEFT,            // Lake left
+    ICON_ID_LAKE_RIGHT,           // Lake right
+    ICON_ID_LAKE_TOP,             // Lake top
+    ICON_ID_LAKE_TOP_LEFT,        // Lake top left
+    ICON_ID_LAKE_TOP_RIGHT,       // Lake top right
+    ICON_ID_ROCK_LARGE,           // Large rock
+    ICON_ID_ROCK_MEDIUM,          // Medium rock
+    ICON_ID_ROCK_SMALL,           // Small rock
+} IconID;
+
 typedef struct
 {
-    char *id;
+    IconID id;
     const Icon *icon;
     Vector size;
 } IconContext;
 
 extern const EntityDescription icon_desc;
-IconContext *get_icon_context(const char *name);
-const char *icon_get_id(const Icon *icon);
+IconContext *get_icon_context(const char *name);