mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
don't use a union for colour8 bitfield as it's unpredictable
This commit is contained in:
@@ -4,39 +4,32 @@
|
||||
#include "SDL_render.h"
|
||||
|
||||
// stores colour in a rrrgggbb format, which maps exactly to 8 bits
|
||||
typedef union {
|
||||
uint8_t packed;
|
||||
struct {
|
||||
uint8_t b : 2;
|
||||
uint8_t g : 3;
|
||||
uint8_t r : 3;
|
||||
};
|
||||
} colour8;
|
||||
typedef uint8_t colour8;
|
||||
|
||||
/* rrrg ggbb */
|
||||
#define COLOUR8_BLACK ((colour8){0x00}) // 0000 0000
|
||||
#define COLOUR8_RED ((colour8){0xE0}) // 1110 0000
|
||||
#define COLOUR8_YELLOW ((colour8){0xFC}) // 1111 1100
|
||||
#define COLOUR8_ORANGE ((colour8){0xEC}) // 1111 1100
|
||||
#define COLOUR8_GREEN ((colour8){0x1C}) // 0001 1100
|
||||
#define COLOUR8_CYAN ((colour8){0x1F}) // 0001 1111
|
||||
#define COLOUR8_BLUE ((colour8){0x03}) // 0000 0011
|
||||
#define COLOUR8_MAGENTA ((colour8){0xE3}) // 1110 0011
|
||||
#define COLOUR8_WHITE ((colour8){0xFF}) // 1111 1111
|
||||
/* rrrg ggbb */
|
||||
#define COLOUR8_BLACK ((colour8)0x00) // 0000 0000
|
||||
#define COLOUR8_RED ((colour8)0xE0) // 1110 0000
|
||||
#define COLOUR8_YELLOW ((colour8)0xFC) // 1111 1100
|
||||
#define COLOUR8_ORANGE ((colour8)0xEC) // 1111 1100
|
||||
#define COLOUR8_GREEN ((colour8)0x1C) // 0001 1100
|
||||
#define COLOUR8_CYAN ((colour8)0x1F) // 0001 1111
|
||||
#define COLOUR8_BLUE ((colour8)0x03) // 0000 0011
|
||||
#define COLOUR8_MAGENTA ((colour8)0xE3) // 1110 0011
|
||||
#define COLOUR8_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
|
||||
|
||||
Reference in New Issue
Block a user