mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
convert colour to a 2 bit format
This commit is contained in:
@@ -3,11 +3,10 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "../main.h"
|
#include "../main.h"
|
||||||
#include "../window/renderer.h"
|
|
||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
|
|
||||||
|
|
||||||
static void _add_shape(Row* row, const Shape shape, const uint8_t colour, const uint8_t pos_x) {
|
static void _add_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x) {
|
||||||
for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
for (uint8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||||
ShapeRow shape_row = shape_get_row(shape, y);
|
ShapeRow shape_row = shape_get_row(shape, y);
|
||||||
|
|
||||||
@@ -16,11 +15,11 @@ static void _add_shape(Row* row, const Shape shape, const uint8_t colour, const
|
|||||||
|
|
||||||
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
for (uint8_t x = 0; x < SHAPE_WIDTH; x++)
|
||||||
if (is_set(shape_row, x))
|
if (is_set(shape_row, x))
|
||||||
row[y].columns[x + pos_x].block = colour;
|
row[y].columns[x + pos_x] = colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void add_shape(Row* row, const Shape shape, const uint8_t colour, const uint8_t pos_x, const uint8_t pos_y) {
|
static inline void add_shape(Row* row, const Shape shape, const Colour colour, const uint8_t pos_x, const uint8_t pos_y) {
|
||||||
_add_shape(&row[pos_y], shape, colour, pos_x);
|
_add_shape(&row[pos_y], shape, colour, pos_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,14 +29,14 @@ void game_update(GameData* game_data, const Uint8* keys) {
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
add_shape(game_data->row, TETROMINO_I, CYAN, 1, 0);
|
add_shape(game_data->row, TETROMINO_I, COLOUR_CYAN, 1, 0);
|
||||||
add_shape(game_data->row, TETROMINO_O, YELLOW, 5, 4);
|
add_shape(game_data->row, TETROMINO_O, COLOUR_YELLOW, 5, 4);
|
||||||
|
|
||||||
add_shape(game_data->row, TETROMINO_Z, GREEN, 1, 8);
|
add_shape(game_data->row, TETROMINO_Z, COLOUR_GREEN, 1, 8);
|
||||||
add_shape(game_data->row, TETROMINO_S, RED, 5, 8);
|
add_shape(game_data->row, TETROMINO_S, COLOUR_RED, 5, 8);
|
||||||
|
|
||||||
add_shape(game_data->row, TETROMINO_J, BLUE, 1, 12);
|
add_shape(game_data->row, TETROMINO_J, COLOUR_BLUE, 1, 12);
|
||||||
add_shape(game_data->row, TETROMINO_L, YELLOW | RED, 5, 12);
|
add_shape(game_data->row, TETROMINO_L, COLOUR_ORANGE, 5, 12);
|
||||||
|
|
||||||
add_shape(game_data->row, TETROMINO_T, MAGENTA, 5, 16);
|
add_shape(game_data->row, TETROMINO_T, COLOUR_MAGENTA, 5, 16);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include "../window/colour.h"
|
||||||
|
|
||||||
typedef uint32_t PackedRow;
|
typedef uint32_t PackedRow;
|
||||||
|
|
||||||
// stores the data used in the game
|
// stores the data used in the game
|
||||||
#define COLUMNS ((uint8_t)(sizeof(PackedRow) * 8 / 3))
|
#define COLUMNS ((uint8_t)(sizeof(PackedRow) * 8 / 3))
|
||||||
#define ROWS ((uint8_t)(COLUMNS * 2))
|
#define ROWS ((uint8_t)(COLUMNS * 2))
|
||||||
|
|
||||||
typedef union {
|
typedef struct {
|
||||||
// NOTE: this has packing issues
|
Colour columns[COLUMNS];
|
||||||
struct {
|
|
||||||
uint8_t block : 3;
|
|
||||||
} columns[COLUMNS];
|
|
||||||
PackedRow val;
|
|
||||||
} Row;
|
} Row;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
22
src/window/colour.h
Normal file
22
src/window/colour.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// fits colours with each channel having a width of 2 bits
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint8_t packed;
|
||||||
|
struct {
|
||||||
|
uint8_t a : 2;
|
||||||
|
uint8_t b : 2;
|
||||||
|
uint8_t g : 2;
|
||||||
|
uint8_t r : 2;
|
||||||
|
};
|
||||||
|
} Colour;
|
||||||
|
|
||||||
|
#define COLOUR_RED ((Colour){0b11000011})
|
||||||
|
#define COLOUR_YELLOW ((Colour){0b11110011})
|
||||||
|
#define COLOUR_ORANGE ((Colour){0b11100011})
|
||||||
|
#define COLOUR_GREEN ((Colour){0b00110011})
|
||||||
|
#define COLOUR_CYAN ((Colour){0b00111111})
|
||||||
|
#define COLOUR_BLUE ((Colour){0b00001111})
|
||||||
|
#define COLOUR_MAGENTA ((Colour){0b11001111})
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "../errors.h"
|
#include "../errors.h"
|
||||||
|
#include "colour.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -40,12 +41,12 @@ void renderer_update(const RenderData* render_data) {
|
|||||||
Row row = data->row[y];
|
Row row = data->row[y];
|
||||||
|
|
||||||
for (uint8_t x = 0; x < COLUMNS; x++) {
|
for (uint8_t x = 0; x < COLUMNS; x++) {
|
||||||
if (row.columns[x].block != 0) {
|
if (row.columns[x].packed != 0) {
|
||||||
success |= SDL_SetRenderDrawColor(renderer,
|
success |= SDL_SetRenderDrawColor(renderer,
|
||||||
0xFF * !!(row.columns[x].block & RED),
|
0x55 * row.columns[x].r, // repeat the 2 bits by multiplying by 0x55
|
||||||
0xFF * !!(row.columns[x].block & GREEN),
|
0x55 * row.columns[x].g,
|
||||||
0xFF * !!(row.columns[x].block & BLUE),
|
0x55 * row.columns[x].b,
|
||||||
0xFF);
|
0x55 * row.columns[x].a);
|
||||||
success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
|
success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "../game/game.h"
|
#include "../game/game.h"
|
||||||
|
|
||||||
@@ -8,19 +9,6 @@
|
|||||||
#define BLOCK_WIDTH (SCREEN_WIDTH / COLUMNS)
|
#define BLOCK_WIDTH (SCREEN_WIDTH / COLUMNS)
|
||||||
#define BLOCK_HEIGHT (SCREEN_HEIGHT / ROWS)
|
#define BLOCK_HEIGHT (SCREEN_HEIGHT / ROWS)
|
||||||
|
|
||||||
// fits colours in a 3 bit length
|
|
||||||
// 4th bit is unused
|
|
||||||
typedef enum {
|
|
||||||
BLACK = 0,
|
|
||||||
RED = 1,
|
|
||||||
GREEN = 2,
|
|
||||||
BLUE = 4,
|
|
||||||
YELLOW = RED | GREEN,
|
|
||||||
CYAN = GREEN | BLUE,
|
|
||||||
MAGENTA = BLUE | RED,
|
|
||||||
WHITE = RED | GREEN | BLUE,
|
|
||||||
} Colour;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
|
|||||||
Reference in New Issue
Block a user