diff options
Diffstat (limited to 'src/game.c')
| -rw-r--r-- | src/game.c | 78 |
1 files changed, 45 insertions, 33 deletions
@@ -64,13 +64,16 @@ void init_game() { memset(&game, 0, sizeof(struct game)); } -void set_background(const char* background_name) { +static void set_background(const char* background_name) { if (strcmp(background_name, "color_hills") == 0) game.background_texture = color_hills; else if (strcmp(background_name, "color_desert") == 0) game.background_texture = color_desert; } +static void load_image_layer_object(const cJSON* layer); +static void load_tile_layer_object(const cJSON* layer); + void load_level_from_tiled_tmj(const char* filename) { cJSON *level_data_json = cJSON_Parse(filename); @@ -94,48 +97,57 @@ void load_level_from_tiled_tmj(const char* filename) { continue; if (strcmp(type->valuestring, "imagelayer") == 0) { - const cJSON* properties = NULL; - properties = cJSON_GetObjectItemCaseSensitive(layer, "properties"); + load_image_layer_object(layer); + } else if (strcmp(type->valuestring, "tilelayer") == 0) { + load_tile_layer_object(layer); + } + } + } - if (cJSON_IsArray(properties)) { - const cJSON* property = NULL; + cJSON_Delete(level_data_json); +} - cJSON_ArrayForEach(property, properties) { - const cJSON* name = cJSON_GetObjectItemCaseSensitive(property, "name"); - const cJSON* bg_name = cJSON_GetObjectItemCaseSensitive(property, "value"); - if (!cJSON_IsString(name) - || !cJSON_IsString(bg_name) - || strcmp(name->valuestring, "bg_name") != 0) - continue; +static void load_image_layer_object(const cJSON* layer) { + const cJSON* properties = NULL; + properties = cJSON_GetObjectItemCaseSensitive(layer, "properties"); - set_background(bg_name->valuestring); - } + if (cJSON_IsArray(properties)) { + const cJSON* property = NULL; - } - } else if (strcmp(type->valuestring, "tilelayer") == 0) { - const cJSON* width = cJSON_GetObjectItemCaseSensitive(layer, "width"); - const cJSON* data = cJSON_GetObjectItemCaseSensitive(layer, "data"); - int c = 0; + cJSON_ArrayForEach(property, properties) { + const cJSON* name = cJSON_GetObjectItemCaseSensitive(property, "name"); + const cJSON* bg_name = cJSON_GetObjectItemCaseSensitive(property, "value"); - if (!cJSON_IsArray(data) || !cJSON_IsNumber(width)) - continue; + if (!cJSON_IsString(name) + || !cJSON_IsString(bg_name) + || strcmp(name->valuestring, "bg_name") != 0) + continue; - const cJSON* tile = NULL; - cJSON_ArrayForEach(tile, data) { + set_background(bg_name->valuestring); + } - if (cJSON_IsNumber(tile) && tile->valueint != 0) { - int x_pos = (c % width->valueint) * BLOCK_WIDTH + BLOCK_WIDTH / 2; - int y_pos = (c / width->valueint) * BLOCK_HEIGHT + BLOCK_HEIGHT / 2; + } +} - add_wall(x_pos, y_pos, tile->valueint - 1); - } +static void load_tile_layer_object(const cJSON* layer) { + const cJSON* width = cJSON_GetObjectItemCaseSensitive(layer, "width"); + const cJSON* data = cJSON_GetObjectItemCaseSensitive(layer, "data"); + int c = 0; - c++; - } - } + if (!cJSON_IsArray(data) || !cJSON_IsNumber(width)) + return; + + const cJSON* tile = NULL; + cJSON_ArrayForEach(tile, data) { + + if (cJSON_IsNumber(tile) && tile->valueint != 0) { + int x_pos = (c % width->valueint) * BLOCK_WIDTH + BLOCK_WIDTH / 2; + int y_pos = (c / width->valueint) * BLOCK_HEIGHT + BLOCK_HEIGHT / 2; + + add_wall(x_pos, y_pos, tile->valueint - 1); } - } - cJSON_Delete(level_data_json); + c++; + } } |
