use correct 8 bit colourspace instead of including alpha

This commit is contained in:
2025-02-03 15:02:00 +01:00
parent 49b3add8c5
commit c546f78c4c
8 changed files with 55 additions and 78 deletions

View File

@@ -1,61 +0,0 @@
#pragma once
#include <stdint.h>
// 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;
}

38
src/window/colour8.h Normal file
View File

@@ -0,0 +1,38 @@
#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;
/* 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);
}

View File

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