mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
use correct 8 bit colourspace instead of including alpha
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "../main.h"
|
||||
#include "../window/colour.h"
|
||||
#include "../window/colour8.h"
|
||||
#include "./tetromino/shapes.h"
|
||||
#include "tetromino/placing.h"
|
||||
|
||||
@@ -39,7 +39,7 @@ void game_init(GameData* const game_data) {
|
||||
|
||||
// allocate size for each row
|
||||
for (int8_t i = 0; i < ROWS; i++) {
|
||||
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour));
|
||||
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour8));
|
||||
// game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../window/colour.h"
|
||||
#include "../window/colour8.h"
|
||||
#include "tetromino/shapes.h"
|
||||
|
||||
// stores the data used in the game
|
||||
#define COLUMNS ((int8_t)10)
|
||||
#define ROWS ((int8_t)(COLUMNS * 2))
|
||||
|
||||
typedef const Colour* CRow;
|
||||
typedef Colour* Row;
|
||||
typedef const Colour8* CRow;
|
||||
typedef Colour8* Row;
|
||||
|
||||
typedef struct {
|
||||
Row rows[ROWS];
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../window/colour.h"
|
||||
#include "../../window/colour8.h"
|
||||
#include "../game.h"
|
||||
#include "shapes.h"
|
||||
|
||||
|
||||
static bool is_filled(CRow row) {
|
||||
for (int8_t x = 0; x < COLUMNS; x++) {
|
||||
if (row[x].packed == NONE) {
|
||||
if (row[x].packed == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ static void clear_rows(Row* rows) {
|
||||
// sets a shape to the screen
|
||||
static void set_shape_i(Row* row, const ShapeId id, const int8_t pos_x) {
|
||||
const Shape shape = shape_from_id(id);
|
||||
const Colour colour = colour_from_id(id);
|
||||
const Colour8 colour = colour_from_id(id);
|
||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||
ShapeRow shape_row = shape_get_row(shape, y);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "shapes.h"
|
||||
|
||||
#include "../../window/colour.h"
|
||||
#include "../../window/colour8.h"
|
||||
|
||||
/* 0 1 2 3 */
|
||||
#define SHAPE_O ((Shape)0x0660) // 0000 0110 0110 0000 the O tetromino with no rotation
|
||||
@@ -53,7 +53,7 @@ Shape shape_from_id(ShapeId id) {
|
||||
return shapes[id & 7][id >> 3];
|
||||
}
|
||||
|
||||
Colour colour_from_id(ShapeId id) {
|
||||
Colour8 colour_from_id(ShapeId id) {
|
||||
switch (id & 7) {
|
||||
case TETROMINO_O: return COLOUR_YELLOW;
|
||||
case TETROMINO_I: return COLOUR_CYAN;
|
||||
@@ -62,6 +62,6 @@ Colour colour_from_id(ShapeId id) {
|
||||
case TETROMINO_T: return COLOUR_MAGENTA;
|
||||
case TETROMINO_L: return COLOUR_ORANGE;
|
||||
case TETROMINO_J: return COLOUR_BLUE;
|
||||
default: return COLOUR_NONE;
|
||||
default: return COLOUR_BLACK;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../../window/colour.h"
|
||||
#include "../../window/colour8.h"
|
||||
|
||||
typedef uint16_t Shape;
|
||||
typedef uint8_t ShapeRow;
|
||||
@@ -36,4 +36,4 @@ static inline bool is_set(ShapeRow row, uint8_t index) {
|
||||
}
|
||||
|
||||
Shape shape_from_id(ShapeId id);
|
||||
Colour colour_from_id(ShapeId id);
|
||||
Colour8 colour_from_id(ShapeId id);
|
||||
|
||||
@@ -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
38
src/window/colour8.h
Normal 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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user