-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskunk_window_runtime.m
More file actions
390 lines (345 loc) · 10.6 KB
/
skunk_window_runtime.m
File metadata and controls
390 lines (345 loc) · 10.6 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#import <Cocoa/Cocoa.h>
#include <ctype.h>
#include <mach/mach_time.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@class SkunkCanvasView;
@class SkunkWindowDelegate;
typedef struct SkunkWindow {
int32_t width;
int32_t height;
uint32_t *pixels;
bool open;
bool headless;
double last_present_time;
double delta_time;
uint8_t key_down[256];
NSWindow *ns_window;
SkunkCanvasView *ns_view;
SkunkWindowDelegate *delegate;
} SkunkWindow;
static double skunk_now_seconds(void) {
static mach_timebase_info_data_t timebase = {0, 0};
if (timebase.denom == 0) {
mach_timebase_info(&timebase);
}
uint64_t ticks = mach_absolute_time();
double nanos = (double)ticks * (double)timebase.numer / (double)timebase.denom;
return nanos / 1000000000.0;
}
static bool skunk_headless_enabled(void) {
const char *value = getenv("SKUNK_WINDOW_HEADLESS");
return value != NULL && value[0] != '\0' && strcmp(value, "0") != 0;
}
static void skunk_window_step_clock(SkunkWindow *window) {
if (window == NULL) {
return;
}
if (window->headless) {
window->delta_time = 1.0 / 60.0;
window->last_present_time = skunk_now_seconds();
return;
}
double now = skunk_now_seconds();
if (window->last_present_time <= 0.0) {
window->delta_time = 1.0 / 60.0;
} else {
window->delta_time = now - window->last_present_time;
if (window->delta_time <= 0.0) {
window->delta_time = 1.0 / 60.0;
}
}
window->last_present_time = now;
}
static void skunk_update_key_state(SkunkWindow *window, NSEvent *event, bool is_down) {
if (window == NULL || event == nil) {
return;
}
NSString *characters = [event charactersIgnoringModifiers];
if (characters == nil || [characters length] == 0) {
return;
}
unichar ch = [characters characterAtIndex:0];
if (ch < 256) {
int lowered = tolower((int)ch);
if (lowered >= 0 && lowered < 256) {
window->key_down[lowered] = is_down ? 1 : 0;
}
}
}
@interface SkunkWindowDelegate : NSObject <NSWindowDelegate> {
@public
SkunkWindow *skunkWindow;
}
@end
@implementation SkunkWindowDelegate
- (BOOL)windowShouldClose:(id)sender {
(void)sender;
if (skunkWindow != NULL) {
skunkWindow->open = false;
}
return YES;
}
@end
@interface SkunkCanvasView : NSView {
@public
SkunkWindow *skunkWindow;
}
@end
@implementation SkunkCanvasView
- (BOOL)isFlipped {
return YES;
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (void)keyDown:(NSEvent *)event {
skunk_update_key_state(skunkWindow, event, true);
}
- (void)keyUp:(NSEvent *)event {
skunk_update_key_state(skunkWindow, event, false);
}
- (void)drawRect:(NSRect)dirtyRect {
(void)dirtyRect;
if (skunkWindow == NULL || skunkWindow->pixels == NULL) {
return;
}
CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData(
NULL,
skunkWindow->pixels,
(size_t)skunkWindow->width * (size_t)skunkWindow->height * sizeof(uint32_t),
NULL
);
CGImageRef image = CGImageCreate(
(size_t)skunkWindow->width,
(size_t)skunkWindow->height,
8,
32,
(size_t)skunkWindow->width * sizeof(uint32_t),
color_space,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little,
provider,
NULL,
false,
kCGRenderingIntentDefault
);
CGRect bounds = CGRectMake(0, 0, skunkWindow->width, skunkWindow->height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, bounds, image);
CGContextRestoreGState(context);
CGImageRelease(image);
CGDataProviderRelease(provider);
CGColorSpaceRelease(color_space);
}
@end
static void skunk_window_pump_events(SkunkWindow *window) {
if (window == NULL || window->headless) {
return;
}
@autoreleasepool {
for (;;) {
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil) {
break;
}
[NSApp sendEvent:event];
}
[NSApp updateWindows];
}
}
void *skunk_window_create(int32_t width, int32_t height, const char *title) {
SkunkWindow *window = (SkunkWindow *)calloc(1, sizeof(SkunkWindow));
if (window == NULL) {
return NULL;
}
window->width = width > 0 ? width : 1;
window->height = height > 0 ? height : 1;
window->pixels = (uint32_t *)calloc(
(size_t)window->width * (size_t)window->height,
sizeof(uint32_t)
);
window->open = true;
window->headless = skunk_headless_enabled();
window->delta_time = 1.0 / 60.0;
window->last_present_time = skunk_now_seconds();
if (window->headless) {
return window;
}
@autoreleasepool {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp finishLaunching];
NSRect frame = NSMakeRect(0, 0, window->width, window->height);
NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
window->ns_window = [[NSWindow alloc] initWithContentRect:frame
styleMask:style
backing:NSBackingStoreBuffered
defer:NO];
window->ns_view = [[SkunkCanvasView alloc] initWithFrame:frame];
window->ns_view->skunkWindow = window;
window->delegate = [[SkunkWindowDelegate alloc] init];
window->delegate->skunkWindow = window;
NSString *ns_title = title != NULL ? [NSString stringWithUTF8String:title] : @"Skunk";
[window->ns_window setTitle:ns_title];
[window->ns_window setDelegate:window->delegate];
[window->ns_window setReleasedWhenClosed:NO];
[window->ns_window setContentView:window->ns_view];
[window->ns_window center];
[window->ns_window makeKeyAndOrderFront:nil];
[window->ns_window makeFirstResponder:window->ns_view];
[NSApp activateIgnoringOtherApps:YES];
}
skunk_window_pump_events(window);
return window;
}
bool skunk_window_is_open(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return false;
}
skunk_window_pump_events(window);
return window->open;
}
void skunk_window_poll(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return;
}
skunk_window_pump_events(window);
}
void skunk_window_clear(void *window_ptr, int32_t color) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL || window->pixels == NULL) {
return;
}
uint32_t packed = (uint32_t)color;
size_t count = (size_t)window->width * (size_t)window->height;
for (size_t i = 0; i < count; ++i) {
window->pixels[i] = packed;
}
}
void skunk_window_draw_rect(
void *window_ptr,
double x,
double y,
double width,
double height,
int32_t color
) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL || window->pixels == NULL) {
return;
}
int x0 = (int)llround(x);
int y0 = (int)llround(y);
int x1 = x0 + (int)llround(width);
int y1 = y0 + (int)llround(height);
if (x0 < 0) {
x0 = 0;
}
if (y0 < 0) {
y0 = 0;
}
if (x1 > window->width) {
x1 = window->width;
}
if (y1 > window->height) {
y1 = window->height;
}
if (x0 >= x1 || y0 >= y1) {
return;
}
uint32_t packed = (uint32_t)color;
for (int row = y0; row < y1; ++row) {
size_t offset = (size_t)row * (size_t)window->width;
for (int col = x0; col < x1; ++col) {
window->pixels[offset + (size_t)col] = packed;
}
}
}
void skunk_window_present(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return;
}
if (!window->headless && window->ns_view != nil) {
@autoreleasepool {
[window->ns_view setNeedsDisplay:YES];
[window->ns_view displayIfNeeded];
}
}
skunk_window_pump_events(window);
skunk_window_step_clock(window);
}
double skunk_window_delta_time(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return 0.0;
}
return window->delta_time;
}
void skunk_window_close(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return;
}
window->open = false;
if (!window->headless && window->ns_window != nil) {
@autoreleasepool {
[window->ns_window orderOut:nil];
[window->ns_window close];
}
}
}
void skunk_window_deinit(void *window_ptr) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL) {
return;
}
if (!window->headless) {
@autoreleasepool {
if (window->ns_window != nil) {
[window->ns_window setDelegate:nil];
}
if (window->delegate != nil) {
[window->delegate release];
window->delegate = nil;
}
if (window->ns_view != nil) {
[window->ns_view release];
window->ns_view = nil;
}
if (window->ns_window != nil) {
[window->ns_window release];
window->ns_window = nil;
}
}
}
free(window->pixels);
free(window);
}
bool skunk_keyboard_is_down(void *window_ptr, uint16_t key) {
SkunkWindow *window = (SkunkWindow *)window_ptr;
if (window == NULL || key >= 256) {
return false;
}
int lowered = tolower((int)key);
if (lowered < 0 || lowered >= 256) {
return false;
}
return window->key_down[lowered] != 0;
}