From 2c5be9685ce8199751779b8fc9ee2f5faef19d9f Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 12 Feb 2025 19:24:21 +0100 Subject: [PATCH] fix: remove bitfield from union --- src/game/tetromino/placing.c | 12 ++++---- src/window/colour8.h | 60 ++++++++++++++++++++++-------------- src/window/renderer.c | 2 +- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index eb00543..4ea95f1 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -10,7 +10,7 @@ static bool is_filled(row_const const row) { for (int8_t x = 0; x < COLUMNS; x++) { - if (row[x].packed == 0) { + if (row[x] == 0) { return false; } } @@ -53,7 +53,7 @@ static void clear_rows(row* const rows, uint16_t* const score) { // zero out the filled row for (int8_t x = 0; x < COLUMNS; x++) - cache[dat.filled][x].packed = 0; + cache[dat.filled][x] = 0; } while (dat.filled > 0); } @@ -82,16 +82,16 @@ static bool shape_intersects(row const* const rows, shape_id const id, int8_t co for (int8_t y0 = 0; y0 < SHAPE_HEIGHT; y0++) { shape_row const shape_row = shape_get_row(shape, y0); // get the shape row - if (shape_row == 0) continue; // if the row doesn't contain data; continue + if (shape_row == 0) continue; // if the row doesn't contain data; continue for (int8_t x0 = 0; x0 < SHAPE_WIDTH; x0++) { if (shape_is_set(shape_row, x0) == false) continue; // if the bit isn't set at this index; continue int8_t const x1 = x + x0; int8_t const y1 = y + y0; - if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds - if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds - if (rows[y1][x1].packed != 0) return true; // if there is a block here + if (x1 < 0 || x1 >= COLUMNS) return true; // if X is out of bounds + if (y1 < 0 || y1 >= ROWS) return true; // if Y is out of bounds + if (rows[y1][x1] != 0) return true; // if there is a block here } } return false; diff --git a/src/window/colour8.h b/src/window/colour8.h index 6e0d7fc..f7c5e3f 100644 --- a/src/window/colour8.h +++ b/src/window/colour8.h @@ -1,39 +1,53 @@ #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; -#define color8 colour8 +#include "SDL_render.h" -/* 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 +// stores colour in a rrrgggbb format, which maps exactly to 8 bits +typedef uint8_t 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 const colour) { - return colour.r * (255 / 7); + return (colour >> 5) * (255 / 7); } // gets the green channel in 32 bit colour space static inline uint8_t colour8_green32(colour8 const colour) { - return colour.g * (255 / 7); + return ((colour >> 2) & 7) * (255 / 7); } // gets the blue channel in 32 bit colour space static inline uint8_t colour8_blue32(colour8 const colour) { - return colour.b * (255 / 3); + return (colour & 3) * (255 / 3); } + +// sets the render colour to a colour8 value +static inline void set_colour8(SDL_Renderer* const renderer, colour8 const c) { + (void)SDL_SetRenderDrawColor(renderer, colour8_red32(c), colour8_green32(c), colour8_blue32(c), 0xFF); +} + +// american macros: +#define color8 colour8 +#define color8_red32 colour8_red32 +#define color8_green32 colour8_green32 +#define color8_blue32 colour8_blue32 +#define COLOR_BLACK COLOUR_BLACK +#define COLOR_RED COLOUR_RED +#define COLOR_YELLOW COLOUR_YELLOW +#define COLOR_ORANGE COLOUR_ORANGE +#define COLOR_GREEN COLOUR_GREEN +#define COLOR_CYAN COLOUR_CYAN +#define COLOR_BLUE COLOUR_BLUE +#define COLOR_MAGENTA COLOUR_MAGENTA +#define COLOR_WHITE COLOUR_WHITE diff --git a/src/window/renderer.c b/src/window/renderer.c index 4d1720f..3f20fb1 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -97,7 +97,7 @@ static void render_level(SDL_Renderer* const renderer, game_data const* const da row_const const row = data->rows[y]; for (int8_t x = 0; x < COLUMNS; x++) { - if (row[x].packed != 0) { + if (row[x] != 0) { set_colour(renderer, row[x]); draw_block(renderer, x, y); }