libdraw

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

libdraw/src/draw.h (92 lines, 2.7 KiB) -rw-r--r-- blame download

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
// Copyright (C) 2024, Jakob Wakeling
// All rights reserved.

#ifndef LIBDRAW_DRAW_H_RNC24Y6G
#define LIBDRAW_DRAW_H_RNC24Y6G

#include "keys.h"

#include <stdint.h>

typedef enum {
	DRAW_MOD_SHIFT     = 0x01,
	DRAW_MOD_CONTROL   = 0x02,
	DRAW_MOD_ALT       = 0x04,
	DRAW_MOD_SUPER     = 0x08,
	DRAW_MOD_CAPS_LOCK = 0x10,
	DRAW_MOD_NUM_LOCK  = 0x20,
} draw_mod_k;

typedef enum {
	DRAW_MOUSE_NONE         = 0,
	DRAW_MOUSE_LEFT         = 1,
	DRAW_MOUSE_MIDDLE       = 2,
	DRAW_MOUSE_RIGHT        = 3,
	DRAW_MOUSE_SCROLL_UP    = 4,
	DRAW_MOUSE_SCROLL_DOWN  = 5,
	DRAW_MOUSE_SCROLL_LEFT  = 6,
	DRAW_MOUSE_SCROLL_RIGHT = 7,
	DRAW_MOUSE_BACK         = 8,
	DRAW_MOUSE_FORWARD      = 9,
} draw_mouse_k;

typedef enum {
	DRAW_EVENT_EXIT = -1,
	DRAW_EVENT_NONE = 0,
	DRAW_EVENT_KEY,
	DRAW_EVENT_MOUSE,
	DRAW_EVENT_PAINT,
	DRAW_EVENT_MOVE,
	DRAW_EVENT_RESIZE,
	DRAW_EVENT_HIDE,
	DRAW_EVENT_SHOW,
} draw_event_k;

typedef enum { DRAW_PRESS, DRAW_RELEASE, DRAW_REPEAT } draw_direction;

typedef struct { draw_key_k code; draw_mod_k modifiers; draw_direction direction; } draw_event_key;
typedef struct { int32_t x, y; draw_mouse_k button; draw_mod_k modifiers; draw_direction direction; } draw_event_mouse;

typedef struct { int32_t x, y, w, h; } draw_event_paint;
typedef struct { int32_t w, h; } draw_event_resize;

typedef struct {
	draw_event_k kind;
	union {
		draw_event_key key;
		draw_event_mouse mouse;
		draw_event_paint paint;
		draw_event_resize resize;
	};
} draw_event;

typedef struct draw_window_s draw_window;

typedef struct { int32_t x, y; } point;
typedef struct { int32_t x, y, w, h; } rect;
typedef struct { rect bounds; uint32_t *buf; } draw_buffer;

extern bool draw_debug, draw_debug_verbose;

extern int draw_window_init(draw_window **window, int32_t w, int32_t h, const char *title, uint32_t flags);
extern void draw_window_free(draw_window **window);

extern draw_event draw_window_event(draw_window *window);
extern void draw_window_set_title(draw_window *window, const char *title);
extern void draw_window_repaint(draw_window *window);

extern int draw_window_draw(draw_window *w, point dp, draw_buffer *buf, rect br);
extern void draw_window_clear(draw_window *w, uint32_t colour);

extern draw_buffer *draw_buffer_init(int32_t w, int32_t h);
extern void draw_buffer_free(draw_buffer **buf);

extern void draw_buffer_copy(draw_buffer *dst, draw_buffer *src, point dp, rect sr);

extern void draw_buffer_clear(draw_buffer *buf, uint32_t colour);
extern void draw_buffer_rect(draw_buffer *buf, point p, rect r, uint32_t colour);
extern void draw_buffer_line(draw_buffer *buf, point p0, point p1, uint32_t colour);
extern void draw_buffer_circle(draw_buffer *buf, point p, int32_t r, uint32_t colour, bool fill);

#endif // LIBDRAW_DRAW_H_RNC24Y6G