summary refs log tree commit diff
path: root/src/main.c
blob: 66a222cbfdd4963583c61701b4539075373c68df (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
144
145
146
147
148
149
150
151
152
153
154
155
156
#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 render_matrix(void);
void make_bag(int *bag);

void render_matrix(void)
{
    /* top left matrix corner */
    int matrix_start_x =
        (SCREEN_WIDTH/2 - (MATRIX_WIDTH * BLOCK_SIZE)) / 2;
    int matrix_start_y =
        (SCREEN_HEIGHT - (3 * MATRIX_HEIGHT / 2 * BLOCK_SIZE)) / 2;

    /* top left queue corner */
    int queue_start_x =
        (SCREEN_WIDTH/2 + BLOCK_SIZE);
    int queue_start_y =
        (SCREEN_HEIGHT - (3 * MATRIX_HEIGHT / 2 * BLOCK_SIZE)) / 2;


    /* draw matrix background */
    DrawRectangle(
        matrix_start_x - 1
        ,matrix_start_y + (MATRIX_HEIGHT/2 * BLOCK_SIZE) - 1
        ,MATRIX_WIDTH * BLOCK_SIZE + 2
        ,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
        ,GRAY
    );

    /* draw blocks from matrix */
    for (int y = 0; y < 40; y++) {
        for (int x = 0; x < 10; x++) {
            DrawRectangle(
                (x * BLOCK_SIZE) + matrix_start_x + 1
                ,(y * BLOCK_SIZE) + matrix_start_y + 1
                ,BLOCK_SIZE - 2
                ,BLOCK_SIZE - 2
                ,matrix[x][y]
            );
        }
    }

    /* 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][i][j]) {
                DrawRectangle(
                    matrix_start_x + ((current_piece.pos_x + j-2) * BLOCK_SIZE) + 1
                    ,matrix_start_y + ((current_piece.pos_y + i-2) * BLOCK_SIZE) + 1
                    ,BLOCK_SIZE - 2
                    ,BLOCK_SIZE - 2
                    ,tetrominos[current_piece.type].color
                );
            }
        }
    }

    // draw queue
    // STUB
    (void)queue_start_x;
    (void)queue_start_y;
}

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 = 5;
    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;
}