summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornot_a_robot <hashimm06@outlook.com>2023-11-19 16:18:23 +0000
committernot_a_robot <hashimm06@outlook.com>2023-11-19 16:18:23 +0000
commitbee57c12a4582d56f2eb18ea632e416cbffe5528 (patch)
tree2e9a4ba2de035bf334f733996a482a3b6cb4d728
downloadretris-bee57c12a4582d56f2eb18ea632e416cbffe5528.tar.gz
retris-bee57c12a4582d56f2eb18ea632e416cbffe5528.tar.bz2
retris-bee57c12a4582d56f2eb18ea632e416cbffe5528.zip
initial commit
-rw-r--r--.gitignore2
-rw-r--r--.gitmodules3
-rw-r--r--Makefile45
m---------src/raylib0
-rw-r--r--src/retris.c88
5 files changed, 138 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cd42ee3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+bin/
+obj/
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..40665e6
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "src/raylib"]
+ path = src/raylib
+ url = https://github.com/raysan5/raylib.git
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b2bb6df
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,45 @@
+ifneq (, $(shell which clang))
+ CC:=clang
+else ifneq (, $(shell which gcc))
+ CC:=gcc
+else ifneq (, $(shell which cc))
+ CC:=cc
+else
+ $(error "No C compiler found (!)")
+endif
+
+CFLAGS:=-Wall -Wextra -Wpedantic -std=c99 -g3
+LDFLAGS:=-I/usr/local/include/ -I/usr/X11R6/include/ -I./src/raylib/src/
+LDLIBS:=-Lobj/ -lraylib -lGL -lm -lpthread -lX11 -ldl -lrt
+DIRS:=obj bin
+BINS:=bin/retris
+
+.PHONY: all dirs clean
+
+all: dirs $(BINS)
+
+run: dirs $(BINS)
+ bin/retris
+
+bin/retris: obj/retris.o obj/libraylib.a
+ $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $^ -o $@
+
+obj/%.o: src/%.c src/%.h
+ $(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@
+obj/%.o: src/%.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@
+obj/libraylib.a: src/raylib/src/*
+ mkdir -p src/raylib/build
+ cmake -DBUILD_EXAMPLES=OFF -S src/raylib -B src/raylib/build
+ $(MAKE) -C src/raylib/build -j8
+ cp src/raylib/build/raylib/libraylib.a obj/
+
+dirs: $(DIRS)
+$(DIRS):
+ @mkdir -p $(DIRS)
+
+clean:
+ rm -rf bin/* obj/* src/raylib/build
+# fast clean - raylib rarely, if ever, needs to be cleaned
+fclean:
+ rm -f bin/* obj/*.o
diff --git a/src/raylib b/src/raylib
new file mode 160000
+Subproject ae50bfa2cc569c0f8d5bc4315d39db64005b1b0
diff --git a/src/retris.c b/src/retris.c
new file mode 100644
index 0000000..7335b9e
--- /dev/null
+++ b/src/retris.c
@@ -0,0 +1,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);
+}