-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWEFX.zig
More file actions
executable file
·343 lines (287 loc) · 8.83 KB
/
WEFX.zig
File metadata and controls
executable file
·343 lines (287 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
allocator: Allocator = undefined,
width: u32 = 0,
height: u32 = 0,
foreground_color: u32 = 0x00_00_00_00,
background_color: u32 = 0x00_00_00_00,
event_queue: EventQueue = undefined,
screen: []u32 = undefined,
buffer: []u32 = undefined,
const Self = @This();
const EventQueue = std.DoublyLinkedList(Event);
pub const EventType = enum {
keydown,
keypress,
keyup,
mousemove,
mousedown,
mouseup,
pub inline fn isKeyboardEvent(event_type: EventType) bool {
return event_type == .keydown or event_type == .keypress or event_type == .keyup;
}
pub inline fn isMouseEvent(event_type: EventType) bool {
return event_type == .mousemove or event_type == .mousedown or event_type == .mouseup;
}
};
pub const MouseEvent = struct {
// alt_key: bool,
button: Button,
// meta_key: bool,
x: u32,
y: u32,
pub const Button = enum {
main,
auxiliary,
secondary,
fourth,
fifth,
};
};
pub const KeyboardEvent = struct {
// alt_key: bool,
// ctrl_key: bool,
// meta_key: bool,
// repeat: bool,
// shift_key: bool,
key: u8,
};
pub const Event = struct {
timestamp: f32,
event_type: EventType,
event: union {
keyboard: KeyboardEvent,
mouse: MouseEvent,
},
pub fn initKeyboardEvent(timestamp: f32, event_type: EventType, kbd_event: KeyboardEvent) Event {
assert(event_type.isKeyboardEvent());
return Event{
.timestamp = timestamp,
.event_type = event_type,
.event = .{
.keyboard = kbd_event,
},
};
}
pub fn initMouseEvent(timestamp: f32, event_type: EventType, mouse_event: MouseEvent) Event {
assert(event_type.isMouseEvent());
return Event{
.timestamp = timestamp,
.event_type = event_type,
.event = .{
.mouse = mouse_event,
},
};
}
pub fn keyboardEvent(self: Event) KeyboardEvent {
assert(self.event_type.isKeyboardEvent());
return self.event.keyboard;
}
pub fn mouseEvent(self: Event) MouseEvent {
assert(self.event_type.isMouseEvent()());
return self.event.mouse;
}
};
pub fn init(allocator: Allocator) Self {
return .{
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
var it = self.event_queue.first;
while (it) |node| {
it = node.next;
self.allocator.destroy(node);
}
self.event_queue = undefined;
self.allocator = undefined;
}
/// wefx.open(width, height) allocates memory for both the screen and buffer slices.
pub fn open(self: *Self, width: u32, height: u32) !void {
self.width = width;
self.height = height;
self.buffer = try self.allocator.alloc(u32, width * height);
self.screen = try self.allocator.alloc(u32, width * height);
}
pub fn close(self: *Self) void {
self.allocator.free(self.buffer);
self.buffer = undefined;
self.allocator.free(self.screen);
self.screen = undefined;
self.width = 0;
self.height = 0;
}
/// wefx.clearColor(r, g, b) sets the background color to RGB(r, g, b).
/// A subsequent call to wefx.clear() will "clear" the screen by setting every pixel to said color.
pub fn clearColor(self: *Self, r: u8, g: u8, b: u8) void {
self.background_color = fromRGB(r, g, b);
}
/// wefx.clear() sets every pixel to the background color.
pub fn clear(self: *Self) void {
const bg_color = self.background_color;
for (self.buffer) |*v| {
v.* = bg_color;
}
}
/// wefx.color(r, g, b) sets the foreground color to RGB(r, g, b).
pub fn color(self: *Self, r: u8, g: u8, b: u8) void {
self.foreground_color = fromRGB(r, g, b);
}
/// wefx.point(x, y) sets the value of the pixel located at (x, y) to the foreground color.
pub fn point(self: *Self, x: u32, y: u32) void {
assert(x <= self.width and y <= self.height);
self.buffer[x + y * self.width] = self.foreground_color;
}
/// wefx.line(x0, y0, x1, y1) draws a line from point (x0, y0) to point (x1, y1).
/// Uses Bresenham's line algorithm.
pub fn line(self: *Self, x0: u32, y0: u32, x1: u32, y1: u32) void {
assert(x0 <= self.width and y0 <= self.height);
assert(x1 <= self.width and y1 <= self.height);
const dx: i64 = @intCast(@abs(@as(i64, x1) - @as(i64, x0)));
//const sx: i64 = if (x0 < x1) 1 else -1;
const dy = -1 * @as(i64, @intCast(@abs(@as(i64, y1) - @as(i64, y0))));
//const sy: i64 = if (y0 < y1) 1 else -1;
var err = dx + dy;
var x = x0;
var y = y0;
while (true) {
self.point(x, y);
if (x == x1 and y == y1) {
break;
}
const err2 = 2 * err;
if (err2 >= dy) {
if (x == x1) {
break;
}
err += dy;
if (x0 < x1) {
x += 1;
} else {
x -= 1;
}
}
if (err2 <= dx) {
if (y == y1) {
break;
}
err += dx;
if (y0 < y1) {
y += 1;
} else {
y -= 1;
}
}
}
}
/// wefx.cicle(x0, y0, r0) draws a circle with center (x0, y0) and radius r0.
/// Uses the midpoint circle algorithm.
pub fn circle(self: *Self, x0: u32, y0: u32, r0: u32) void {
var x: u32 = r0;
var y: u32 = 0;
var err: i64 = 0;
while (x >= y) {
self.point(x0 + x, y0 + y);
self.point(x0 + y, y0 + x);
self.point(x0 - y, y0 + x);
self.point(x0 - x, y0 + y);
self.point(x0 - x, y0 - y);
self.point(x0 - y, y0 - x);
self.point(x0 + y, y0 - x);
self.point(x0 + x, y0 - y);
y += 1;
err += 2 * @as(i64, y) + 1;
if (err > 0) {
x -= 1;
err -= 2 * @as(i64, x) + 1;
}
}
}
pub fn flush(self: *Self) void {
for (self.buffer, 0..) |val, i| {
self.screen[i] = val;
}
}
fn eventPushBack(self: *Self, event: Event) !void {
var node_ptr = try self.allocator.create(EventQueue.Node);
node_ptr.data = event;
// TODO: set a maximum length for queue?
self.event_queue.append(node_ptr);
}
pub fn eventPopFront(self: *Self) ?Event {
const node_ptr = self.event_queue.popFirst() orelse return null;
const event = node_ptr.data;
self.allocator.destroy(node_ptr);
return event;
}
// Utilities
inline fn fromRGB(r: u8, g: u8, b: u8) u32 {
return fromRGBA(r, g, b, 0xFF);
}
inline fn fromRGBA(r: u8, g: u8, b: u8, a: u8) u32 {
return (@as(u32, a) << 24) + (@as(u32, r) << 16) + (@as(u32, g) << 8) + (@as(u32, b) << 0);
}
// Exported functions
export fn wefx_xsize(self: *Self) u32 {
return self.width;
}
export fn wefx_ysize(self: *Self) u32 {
return self.height;
}
export fn wefx_screen_offset(self: *Self) [*]u32 {
return self.screen.ptr;
}
export fn wefx_flush(self: *Self) void {
self.flush();
}
export fn wefx_add_keyboard_event(
self: *Self,
event_type_val: u8,
timestamp: f32,
key: u8,
) void {
const event_type: EventType = @enumFromInt(event_type_val);
const kbd_event: KeyboardEvent = .{
.key = key,
};
const event = Event.initKeyboardEvent(timestamp, event_type, kbd_event);
// TODO: handle error on JS?
self.eventPushBack(event) catch unreachable;
}
export fn wefx_add_mouse_event(
self: *Self,
event_type_val: u8,
timestamp: f32,
button_val: u8,
x: f32,
y: f32,
) void {
const event_type: EventType = @enumFromInt(event_type_val);
const button: MouseEvent.Button = @enumFromInt(button_val);
const mouse_event: MouseEvent = .{
.button = button,
.x = @as(u32, @intFromFloat(x)), // The @as(...) here shouldn't be necessary
.y = @as(u32, @intFromFloat(y)),
};
const event = Event.initMouseEvent(timestamp, event_type, mouse_event);
// TODO: handle error on JS?
self.eventPushBack(event) catch unreachable;
}
// comptime {
// inline for (@typeInfo(EventType).Enum.fields) |field| {
// const enum_val = @intToEnum(EventType, field.value);
// const enum_name = @tagName(enum_val);
// var field_value = struct {
// pub const field_value: i32 = field.value;
// }.field_value;
// const opts = .{ .name = "wefx_" ++ enum_name, .linkage = .Strong };
// @export(field_value, opts);
// }
// }
export var wefx_keydown: c_int = @intFromEnum(EventType.keydown);
export var wefx_keypress: c_int = @intFromEnum(EventType.keypress);
export var wefx_keyup: c_int = @intFromEnum(EventType.keyup);
export var wefx_mousemove: c_int = @intFromEnum(EventType.mousemove);
export var wefx_mousedown: c_int = @intFromEnum(EventType.mousedown);
export var wefx_mouseup: c_int = @intFromEnum(EventType.mouseup);