mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add colour
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
typedef Uint16 Shape;
|
||||
typedef uint16_t Shape;
|
||||
typedef uint16_t Row;
|
||||
|
||||
// 0 1 2 3
|
||||
#define TETROMINO_I ((Shape)0b1000100010001000) // 1000 1000 1000 1000 the I tetromino with no rotation
|
||||
@@ -26,8 +27,9 @@ typedef Uint16 Shape;
|
||||
|
||||
// stores the data used in the game
|
||||
#define ROWS 32
|
||||
#define COLUMNS (sizeof(Row) * 8 / 3)
|
||||
typedef struct {
|
||||
Uint16 row[ROWS];
|
||||
Row row[ROWS];
|
||||
} GameData;
|
||||
|
||||
// updates the game's state
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "errors.h"
|
||||
#include "game/game.h"
|
||||
#include "window/audio.h"
|
||||
// #include "window/audio.h"
|
||||
#include "window/renderer.h"
|
||||
|
||||
#ifdef __EMSCRIPTEN__ // for web builds
|
||||
@@ -37,7 +37,7 @@ static void init(void) {
|
||||
}
|
||||
|
||||
// initialize audio
|
||||
AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||
// AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||
// AudioData audio1 = audio_load_wav(audio_device, "FILE MANE");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../errors.h"
|
||||
#include "../main.h"
|
||||
#include "SDL_render.h"
|
||||
|
||||
|
||||
int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
||||
@@ -26,6 +26,16 @@ int renderer_init(SDL_Window** window, SDL_Renderer** renderer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int set_block(SDL_Renderer* renderer, const Row row, const uint8_t block, const uint8_t filter, const uint8_t x, const uint8_t y) {
|
||||
uint8_t colour = block & filter;
|
||||
return SDL_SetRenderDrawColor(renderer,
|
||||
0xFF * (0 != (colour & (RED | RED << 3))),
|
||||
0xFF * (0 != (colour & (GREEN | GREEN << 3))),
|
||||
0xFF * (0 != (colour & (BLUE | BLUE << 3))),
|
||||
0xFF) |
|
||||
SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
|
||||
}
|
||||
|
||||
void renderer_update(const RenderData* render_data) {
|
||||
SDL_Renderer* renderer = render_data->renderer;
|
||||
GameData* data = render_data->game_data;
|
||||
@@ -36,14 +46,17 @@ void renderer_update(const RenderData* render_data) {
|
||||
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x50);
|
||||
success |= SDL_RenderClear(renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
for (int y = 0; y < ROWS; y++) {
|
||||
Uint16 row = data->row[y];
|
||||
for (uint8_t y = 0; y < ROWS; y++) {
|
||||
Row row = data->row[y];
|
||||
|
||||
for (int x = 16; x >= 0; x--) {
|
||||
if ((row & (1 << x)) != 0) {
|
||||
success |= SDL_RenderFillRect(renderer, &(SDL_Rect){x * BLOCK_WIDTH + 1, y * BLOCK_HEIGHT + 1, BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1});
|
||||
}
|
||||
for (uint8_t x = 0; x < COLUMNS; x += 2) {
|
||||
uint8_t block = row >> (x * 3); // get the two blocks stored at this column
|
||||
|
||||
if ((block & 0x0F) != 0)
|
||||
success |= set_block(renderer, row, block, 0x0F, x + 1, y);
|
||||
|
||||
if ((block & 0xF0) != 0)
|
||||
success |= set_block(renderer, row, block, 0xF0, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
#pragma once
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "../game/game.h"
|
||||
|
||||
#define SCREEN_WIDTH 256
|
||||
#define SCREEN_HEIGHT (SCREEN_WIDTH * 2)
|
||||
#define BLOCK_WIDTH (SCREEN_WIDTH / 16)
|
||||
#define BLOCK_WIDTH (SCREEN_WIDTH / COLUMNS)
|
||||
#define BLOCK_HEIGHT (SCREEN_HEIGHT / (BLOCK_WIDTH * 2))
|
||||
|
||||
// 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 {
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
|
||||
Reference in New Issue
Block a user