mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
add colour definitions
This commit is contained in:
24
src/window/colour/colour32.h
Normal file
24
src/window/colour/colour32.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
// fits colours with each channel having a width of 2 bits
|
||||
typedef union {
|
||||
uint32_t packed;
|
||||
struct {
|
||||
uint8_t a;
|
||||
uint8_t b;
|
||||
uint8_t g;
|
||||
uint8_t r;
|
||||
};
|
||||
} colour32;
|
||||
#define color32 colour32
|
||||
|
||||
#define COLOUR32_BLACK ((colour32){0x000000FF})
|
||||
#define COLOUR32_RED ((colour32){0xFF0000FF})
|
||||
#define COLOUR32_YELLOW ((colour32){0xFFFF00FF})
|
||||
#define COLOUR32_ORANGE ((colour32){0xFF6D00FF})
|
||||
#define COLOUR32_GREEN ((colour32){0x00FF00FF})
|
||||
#define COLOUR32_CYAN ((colour32){0x00FFFFFF})
|
||||
#define COLOUR32_BLUE ((colour32){0x0000FFFF})
|
||||
#define COLOUR32_MAGENTA ((colour32){0xFF00FFFF})
|
||||
#define COLOUR32_WHITE ((colour32){0xFFFFFFFF})
|
||||
39
src/window/colour/colour8.h
Normal file
39
src/window/colour/colour8.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
// gets the green channel in 32 bit colour space
|
||||
static inline uint8_t colour8_green32(colour8 const colour) {
|
||||
return colour.g * (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);
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "../errors.h"
|
||||
#include "../game/game.h"
|
||||
#include "colour/colour32.h"
|
||||
#include "colour/colour8.h"
|
||||
|
||||
|
||||
// initializes the window and renderer
|
||||
@@ -28,13 +30,23 @@ void renderer_init(render_data* const render_dat, game_data const* const game_da
|
||||
};
|
||||
}
|
||||
|
||||
// sets the colour32 from the colour8
|
||||
static inline void set_colour32(SDL_Renderer* const renderer, colour32 const c) {
|
||||
(void)SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
|
||||
}
|
||||
|
||||
// sets the colour32 from the colour8
|
||||
//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);
|
||||
//}
|
||||
|
||||
void renderer_update(const render_data* render_data) {
|
||||
SDL_Renderer* renderer = render_data->renderer;
|
||||
|
||||
int success = 0; // if an error occurs, this value is <0
|
||||
|
||||
// clear render
|
||||
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x50);
|
||||
set_colour32(renderer, COLOUR32_BLACK);
|
||||
success |= SDL_RenderClear(renderer);
|
||||
|
||||
if (success < 0) {
|
||||
|
||||
Reference in New Issue
Block a user