-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_window.h
703 lines (565 loc) · 19.8 KB
/
rjd_window.h
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#pragma once
#define RJD_WINDOW_H 1
struct rjd_window;
struct rjd_window_environment;
typedef void rjd_window_environment_init_func(const struct rjd_window_environment* env);
typedef void rjd_window_environment_close_func(const struct rjd_window_environment* env);
typedef void rjd_window_on_init_func(struct rjd_window* window, const struct rjd_window_environment* env);
typedef bool rjd_window_on_update_func(struct rjd_window* window, const struct rjd_window_environment* env);
typedef void rjd_window_on_close_func(struct rjd_window* window, const struct rjd_window_environment* env);
// Note that we use void* instead of the win32 types HINSTANCE and HWND to avoid taking a dependency
// on a windows header.
struct rjd_window_data_win32
{
#if RJD_PLATFORM_WINDOWS
void* hinstance; // HINSTANCE
#else
uint64_t unused;
#endif
};
struct rjd_window_environment
{
void* userdata;
const char** argv;
int argc;
struct rjd_window_data_win32 win32;
};
struct rjd_window_size
{
uint16_t width;
uint16_t height;
};
struct rjd_window_desc
{
const char* title;
struct rjd_window_size requested_size;
struct rjd_window_environment env;
rjd_window_on_init_func* init_func;
rjd_window_on_update_func* update_func;
rjd_window_on_close_func* close_func;
};
struct rjd_window
{
char impl[64];
};
void rjd_window_enter_windowed_environment(struct rjd_window_environment env, rjd_window_environment_init_func* init_func, rjd_window_environment_close_func* close_func);
struct rjd_result rjd_window_create(struct rjd_window* out, struct rjd_window_desc desc);
void rjd_window_runloop(struct rjd_window* window);
struct rjd_window_size rjd_window_size_get(const struct rjd_window* window);
void rjd_window_close(struct rjd_window* window);
#if RJD_PLATFORM_WINDOWS
void* rjd_window_win32_get_hwnd(const struct rjd_window* window);
#elif RJD_PLATFORM_OSX
#if RJD_LANG_OBJC
@class MTKView;
RJD_STATIC_ASSERT(sizeof(MTKView*) == sizeof(void*));
@class BasicView;
RJD_STATIC_ASSERT(sizeof(BasicView*) == sizeof(void*));
@class NSWindow;
RJD_STATIC_ASSERT(sizeof(NSWindow*) == sizeof(void*));
#else
typedef void MTKView;
typedef void BasicView;
typedef void NSWindow;
#endif
MTKView* rjd_window_osx_get_mtkview(const struct rjd_window* window);
BasicView* rjd_window_osx_get_basicview(const struct rjd_window* window);
NSWindow* rjd_window_osx_get_nswindow(const struct rjd_window* window);
#endif
////////////////////////////////////////////////////////////////////////////////
// implementation
#if RJD_IMPL
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// windows os
static struct rjd_atomic_uint32 global_window_count = {0};
#if RJD_PLATFORM_WINDOWS
struct rjd_window_win32
{
struct rjd_window_environment env;
rjd_window_on_init_func* init_func;
rjd_window_on_update_func* update_func;
rjd_window_on_close_func* close_func;
void* hwnd; // HWND
};
RJD_STATIC_ASSERT(sizeof(struct rjd_window) >= sizeof(struct rjd_window_win32));
static LRESULT CALLBACK WindowProc(HWND handle_window, UINT msg, WPARAM wparam, LPARAM lparam);
void rjd_window_enter_windowed_environment(struct rjd_window_environment env, rjd_window_environment_init_func* init_func, rjd_window_environment_close_func* close_func)
{
if (init_func) {
init_func(&env);
}
while (rjd_atomic_uint32_get(&global_window_count) > 0)
{
// other threads could be running their own window loops, so just wait until
// all of them are closed before exiting
}
if (close_func) {
close_func(&env);
}
}
struct rjd_result rjd_window_create(struct rjd_window* out, struct rjd_window_desc desc)
{
if (desc.title == NULL) {
desc.title = "";
}
static bool s_did_register_window_class = false;
if (!s_did_register_window_class) {
s_did_register_window_class = true;
WNDCLASSEX window_class = { 0 };
window_class.cbSize = sizeof(WNDCLASSEX);
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = WindowProc;
window_class.hInstance = desc.env.win32.hinstance;
window_class.lpszClassName = "rjd_window_class";
window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
//window_class.hIcon = LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
if (!RegisterClassEx(&window_class))
{
return RJD_RESULT("Failed to create window class");
}
}
const DWORD window_style = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU;
RECT window_rect = {0, 0, desc.requested_size.width, desc.requested_size.height};
if (!AdjustWindowRect(&window_rect, window_style, FALSE))
{
return RJD_RESULT("Failed to adjust window rect");
}
HWND hwnd = CreateWindowEx(0,
"rjd_window_class",
desc.title,
window_style,
1024, 80,
window_rect.right - window_rect.left, window_rect.bottom - window_rect.top,
NULL,
NULL,
desc.env.win32.hinstance,
NULL);
if (!hwnd)
{
return RJD_RESULT("Failed to create window from class");
}
struct rjd_window_win32* window_win32 = (struct rjd_window_win32*)out;
window_win32->env = desc.env;
window_win32->init_func = desc.init_func;
window_win32->update_func = desc.update_func;
window_win32->close_func = desc.close_func;
window_win32->hwnd = hwnd;
return RJD_RESULT_OK();
}
void rjd_window_runloop(struct rjd_window* window)
{
rjd_atomic_uint32_inc(&global_window_count);
struct rjd_window_win32* window_win32 = (struct rjd_window_win32*)window;
if (window_win32->init_func) {
window_win32->init_func(window, &window_win32->env);
}
// TODO support running multiple windows in the same thread?
bool running = true;
while (running)
{
MSG msg = {0};
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
{
running = false;
}
}
if (running && window_win32->update_func) {
running = window_win32->update_func(window, &window_win32->env);
}
}
if (window_win32->close_func) {
window_win32->close_func(window, &window_win32->env);
}
if (IsWindow(window_win32->hwnd)) {
DestroyWindow(window_win32->hwnd);
}
rjd_atomic_uint32_dec(&global_window_count);
}
struct rjd_window_size rjd_window_size_get(const struct rjd_window* window)
{
struct rjd_window_win32* window_win32 = (struct rjd_window_win32*)window;
RECT rect = {0};
GetClientRect(window_win32->hwnd, &rect);
uint32_t width = rect.right - rect.left;
uint32_t height = rect.bottom - rect.top;
RJD_ASSERT(width <= UINT16_MAX);
RJD_ASSERT(height <= UINT16_MAX);
struct rjd_window_size size =
{
.width = (uint16_t)width,
.height = (uint16_t)height,
};
return size;
}
void rjd_window_close(struct rjd_window* window)
{
struct rjd_window_win32* window_win32 = (struct rjd_window_win32*)window;
PostMessageA(window_win32->hwnd, WM_CLOSE, 0, 0);
}
void* rjd_window_win32_get_hwnd(const struct rjd_window* window)
{
struct rjd_window_win32* window_win32 = (struct rjd_window_win32*)window;
return window_win32->hwnd;
}
////////////////////////////////////////////////////////////////////////////////
// Local helpers
LRESULT CALLBACK WindowProc(HWND handle_window, UINT msg, WPARAM wparam, LPARAM lparam)
{
const POINT kMinSize = {1, 1};
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_GETMINMAXINFO:
((MINMAXINFO*)lparam)->ptMinTrackSize = kMinSize;
return 0;
// TODO forward resize message
//case WM_SIZE:
// //width = LOWORD(lparam);
// //height = HIWORD(lparam);
// //glViewport(0, 0, g_window_size.width, g_window_size.height);
// break;
default:
return DefWindowProc(handle_window, msg, wparam, lparam);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// osx os
#elif RJD_PLATFORM_OSX
#if !RJD_LANG_OBJC
#error "rjd_window.h must be implemented in a .m file on OSX"
#endif
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <MetalKit/MetalKit.h>
struct rjd_window_osx
{
NSWindow* nswindow;
MTKView* view_metal;
BasicView* view_basic;
rjd_window_on_init_func* init_func;
rjd_window_on_update_func* update_func;
rjd_window_on_close_func* close_func;
};
RJD_STATIC_ASSERT(sizeof(struct rjd_window) >= sizeof(struct rjd_window_osx));
bool s_is_app_initialized = false;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (retain) NSWindow *window;
-(instancetype)initWithEnvFunc:(rjd_window_environment_init_func*)init_func closeFunc:(rjd_window_environment_close_func*)close_func env:(struct rjd_window_environment)env;
@end
@interface CustomViewController : NSViewController
-(instancetype)initWithWidth:(uint16_t)width height:(uint16_t)height window:(struct rjd_window*)window env:(struct rjd_window_environment)env;
@end
@interface CustomWindow : NSWindow
@end
@interface Renderer : NSObject <MTKViewDelegate>
-(instancetype)initWithWindow:(struct rjd_window*)window env:(struct rjd_window_environment)env;
@end
@interface BasicView : NSView
@end
@interface BasicWindowDelegate : NSObject <NSWindowDelegate>
-(instancetype)initWithWindow:(struct rjd_window*)window env:(struct rjd_window_environment)env;
@end
////////////////////////////////////////////////////////////////////////////////
// interface implementation
void rjd_window_enter_windowed_environment(struct rjd_window_environment env, rjd_window_environment_init_func* init_func, rjd_window_environment_close_func* close_func)
{
NSApplicationLoad();
AppDelegate* delegate = [[AppDelegate alloc] initWithEnvFunc:init_func closeFunc:close_func env:env];
NSApplication* app = [NSApplication sharedApplication];
[app setDelegate:delegate];
[app activateIgnoringOtherApps:YES];
// The applicationDidFinishLaunching notification isn't sent multiple times. We need to keep track
// to ensure the init_func() is called upon reentry to this function
if (s_is_app_initialized && init_func) {
init_func(&env);
}
[app run];
if (close_func) {
close_func(&env);
}
}
struct rjd_result rjd_window_create(struct rjd_window* out, struct rjd_window_desc desc)
{
if (desc.title == NULL) {
desc.title = "";
}
NSRect rect = {0};
rect.size.height = desc.requested_size.height;
rect.size.width = desc.requested_size.width;
NSWindowStyleMask style = NSWindowStyleMaskResizable |
NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable;
NSWindow* nswindow = [[CustomWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:false];
nswindow.title = [NSString stringWithUTF8String:desc.title];
struct rjd_window_size size = desc.requested_size;
CustomViewController* viewController = [[CustomViewController alloc]
initWithWidth:size.width height:size.height window:out env:desc.env];
#if RJD_GFX_BACKEND_METAL
MTKView* view = [[MTKView alloc] initWithFrame:rect device:MTLCreateSystemDefaultDevice()];
#else
BasicView* view = [[BasicView alloc] initWithFrame:rect];
#endif
viewController.view = view;
[viewController loadView];
nswindow.contentViewController = viewController;
RJD_ASSERT(nswindow.canBecomeKeyWindow == YES);
RJD_ASSERT(nswindow.canBecomeMainWindow == YES);
[nswindow makeKeyAndOrderFront:nil];
rjd_atomic_uint32_inc(&global_window_count);
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)out;
window_osx->nswindow = nswindow;
window_osx->init_func = desc.init_func;
window_osx->update_func = desc.update_func;
window_osx->close_func = desc.close_func;
#if RJD_GFX_BACKEND_METAL
window_osx->view_metal = (view != nil && view.device != nil) ? view : NULL;
window_osx->view_basic = (window_osx->view_metal == NULL) ? (BasicView*)viewController.view : NULL;
#else
window_osx->view_metal = NULL;
window_osx->view_basic = (BasicView*)viewController.view;
#endif
if (window_osx->init_func) {
window_osx->init_func((struct rjd_window*)window_osx, &desc.env);
}
return RJD_RESULT_OK();
}
void rjd_window_runloop(struct rjd_window* window)
{
// no-op since OSX is event driven
RJD_UNUSED_PARAM(window);
}
struct rjd_window_size rjd_window_size_get(const struct rjd_window* window)
{
const struct rjd_window_osx* window_osx = (const struct rjd_window_osx*)window;
CGSize size = window_osx->view_metal ? window_osx->view_metal.drawableSize : window_osx->view_basic.bounds.size;
struct rjd_window_size windowsize = {
.width = size.width,
.height = size.height,
};
return windowsize;
}
void rjd_window_close(struct rjd_window* window)
{
const struct rjd_window_osx* window_osx = (const struct rjd_window_osx*)window;
[window_osx->nswindow close];
rjd_atomic_uint32_dec(&global_window_count);
}
MTKView* rjd_window_osx_get_mtkview(const struct rjd_window* window)
{
const struct rjd_window_osx* window_osx = (const struct rjd_window_osx*)window;
return window_osx->view_metal;
}
BasicView* rjd_window_osx_get_basicview(const struct rjd_window* window)
{
const struct rjd_window_osx* window_osx = (const struct rjd_window_osx*)window;
return window_osx->view_basic;
}
NSWindow* rjd_window_osx_get_nswindow(const struct rjd_window* window)
{
const struct rjd_window_osx* window_osx = (const struct rjd_window_osx*)window;
return window_osx->nswindow;
}
////////////////////////////////////////////////////////////////////////////////
// local helpers
@implementation AppDelegate
{
rjd_window_environment_init_func* init_func;
rjd_window_environment_close_func* close_func;
struct rjd_window_environment env;
}
-(instancetype)initWithEnvFunc:(rjd_window_environment_init_func*)_init_func closeFunc:(rjd_window_environment_close_func*)_close_func env:(struct rjd_window_environment)_env
{
if (self = [super init]) {
self->init_func = _init_func;
self->close_func = _close_func;
self->env = _env;
}
return self;
}
-(void)applicationDidFinishLaunching:(NSNotification*)notification
{
RJD_UNUSED_PARAM(notification);
NSMenuItem* testItem = [[NSMenuItem alloc] initWithTitle:@"TestItem" action:nil keyEquivalent:@""];
NSMenu* menu = [[NSMenu alloc] initWithTitle:@"TestMenu"];
[menu addItem:testItem];
NSApplication.sharedApplication.mainMenu = menu;
s_is_app_initialized = true;
if (self->init_func) {
self->init_func(&self->env);
}
}
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender
{
RJD_UNUSED_PARAM(sender);
// The NSApplication event loop will only check the stop flag *after* it handles an event.
[sender stop:nil];
NSEvent* force_handler_event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
location:NSZeroPoint
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
subtype:0
data1:0
data2:0];
[NSApp postEvent:force_handler_event atStart:TRUE];
return NO;
}
@end
@implementation CustomViewController
{
Renderer* renderer;
BasicWindowDelegate* basic_delegate;
uint16_t width;
uint16_t height;
struct rjd_window* window;
struct rjd_window_environment env;
}
-(instancetype)initWithWidth:(uint16_t)_width height:(uint16_t)_height window:(struct rjd_window*)_window env:(struct rjd_window_environment)_env
{
if (self = [super init]) {
self->width = _width;
self->height = _height;
self->window = _window;
self->env = _env;
}
return self;
}
-(void)loadView
{
#if RJD_GFX_BACKEND_METAL
MTKView* view = (MTKView*)self.view;
if(view.device) {
// We need to hold a strong reference to the Renderer or it will go out of scope
// after this function and be destroyed. MTKView.delegate is a weak reference.
self->renderer = [[Renderer alloc] initWithWindow:self->window env:self->env];
[self->renderer mtkView:view drawableSizeWillChange:view.bounds.size];
view.delegate = self->renderer;
self.view = view;
} else {
NSLog(@"Metal is not supported on this device. Falling back to basic NSView with timer updates.");
self->basic_delegate = [[BasicWindowDelegate alloc] initWithWindow:window env:self->env];
BasicView* view = [[BasicView alloc] initWithFrame:self.view.frame];
self.view = view;
}
#else
self->basic_delegate = [[BasicWindowDelegate alloc] initWithWindow:window env:self->env];
#endif
}
@end
@implementation CustomWindow
{
}
-(BOOL)canBecomeMainWindow
{
return YES;
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
@end
@implementation Renderer
{
struct rjd_window* window;
struct rjd_window_environment env;
}
-(instancetype)initWithWindow:(struct rjd_window*)_window env:(struct rjd_window_environment)_env
{
if (self = [super init]) {
self->window = _window;
self->env = _env;
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)window;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification
object:window_osx->nswindow];
}
return self;
}
-(void)drawInMTKView:(MTKView*)view
{
RJD_UNUSED_PARAM(view);
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)self->window;
if (window_osx->update_func) {
if (window_osx->update_func(self->window, &self->env) == false) {
rjd_window_close(self->window);
}
}
}
-(void)windowWillClose:(NSNotification*)notification
{
RJD_UNUSED_PARAM(notification);
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)window;
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:window_osx->nswindow];
if (window_osx->close_func) {
window_osx->close_func(self->window, &self->env);
}
}
-(void)mtkView:(MTKView*)view drawableSizeWillChange:(CGSize)size
{
RJD_UNUSED_PARAM(view);
RJD_UNUSED_PARAM(size);
}
@end
@implementation BasicView
@end
@implementation BasicWindowDelegate
{
struct rjd_window* window;
struct rjd_window_environment env;
NSTimer* ticker;
}
-(instancetype)initWithWindow:(struct rjd_window*)_window env:(struct rjd_window_environment)_env
{
self->window = _window;
self->env = _env;
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)self->window;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification
object:window_osx->nswindow];
void (^update_tick_block)(NSTimer*) = ^void(NSTimer* timer) {
[self timerUpdate:timer];
};
self->ticker = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 repeats:YES block:update_tick_block];
return self;
}
-(void)timerUpdate:(NSTimer*)timer
{
RJD_UNUSED_PARAM(timer);
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)self->window;
if (window_osx->update_func) {
if (window_osx->update_func(self->window, &self->env) == false) {
rjd_window_close(self->window);
}
}
}
-(void)windowWillClose:(NSNotification*)notification
{
RJD_UNUSED_PARAM(notification);
struct rjd_window_osx* window_osx = (struct rjd_window_osx*)window;
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:window_osx->nswindow];
if (window_osx->close_func) {
window_osx->close_func(self->window, &self->env);
}
[self->ticker invalidate];
self->ticker = nil;
}
@end
#else
#error Unsupported platform.
#endif
#endif // RJD_IMPL