diff --git a/assets/map/map.sv b/assets/maps/map.sv similarity index 100% rename from assets/map/map.sv rename to assets/maps/map.sv diff --git a/src/main.cpp b/src/main.cpp index 58c5b88..64fd513 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,8 @@ using namespace M; +#define ASSETS_PATH "../assets/" + #define NEAR_PLANE (0.01f) #define TILE_SIZE (32) @@ -160,7 +162,7 @@ struct Map { Uint32 *tiles; - char *save_path; + char *name; SDL_GPUBuffer *gpu_buffer; }; @@ -190,25 +192,25 @@ typedef struct { } TileInfo; static TileInfo tile_infos[] = { - { 0x0001, "../assets/tiles/error.png" }, - { 0x0000, "../assets/tiles/empty.png" }, - { 0x0100, "../assets/tiles/grass_1.png" }, - { 0x0101, "../assets/tiles/grass_2.png" }, - { 0x0102, "../assets/tiles/grass_3.png" }, - { 0x0103, "../assets/tiles/grass_4.png" }, - { 0x0200, "../assets/tiles/ground_1.png" }, - { 0x0201, "../assets/tiles/ground_2.png" }, - { 0x0202, "../assets/tiles/ground_3.png" }, - { 0x0300, "../assets/tiles/water_1.png" }, - { 0x0301, "../assets/tiles/water_2.png" }, - { 0x0400, "../assets/tiles/grass_ground_1.png" }, - { 0x0401, "../assets/tiles/grass_ground_2.png" }, - { 0x0402, "../assets/tiles/grass_ground_3.png" }, - { 0x0410, "../assets/tiles/grass_ground_outer_corner.png" }, - { 0x0411, "../assets/tiles/grass_ground_outer_corner_2.png" }, - { 0x0420, "../assets/tiles/grass_ground_inner_corner.png" }, - { 0x0421, "../assets/tiles/grass_ground_inner_corner_2.png" }, - { 0x0422, "../assets/tiles/grass_ground_inner_corner_3.png" }, + { 0x0001, "tiles/error.png" }, + { 0x0000, "tiles/empty.png" }, + { 0x0100, "tiles/grass_1.png" }, + { 0x0101, "tiles/grass_2.png" }, + { 0x0102, "tiles/grass_3.png" }, + { 0x0103, "tiles/grass_4.png" }, + { 0x0200, "tiles/ground_1.png" }, + { 0x0201, "tiles/ground_2.png" }, + { 0x0202, "tiles/ground_3.png" }, + { 0x0300, "tiles/water_1.png" }, + { 0x0301, "tiles/water_2.png" }, + { 0x0400, "tiles/grass_ground_1.png" }, + { 0x0401, "tiles/grass_ground_2.png" }, + { 0x0402, "tiles/grass_ground_3.png" }, + { 0x0410, "tiles/grass_ground_outer_corner.png" }, + { 0x0411, "tiles/grass_ground_outer_corner_2.png" }, + { 0x0420, "tiles/grass_ground_inner_corner.png" }, + { 0x0421, "tiles/grass_ground_inner_corner_2.png" }, + { 0x0422, "tiles/grass_ground_inner_corner_3.png" }, }; static V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)]; @@ -302,7 +304,10 @@ static SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_by } static bool save_map(Map map) { - SDL_IOStream *file = SDL_IOFromFile(map.save_path, "wb"); + char path[256] = ASSETS_PATH "maps/"; + SDL_strlcat(path, map.name, SDL_arraysize(path)); + + SDL_IOStream *file = SDL_IOFromFile(path, "wb"); if (!file) { log_error("Failed to open map file for writing."); return false; @@ -339,14 +344,17 @@ static bool save_map(Map map) { return true; } -static bool load_map(const char *path, Map *result) { +static bool load_map(const char *name, Map *result) { + char path[256] = ASSETS_PATH "maps/"; + SDL_strlcat(path, name, SDL_arraysize(path)); + SDL_IOStream *file = SDL_IOFromFile(path, "rb"); if (!file) { log_error("Failed to open map file for reading."); return false; } defer(SDL_CloseIO(file)); - result->save_path = SDL_strdup(path); + result->name = SDL_strdup(name); if (!SDL_ReadS32LE(file, &result->width)) { log_error("Failed read width from map file."); @@ -382,7 +390,7 @@ static bool load_map(const char *path, Map *result) { } char buffer_name[256] = "Map "; - SDL_strlcat(buffer_name, result->save_path, SDL_arraysize(buffer_name)); + SDL_strlcat(buffer_name, result->name, SDL_arraysize(buffer_name)); result->gpu_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, result->width * result->height * 4, result->tiles, buffer_name); if (!result->gpu_buffer) { @@ -399,7 +407,7 @@ static void unload_map(Map *map) { map->width = 0; map->height = 0; free(map->tiles); - SDL_free(map->save_path); + SDL_free(map->name); SDL_ReleaseGPUBuffer(device, map->gpu_buffer); } @@ -577,16 +585,19 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, } static SDL_GPUTexture *create_shader_texture(const char *path) { + char path_to_load[256] = ASSETS_PATH; + SDL_strlcat(path_to_load, path, SDL_arraysize(path_to_load)); + int width = 0, height = 0, channels = 0; - stbi_uc *data = stbi_load(path, &width, &height, &channels, 0); + stbi_uc *data = stbi_load(path_to_load, &width, &height, &channels, 0); if (!data) { - log_error("Failed to load texture (\"%s\").", path); + log_error("Failed to load texture (\"%s\").", path_to_load); return NULL; } SDL_GPUTexture *result = create_shader_texture(path, (char *)data, width, height, channels); if (!result) { - log_error("Failed to load texture (\"%s\").", path); + log_error("Failed to load texture (\"%s\").", path_to_load); stbi_image_free(data); return NULL; } @@ -1199,10 +1210,13 @@ bool recreate_tile_textures() { } for (Uint32 i = 0; i < SDL_arraysize(tile_infos); i++) { + char path[256] = ASSETS_PATH; + SDL_strlcat(path, tile_infos[i].asset_path, SDL_arraysize(path)); + int width = 0, height = 0; - stbi_uc *data = stbi_load(tile_infos[i].asset_path, &width, &height, NULL, 4); + stbi_uc *data = stbi_load(path, &width, &height, NULL, 4); if (!data) { - log_error("Failed to load texture (\"%s\"). Exiting.", tile_infos[i].asset_path); + log_error("Failed to load texture (\"%s\"). Exiting.", path); SDL_ReleaseGPUTexture(device, tile_textures_array); SDL_CancelGPUCommandBuffer(command_buffer); return false; @@ -1309,12 +1323,12 @@ int main(int argc, char **argv) { return 1; } - if (!load_map("../assets/map/map.sv", ¤t_map)) { + if (!load_map("map.sv", ¤t_map)) { log_error("Failed to load initial map. Exiting."); return 1; } - SDL_GPUTexture *player_texture = create_shader_texture("../assets/decorations/strawberry.png"); + SDL_GPUTexture *player_texture = create_shader_texture("decorations/strawberry.png"); if (!player_texture) { log_error("Failed to create shader texture. Exiting."); return 1; @@ -1694,7 +1708,7 @@ int main(int argc, char **argv) { } if (event.key.key == SDLK_F4) { - char *map_path = SDL_strdup(current_map.save_path); + char *map_path = SDL_strdup(current_map.name); unload_map(¤t_map); load_map(map_path, ¤t_map); SDL_free(map_path);