mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add selected shape drawing
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user