diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-12-12 03:12:11 +0100 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-12-12 03:12:11 +0100 |
| commit | d72d3f8c4973c2bfb307b18abf2365823f3bb71c (patch) | |
| tree | b873ad7ead6906176abf560f5537f1562feb57ac /src | |
| parent | 815c9a41d35b1c4984c99ddd99068d8f1334b3bc (diff) | |
snake: simplify Snake::Draw() a little bit
Diffstat (limited to 'src')
| -rw-r--r-- | src/games/snake/Snake.cpp | 100 |
1 files changed, 40 insertions, 60 deletions
diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp index ef9b678..b7c9004 100644 --- a/src/games/snake/Snake.cpp +++ b/src/games/snake/Snake.cpp @@ -223,17 +223,16 @@ Snake::Draw() { float world_width = 4.0f; float world_height = 3.0f; - float tile_size = (world_width / 2) / max_map_width; + float bodypart_size = 0.8f * tile_size; float bodypart_offset = (tile_size - bodypart_size) / 2; + int32_t max_bodypart_count = ARRAY_COUNT(m_body_positions); - float map_view_width = tile_size * (float)m_map_width; - float map_view_height = tile_size * (float)m_map_height; - float map_x = (world_width - map_view_width) / 2; - float map_y = (world_height - map_view_height) / 2; - - int32_t max_positions = ARRAY_COUNT(m_body_positions); + float map_width = tile_size * (float)m_map_width; + float map_height = tile_size * (float)m_map_height; + float map_x = (world_width - map_width) / 2; + float map_y = (world_height - map_height) / 2; uint32_t z_bg = z_layer1; uint32_t z_food = z_layer2; @@ -245,13 +244,11 @@ Snake::Draw() /* draw map background */ - V3F32 map_world_pos = {map_x, map_y}; - V2F32 map_world_dim = {map_view_width, map_view_height}; Rectangle map_world_rect = { - map_world_pos.x, - map_world_pos.y, - map_world_pos.x + map_world_dim.x, - map_world_pos.y + map_world_dim.y + map_x, + map_y, + map_x + map_width, + map_y + map_height }; g_renderer.PushRectangle(map_world_rect, color_bg, z_bg); @@ -260,70 +257,53 @@ Snake::Draw() // 1) if tail > head: advance to end first int32_t tail = m_tail; if (tail > m_head) { - while (tail < max_positions) { - V3F32 local_pos = { - (float)m_body_positions[tail].x * tile_size + bodypart_offset, - (float)m_body_positions[tail].y * tile_size + bodypart_offset, - 1.0f - }; - V2F32 local_dim = {bodypart_size, bodypart_size}; - - V3F32 world_pos = { - map_world_pos.x + local_pos.x, - map_world_pos.y + local_pos.y, - 1.0f - }; - V2F32 world_dim = local_dim; - Rectangle world_rect = { - world_pos.x, - world_pos.y, - world_pos.x + world_dim.x, - world_pos.y + world_dim.y, + while (tail < max_bodypart_count) { + float xoff = (float)m_body_positions[tail].x * tile_size + bodypart_offset; + float yoff = (float)m_body_positions[tail].y * tile_size + bodypart_offset; + + float x = map_x + xoff; + float y = map_y + yoff; + + Rectangle rect = { + x, + y, + x + bodypart_size, + y + bodypart_size }; - g_renderer.PushRectangle(world_rect, color_snake, z_snake); + g_renderer.PushRectangle(rect, color_snake, z_snake); tail++; } tail = 0; } // 2) advance to head while (tail <= m_head) { - V3F32 local_pos = { - (float)m_body_positions[tail].x * tile_size + bodypart_offset, - (float)m_body_positions[tail].y * tile_size + bodypart_offset, - 1.0f - }; - V2F32 local_dim = {bodypart_size, bodypart_size}; + float xoff = (float)m_body_positions[tail].x * tile_size + bodypart_offset; + float yoff = (float)m_body_positions[tail].y * tile_size + bodypart_offset; - V3F32 world_pos = { - map_world_pos.x + local_pos.x, - map_world_pos.y + local_pos.y, - 1.0f - }; - V2F32 world_dim = local_dim; - Rectangle world_rect = { - world_pos.x, - world_pos.y, - world_pos.x + world_dim.x, - world_pos.y + world_dim.y, + float x = map_x + xoff; + float y = map_y + yoff; + + Rectangle rect = { + x, + y, + x + bodypart_size, + y + bodypart_size }; - g_renderer.PushRectangle(world_rect, color_snake, z_snake); + g_renderer.PushRectangle(rect, color_snake, z_snake); tail++; } /* draw food */ - V2F32 pos = { - map_world_pos.x + (float)m_food_position.x * tile_size + bodypart_offset, - map_world_pos.y + (float)m_food_position.y * tile_size + bodypart_offset, - }; - V2F32 dim = {bodypart_size, bodypart_size}; + float food_x = map_x + (float)m_food_position.x * tile_size + bodypart_offset; + float food_y = map_y + (float)m_food_position.y * tile_size + bodypart_offset; Rectangle rect = { - pos.x, - pos.y, - pos.x + dim.x, - pos.y + dim.y + food_x, + food_y, + food_x + bodypart_size, + food_y + bodypart_size }; g_renderer.PushRectangle(rect, color_food, z_food); } |
