From 2dbdc5ac40eeae570b39af3070588a7901a6689d Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 27 Jan 2025 16:19:07 +0100 Subject: [PATCH] add colour selection based on ID --- src/game/tetromino/placing.c | 9 +++++++-- src/game/tetromino/shapes.c | 15 +++++++++++++++ src/game/tetromino/shapes.h | 3 +++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index d8a9b2b..c468df5 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -39,7 +39,12 @@ void tmp_set_all(GameData* game_data) { } void tmp_set_random(GameData* game_data) { - uint32_t x = rand() % TETROMINO_COUNT; + ShapeId id = rand() % TETROMINO_COUNT; - //set_shape(game_data->row, shape, colour, 1, 1); + static int finished = 0; + if (finished) + return; + + finished = 1; + set_shape(game_data->row, shape_from_id(id), colour_from_id(id), 1, 1); } diff --git a/src/game/tetromino/shapes.c b/src/game/tetromino/shapes.c index 0108900..0ad35c6 100644 --- a/src/game/tetromino/shapes.c +++ b/src/game/tetromino/shapes.c @@ -1,5 +1,7 @@ #include "shapes.h" +#include "../../window/colour.h" + /* 0 1 2 3 */ #define TET_SHAPE_O ((Shape)0xCC00) // 1100 1100 0000 0000 the O tetromino with no rotation #define TET_SHAPE_I ((Shape)0x8888) // 1000 1000 1000 1000 the I tetromino with no rotation @@ -38,3 +40,16 @@ Shape shape_from_id(ShapeId id) { // first 3 bits is the shape type, the rest is rotation data return shapes[id & 7][id >> 3]; } + +Colour colour_from_id(ShapeId id) { + switch (id & 7) { + case TETROMINO_O: return COLOUR_YELLOW; + case TETROMINO_I: return COLOUR_CYAN; + case TETROMINO_S: return COLOUR_RED; + case TETROMINO_Z: return COLOUR_GREEN; + case TETROMINO_T: return COLOUR_MAGENTA; + case TETROMINO_L: return COLOUR_ORANGE; + case TETROMINO_J: return COLOUR_BLUE; + default: return COLOUR_NONE; + } +} diff --git a/src/game/tetromino/shapes.h b/src/game/tetromino/shapes.h index e87eb72..120be3d 100644 --- a/src/game/tetromino/shapes.h +++ b/src/game/tetromino/shapes.h @@ -1,6 +1,8 @@ #pragma once #include +#include "../../window/colour.h" + typedef uint16_t Shape; typedef uint8_t ShapeRow; @@ -33,3 +35,4 @@ static inline _Bool is_set(ShapeRow row, uint8_t index) { } Shape shape_from_id(ShapeId id); +Colour colour_from_id(ShapeId id);