From: Brendan Hansen Date: Tue, 2 Nov 2021 22:36:12 +0000 (-0500) Subject: added circle rendering X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=25b8fe540b8728afd023b869d0ed2c26f908ed3c;p=heartbreak.git added circle rendering --- diff --git a/CHANGELOG b/CHANGELOG index fb76763..13b167f 100644 --- 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: diff --git a/Makefile b/Makefile index 4c645d0..158f6a9 100644 --- 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 index 0000000..9fe7500 --- /dev/null +++ b/docs/api.md @@ -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)` + + + + + + diff --git a/misc/onyx/heartbreak_graphics.onyx b/misc/onyx/heartbreak_graphics.onyx index 0716e15..ea9de1c 100644 --- a/misc/onyx/heartbreak_graphics.onyx +++ b/misc/onyx/heartbreak_graphics.onyx @@ -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 diff --git a/misc/onyx/heartbreak_input.onyx b/misc/onyx/heartbreak_input.onyx index b5c8711..248dea3 100644 --- a/misc/onyx/heartbreak_input.onyx +++ b/misc/onyx/heartbreak_input.onyx @@ -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 index 0000000..10dd8ae --- /dev/null +++ b/misc/onyx/qhb.onyx @@ -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 diff --git a/src/heartbreak_graphics.c b/src/heartbreak_graphics.c index b255f86..449a666 100644 --- a/src/heartbreak_graphics.c +++ b/src/heartbreak_graphics.c @@ -2,6 +2,9 @@ #include "heartbreak.h" #include "gfx.h" +#include +#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 diff --git a/src/heartbreak_system.c b/src/heartbreak_system.c index eebbf16..aa259d5 100644 --- a/src/heartbreak_system.c +++ b/src/heartbreak_system.c @@ -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); diff --git a/tests/simp.onyx b/tests/simp.onyx index fcd5397..9e9ea34 100644 --- a/tests/simp.onyx +++ b/tests/simp.onyx @@ -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