From e6683dfec11675d2d285799fe180c29bcd946853 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 19 Aug 2023 16:40:12 +0800 Subject: [PATCH] Draw only things within the game rectangle Testing it out on sandbox for now --- scenes/editor_scene.c | 181 +++++++++++++++++++++++++----------------- 1 file changed, 106 insertions(+), 75 deletions(-) diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 0b07b63..1ef0bf3 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -81,92 +81,115 @@ static void level_scene_render_func(Scene_t* scene) TileGrid_t tilemap = data->tilemap; Entity_t* p_ent; + Vector2 min = GetScreenToWorld2D((Vector2){data->game_rec.x, data->game_rec.y}, data->cam); + Vector2 max = GetScreenToWorld2D( + (Vector2){ + data->game_rec.x + data->game_rec.width, + data->game_rec.y + data->game_rec.height + }, + data->cam + ); + min = Vector2Scale(min, 1.0f/tilemap.tile_size); + max = Vector2Scale(max, 1.0f/tilemap.tile_size); + min.x = (int)fmax(0, min.x - 1); + min.y = (int)fmax(0, min.y - 1); + max.x = (int)fmin(tilemap.width-1, max.x + 1); + max.y = (int)fmin(tilemap.height-1, max.y + 1); + printf("(%d,%d %d,%d\n", (int)min.x, (int)min.y, (int)max.x, (int)max.y); BeginTextureMode(data->game_viewport); ClearBackground(WHITE); BeginMode2D(data->cam); - for (size_t i = 0; i < tilemap.n_tiles; ++i) + //for (size_t i = 0; i < tilemap.n_tiles; ++i) + //{ + for (int tile_y = min.y; tile_y <= max.y; tile_y++) { - char buffer[6] = {0}; - int x = (i % tilemap.width) * TILE_SIZE; - int y = (i / tilemap.width) * TILE_SIZE; - sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set)); - - if (!tilemap.tiles[i].moveable) + //for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) + for (int tile_x = min.x; tile_x <= max.x; tile_x++) { + //int i = get_tile_idx(x, y, &tilemap); + int i = tile_x + tile_y * tilemap.width; + char buffer[6] = {0}; + int x = tile_x * TILE_SIZE; + int y = tile_y * TILE_SIZE; + sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set)); + + if (!tilemap.tiles[i].moveable) + { + // Draw water tile + Color water_colour = ColorAlpha(RED, 0.2); + DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour); + } + + uint8_t tile_sprite_idx = tilemap.tiles[i].tile_type + tilemap.tiles[i].rotation; + if (data->tile_sprites[tile_sprite_idx] != NULL) + { + draw_sprite(data->tile_sprites[tile_sprite_idx], (Vector2){x,y}, false); + } + else if (tilemap.tiles[i].tile_type == SOLID_TILE) + { + DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK); + } + else if (tilemap.tiles[i].tile_type == ONEWAY_TILE) + { + DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, MAROON); + } + else if (tilemap.tiles[i].tile_type == LADDER) + { + DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, ORANGE); + } + else if (tilemap.tiles[i].tile_type == SPIKES) + { + DrawRectangle( + x + tilemap.tiles[i].offset.x, y + tilemap.tiles[i].offset.y, + tilemap.tiles[i].size.x, tilemap.tiles[i].size.y, RED + ); + } + if (tilemap.tiles[i].wet) + { +#define SURFACE_THICKNESS 4 + int up = i - tilemap.width; + int bot = i + tilemap.width; + int right = i + 1; + int left = i - 1; + int bot_line = y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP - SURFACE_THICKNESS / 2; + if (up >= 0 && tilemap.tiles[up].wet) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, y}, (Vector2){x + TILE_SIZE / 2, y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + + + if ( + bot <= tilemap.n_tiles + && tilemap.tiles[i].water_level == 0 + ) + { + if (i % tilemap.width != 0 && tilemap.tiles[left].wet && (tilemap.tiles[bot].solid == SOLID || tilemap.tiles[bot-1].solid == SOLID)) + { + DrawLineEx((Vector2){x, bot_line}, (Vector2){x + TILE_SIZE / 2, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + if (right % tilemap.width != 0 && tilemap.tiles[right].wet && (tilemap.tiles[bot].solid == SOLID || tilemap.tiles[bot+1].solid == SOLID)) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, bot_line}, (Vector2){x + TILE_SIZE, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + } + } + // Draw water tile - Color water_colour = ColorAlpha(RED, 0.2); - DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour); - } - - uint8_t tile_sprite_idx = tilemap.tiles[i].tile_type + tilemap.tiles[i].rotation; - if (data->tile_sprites[tile_sprite_idx] != NULL) - { - draw_sprite(data->tile_sprites[tile_sprite_idx], (Vector2){x,y}, false); - } - else if (tilemap.tiles[i].tile_type == SOLID_TILE) - { - DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK); - } - else if (tilemap.tiles[i].tile_type == ONEWAY_TILE) - { - DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, MAROON); - } - else if (tilemap.tiles[i].tile_type == LADDER) - { - DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, ORANGE); - } - else if (tilemap.tiles[i].tile_type == SPIKES) - { + uint32_t water_height = tilemap.tiles[i].water_level * WATER_BBOX_STEP; + // Draw water tile + Color water_colour = ColorAlpha(BLUE, 0.5); DrawRectangle( - x + tilemap.tiles[i].offset.x, y + tilemap.tiles[i].offset.y, - tilemap.tiles[i].size.x, tilemap.tiles[i].size.y, RED + x, + y + (TILE_SIZE - water_height), + TILE_SIZE, + water_height, + water_colour ); - } - if (tilemap.tiles[i].wet) - { -#define SURFACE_THICKNESS 4 - int up = i - tilemap.width; - int bot = i + tilemap.width; - int right = i + 1; - int left = i - 1; - int bot_line = y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP - SURFACE_THICKNESS / 2; - if (up >= 0 && tilemap.tiles[up].wet) + if (tilemap.tiles[i].max_water_level < MAX_WATER_LEVEL) { - DrawLineEx((Vector2){x + TILE_SIZE / 2, y}, (Vector2){x + TILE_SIZE / 2, y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + DrawRectangleLinesEx((Rectangle){x, y, TILE_SIZE, TILE_SIZE}, 2.0, ColorAlpha(BLUE, 0.5)); } - - - if ( - bot <= tilemap.n_tiles - && tilemap.tiles[i].water_level == 0 - ) - { - if (i % tilemap.width != 0 && tilemap.tiles[left].wet && (tilemap.tiles[bot].solid == SOLID || tilemap.tiles[bot-1].solid == SOLID)) - { - DrawLineEx((Vector2){x, bot_line}, (Vector2){x + TILE_SIZE / 2, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); - } - if (right % tilemap.width != 0 && tilemap.tiles[right].wet && (tilemap.tiles[bot].solid == SOLID || tilemap.tiles[bot+1].solid == SOLID)) - { - DrawLineEx((Vector2){x + TILE_SIZE / 2, bot_line}, (Vector2){x + TILE_SIZE, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); - } - } - } - - // Draw water tile - uint32_t water_height = tilemap.tiles[i].water_level * WATER_BBOX_STEP; - // Draw water tile - Color water_colour = ColorAlpha(BLUE, 0.5); - DrawRectangle( - x, - y + (TILE_SIZE - water_height), - TILE_SIZE, - water_height, - water_colour - ); - if (tilemap.tiles[i].max_water_level < MAX_WATER_LEVEL) - { - DrawRectangleLinesEx((Rectangle){x, y, TILE_SIZE, TILE_SIZE}, 2.0, ColorAlpha(BLUE, 0.5)); } } @@ -175,6 +198,14 @@ static void level_scene_render_func(Scene_t* scene) { CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T); CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T); + + if ( + p_ct->position.x < min.x * tilemap.tile_size + || p_ct->position.x + p_bbox->size.x > max.x * tilemap.tile_size + || p_ct->position.y < min.y * tilemap.tile_size + || p_ct->position.y + p_bbox->size.y > max.y * tilemap.tile_size + ) continue; + Color colour; switch(p_ent->m_tag) {