-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_macos.mm
302 lines (239 loc) · 7.85 KB
/
main_macos.mm
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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2021-2024 Chris Dragan
#import <AVFoundation/AVAudioPlayer.h>
#import <Cocoa/Cocoa.h>
#import <QuartzCore/CAMetalLayer.h>
#include "gui.h"
#include "main_macos.h"
#include "minivulkan.h"
#include "mstdc.h"
#include "d_printf.h"
#include <time.h>
struct Window {
CAMetalLayer* layer;
};
bool create_surface(struct Window* w)
{
static VkMetalSurfaceCreateInfoEXT surf_create_info = {
VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT
};
surf_create_info.pLayer = w->layer;
const VkResult res = CHK(vkCreateMetalSurfaceEXT(vk_instance,
&surf_create_info,
nullptr,
&vk_surface));
return res == VK_SUCCESS;
}
uint64_t get_current_time_ms()
{
uint64_t time_ms = 0;
struct timespec ts;
if ( ! clock_gettime(CLOCK_UPTIME_RAW, &ts)) {
time_ms = (uint64_t)ts.tv_sec * 1000;
time_ms += (uint64_t)ts.tv_nsec / 1000000;
}
return time_ms;
}
static AVAudioPlayer* sound_track;
bool load_sound_track(const void* data, uint32_t size)
{
assert( ! sound_track);
NSData *sound_data = [NSData dataWithBytes: data
length: size];
sound_track = [[AVAudioPlayer alloc] initWithData: sound_data
error: nullptr];
if ( ! sound_track) {
d_printf("Failed to load soundtrack\n");
return false;
}
d_printf("Soundtrack duration %.3f s\n", sound_track.duration);
if ( ! [sound_track prepareToPlay]) {
d_printf("Failed to initialize soundtrack for playback\n");
return false;
}
return true;
}
bool play_sound_track()
{
assert(sound_track);
if ( ! [sound_track play]) {
d_printf("Failed to play soundtrack\n");
return false;
}
return true;
}
@interface VulkanView: NSView
@end
@interface AppDelegate: NSObject<NSApplicationDelegate>
- (void)createMenu;
@end
@implementation VulkanViewController
{
NSSize m_size;
CVDisplayLinkRef m_display_link;
}
- (id)initWithSize: (NSSize)aSize
{
self = [super init];
if (self) {
m_size = aSize;
}
return self;
}
- (void)dealloc
{
CVDisplayLinkRelease(m_display_link);
[super dealloc];
}
- (void)loadView
{
NSView *view = [[VulkanView alloc]
initWithFrame: NSMakeRect(0, 0, m_size.width, m_size.height)
];
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
self.view = view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.wantsLayer = YES;
struct Window win;
win.layer = (CAMetalLayer *)self.view.layer;
if ( ! init_vulkan(&win)) {
[NSApp terminate: nil];
return;
}
init_mouse_tracking(self, self.view);
init_os_gui(self.view);
CVDisplayLinkCreateWithActiveCGDisplays(&m_display_link);
CVDisplayLinkSetOutputCallback(m_display_link, &display_link_callback, (__bridge void *)self.view);
}
- (void)viewDidAppear
{
CVDisplayLinkStart(m_display_link);
}
- (void)viewWillDisappear
{
CVDisplayLinkStop(m_display_link);
}
static CVReturn display_link_callback(CVDisplayLinkRef displayLink,
const CVTimeStamp *now,
const CVTimeStamp *outputTime,
CVOptionFlags flagsIn,
CVOptionFlags *flagsOut,
void *target)
{
init_os_gui_frame((__bridge NSView *)target);
if ( ! need_redraw(nullptr) && skip_frame(nullptr))
return kCVReturnSuccess;
if ( ! draw_frame()) {
[NSApp terminate: nil];
return kCVReturnError;
}
return kCVReturnSuccess;
}
@end
@implementation VulkanView
- (BOOL)wantsUpdateLayer
{
return YES;
}
+ (Class)layerClass
{
return [CAMetalLayer class];
}
- (CALayer *)makeBackingLayer
{
CAMetalLayer* layer = [self.class.layerClass layer];
const NSSize view_scale = [self convertSizeToBacking: NSMakeSize(1, 1)];
layer.contentsScale = MIN(view_scale.width, view_scale.height);
return layer;
}
- (BOOL)performKeyEquivalent: (NSEvent *)event
{
const uint32_t key_esc = 53;
const uint32_t key_q = 12;
const uint32_t mods = NSEventModifierFlagCommand
| NSEventModifierFlagOption
| NSEventModifierFlagControl
| NSEventModifierFlagShift;
const uint32_t key = event.keyCode;
const uint32_t modifier = event.modifierFlags & mods;
if ((key == key_esc) || (key == key_q && modifier == NSEventModifierFlagCommand)) {
[NSApp terminate: nil];
}
return TRUE;
}
@end
@implementation AppDelegate
- (void)createMenu
{
id main_menu = [NSMenu new];
NSApp.mainMenu = main_menu;
NSMenuItem *app_item = [NSMenuItem new];
[main_menu addItem: app_item];
id app_menu = [NSMenu new];
app_item.submenu = app_menu;
id quit_item = [[NSMenuItem alloc]
initWithTitle: @"Quit"
action: @selector(terminate:)
keyEquivalent: @"q"
];
[app_menu addItem: quit_item];
}
- (void)applicationDidFinishLaunching: (NSNotification *)notification
{
const bool full_screen = is_full_screen();
NSRect screen_frame = [[NSScreen mainScreen] frame];
NSRect frame_rect = NSMakeRect(0, 0,
screen_frame.size.width,
screen_frame.size.height);
if ( ! full_screen)
frame_rect = NSMakeRect(0, 0, get_main_window_width(), get_main_window_height());
NSWindow *window = [[NSWindow alloc]
initWithContentRect: frame_rect
styleMask: (NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable)
backing: NSBackingStoreBuffered
defer: NO
];
[window center];
[window makeKeyAndOrderFront: nil];
window.title = [[NSString alloc]
initWithCString: app_name
encoding: NSASCIIStringEncoding];
window.minSize = NSMakeSize(512, 384);
id view_ctrl = [[VulkanViewController alloc]
initWithSize: frame_rect.size
];
window.contentViewController = view_ctrl;
if (full_screen) {
[NSCursor hide];
[window setBackgroundColor: NSColor.blackColor];
[window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
[window setFrame: screen_frame display: YES];
[window toggleFullScreen: self];
}
else
[NSApp activateIgnoringOtherApps: YES];
}
- (void)applicationWillTerminate: (NSNotification *)notification
{
idle_queue();
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)sender
{
return YES;
}
@end
int main()
{
[NSApplication sharedApplication];
id delegate = [AppDelegate new];
NSApp.delegate = delegate;
[delegate createMenu];
[NSApp run];
return 0;
}