more work on snake game
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 03:15:17 +0000 (21:15 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 03:15:17 +0000 (21:15 -0600)
build.bat
misc/onyx/heartbreak.onyx
tests/snake.onyx

index 61d81511f02eb168c48c548e1c9cb27127aef972..9b376d8218df572d316b0d97c3937ccc613d3df5 100644 (file)
--- a/build.bat
+++ b/build.bat
@@ -1,6 +1,15 @@
 @echo off
 
-set FLAGS=/O2 /MT /Z7 /TC /std:c17
+call "\tools\dev.bat"
+
+set TEST_FILE=snake
+
+if %1 EQU debug (
+    set FLAGS=/O2 /MT /Z7 /TC /std:c17
+) else (
+    set FLAGS=/Od /MTd /Z7 /TC /std:c17
+)
+
 set SOURCE=src/gfx.c src/heartbreak.c src/heartbreak_graphics.c src/heartbreak_input.c src/heartbreak_system.c src/heartbreak_thread.c src/heartbreak_timer.c src/heartbreak_window.c src/utils.c
 set LIBS=lib\windows_x86_64\lib\wasmer.lib C:\tools\glfw-3.3.5.bin.WIN64\lib-vc2019\glfw3.lib C:\tools\glfw-3.3.5.bin.WIN64\lib-vc2019\glfw3dll.lib opengl32.lib ws2_32.lib Advapi32.lib userenv.lib bcrypt.lib
 set LINKFLAGS=/incremental:no /opt:ref /subsystem:console
@@ -8,4 +17,9 @@ set OUT=bin\heartbreak.exe
 
 cl.exe %FLAGS% /Iinclude /Ilib\common\include /Ilib\windows_x86_64\include /IC:\tools\glfw-3.3.5.bin.WIN64\include %SOURCE% /link /IGNORE:4217 %LIBS% /DEBUG /OUT:%OUT% %LINKFLAGS%
 
-del *.obj
\ No newline at end of file
+del *.obj
+
+if %1 EQU run (
+    \dev\onyx\onyx -V tests\%TEST_FILE%.onyx -o tests\%TEST_FILE%.wasm -r wasi --multi-threaded
+    bin\heartbreak tests\snake.wasm
+)
\ No newline at end of file
index 0e0d62add41590bce2a8119e1b4054582d1f6bfb..d512831a40acbc1b1e5acbd6f6111bd5878d0a71 100644 (file)
@@ -23,6 +23,9 @@ run :: (use funcs: HeartbreakFuncs) {
 
     load();
 
+    // Don't include the time taken to load on the first frame.
+    timer.step();
+
     while system.end_frame() {
         dt := timer.step();
         update(dt);
index 64471d511aba4d806616d884d9f83116eb3b43f9..d6c42caba608f7e08aee9e4761c68d4c3d89c83a 100644 (file)
@@ -17,12 +17,13 @@ Vec2 :: struct (T: type_expr) {
         return .{ -this.y, this.x };
     }
 }
+
 #operator + (v1, v2: Vec2($T))   => Vec2(T).{ v1.x + v2.x, v1.y + v2.y };
 #operator - (v1, v2: Vec2($T))   => Vec2(T).{ v1.x - v2.x, v1.y - v2.y };
 #operator * (v1: Vec2($T), s: T) => Vec2(T).{ v1.x * s,    v1.y * s };
 #operator == (v1, v2: Vec2($T)) => v1.x == v2.x && v1.y == v2.y;
 
-Vec2i :: #type Vec2(i32);
+Vec2i :: Vec2(i32);
 
 Snake :: struct {
     head: Vec2i;
@@ -37,7 +38,7 @@ snake_make :: (head: Vec2i) -> Snake {
     s.direction = .{ 1, 0 };
     
     array.init(^s.body);
-    for i: 10 do s.body << head;
+    for i: 3 do s.body << head;
 
     return s;
 }
@@ -51,10 +52,15 @@ snake_move :: (use this: ^Snake) {
     head += direction;
 }
 
+snake_grow :: (use this: ^Snake, amount := 1) {
+    last := body[body.count - 1];
+    for i: amount do body << last;
+}
+
 snake_draw :: (use this: ^Snake, cell_size: f32) {
-    hb.graphics.setColor(0, 0.6, 0);
-    for ^it: body {
-        hb.graphics.rectangle(.Fill, ~~it.x * cell_size, ~~it.y * cell_size, cell_size, cell_size);
+    for it: iter.enumerate(iter.as_iterator(body)) {
+        hb.graphics.setColor(0, 0.6 - 0.4 * (~~it.index / cast(f32) body.count), 0);
+        hb.graphics.rectangle(.Fill, ~~it.value.x * cell_size, ~~it.value.y * cell_size, cell_size, cell_size);
     }
 
     hb.graphics.setColor(0, 1, 0);
@@ -102,6 +108,11 @@ update :: (dt: f32) {
     if snake_timer <= 0 {
         snake_timer = 0.2;
         snake_move(^the_snake);
+
+        if the_snake.head == the_food {
+            the_food = .{ random.between(0, 10), random.between(0, 10) };
+            snake_grow(^the_snake, 2);
+        }
     }
 }
 
@@ -115,4 +126,9 @@ draw :: () {
 
     hb.graphics.setColor(0.8, 0.9, 0.1);
     hb.graphics.circle(.Fill, cell_size * ~~the_food.x + cell_size / 2, cell_size * ~~the_food.y + cell_size / 2, cell_size / 2);
+
+    hb.graphics.setColor(1, 0, 0);
+    fps_buffer: [20] u8;
+    fps_str := conv.str_format(fps_buffer, "FPS: {}", cast(i32) hb.timer.getFPS());
+    hb.graphics.print(fps_str, ~~hb.window.getWidth() - hb.graphics.getTextWidth(fps_str), 30);
 }