fix: remove bitfield from union

This commit is contained in:
2025-02-12 19:24:21 +01:00
parent 0596f8d5d5
commit 2c5be9685c
3 changed files with 44 additions and 30 deletions

View File

@@ -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);
}
@@ -91,7 +91,7 @@ static bool shape_intersects(row const* const rows, shape_id const id, int8_t co
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 (rows[y1][x1] != 0) return true; // if there is a block here
}
}
return false;

View File

@@ -1,39 +1,53 @@
#pragma once
#include <stdint.h>
// 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"
// 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
#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

View File

@@ -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);
}