174 lines
6.1 KiB
C
174 lines
6.1 KiB
C
#pragma once
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <math.h>
|
|
#include "raylib.h"
|
|
|
|
|
|
typedef enum {
|
|
PC_EMPTY = 0,
|
|
PC_CYAN = 1,
|
|
PC_BLUE = 3,
|
|
PC_ORANGE = 4,
|
|
PC_YELLOW = 5,
|
|
PC_GREEN = 6,
|
|
PC_PURPLE = 7,
|
|
PC_RED = 8
|
|
} ColorCode;
|
|
|
|
|
|
typedef struct {
|
|
int** shape;
|
|
bool has_origin;
|
|
Vector2 dims;
|
|
Vector2 origin;
|
|
char color;
|
|
} Piece;
|
|
|
|
|
|
// Helper function to create shape from static array
|
|
int** create_shape_from_array(int rows, int cols, int* data);
|
|
void free_shape(int** shape, int rows);
|
|
|
|
#define T_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 3, (int[]){ \
|
|
0,1,0, \
|
|
1,1,1}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {3, 2}, \
|
|
.origin = (Vector2) {1, 1}, \
|
|
.color = PC_PURPLE \
|
|
}
|
|
|
|
#define O_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 2, (int[]){ \
|
|
1,1, \
|
|
1,1}), \
|
|
.has_origin = false, \
|
|
.dims = (Vector2) {2, 2}, \
|
|
.origin = (Vector2) {0, 0}, \
|
|
.color = PC_YELLOW \
|
|
}
|
|
|
|
#define Z_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 3, (int[]){ \
|
|
1,1,0, \
|
|
0,1,1}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {3, 2}, \
|
|
.origin = (Vector2) {1, 1}, \
|
|
.color = PC_RED \
|
|
}
|
|
|
|
#define S_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 3, (int[]){ \
|
|
0,1,1, \
|
|
1,1,0}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {3, 2}, \
|
|
.origin = (Vector2) {1, 1}, \
|
|
.color = PC_GREEN \
|
|
}
|
|
|
|
#define I_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(4, 1, (int[]){ \
|
|
1, \
|
|
1, \
|
|
1, \
|
|
1}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {1, 4}, \
|
|
.origin = (Vector2) {0, 2}, \
|
|
.color = PC_CYAN \
|
|
}
|
|
|
|
#define J_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 3, (int[]){ \
|
|
1,0,0, \
|
|
1,1,1}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {3, 2}, \
|
|
.origin = (Vector2) {1, 1}, \
|
|
.color = PC_BLUE \
|
|
}
|
|
|
|
#define L_PIECE (Piece) { \
|
|
.shape = create_shape_from_array(2, 3, (int[]){ \
|
|
0,0,1, \
|
|
1,1,1}), \
|
|
.has_origin = true, \
|
|
.dims = (Vector2) {3, 2}, \
|
|
.origin = (Vector2) {1, 1}, \
|
|
.color = PC_ORANGE \
|
|
}
|
|
|
|
#define COLOR_GRID_BG (Color) { 0x00, 0x00, 0x00, 0xff}
|
|
#define COLOR_GRID_FG (Color) { 0xD0, 0xD0, 0xD0, 0xff}
|
|
|
|
|
|
|
|
#define BOARD_WIDTH 10
|
|
#define BOARD_HEIGHT 20
|
|
|
|
#define PIECE_BUFFER_OFFSET 4
|
|
|
|
#define BORDERBUFFER_WIDTH 1
|
|
#define BORDERBUFFER_HEIGHT 1
|
|
|
|
#define TOTAL_WIDTH (BOARD_WIDTH + 2 * BORDERBUFFER_WIDTH)
|
|
#define TOTAL_HEIGHT (BOARD_HEIGHT + 2 * BORDERBUFFER_HEIGHT + PIECE_BUFFER_OFFSET)
|
|
#define GRID_BITS (TOTAL_WIDTH * TOTAL_HEIGHT)
|
|
|
|
#define GRID_WORDS ((GRID_BITS + 31) / 32)
|
|
|
|
typedef struct {
|
|
uint32_t bits[GRID_WORDS];
|
|
} bitgrid_t;
|
|
|
|
|
|
#define PIXEL_SCALE 8
|
|
#define BLOCK_SIZE (4 * PIXEL_SCALE)
|
|
#define GRID_SCREEN_PIXELS (BLOCK_SIZE + 2 * PIXEL_SCALE)
|
|
|
|
#define NDEBUG
|
|
|
|
|
|
typedef struct {
|
|
int x;
|
|
int y;
|
|
} IntVector2;
|
|
|
|
typedef struct{
|
|
|
|
} BoardBits;
|
|
|
|
|
|
// Function prototypes
|
|
Color resolve_color(unsigned char colorValue);
|
|
void zero_board();
|
|
void zero_board_mask();
|
|
void zero_piece_mask();
|
|
void populate_board_mask();
|
|
bool next_tick_valid();
|
|
bool piece_tick();
|
|
void place_piece();
|
|
void spawn_piece();
|
|
void turn_piece(bool clockwise);
|
|
void game_tick();
|
|
void debugDraw(int x, int y, Color color);
|
|
void debug_draw_blocks(Vector2 block_vecs[], Color color);
|
|
void debug_draw_blocks_on_axis(Vector2 block_vecs[], Color color);
|
|
void draw();
|
|
void update();
|
|
void init_game();
|
|
void init_shapes();
|
|
void cleanup_shapes();
|
|
void get_vecs_from_shape(Piece* cpiece, Vector2* blocks);
|
|
void sort_blocks(Vector2* blocks);
|
|
Vector2 find_topleftmost_block_from_buffer();
|
|
Vector2 get_origin_offset();
|
|
Vector2* get_shape_bounds();
|
|
Vector2* get_shape_dimensions(Piece* piece); |