summaryrefslogtreecommitdiff
path: root/userland/snake
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-22 19:50:38 +0200
committerAnton Kling <anton@kling.gg>2023-10-22 19:50:38 +0200
commit4e09bca9e34c226b6d7e34b4fa11248405fd988e (patch)
tree80f156b7940d9d19971395f335530170c69516c7 /userland/snake
Move everything into a new repo.
Diffstat (limited to 'userland/snake')
-rw-r--r--userland/snake/Makefile15
-rw-r--r--userland/snake/snake.c109
2 files changed, 124 insertions, 0 deletions
diff --git a/userland/snake/Makefile b/userland/snake/Makefile
new file mode 100644
index 0000000..f29df30
--- /dev/null
+++ b/userland/snake/Makefile
@@ -0,0 +1,15 @@
+CC="/home/anton/prj/osdev/sysroot/bin/i686-sb-gcc"
+CFLAGS = -ggdb -ffreestanding -O0 -Wall -Wextra -pedantic -mgeneral-regs-only -Wimplicit-fallthrough -static -fsanitize=shift,signed-integer-overflow,bounds
+LIB=-L../libgui -lgui -L../json -ljson -L../libc -lc -lgcc
+INC=-I../libc/ -I../json/ -I../libgui/
+BINS=snake
+all: $(BINS)
+
+snake.o: snake.c
+ $(CC) $(CFLAGS) $(INC) $(LIB) -o $@ -c $<
+
+snake: snake.o
+ $(CC) -nostdlib $(CFLAGS) -o $@ $^ $(LIB)
+
+clean:
+ rm $(BINS) *.o
diff --git a/userland/snake/snake.c b/userland/snake/snake.c
new file mode 100644
index 0000000..7bfb312
--- /dev/null
+++ b/userland/snake/snake.c
@@ -0,0 +1,109 @@
+#include <assert.h>
+#include <fcntl.h>
+#include <json.h>
+#include <libgui.h>
+#include <poll.h>
+#include <pty.h>
+#include <socket.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+GUI_Window *global_w;
+
+#define SNAKE_WIDTH 10
+
+int get_key_event(char *c) {
+ if (0 >= read(global_w->ws_socket, c, 1))
+ return 0;
+ if (0 == *c)
+ return 0;
+ return 1;
+}
+
+void draw_snake(int x, int y) {
+ int sx = global_w->sx;
+ uint32_t *b = &global_w->bitmap_ptr[y * sx + x];
+ for (int i = 0; i < SNAKE_WIDTH; i++) {
+ for (int j = 0; j < SNAKE_WIDTH; j++) {
+ b[j] = 0xFF00FF;
+ }
+ b += sx;
+ }
+}
+
+int clamp(int *x, int *y) {
+ int clamped = 0;
+ if (*x + SNAKE_WIDTH > global_w->sx) {
+ *x = global_w->sx - SNAKE_WIDTH;
+ clamped = 1;
+ }
+ if (*y + SNAKE_WIDTH > global_w->sy) {
+ *y = global_w->sy - SNAKE_WIDTH;
+ clamped = 1;
+ }
+ return clamped;
+}
+
+void loop() {
+ int vel = SNAKE_WIDTH;
+ static int y_dir = SNAKE_WIDTH;
+ static int x_dir = 0;
+ static int snake_x = 10;
+ static int snake_y = 10;
+ static int ticks = 0;
+ ticks++;
+ char c;
+ if (get_key_event(&c)) {
+ switch (c) {
+ case 'w':
+ y_dir = -1 * vel;
+ x_dir = 0;
+ break;
+ case 's':
+ y_dir = vel;
+ x_dir = 0;
+ break;
+ case 'a':
+ x_dir = -1 * vel;
+ y_dir = 0;
+ break;
+ case 'd':
+ x_dir = vel;
+ y_dir = 0;
+ break;
+ case 'q':
+ exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!(ticks > 5))
+ return;
+ ticks = 0;
+
+ snake_x += x_dir;
+ snake_y += y_dir;
+ if (clamp(&snake_x, &snake_y))
+ return;
+ draw_snake(snake_x, snake_y);
+ if (y_dir || x_dir)
+ GUI_UpdateWindow(global_w);
+}
+
+int main(void) {
+ global_w = GUI_CreateWindow(10, 10, 150 * 4, 150 * 4);
+ assert(global_w);
+ for (int i = 0; i < 150 * 150 * 4 * 4; i++)
+ global_w->bitmap_ptr[i] = 0xFFFFFF;
+ GUI_UpdateWindow(global_w);
+ for (;;) {
+ loop();
+ msleep(50);
+ }
+ return 0;
+}