From c546f78c4c746c3b0ed50b17efd15dea47231451 Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 3 Feb 2025 15:02:00 +0100 Subject: [PATCH] use correct 8 bit colourspace instead of including alpha --- src/game/game.c | 4 +-- src/game/game.h | 6 ++-- src/game/tetromino/placing.c | 6 ++-- src/game/tetromino/shapes.c | 6 ++-- src/game/tetromino/shapes.h | 4 +-- src/window/colour.h | 61 ------------------------------------ src/window/colour8.h | 38 ++++++++++++++++++++++ src/window/renderer.c | 8 ++--- 8 files changed, 55 insertions(+), 78 deletions(-) delete mode 100644 src/window/colour.h create mode 100644 src/window/colour8.h diff --git a/src/game/game.c b/src/game/game.c index 5fe4843..79d7e64 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -8,7 +8,7 @@ #include #include "../main.h" -#include "../window/colour.h" +#include "../window/colour8.h" #include "./tetromino/shapes.h" #include "tetromino/placing.h" @@ -39,7 +39,7 @@ void game_init(GameData* const game_data) { // allocate size for each row for (int8_t i = 0; i < ROWS; i++) { - game_data->rows[i] = calloc(COLUMNS, sizeof(Colour)); + game_data->rows[i] = calloc(COLUMNS, sizeof(Colour8)); // game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging } diff --git a/src/game/game.h b/src/game/game.h index 3cb948a..c6ecd02 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -2,15 +2,15 @@ #include -#include "../window/colour.h" +#include "../window/colour8.h" #include "tetromino/shapes.h" // stores the data used in the game #define COLUMNS ((int8_t)10) #define ROWS ((int8_t)(COLUMNS * 2)) -typedef const Colour* CRow; -typedef Colour* Row; +typedef const Colour8* CRow; +typedef Colour8* Row; typedef struct { Row rows[ROWS]; diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index 3f9e1c8..c9e09a2 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -2,14 +2,14 @@ #include -#include "../../window/colour.h" +#include "../../window/colour8.h" #include "../game.h" #include "shapes.h" static bool is_filled(CRow row) { for (int8_t x = 0; x < COLUMNS; x++) { - if (row[x].packed == NONE) { + if (row[x].packed == 0) { return false; } } @@ -55,7 +55,7 @@ static void clear_rows(Row* rows) { // sets a shape to the screen static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) { const Shape shape = shape_from_id(id); - const Colour colour = colour_from_id(id); + const Colour8 colour = colour_from_id(id); for (int8_t y = 0; y < SHAPE_HEIGHT; y++) { ShapeRow shape_row = shape_get_row(shape, y); diff --git a/src/game/tetromino/shapes.c b/src/game/tetromino/shapes.c index dec5e6a..1c36700 100644 --- a/src/game/tetromino/shapes.c +++ b/src/game/tetromino/shapes.c @@ -1,6 +1,6 @@ #include "shapes.h" -#include "../../window/colour.h" +#include "../../window/colour8.h" /* 0 1 2 3 */ #define SHAPE_O ((Shape)0x0660) // 0000 0110 0110 0000 the O tetromino with no rotation @@ -53,7 +53,7 @@ Shape shape_from_id(ShapeId id) { return shapes[id & 7][id >> 3]; } -Colour colour_from_id(ShapeId id) { +Colour8 colour_from_id(ShapeId id) { switch (id & 7) { case TETROMINO_O: return COLOUR_YELLOW; case TETROMINO_I: return COLOUR_CYAN; @@ -62,6 +62,6 @@ Colour colour_from_id(ShapeId id) { case TETROMINO_T: return COLOUR_MAGENTA; case TETROMINO_L: return COLOUR_ORANGE; case TETROMINO_J: return COLOUR_BLUE; - default: return COLOUR_NONE; + default: return COLOUR_BLACK; } } diff --git a/src/game/tetromino/shapes.h b/src/game/tetromino/shapes.h index 0c7eb37..6722527 100644 --- a/src/game/tetromino/shapes.h +++ b/src/game/tetromino/shapes.h @@ -2,7 +2,7 @@ #include #include -#include "../../window/colour.h" +#include "../../window/colour8.h" typedef uint16_t Shape; typedef uint8_t ShapeRow; @@ -36,4 +36,4 @@ static inline bool is_set(ShapeRow row, uint8_t index) { } Shape shape_from_id(ShapeId id); -Colour colour_from_id(ShapeId id); +Colour8 colour_from_id(ShapeId id); diff --git a/src/window/colour.h b/src/window/colour.h deleted file mode 100644 index b75b141..0000000 --- a/src/window/colour.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once -#include - -// fits colours with each channel having a width of 2 bits -typedef union { - uint8_t packed; - struct { - uint8_t a : 2; - uint8_t b : 2; - uint8_t g : 2; - uint8_t r : 2; - }; -} Colour; - - -#define NONE ((uint8_t)0) -#define BLACK ((uint8_t)3) -#define RED ((uint8_t)0xC0 | BLACK) -#define YELLOW ((uint8_t)0xF0 | BLACK) -#define ORANGE ((uint8_t)0xE0 | BLACK) -#define GREEN ((uint8_t)0x30 | BLACK) -#define CYAN ((uint8_t)0x3C | BLACK) -#define BLUE ((uint8_t)0x0C | BLACK) -#define MAGENTA ((uint8_t)0xCC | BLACK) -#define WHITE ((uint8_t)0xFF) - -#define COLOUR_NONE ((Colour){NONE}) -#define COLOUR_BLACK ((Colour){BLACK}) -#define COLOUR_RED ((Colour){RED}) -#define COLOUR_YELLOW ((Colour){YELLOW}) -#define COLOUR_ORANGE ((Colour){ORANGE}) -#define COLOUR_GREEN ((Colour){GREEN}) -#define COLOUR_CYAN ((Colour){CYAN}) -#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/colour8.h b/src/window/colour8.h new file mode 100644 index 0000000..7e2f8a5 --- /dev/null +++ b/src/window/colour8.h @@ -0,0 +1,38 @@ +#pragma once +#include + +// fits colours with each channel having a width of 2 bits +typedef union { + uint8_t packed; + struct { + uint8_t b : 2; + uint8_t g : 3; + uint8_t r : 3; + }; +} Colour8; + +/* rrrg ggbb */ +#define COLOUR_BLACK ((Colour8){0x00}) // 0000 0000 +#define COLOUR_RED ((Colour8){0xE0}) // 1110 0000 +#define COLOUR_YELLOW ((Colour8){0xFC}) // 1111 1100 +#define COLOUR_ORANGE ((Colour8){0xEC}) // 1111 1100 +#define COLOUR_GREEN ((Colour8){0x1C}) // 0001 1100 +#define COLOUR_CYAN ((Colour8){0x1F}) // 0001 1111 +#define COLOUR_BLUE ((Colour8){0x03}) // 0000 0011 +#define COLOUR_MAGENTA ((Colour8){0xE3}) // 1110 0011 +#define COLOUR_WHITE ((Colour8){0xFF}) // 1111 1111 + +// gets the red channel in 32 bit colour space +static inline uint8_t colour8_red32(Colour8 colour) { + return colour.r * (255 / 7); +} + +// gets the green channel in 32 bit colour space +static inline uint8_t colour8_green32(Colour8 colour) { + return colour.g * (255 / 7); +} + +// gets the blue channel in 32 bit colour space +static inline uint8_t colour8_blue32(Colour8 colour) { + return colour.b * (255 / 3); +} diff --git a/src/window/renderer.c b/src/window/renderer.c index 7f69126..bc2b4f4 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -11,7 +11,7 @@ #include "../errors.h" #include "../game/game.h" #include "../game/tetromino/shapes.h" -#include "colour.h" +#include "colour8.h" #include "renderer.h" @@ -39,8 +39,8 @@ static inline int draw_block(SDL_Renderer* renderer, int8_t x, int8_t y) { } // sets the colour32 from the colour8 -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)); +static inline void set_colour(SDL_Renderer* renderer, Colour8 c) { + (void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF); } // draw the selected block @@ -71,7 +71,7 @@ static void render_level(SDL_Renderer* renderer, GameData* data) { set_colour(renderer, row[x]); draw_block(renderer, x, y); } else { - set_colour(renderer, (Colour){0}); + set_colour(renderer, (Colour8){0}); draw_block(renderer, x, y); } }