From: Brendan Hansen Date: Wed, 24 Nov 2021 03:15:17 +0000 (-0600) Subject: more work on snake game X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=8cdd262452915b90a95381ede1429ac658149205;p=heartbreak.git more work on snake game --- diff --git a/build.bat b/build.bat index 61d8151..9b376d8 100644 --- 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 diff --git a/misc/onyx/heartbreak.onyx b/misc/onyx/heartbreak.onyx index 0e0d62a..d512831 100644 --- a/misc/onyx/heartbreak.onyx +++ b/misc/onyx/heartbreak.onyx @@ -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); diff --git a/tests/snake.onyx b/tests/snake.onyx index 64471d5..d6c42ca 100644 --- a/tests/snake.onyx +++ b/tests/snake.onyx @@ -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); }