forked from DaveDisI/Battery_Barrage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosx_platform.mm
More file actions
376 lines (328 loc) · 13.1 KB
/
osx_platform.mm
File metadata and controls
376 lines (328 loc) · 13.1 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
#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h> //for key codes
#include <Metal/Metal.h>
#include <MetalKit/MetalKit.h>
#include <GameController/GameController.h>
#include <dlfcn.h>
#include "battery_barrage.cpp"
#include "osx_renderer_metal.mm"
#define WIDTH 1280
#define HEIGHT 720
OSDevice osDevice;
RenderDevice renderDevice = {};
TextObjectManager txtObjMgr = {};
TextRenderer textRenderer = {};
TerrainRenderer terrainRenderer = {};
MeshRenderer meshRenderer = {};
Mesh mesh;
@class WindowDelegate;
@interface WindowDelegate : NSView <NSWindowDelegate> {
@public
NSRect windowRect;
}
@end
@implementation WindowDelegate
-(void)windowWillClose:(NSNotification *)notification {
[NSApp terminate:self];
}
@end
void OSXreadBinaryFile(const s8* fileName, u8** fileData, u64* fileLength){
NSString* string = [[NSString alloc] initWithUTF8String: fileName];
NSData* data = [NSData dataWithContentsOfFile: string];
*fileLength = [data length];
*fileData = (u8*)[data bytes];
[string release];
//[data release];//this might cause problems
}
void OSXreadTextFile(const s8* fileName, s8** fileData, u64* fileLength){
NSError* err;
const s8* fn = fileName;
NSString* fl = [[NSString alloc] initWithUTF8String: fn];
NSString* string = [NSString stringWithContentsOfFile: fl
encoding: NSUTF8StringEncoding
error: &err];
[fl release];
*fileLength = [string length];
*fileData = (s8*)[string UTF8String];
[string release];//this might cause problems
}
const s8* OSXgetPathToExecutable(){
static bool initialized = false;
static const s8* path;
if(!initialized){
NSString *appParentDirectory = [[NSBundle mainBundle] bundlePath];
path = [appParentDirectory UTF8String];
}
//[appParentDirectory release];
return path;
}
const s8* OSXgetPathFromExecutable(const s8* path){
const s8* execPath = OSXgetPathToExecutable();
u32 epLen = getStringLength(execPath);
u32 pLen = getStringLength(path);
u32 fullLen = epLen + pLen;
s8* fullPath = new s8[fullLen];
for(u32 i = 0; i < epLen - 1; i++){
fullPath[i] = execPath[i];
}
fullPath[epLen - 1] = '/';
for(u32 i = 0; i < pLen; i++){
fullPath[i + epLen] = path[i];
}
return (const s8*)fullPath;
}
void OSXdisplayMessageBox(const char* message){
NSString* string = [[NSString alloc] initWithCString: message
encoding: NSUTF8StringEncoding];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:string];
[alert runModal];
[string release];
[alert release];
}
int main(int argc, char** argv){
osDevice.getPathToExecutable = OSXgetPathToExecutable;
osDevice.getPathFromExecutable = OSXgetPathFromExecutable;
osDevice.readBinaryFile = OSXreadBinaryFile;
osDevice.readTextFile = OSXreadTextFile;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[NSApp sharedApplication];
NSUInteger windowStyle = NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskResizable |
NSWindowStyleMaskMiniaturizable;
NSRect screenRect = [[NSScreen mainScreen] frame];
NSRect viewRect = NSMakeRect(0, 0, WIDTH, HEIGHT);
NSRect windowRect = NSMakeRect(NSMidX(screenRect) - NSMidX(viewRect),
NSMidY(screenRect) - NSMidY(viewRect),
viewRect.size.width,
viewRect.size.height);
NSWindow * window = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:windowStyle
backing:NSBackingStoreBuffered
defer:false];
[window autorelease];
NSWindowController * windowController = [[NSWindowController alloc] initWithWindow:window];
[windowController autorelease];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
WindowDelegate *delegate = [WindowDelegate alloc];
[delegate autorelease];
[window setDelegate:delegate];
[window setTitle:[[NSProcessInfo processInfo] processName]];
[window setAcceptsMouseMovedEvents:true];
[window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
[window orderFrontRegardless];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:true];
initializeRenderDevice(&renderDevice, window);
renderDevice.setClearColor(0.4, 0.8, 1, 1);
initializeTextRenderer(&osDevice, &renderDevice, &textRenderer);
setTextRendererProjection(&textRenderer, 0, WIDTH, 0, HEIGHT);
initializeTerrainRenderer(&osDevice, &renderDevice, &terrainRenderer);
initializeMeshRenderer(&osDevice, &renderDevice, &meshRenderer);
mesh.indexCount = 6;
void* handle = dlopen(OSXgetPathFromExecutable("libbb.so"), RTLD_LAZY);
typedef void (*fnPtr)(float, BatteryBarrageState*);
fnPtr update = (fnPtr)dlsym(handle, "updateGameState");
NSString * path = [[NSString alloc] initWithUTF8String: OSXgetPathFromExecutable("libbb.so")];
NSDate * fileLastModifiedDate = 0;
NSError * error = 0;
NSDictionary * attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if (attrs && !error){
fileLastModifiedDate = [attrs fileModificationDate];
}
TextObject* nums = createTextObject(&txtObjMgr, "1234567890", 50, 650);
TextObject* spec = createTextObject(&txtObjMgr, "!@#$\\/%^&*()-_=+?><\";", 50, 600);
TextObject* tpLo = createTextObject(&txtObjMgr, "qwertyuiop", 50, 550);
TextObject* mdLo = createTextObject(&txtObjMgr, "asdfghjkl", 50, 500);
TextObject* btLo = createTextObject(&txtObjMgr, "zxcvbnm", 50, 450);
TextObject* tpUp = createTextObject(&txtObjMgr, "QWERTYUIOP", 50, 400);
TextObject* mdUp = createTextObject(&txtObjMgr, "ASDFGHJKL", 50, 350);
TextObject* btUp = createTextObject(&txtObjMgr, "ZXCVBNM", 50, 300);
Camera camera;
camera.forward = Vector3(0, 0, 1);
camera.right = Vector3(1, 0, 0);
camera.up = Vector3(0, 1, 0);
camera.position = Vector3(0, 0, 0);
f32 moveSpeed = 0.2;
bool moveForward = false;
bool moveBack = false;
bool moveLeft = false;
bool moveRight = false;
bool moveUp = false;
bool moveDown = false;
bool yawLeft = false;
bool yawRight = false;
bool pitchUp = false;
bool pitchDown = false;
bool rollRight = false;
bool rollLeft = false;
CGDisplayHideCursor(kCGDirectMainDisplay);
NSEvent* ev;
while(true){
do {
ev = [NSApp nextEventMatchingMask: NSEventMaskAny
untilDate: 0
inMode: NSDefaultRunLoopMode
dequeue: true];
if (ev) {
if([ev type] == NSEventTypeKeyDown){
switch([ev keyCode]){
case kVK_Escape:{
[NSApp terminate: NSApp];
break;
}
case kVK_ANSI_W:{
moveForward = true;
break;
}
case kVK_ANSI_S:{
moveBack = true;
break;
}
case kVK_ANSI_A:{
moveRight = true;
break;
}
case kVK_ANSI_D:{
moveLeft = true;
break;
}
case kVK_ANSI_R:{
moveUp = true;
break;
}
case kVK_ANSI_F:{
moveDown = true;
break;
}
case kVK_RightArrow:{
yawRight = true;
break;
}
case kVK_LeftArrow:{
yawLeft = true;
break;
}
case kVK_UpArrow:{
pitchUp = true;
break;
}
case kVK_DownArrow:{
pitchDown = true;
break;
}
case kVK_ANSI_Q:{
rollLeft = true;
break;
}
case kVK_ANSI_E:{
rollRight = true;
break;
}
}
}else if([ev type] == NSEventTypeKeyUp){
switch([ev keyCode]){
case kVK_ANSI_W:{
moveForward = false;
break;
}
case kVK_ANSI_S:{
moveBack = false;
break;
}
case kVK_ANSI_A:{
moveRight = false;
break;
}
case kVK_ANSI_D:{
moveLeft = false;
break;
}
case kVK_ANSI_R:{
moveUp = false;
break;
}
case kVK_ANSI_F:{
moveDown = false;
break;
}
case kVK_RightArrow:{
yawRight = false;
break;
}
case kVK_LeftArrow:{
yawLeft = false;
break;
}
case kVK_UpArrow:{
pitchUp = false;
break;
}
case kVK_DownArrow:{
pitchDown = false;
break;
}
case kVK_ANSI_Q:{
rollLeft = false;
break;
}
case kVK_ANSI_E:{
rollRight = false;
break;
}
}
}else{
[NSApp sendEvent: ev];
}
}
} while (ev);
s32 mx, my;
CGGetLastMouseDelta(&mx, &my);
printf("%i\t%i\n", mx, my);
CGPoint p;
p.x = WIDTH / 2;
p.y = HEIGHT / 2;
CGWarpMouseCursorPosition(p);
#ifdef DEBUG_COMPILE
@autoreleasepool {
attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if (attrs && !error){
NSDate* date = [attrs fileModificationDate];
if([date compare:fileLastModifiedDate] != NSOrderedSame){
dlclose(handle);
handle = dlopen(OSXgetPathFromExecutable("libbb.so"), RTLD_LAZY);
update = (fnPtr)dlsym(handle, "updateGameState");
fileLastModifiedDate = [attrs fileModificationDate];
NSLog(@"%@\t%@", [attrs fileModificationDate], fileLastModifiedDate);
}
}
}
#endif
renderDevice.prepareRenderer();
camera.position -= (camera.forward * moveForward * moveSpeed);
camera.position += (camera.forward * moveBack * moveSpeed);
camera.position += (camera.right * moveRight * moveSpeed);
camera.position -= (camera.right * moveLeft * moveSpeed);
camera.position -= (camera.up * moveUp * moveSpeed);
camera.position += (camera.up * moveDown * moveSpeed);
rotate(&camera.orientation, camera.up, yawRight * 0.01);
rotate(&camera.orientation, camera.up, yawLeft * -0.01);
rotate(&camera.orientation, camera.forward, rollLeft * 0.01);
rotate(&camera.orientation, camera.forward, rollRight * -0.01);
rotate(&camera.orientation, camera.right, pitchUp * -0.01);
rotate(&camera.orientation, camera.right, pitchDown * 0.01);
rotate(&camera.orientation, camera.up, mx * 0.01);
rotate(&camera.orientation, camera.right, my * 0.01);
prepareTerrainRenderer(&terrainRenderer);
renderTerrain(&terrainRenderer, &camera);
prepareMeshRenderer(&meshRenderer);
renderMeshes(&meshRenderer, &mesh, 1, &camera);
prepareTextRenderer(&textRenderer);
renderTextObjects(&textRenderer, &txtObjMgr);
finalizeTextRenderer(&textRenderer);
renderDevice.finalizeRenderer();
}
[pool drain];
return 0;
}