summaryrefslogtreecommitdiff
path: root/src/retris.c
blob: 7335b9e25f92181e94dfc6b16d8bc3eb14288a67 (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
#include <stdlib.h>
#include <time.h>
#include "raylib.h"

/*
 * each cube is 1 unit, so FACT is used as a scale factor for the size of the
 * screen
 */
#define WIDTH 10
#define HEIGHT 22
#define VIS_HEIGHT 20
#define FACT 40
/* since the game only moves on a block per block basis, no smoothness is
 * necessary, so the only thing FPS controls is how fast the blocks move left
 * and right, due to how often it polls */
#define FPS 30

typedef struct Vector2Int {
  int x, y;
} Vector2Int;

typedef struct Tetromino {
  Color colour;
  Vector2Int pos[4]; // position of 4 cubes making the tetromino
} Tetromino;

void Draw2DGrid(Color grid[WIDTH][HEIGHT]);

int
main(void){
  int const width = WIDTH * FACT, height = HEIGHT * FACT,
    vis_height = VIS_HEIGHT * FACT;
  Color grid[WIDTH][HEIGHT]; // if colour is WHITE, the position is empty
  Vector2Int cur_cube = {0,0};
  int interval = FPS, count, i, j;

  srand(time(NULL));

  for (i = 0; i < WIDTH; i++)
    for (j = 0; j < HEIGHT; j++)
      grid[i][j] = WHITE;
  grid[0][0] = RED;

  InitWindow(width, height, "retris");
  SetTargetFPS(FPS);
  for (count = 0; !WindowShouldClose(); count++) {
    if (count == (interval - (IsKeyDown(KEY_DOWN) ? interval/2 : 0))) {
      count = 0;
      grid[cur_cube.x][cur_cube.y] = WHITE;
      cur_cube.y++;
      grid[cur_cube.x][cur_cube.y] = RED;
    }

    if (IsKeyDown(KEY_RIGHT)) {
      grid[cur_cube.x][cur_cube.y] = WHITE;
      cur_cube.x++;
      grid[cur_cube.x][cur_cube.y] = RED;
    }
    else if (IsKeyDown(KEY_LEFT)) {
      grid[cur_cube.x][cur_cube.y] = WHITE;
      cur_cube.x--;
      grid[cur_cube.x][cur_cube.y] = RED;
    }

    BeginDrawing();
      Draw2DGrid(grid);
    EndDrawing();
  }

  CloseWindow();
  return 0;
}

void
Draw2DGrid(Color grid[WIDTH][HEIGHT]){
  int i, j;

  for (i = 0; i < WIDTH; i++) {
    for (j = 0; j < HEIGHT; j++) {
      DrawRectangle(i*FACT, j*FACT, FACT, FACT, grid[i][j]);
    }
  }

  for (i = 0; i <= WIDTH; i++)
    DrawLine(i*FACT, 0, i*FACT, HEIGHT*FACT, LIGHTGRAY);
  for (i = 0; i <= HEIGHT; i++)
    DrawLine(0, i*FACT, WIDTH*FACT, i*FACT, LIGHTGRAY);
}