summary refs log tree commit diff
path: root/src/main.c
blob: 0b65f7d58c798a2bd9eb12f36d0c236f5b6f539e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "raylib.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

#define MATRIX_WIDTH  10
#define MATRIX_HEIGHT 40

#define BLOCK_SIZE 25

#define FALL_TICK 0.5

enum Phases {
    GENERATION,
    FALL,
    LOCK,
    PATTERN,
    MARK,
    ITERATE,
    ANIMATE,
    ELIMINATE,
    COMPLETE
} game_phase;

#include "tetromino.h"

struct {
    int pos_x;
    int pos_y;
    int rotation;
    int type;
} current_piece;

struct Color matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
int piece_queue[2][TETROMINO_COUNT] = {
    {0, 1, 2, 3, 4, 5, 6},
    {0, 1, 2, 3, 4, 5, 6}
};
int current_bag = 0;
int current_piece_index = 0;

void draw_block(int x, int y, int width, int height, struct Color color)
{
    DrawRectangle(x, -y + SCREEN_HEIGHT - height, width, height, color);
}

void render_matrix(void)
{
    int matrix_origin_x = (SCREEN_WIDTH/2 - (MATRIX_WIDTH * BLOCK_SIZE))/2;
    int matrix_origin_y = (SCREEN_HEIGHT - MATRIX_HEIGHT/2 * BLOCK_SIZE)/2;

    /* draw matrix background */
    draw_block(
        matrix_origin_x - 1
        ,matrix_origin_y - 1
        ,MATRIX_WIDTH * BLOCK_SIZE + 2
        ,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
        ,GRAY
    );

    /* draw matrix */
    for (int i = 0; i < MATRIX_WIDTH; i++) {
        for (int j = 0; j < MATRIX_HEIGHT; j++) {
            draw_block(
                matrix_origin_x + i * BLOCK_SIZE + 1
                ,matrix_origin_y + j * BLOCK_SIZE + 1
                ,BLOCK_SIZE - 2
                ,BLOCK_SIZE - 2
                ,matrix[i][j]
            );
        }
    }

    /* draw current piece */
    for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
        for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
            if (tetrominos[current_piece.type].directions[current_piece.rotation][j][i] == 1) {
                draw_block(
                    (matrix_origin_x + ((current_piece.pos_x + i - 1) * BLOCK_SIZE)) + 1
                    ,(matrix_origin_y + ((current_piece.pos_y - j + 1) * BLOCK_SIZE)) + 1
                    ,BLOCK_SIZE - 2
                    ,BLOCK_SIZE - 2
                    ,tetrominos[current_piece.type].color
                );
            }
        }
    }
}

void shuffle_bag(int *bag)
{
    for (int i = 0; i < TETROMINO_COUNT; i++) {
        int r = i + rand() / (RAND_MAX / (TETROMINO_COUNT - i) + 1);
        int t = bag[r];
        bag[r] = bag[i];
        bag[i] = t;
    }
}

void generation_phase(void)
{
    // bag generation
    if (current_piece_index > TETROMINO_COUNT) {
        current_piece_index = 0;
        shuffle_bag(piece_queue[current_bag]);
        current_bag = !current_bag;
    }

    // set current piece
    current_piece.type = piece_queue[current_bag][current_piece_index++];
    current_piece.rotation = NORTH;
    current_piece.pos_x = 4;
    current_piece.pos_y = 20;
}

int main(void)
{
    //srand(time(NULL));
    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "~turnipGod's Tetris");

    // set all blocks to white
    for (int y = 0; y < 40; y++) {
        for (int x = 0; x < 10; x++) {
            matrix[x][y] = WHITE;
        }
    }

    shuffle_bag(piece_queue[0]);
    generation_phase();
    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(WHITE);
        render_matrix();
        EndDrawing();
    }

    CloseWindow();

    return 0;
}