libdraw

Minimal window and drawing library
git clone http://git.omkov.net/libdraw
Log | Tree | Refs | Download

AuthorJakob Wakeling <[email protected]>
Date2024-03-10 06:36:28
Commit345d96ccfc7061d28302d386dcee06bbac35fd53
Parentccd60e6699b2d7bee0da5001872d696de05ba521

Add "draw" example program

Diffstat

A examples/draw.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++

1 files changed, 51 insertions, 0 deletions

diff --git a/examples/draw.c b/examples/draw.c
new file mode 100644
index 0000000..623e0f0
--- /dev/null
+++ b/examples/draw.c
@@ -0,0 +1,51 @@
+#include "../src/draw.h"
+
+int main() {
+	draw_debug = true;
+	draw_window *window; int32_t w = 800, h = 600;
+	draw_window_init(&window, w, h, "Draw", 0);
+
+	bool left = false, right = false, draw = false;
+	uint32_t colour = 0x00ff0000;
+
+	draw_buffer *buf = draw_buffer_init(w, h);
+	draw_buffer_clear(buf, 0x00000000);
+
+	for (draw_event e; (e = draw_window_event(window)).kind != DRAW_EVENT_EXIT;) switch (e.kind) {
+	case DRAW_EVENT_KEY:    {
+		if (e.key.code == DRAW_KEY_1) { colour = 0x00ff0000; }
+		if (e.key.code == DRAW_KEY_2) { colour = 0x0000ff00; }
+		if (e.key.code == DRAW_KEY_3) { colour = 0x000000ff; }
+	} break;
+	case DRAW_EVENT_MOUSE:  {
+		if (e.mouse.button == DRAW_MOUSE_LEFT) {
+			if (e.mouse.direction == DRAW_PRESS) { left = true; }
+			if (e.mouse.direction == DRAW_RELEASE) { left = false; }
+		}
+		else if (e.mouse.button == DRAW_MOUSE_RIGHT) {
+			if (e.mouse.direction == DRAW_PRESS) { right = true; }
+			if (e.mouse.direction == DRAW_RELEASE) { right = false; }
+		}
+
+		if (left) { draw_buffer_rect(buf, (point){e.mouse.x, e.mouse.y}, (rect){-5, -5, 6, 6}, colour); }
+		if (right) { draw_buffer_rect(buf, (point){e.mouse.x, e.mouse.y}, (rect){-5, -5, 6, 6}, 0x00000000); }
+		if ((left || right) && !draw) { draw_window_repaint(window); }
+	} break;
+	case DRAW_EVENT_PAINT:  {
+		draw_window_draw(window, (point){0, 0}, buf, buf->bounds); draw = false;
+	} break;
+	case DRAW_EVENT_RESIZE: {
+		w = e.resize.w; h = e.resize.h;
+
+		draw_buffer *old = buf;
+		buf = draw_buffer_init(w, h);
+		draw_buffer_clear(buf, 0x00000000);
+		draw_buffer_copy(buf, old, (point){0, 0}, old->bounds);
+		draw_buffer_free(&old);
+	} break;
+	default: { /* ignore */ } break;
+	}
+
+quit:
+	draw_window_free(&window); return 0;
+}