@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
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
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;
s.direction = .{ 1, 0 };
array.init(^s.body);
- for i: 10 do s.body << head;
+ for i: 3 do s.body << head;
return s;
}
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);
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);
+ }
}
}
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);
}