#include #include #include #include #include "cJSON.h" #include "game.h" #include "player.h" #include "wall.h" extern Texture color_hills; extern Texture color_desert; struct game game; void update_game(float dt) { for (int i = 0; i < MAX_ENTITY_COUNT; i++) { struct entity* current = &game.entities[i]; if (!entity_active(current)) continue; update_entity(current, dt); } } void draw_background() { float image_scale = (float)WINDOW_HEIGHT / game.background_texture.height; float xpos = 0; while (xpos < WINDOW_WIDTH) { DrawTexturePro(game.background_texture, (Rectangle){ .x = 0, .y = 0, .width = game.background_texture.width, .height = game.background_texture.height }, (Rectangle) { .x = xpos, .y = 0, .width = game.background_texture.width * image_scale, .height = game.background_texture.height * image_scale }, (Vector2) {0, 0}, 0, WHITE); xpos += game.background_texture.width * image_scale; } } void draw_game() { draw_background(); for (int i = 0; i < MAX_ENTITY_COUNT; i++) { const struct entity* current = &game.entities[i]; //Skip inactive entities if (!entity_active(current)) continue; draw_entity(current); } } void init_game() { memset(&game, 0, sizeof(struct game)); } 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; } void load_level_from_tiled_tmj(const char* filename) { cJSON *level_data_json = cJSON_Parse(filename); { int length; unsigned char* level_data_raw = LoadFileData(filename, &length); level_data_json = cJSON_ParseWithLength((char*)level_data_raw, length); UnloadFileData(level_data_raw); } const cJSON* layers = cJSON_GetObjectItemCaseSensitive(level_data_json, "layers"); if (cJSON_IsArray(layers)) { const cJSON* layer = NULL; cJSON_ArrayForEach(layer, layers) { const cJSON* type = cJSON_GetObjectItemCaseSensitive(layer, "type"); if (!cJSON_IsString(type)) continue; if (strcmp(type->valuestring, "imagelayer") == 0) { const cJSON* properties = NULL; properties = cJSON_GetObjectItemCaseSensitive(layer, "properties"); if (cJSON_IsArray(properties)) { const cJSON* property = NULL; 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; set_background(bg_name->valuestring); } } } else if (strcmp(type->valuestring, "tilelayer") == 0) { const cJSON* width = cJSON_GetObjectItemCaseSensitive(layer, "width"); const cJSON* data = cJSON_GetObjectItemCaseSensitive(layer, "data"); int c = 0; if (!cJSON_IsArray(data) || !cJSON_IsNumber(width)) continue; 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); } c++; } } } } cJSON_Delete(level_data_json); }