From 0adca22d9e86130dfbcbfe2fc021710a8d45a927 Mon Sep 17 00:00:00 2001 From: schererleander Date: Thu, 12 Mar 2026 02:01:02 +0100 Subject: refactor(map): return Map by value instead of heap allocation --- src/map.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'src/map.c') diff --git a/src/map.c b/src/map.c index 468024f..29d8dfe 100644 --- a/src/map.c +++ b/src/map.c @@ -6,7 +6,6 @@ #include "raytmx.h" #include -#include #include static TmxLayer *FindLayerByName(TmxMap *map, const char *name) { @@ -115,34 +114,30 @@ static void LoadMarkers(Map *map, TmxLayer *layer) { } } -Map *LoadMap(const char *filename, Entities *entities) { - Map *map = (Map *)malloc(sizeof(Map)); - if (!map) - return NULL; +Map LoadMap(const char *filename, Entities *entities) { + Map map = {0}; - map->map = LoadTMX(filename); - if (!map->map) { - free(map); - return NULL; - } + map.map = LoadTMX(filename); + if (!map.map) + return map; - map->wallLayer = FindLayerByName(map->map, "Walls"); - map->groundLayer = FindLayerByName(map->map, "Ground"); - map->decorLayer = FindLayerByName(map->map, "Decor"); + map.wallLayer = FindLayerByName(map.map, "Walls"); + map.groundLayer = FindLayerByName(map.map, "Ground"); + map.decorLayer = FindLayerByName(map.map, "Decor"); if (entities) { - TmxLayer *pickupsLayer = FindLayerByName(map->map, "Pickups"); + TmxLayer *pickupsLayer = FindLayerByName(map.map, "Pickups"); if (pickupsLayer) - LoadPickups(map->map, pickupsLayer, entities); + LoadPickups(map.map, pickupsLayer, entities); - TmxLayer *itemsLayer = FindLayerByName(map->map, "Items"); + TmxLayer *itemsLayer = FindLayerByName(map.map, "Items"); if (itemsLayer) - LoadKeys(map->map, itemsLayer, entities); + LoadKeys(map.map, itemsLayer, entities); } - TmxLayer *markersLayer = FindLayerByName(map->map, "Markers"); + TmxLayer *markersLayer = FindLayerByName(map.map, "Markers"); if (markersLayer) - LoadMarkers(map, markersLayer); + LoadMarkers(&map, markersLayer); return map; } @@ -212,12 +207,10 @@ Marker *FindMarker(Map *map, const char *name) { for (int i = 0; i < map->markersCount; i++) if (strcmp(map->markers[i].name, name) == 0) return &map->markers[i]; + TraceLog(2, "Marker not found"); return NULL; } -void UnloadMap(Map *map) { - if (!map) - return; - UnloadTMX(map->map); - free(map); +void UnloadMap(Map map) { + UnloadTMX(map.map); } -- cgit v1.3.1