diff options
| author | schererleander <leander@schererleander.de> | 2026-03-12 02:01:02 +0100 |
|---|---|---|
| committer | schererleander <leander@schererleander.de> | 2026-03-12 02:01:02 +0100 |
| commit | 0adca22d9e86130dfbcbfe2fc021710a8d45a927 (patch) | |
| tree | ce009a5c3464abd5ad66a352f6b79d8a4d0c195e | |
| parent | aafdfa0734b7c5dcea9f0e914ef6c66852b9d3a7 (diff) | |
refactor(map): return Map by value instead of heap allocation
| -rw-r--r-- | include/map.h | 4 | ||||
| -rw-r--r-- | src/main.c | 14 | ||||
| -rw-r--r-- | src/map.c | 41 |
3 files changed, 27 insertions, 32 deletions
diff --git a/include/map.h b/include/map.h index d1587fa..8bd5096 100644 --- a/include/map.h +++ b/include/map.h @@ -19,7 +19,7 @@ typedef struct { typedef struct Entities Entities; -Map *LoadMap(const char *filename, Entities *entities); +Map LoadMap(const char *filename, Entities *entities); bool isCollision(Map *map, Rectangle bounds); @@ -29,4 +29,4 @@ void DrawKeys(Entities *entities, Map *map); Marker *FindMarker(Map *map, const char *name); -void UnloadMap(Map *map); +void UnloadMap(Map map); @@ -14,11 +14,12 @@ int main(void) { SetTargetFPS(TARGET_FPS); Entities entities = {0}; - Map *map = LoadMap("assets/maps/debug.tmx", &entities); + Map map = LoadMap("assets/maps/debug.tmx", &entities); - Marker *spawn = FindMarker(map, "player_spawn"); + Marker *spawn = FindMarker(&map, "player_spawn"); Vector2 spawnPos = spawn ? spawn->position : (Vector2){0, 0}; + RaytmxExternalTileset playerTileset = LoadTSX("assets/tilesets/elf.tsx"); Player player = {.position = spawnPos, .speed = 80.0f, @@ -39,16 +40,16 @@ int main(void) { }; while (!WindowShouldClose()) { - UpdatePlayer(&player, map, &entities); + UpdatePlayer(&player, &map, &entities); UpdateFairy(&fairy, player.position); camera.target = player.position; BeginDrawing(); ClearBackground(BLACK); BeginMode2D(camera); - DrawMap(map); - DrawPickups(&entities, map); - DrawKeys(&entities, map); + DrawMap(&map); + DrawPickups(&entities, &map); + DrawKeys(&entities, &map); DrawPlayer(&player); DrawFairy(&fairy); EndMode2D(); @@ -56,6 +57,7 @@ int main(void) { } UnloadMap(map); + CloseWindow(); return 0; } @@ -6,7 +6,6 @@ #include "raytmx.h" #include <assert.h> -#include <stdlib.h> #include <string.h> 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); } |
