added circle rendering
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 2 Nov 2021 22:36:12 +0000 (17:36 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 2 Nov 2021 22:36:12 +0000 (17:36 -0500)
CHANGELOG
Makefile
docs/api.md [new file with mode: 0644]
misc/onyx/heartbreak_graphics.onyx
misc/onyx/heartbreak_input.onyx
misc/onyx/qhb.onyx [new file with mode: 0644]
src/heartbreak_graphics.c
src/heartbreak_system.c
tests/simp.onyx

index fb7676352e39142e6cae56fd02c73f55339d03fe..13b167f5f222db29cc48a0613a0b32dc784c4e30 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,21 @@
 Release v0.0.1a
 Additions:
+* graphics_circle(FillMode mode, f32 x, f32 y, f32 r, i32 segments)
+* graphics_clear()
+* graphics_rectangle(FillMode mode, f32 x, f32 y, f32 w, f32 h)
+* graphics_set_color(f32 r, f32 g, f32 b, f32 a)
+* graphics_set_clear_color(f32 r, f32 g, f32 b, f32 a)
+* input_key_is_down(KeyConstant key) -> bool
+* input_mouse_get_x() -> i32
+* input_mouse_get_y() -> i32
+* input_mouse_is_down(ButtonConstant button) -> bool
+* system_destroy()
+* system_end_frame() -> bool
+* system_init()
+* window_get_height() -> i32
+* window_get_width() -> i32
+* window_set_dimensions(i32 width, i32 height) -> i32
+* window_set_should_close(bool close)
 
 Removals:
 
index 4c645d048f2c680bcac1c65efeed41dbec926876..158f6a9bd4d95bd63c436dcff6ac321da07b7cc2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CC=gcc
 WARNINGS=-Wimplicit -Wmisleading-indentation -Wparentheses -Wsequence-point -Wreturn-type -Wshift-negative-value -Wunused-but-set-parameter -Wunused-but-set-variable -Wunused-function -Wunused-label -Wmaybe-uninitialized -Wsign-compare -Wstrict-overflow -Wduplicated-branches -Wduplicated-cond -Wtrigraphs -Waddress -Wlogical-op
-FLAGS=$(WARNINGS) -g3
+FLAGS=$(WARNINGS) -g3 -O2
 
 INCLUDES=-I./include -I./lib/linux_x86_64/include
 LIBS=-L./lib/linux_x86_64/lib -lwasmer -Wl,-rpath=./lib/linux_x86_64/lib -lglfw -lGL -lm
diff --git a/docs/api.md b/docs/api.md
new file mode 100644 (file)
index 0000000..9fe7500
--- /dev/null
@@ -0,0 +1,35 @@
+# Heartbreak API Reference
+
+This document describes all functions that are available to the WebAssembly program to
+interact with Heartbreak. All functions are under the WebAssembly Module named "heartbreak".
+Below only lists the _name_ of the function.
+
+`graphics` module
+* `graphics_circle(FillMode mode, f32 x, f32 y, f32 r, i32 segments)`
+* `graphics_clear()`
+* `graphics_rectangle(FillMode mode, f32 x, f32 y, f32 w, f32 h)`
+* `graphics_set_color(f32 r, f32 g, f32 b, f32 a)`
+* `graphics_set_clear_color(f32 r, f32 g, f32 b, f32 a)`
+
+`input` module
+* `input_key_is_down(KeyConstant key) -> bool`
+* `input_mouse_get_x() -> i32`
+* `input_mouse_get_y() -> i32`
+* `input_mouse_is_down(ButtonConstant button) -> bool`
+
+`system` module
+* `system_destroy()`
+* `system_end_frame() -> bool`
+* `system_init()`
+
+`window` module
+* `window_get_height() -> i32`
+* `window_get_width() -> i32`
+* `window_set_dimensions(i32 width, i32 height) -> i32`
+* `window_set_should_close(bool close)`
+
+
+
+
+
+
index 0716e1547c88b4edc0f13b5801db6d8c1883f21b..ea9de1c98d3d31d5366148af345ca850c9fa759f 100644 (file)
@@ -10,3 +10,4 @@ FillMode :: enum {
 
 setColor  :: (r, g, b: f32, a: f32 = 1) -> void        #foreign "heartbreak" "graphics_set_color" ---
 rectangle :: (mode: FillMode, x, y, w, h: f32) -> void #foreign "heartbreak" "graphics_rectangle" ---
+circle    :: (mode: FillMode, x, y, r: f32, segments: i32 = 24) -> void #foreign "heartbreak" "graphics_circle" ---
\ No newline at end of file
index b5c8711f89a5a761ed1e99fd09c3275a0ba9f5b4..248dea3e3b5db44ab97bbaaad672d81fc13e63c7 100644 (file)
@@ -143,6 +143,9 @@ ButtonConstant :: enum {
 }
 
 keyIsDown :: (key: KeyConstant) -> bool #foreign "heartbreak" "input_key_is_down" ---
+
 mouseGetX :: () -> i32 #foreign "heartbreak" "input_mouse_get_x" ---
 mouseGetY :: () -> i32 #foreign "heartbreak" "input_mouse_get_y" ---
+mouseGetPos :: () => mouseGetX(), mouseGetY();
+
 mouseIsDown :: (button: ButtonConstant) -> bool #foreign "heartbreak" "input_mouse_is_down" ---
diff --git a/misc/onyx/qhb.onyx b/misc/onyx/qhb.onyx
new file mode 100644 (file)
index 0000000..10dd8ae
--- /dev/null
@@ -0,0 +1,23 @@
+// 'Quick Heartbreak'
+// Include this to quickly get access to Heartbreak in your Onyx program.
+
+package main
+
+#load "core/std"
+#load "./heartbreak"
+
+#private hb :: package heartbreak
+
+#if !#defined((package main).init) {
+    init :: () {}
+}
+
+#if !#defined((package main).update) {
+    update :: (dt: f32) {}
+}
+
+#if !#defined((package main).draw) {
+    draw :: () {}
+}
+
+main :: (_) => hb.run(.{ init, update, draw });
\ No newline at end of file
index b255f8637b7abfb86a21f99a99fd4970456290b5..449a6668d1e5236b3cf39c3cecf895640e642dc0 100644 (file)
@@ -2,6 +2,9 @@
 #include "heartbreak.h"
 #include "gfx.h"
 
+#include <math.h>
+#define PI 3.14159365
+
 static f32 clear_r, clear_g, clear_b, clear_a;
 
 HEARTBREAK_DEF(set_clear_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) {
@@ -29,6 +32,8 @@ HEARTBREAK_DEF(set_color, (WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) {
 }
 
 HEARTBREAK_DEF(rectangle, (WASM_I32,WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) {
+    // @TODO: Use fill mode
+
     f32 x = params->data[1].of.f32;
     f32 y = params->data[2].of.f32;
     f32 w = params->data[3].of.f32;
@@ -47,11 +52,42 @@ HEARTBREAK_DEF(rectangle, (WASM_I32,WASM_F32,WASM_F32,WASM_F32,WASM_F32), ()) {
     return NULL;
 }
 
+HEARTBREAK_DEF(circle, (WASM_I32, WASM_F32, WASM_F32, WASM_F32, WASM_I32), ()) {
+    // @TODO: Use fill mode
+
+    f32 x = params->data[1].of.f32;
+    f32 y = params->data[2].of.f32;
+    f32 r = params->data[3].of.f32;
+    i32 segments = params->data[4].of.i32;
+
+    f32 cx1 = x + r;
+    f32 cy1 = y;
+    f32 cx2 = x + cos(PI * 2 / segments) * r;
+    f32 cy2 = y + sin(PI * 2 / segments) * r;
+    f32 cx3, cy3;
+
+    fori (i, 2, segments) {
+        cx3 = x + cos(i * PI * 2 / segments) * r;
+        cy3 = y + sin(i * PI * 2 / segments) * r;
+
+        gfx_immediate_renderer_push_triangle(&renderer,
+            cx1, cy1, 0, 0,
+            cx2, cy2, 0, 0,
+            cx3, cy3, 0, 0);
+
+        cx2 = cx3;
+        cy2 = cy3;
+    }
+
+    return NULL;
+}
+
 HEARTBREAK_MODULE {
     HEARTBREAK_FUNC(set_clear_color)
     HEARTBREAK_FUNC(clear)
     HEARTBREAK_FUNC(set_color)
     HEARTBREAK_FUNC(rectangle)
+    HEARTBREAK_FUNC(circle)
 
     { NULL }
 };
\ No newline at end of file
index eebbf162db7bf240ee62d18581792300274db94c..aa259d583a1e47dd1a9db27b66d28e1619e86221 100644 (file)
@@ -27,6 +27,7 @@ HEARTBREAK_DEF(init, (), (WASM_I32)) {
     glfwSetWindowSizeCallback(glfw_window, __glfw_window_size_callback);
 
     glfwSwapInterval(1);
+    // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
     gfx_immediate_renderer_init(&renderer);
 
index fcd539745920b2007e8480f104bef5908fb498af..9e9ea347779b0bb8de45f89f7ba6a3a6c224e876 100644 (file)
@@ -1,8 +1,6 @@
-#load "core/std"
-#load "./../misc/onyx/heartbreak"
+#load "./../misc/onyx/qhb"
 
 use package core
-hb :: package heartbreak
 
 init :: () {
     w := hb.window.getWidth();
@@ -34,14 +32,12 @@ draw :: () {
     hb.graphics.setColor(1, 1, 0);
     hb.graphics.rectangle(.Fill, 100, 100, 200, 100);
 
-    mx := hb.input.mouseGetX();
-    my := hb.input.mouseGetY();
-    hb.graphics.rectangle(.Fill, ~~mx, ~~my, 100, 100);
+    mx, my := hb.input.mouseGetPos();
+    hb.graphics.setColor(1, 0, 0);
+    hb.graphics.circle(.Fill, ~~mx, ~~my, 25);
 
     w := cast(f32) hb.window.getWidth();
     h := cast(f32) hb.window.getHeight();
     hb.graphics.setColor(1, 1, 1);
     hb.graphics.rectangle(.Fill, w - 100, h - 100, 100, 100);
-}
-
-main :: (_) => hb.run(.{ init, update, draw });
\ No newline at end of file
+}
\ No newline at end of file