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:
@@ -51,7 +51,7 @@ static void update_input(GameData* game_data, const uint8_t* keys) {
|
|||||||
game_data->selected.x--;
|
game_data->selected.x--;
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D])
|
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D])
|
||||||
game_data->selected.y++;
|
game_data->selected.x++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,3 +34,28 @@ typedef union {
|
|||||||
#define COLOUR_BLUE ((Colour){BLUE})
|
#define COLOUR_BLUE ((Colour){BLUE})
|
||||||
#define COLOUR_MAGENTA ((Colour){MAGENTA})
|
#define COLOUR_MAGENTA ((Colour){MAGENTA})
|
||||||
#define COLOUR_WHITE ((Colour){WHITE})
|
#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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "../errors.h"
|
#include "../errors.h"
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
|
#include "../game/tetromino/shapes.h"
|
||||||
#include "colour.h"
|
#include "colour.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
@@ -32,6 +33,14 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
|||||||
return 0;
|
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) {
|
void renderer_update(const RenderData* render_data) {
|
||||||
SDL_Renderer* renderer = render_data->renderer;
|
SDL_Renderer* renderer = render_data->renderer;
|
||||||
GameData* data = render_data->game_data;
|
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_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
||||||
success |= SDL_RenderClear(renderer);
|
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++) {
|
for (uint8_t y = 0; y < ROWS; y++) {
|
||||||
Row row = data->row[y];
|
Row row = data->row[y];
|
||||||
|
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++) {
|
for (uint8_t x = 0; x < COLUMNS; x++) {
|
||||||
if (row.columns[x].packed != 0) {
|
if (row.columns[x].packed != 0) {
|
||||||
success |= SDL_SetRenderDrawColor(renderer,
|
set_colour(renderer, row.columns[x]);
|
||||||
0x55 * row.columns[x].r, // repeat the 2 bits by multiplying by 0x55
|
success |= draw_block(renderer, x, y);
|
||||||
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});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user