From 1a342d764c65b1d9c9e4183310c04398dee169e7 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 29 Jan 2025 10:45:28 +0100 Subject: [PATCH] add selected shape drawing --- src/game/game.c | 2 +- src/window/colour.h | 25 +++++++++++++++++++++++++ src/window/renderer.c | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/game/game.c b/src/game/game.c index fbdd7ec..05a17aa 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -51,7 +51,7 @@ static void update_input(GameData* game_data, const uint8_t* keys) { game_data->selected.x--; if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) - game_data->selected.y++; + game_data->selected.x++; } diff --git a/src/window/colour.h b/src/window/colour.h index 3a4c8a7..b75b141 100644 --- a/src/window/colour.h +++ b/src/window/colour.h @@ -34,3 +34,28 @@ typedef union { #define COLOUR_BLUE ((Colour){BLUE}) #define COLOUR_MAGENTA ((Colour){MAGENTA}) #define COLOUR_WHITE ((Colour){WHITE}) + +// gets the red channel in 32 bit colour space +static inline uint8_t colour_red32(Colour colour) { + return colour.r * 0x55; +} + +// gets the green channel in 32 bit colour space +static inline uint8_t colour_green32(Colour colour) { + return colour.g * 0x55; +} + +// gets the blue channel in 32 bit colour space +static inline uint8_t colour_blue32(Colour colour) { + return colour.b * 0x55; +} + +// gets the alpha channel in 32 bit colour space +static inline uint8_t colour_alpha32(Colour colour) { + return colour.a * 0x55; +} + +// gets the packed colour in 32 bit colour space +static inline uint32_t colour_packed32(Colour colour) { + return colour.packed * 0x55; +} diff --git a/src/window/renderer.c b/src/window/renderer.c index 31a4112..ce2bc0a 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -10,6 +10,7 @@ #include "../errors.h" #include "../game/game.h" +#include "../game/tetromino/shapes.h" #include "colour.h" #include "renderer.h" @@ -32,6 +33,14 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) { return 0; } +static inline int draw_block(SDL_Renderer* renderer, uint8_t x, uint8_t y) { + return SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1}); +} + +static inline void set_colour(SDL_Renderer* renderer, Colour c) { + (void)SDL_SetRenderDrawColor(renderer, colour_red32(c), colour_green32(c), colour_blue32(c), colour_alpha32(c)); +} + void renderer_update(const RenderData* render_data) { SDL_Renderer* renderer = render_data->renderer; GameData* data = render_data->game_data; @@ -42,17 +51,32 @@ void renderer_update(const RenderData* render_data) { success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); success |= SDL_RenderClear(renderer); + SelectedShape selected = render_data->game_data->selected; + Shape selected_shape = shape_from_id(selected.id); + Colour selected_colour = colour_from_id(selected.id); + + set_colour(renderer, selected_colour); + + // draw the selected block + for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) { + ShapeRow shape_row = shape_get_row(selected_shape, y); + + if (shape_row == 0) + continue; + + for (uint8_t x = 0; x < SHAPE_WIDTH; x++) + if (is_set(shape_row, x)) + draw_block(renderer, selected.x + x, selected.y + y); + } + + // draw the block data in the level for (uint8_t y = 0; y < ROWS; y++) { Row row = data->row[y]; for (uint8_t x = 0; x < COLUMNS; x++) { if (row.columns[x].packed != 0) { - success |= SDL_SetRenderDrawColor(renderer, - 0x55 * row.columns[x].r, // repeat the 2 bits by multiplying by 0x55 - 0x55 * row.columns[x].g, - 0x55 * row.columns[x].b, - 0x55 * row.columns[x].a); - success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1}); + set_colour(renderer, row.columns[x]); + success |= draw_block(renderer, x, y); } } }