-
Notifications
You must be signed in to change notification settings - Fork 1
/
handmadehero.mac.mm
838 lines (714 loc) · 25.2 KB
/
handmadehero.mac.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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
#include <Cocoa/Cocoa.h>
#include "casey/handmade_platform.h"
#include <mach-o/dyld.h>
#include "AudioToolbox/AudioToolbox.h"
#include <sys/stat.h>
#include <dlfcn.h>
#include <mach/mach_time.h>
#include "handmadehero.mac.h"
global_variable BOOL GlobalRunning;
global_variable bool32 GlobalPause;
global_variable mac_offscreen_buffer GlobalBackbuffer;
global_variable void *GlobalSecondaryBuffer;
global_variable mach_timebase_info_data_t GlobalPerfCountFrequency;
global_variable mac_state MacState;
internal void
CatStrings(size_t SourceACount, char *SourceA,
size_t SourceBCount, char *SourceB,
size_t DestCount, char *Dest)
{
// TODO(casey): Dest bounds checking!
for(int Index = 0;
Index < SourceACount;
++Index)
{
*Dest++ = *SourceA++;
}
for(int Index = 0;
Index < SourceBCount;
++Index)
{
*Dest++ = *SourceB++;
}
*Dest++ = 0;
}
internal void
MacGetAppFileName(mac_state *State)
{
uint32 buffsize = sizeof(State->AppFileName);
if (_NSGetExecutablePath(State->AppFileName, &buffsize) == 0) {
for(char *Scan = State->AppFileName;
*Scan;
++Scan)
{
if(*Scan == '/')
{
State->OnePastLastAppFileNameSlash = Scan + 1;
}
}
}
}
internal int
StringLength(char *String)
{
int Count = 0;
while(*String++)
{
++Count;
}
return(Count);
}
internal void
MacBuildAppPathFileName(mac_state *State, char *FileName,
int DestCount, char *Dest)
{
CatStrings(State->OnePastLastAppFileNameSlash - State->AppFileName, State->AppFileName,
StringLength(FileName), FileName,
DestCount, Dest);
}
DEBUG_PLATFORM_FREE_FILE_MEMORY(DEBUGPlatformFreeFileMemory)
{
if(Memory)
{
free(Memory);
}
}
DEBUG_PLATFORM_READ_ENTIRE_FILE(DEBUGPlatformReadEntireFile)
{
debug_read_file_result Result = {};
char FullPath[MAC_MAX_FILENAME_SIZE];
CatStrings(MacState.ResourcesDirectorySize, MacState.ResourcesDirectory,
StringLength(Filename), Filename,
sizeof(FullPath), FullPath);
FILE *FileHandle = fopen(FullPath, "r");
if(FileHandle != NULL)
{
fseek(FileHandle, 0, SEEK_END);
int FileSize = ftell(FileHandle);
if(FileSize)
{
rewind(FileHandle);
Result.Contents = malloc(FileSize);
if(Result.Contents)
{
uint32 BytesRead = fread(Result.Contents, 1, FileSize, FileHandle);
if(FileSize == BytesRead)
{
// NOTE(casey): File read successfully
Result.ContentsSize = FileSize;
}
else
{
// TODO(casey): Logging
DEBUGPlatformFreeFileMemory(Thread, Result.Contents);
Result.Contents = 0;
}
}
else
{
// TODO(casey): Logging
}
}
else
{
// TODO(casey): Logging
}
fclose(FileHandle);
}
else
{
// TODO(casey): Logging
}
return(Result);
}
DEBUG_PLATFORM_WRITE_ENTIRE_FILE(DEBUGPlatformWriteEntireFile)
{
bool32 Result = false;
FILE *FileHandle = fopen(Filename, "w");
if(FileHandle)
{
size_t BytesWritten = fwrite(Memory, 1, MemorySize, FileHandle);
if(BytesWritten)
{
// NOTE(casey): File read successfully
Result = (BytesWritten == MemorySize);
}
else
{
// TODO(casey): Logging
}
fclose(FileHandle);
}
else
{
// TODO(casey): Logging
}
return(Result);
}
inline time_t
MacGetLastWriteTime(char *Filename)
{
time_t LastWriteTime = 0;
struct stat StatData = {};
if (stat(Filename, &StatData) == 0)
{
LastWriteTime = StatData.st_mtime;
}
return(LastWriteTime);
}
internal mac_game_code
MacLoadGameCode(char *SourceDLLName)
{
mac_game_code Result = {};
Result.DLLLastWriteTime = MacGetLastWriteTime(SourceDLLName);
Result.GameCodeDLL = dlopen(SourceDLLName, RTLD_NOW);
if (Result.GameCodeDLL)
{
Result.UpdateAndRender = (game_update_and_render *)
dlsym(Result.GameCodeDLL, "GameUpdateAndRender");
Result.GetSoundSamples = (game_get_sound_samples *)
dlsym(Result.GameCodeDLL, "GameGetSoundSamples");
Result.IsValid = (Result.UpdateAndRender &&
Result.GetSoundSamples);
}
if(!Result.IsValid)
{
Result.UpdateAndRender = 0;
Result.GetSoundSamples = 0;
}
return(Result);
}
internal void
MacUnloadGameCode(mac_game_code *GameCode)
{
if(GameCode->GameCodeDLL)
{
dlclose(GameCode->GameCodeDLL);
GameCode->GameCodeDLL = 0;
}
GameCode->IsValid = false;
GameCode->UpdateAndRender = 0;
GameCode->GetSoundSamples = 0;
}
void MacAudioQueueOutputCallback (void *inUserData, AudioQueueRef queue, AudioQueueBufferRef buffer)
{
NSLog(@"Callback");
mac_sound_output *SoundOutput = (mac_sound_output *)inUserData;
// int16 *SourceSample = (int16 *)GlobalSecondaryBuffer;
// int16 *DestSample = (int16 *)buffer->mUserData;
// if (SourceSample) {
// for(uint32 SampleIndex = 0;
// SampleIndex < SoundOutput->SecondaryBufferSize;
// ++SampleIndex)
// {
// *DestSample++ = *SourceSample++;
// ++SoundOutput->RunningSampleIndex;
// }
// }
// buffer->mAudioDataByteSize = SoundOutput->SecondaryBufferSize;
uint32_t framesToGen = buffer->mAudioDataBytesCapacity / 4;
buffer->mAudioDataByteSize = framesToGen * 4;
AudioQueueEnqueueBuffer(queue, buffer, 0, 0);
}
internal void
MacInitSound(mac_sound_output *SoundOutput, int32 SamplesPerSecond, int32 BufferSize)
{
AudioStreamBasicDescription StreamDescription = { 0 };
StreamDescription.mSampleRate = SamplesPerSecond;
StreamDescription.mFormatID = kAudioFormatLinearPCM;
StreamDescription.mFormatFlags = kLinearPCMFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
StreamDescription.mBytesPerPacket = 4;
StreamDescription.mFramesPerPacket = 1;
StreamDescription.mBytesPerFrame = 4;
StreamDescription.mChannelsPerFrame = 2;
StreamDescription.mBitsPerChannel = 16;
OSStatus QueueStatus = AudioQueueNewOutput (&StreamDescription, MacAudioQueueOutputCallback, SoundOutput, 0, 0, 0, &SoundOutput->Queue);
if (QueueStatus == kAudioFormatUnsupportedDataFormatError) {
NSLog(@"OOPS");
Assert(0);
}
AudioQueueSetParameter(SoundOutput->Queue, kAudioQueueParam_Volume, 0.005);
for (uint32 i = 0;
i < ArrayCount(SoundOutput->Buffers);
i++) {
QueueStatus = AudioQueueAllocateBuffer (SoundOutput->Queue, BufferSize, &SoundOutput->Buffers[i]);
// TODO (Mike): We may not need to prime buffers the first time through.
// What is it playing if we do? We have no sound yet.
MacAudioQueueOutputCallback (&SoundOutput, SoundOutput->Queue, SoundOutput->Buffers[i]);
}
QueueStatus = AudioQueueStart (SoundOutput->Queue, NULL);
printf ("Audio Queue Started: %d\n", QueueStatus);
}
internal void
MacClearBuffer(mac_sound_output *SoundOutput)
{
uint16 *DestSample = (uint16 *)GlobalSecondaryBuffer;
for(uint32 ByteIndex = 0;
ByteIndex < SoundOutput->BytesPerSample;
++ByteIndex)
{
*DestSample++ = 0;
}
}
internal void
MacFillSoundBuffer(mac_sound_output *SoundOutput, uint32 ByteToLock, uint32 BytesToWrite,
game_sound_output_buffer *SourceBuffer)
{
int16 *DestSample = (int16 *)GlobalSecondaryBuffer;
int16 *SourceSample = SourceBuffer->Samples;
for(uint32 SampleIndex = 0;
SampleIndex < BytesToWrite;
++SampleIndex)
{
*DestSample++ = *SourceSample++;
++SoundOutput->RunningSampleIndex;
}
}
internal void
MacProcessKeyboardMessage(game_button_state *NewState, bool32 IsDown)
{
//Assert(NewState->EndedDown != IsDown);
if (NewState->EndedDown != IsDown)
{
NewState->EndedDown = IsDown;
++NewState->HalfTransitionCount;
}
}
internal void
MacProcessPendingMessages(game_controller_input *KeyboardController)
{
NSEvent *event;
do
{
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:nil
inMode:NSDefaultRunLoopMode
dequeue:YES];
switch(event.type)
{
case NSKeyDown:
case NSKeyUp:
{
bool32 WasDown = (event.isARepeat == YES);
bool32 IsDown = (event.type == NSKeyDown);
if (event.keyCode == HHWKey) {
MacProcessKeyboardMessage(&KeyboardController->MoveUp, IsDown);
} else if (event.keyCode == HHAKey) {
MacProcessKeyboardMessage(&KeyboardController->MoveLeft, IsDown);
} else if (event.keyCode == HHSKey) {
MacProcessKeyboardMessage(&KeyboardController->MoveDown, IsDown);
} else if (event.keyCode == HHDKey) {
MacProcessKeyboardMessage(&KeyboardController->MoveRight, IsDown);
} else if (event.keyCode == HHQKey) {
if (([event modifierFlags] & NSCommandKeyMask)) {
[NSApp sendEvent:event];
[NSApp updateWindows];
} else {
MacProcessKeyboardMessage(&KeyboardController->LeftShoulder, IsDown);
}
} else if (event.keyCode == HHEKey) {
MacProcessKeyboardMessage(&KeyboardController->RightShoulder, IsDown);
} else if (event.keyCode == HHPKey) {
if (IsDown && !WasDown) {
GlobalPause = !GlobalPause;
}
} else if (event.keyCode == HHReturnKey) {
if (IsDown && !WasDown) {
if (([event modifierFlags] & NSCommandKeyMask)) {
local_persist NSRect oldFrame;
NSWindow *window = [NSApp keyWindow];
if ((window.styleMask & NSFullScreenWindowMask) == NSFullScreenWindowMask) {
[window setStyleMask:NSResizableWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask|NSTitledWindowMask];
[window setFrame:oldFrame display:YES];
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
} else {
oldFrame = [window frame];
[window setStyleMask:NSFullScreenWindowMask];
[window setFrame:[[NSScreen mainScreen] frame] display:YES];
[NSApp setPresentationOptions:NSApplicationPresentationHideMenuBar|NSApplicationPresentationHideDock];
}
}
}
} else if (event.keyCode == HHLKey) {
NSLog(@"SaveGameState");
} else if (event.keyCode == HHUpKey) {
MacProcessKeyboardMessage(&KeyboardController->ActionUp, IsDown);
} else if (event.keyCode == HHLeftKey) {
MacProcessKeyboardMessage(&KeyboardController->ActionLeft, IsDown);
} else if (event.keyCode == HHDownKey) {
MacProcessKeyboardMessage(&KeyboardController->ActionDown, IsDown);
} else if (event.keyCode == HHRightKey) {
MacProcessKeyboardMessage(&KeyboardController->ActionRight, IsDown);
} else if (event.keyCode == HHEscKey) {
MacProcessKeyboardMessage(&KeyboardController->Start, IsDown);
} else if (event.keyCode == HHSpaceKey) {
MacProcessKeyboardMessage(&KeyboardController->Back, IsDown);
} else {
NSLog(@"KeyCode: %d", event.keyCode);
}
} break;
default:
{
[NSApp sendEvent:event];
[NSApp updateWindows];
} break;
}
} while (event != nil);
}
inline real32
MacGetSecondsElapsed(uint64 Start, uint64 End)
{
uint64 elapsed = (End - Start);
real32 Result = (real32)(elapsed * (GlobalPerfCountFrequency.numer / GlobalPerfCountFrequency.denom)) / 1000.f / 1000.f / 1000.f;
return(Result);
}
internal void
MacResizeBuffer(mac_offscreen_buffer *Buffer, int Width, int Height)
{
int BytesPerPixel = 4;
Buffer->BytesPerPixel = BytesPerPixel;
if (Buffer->Memory) {
free(Buffer->Memory);
}
Buffer->Width = Width;
Buffer->Height = Height;
Buffer->BytesPerPixel = BytesPerPixel;
Buffer->Pitch = Width*BytesPerPixel;
int BitmapMemorySize = (Buffer->Width*Buffer->Height)*BytesPerPixel;
Buffer->Memory = malloc(BitmapMemorySize);
Assert(Buffer->Memory);
}
internal mac_window_dimension
MacGetWindowDimension(NSWindow *Window)
{
mac_window_dimension Result;
Result.Height = [Window.contentView frame].size.height ;
Result.Width = [Window.contentView frame].size.width;
return(Result);
}
internal void
MacDisplayBufferInWindow(mac_offscreen_buffer *Buffer, CGContextRef DeviceContext,
int WindowWidth, int WindowHeight)
{
CGRect rect = CGRectMake(0,(WindowHeight - Buffer->Height), Buffer->Width, Buffer->Height);
if((WindowWidth >= Buffer->Width*2) &&
(WindowHeight >= Buffer->Height*2))
{
real32 WindowRatio = (real32)WindowWidth / (real32)WindowHeight;
real32 Ratio = (real32)Buffer->Width / (real32)Buffer->Height;
if (WindowRatio > Ratio) {
// Wider than tall
rect = CGRectMake(0,0, WindowHeight * Ratio, WindowHeight);
} else {
rect = CGRectMake(0,0, WindowWidth, WindowWidth / Ratio);
}
}
CGRect windowRect = CGRectMake(0,0, WindowWidth, WindowHeight);
CGContextSetRGBFillColor(DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextFillRect ( DeviceContext, windowRect );
size_t bitsPerComponent = 8;
size_t bitsPerPixel = bitsPerComponent * 4;
size_t BitmapMemorySize = (Buffer->Width * Buffer->Height * 4);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData (NULL, Buffer->Memory, BitmapMemorySize, 0);
CGImageRef image = CGImageCreate(Buffer->Width, Buffer->Height, bitsPerComponent, bitsPerPixel,
Buffer->Pitch, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, provider, 0, 0, kCGRenderingIntentDefault);
CGContextDrawImage(DeviceContext, rect, image);
CGContextFlush(DeviceContext);
CGImageRelease(image);
CGDataProviderRelease(provider);
CGColorSpaceRelease(space);
}
int main(int argc, char** argv) {
MacState = {};
mach_timebase_info(&GlobalPerfCountFrequency);
MacGetAppFileName(&MacState);
char SourceGameCodeDLLFullPath[MAC_MAX_FILENAME_SIZE];
MacBuildAppPathFileName(&MacState, "../Resources/GameCode.dylib",
sizeof(SourceGameCodeDLLFullPath), SourceGameCodeDLLFullPath);
MacBuildAppPathFileName(&MacState, "../Resources/",
sizeof(MacState.ResourcesDirectory), MacState.ResourcesDirectory);
MacState.ResourcesDirectorySize = StringLength(MacState.ResourcesDirectory);
// Set up application and window
NSApplication *application = [NSApplication sharedApplication];
[application setActivationPolicy:NSApplicationActivationPolicyRegular];
NSMenu* menubar = [NSMenu new];
NSMenuItem* appMenuItem = [NSMenuItem new];
[menubar addItem:appMenuItem];
[application setMainMenu:menubar];
NSMenu* appMenu = [NSMenu new];
NSString* appName = @"Handmade Hero";
NSString* quitTitle = [@"Quit " stringByAppendingString:appName];
NSMenuItem* quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:)
keyEquivalent:@"q"];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
int WindowWidth = 960;
int WindowHeight = 540;
MacResizeBuffer(&GlobalBackbuffer, WindowWidth, WindowHeight);
NSRect screenRect = [[NSScreen mainScreen] frame];
NSRect frame = NSMakeRect((screenRect.size.width - (real32)WindowWidth) * 0.5,
(screenRect.size.height - (real32)WindowHeight) * 0.5,
(real32)WindowWidth,
(real32)WindowHeight);
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSResizableWindowMask|NSMiniaturizableWindowMask|NSClosableWindowMask|NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setMinSize:NSMakeSize(WindowWidth, WindowHeight)];
[window setTitle:@"HandmadeHero OSX"];
[window makeKeyAndOrderFront:nil];
[application finishLaunching];
int MonitorRefreshHz = 60;
real32 GameUpdateHz = (MonitorRefreshHz / 2.0f);
real32 TargetSecondsPerFrame = 1.0f / (real32)GameUpdateHz;
uint32 BytesToWrite = 16;
// our persistent state for sound playback
mac_sound_output SoundOutput = {};
// TODO(casey): Make this like sixty seconds?
SoundOutput.SamplesPerSecond = 48000;
SoundOutput.BytesPerSample = sizeof(int16)*2;
SoundOutput.SecondaryBufferSize = SoundOutput.SamplesPerSecond*SoundOutput.BytesPerSample;
// TODO(casey): Actually compute this variance and see
// what the lowest reasonable value is.
SoundOutput.SafetyBytes = (int)(((real32)SoundOutput.SamplesPerSecond*(real32)SoundOutput.BytesPerSample / GameUpdateHz)/3.0f);
GlobalSecondaryBuffer = malloc(SoundOutput.SecondaryBufferSize);
Assert(GlobalSecondaryBuffer != MAP_FAILED);
//MacInitSound(&SoundOutput, SoundOutput.SamplesPerSecond, SoundOutput.SecondaryBufferSize);
GlobalRunning = YES;
#if 0
// NOTE(casey): This tests the PlayCursor/WriteCursor update frequency
// On the Handmade Hero machine, it was 480 samples.
while(GlobalRunning)
{
uint32 PlayCursor;
uint32 WriteCursor;
GlobalSecondaryBuffer->GetCurrentPosition(&PlayCursor, &WriteCursor);
char TextBuffer[256];
_snprintf_s(TextBuffer, sizeof(TextBuffer),
"PC:%u WC:%u\n", PlayCursor, WriteCursor);
OutputDebugStringA(TextBuffer);
}
#endif
// Allocate Memory
game_memory GameMemory = {};
GameMemory.PermanentStorageSize = Megabytes(64);
GameMemory.TransientStorageSize = Gigabytes(1);
GameMemory.DEBUGPlatformFreeFileMemory = DEBUGPlatformFreeFileMemory;
GameMemory.DEBUGPlatformReadEntireFile = DEBUGPlatformReadEntireFile;
GameMemory.DEBUGPlatformWriteEntireFile = DEBUGPlatformWriteEntireFile;
uint64 totalSize = GameMemory.PermanentStorageSize + GameMemory.TransientStorageSize;
#if HANDMADE_INTERNAL
char *BaseAddress = (char *)Terabytes(2);
GameMemory.PermanentStorage = mmap(BaseAddress, totalSize,
PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANON,
-1, 0);
Assert(GameMemory.PermanentStorage != MAP_FAILED);
#else
char *BaseAddress = malloc(totalSize);
#endif
GameMemory.TransientStorage = ((uint8*)GameMemory.PermanentStorage
+ GameMemory.PermanentStorageSize);
for(int ReplayIndex = 0;
ReplayIndex < ArrayCount(MacState.ReplayBuffers);
++ReplayIndex)
{
// mac_replay_buffer *ReplayBuffer = &MacState.ReplayBuffers[ReplayIndex];
//
// // TODO(casey): Recording system still seems to take too long
// // on record start - find out what Windows is doing and if
// // we can speed up / defer some of that processing.
//
// MacGetInputFileLocation(&MacState, false, ReplayIndex,
// sizeof(ReplayBuffer->FileName), ReplayBuffer->FileName);
//
// ReplayBuffer->FileHandle = FILE *FileHandle = fopen(ReplayBuffer->FileName, "w+");
//
// uint64 MaxSize;
// MaxSize = Win32State.TotalSize;
// ReplayBuffer->MemoryMap = CreateFileMapping(
// ReplayBuffer->FileHandle, 0, PAGE_READWRITE,
// MaxSize., MaxSize.LowPart, 0);
//
// ReplayBuffer->MemoryBlock = MapViewOfFile(
// ReplayBuffer->MemoryMap, FILE_MAP_ALL_ACCESS, 0, 0, Win32State.TotalSize);
// if(ReplayBuffer->MemoryBlock)
// {
// }
// else
// {
// // TODO(casey): Diagnostic
// }
}
if(!(GameMemory.PermanentStorage && GameMemory.TransientStorage))
{
NSLog(@"Memory not allocated correctly");
return 0;
}
game_input Input[2] = {};
game_input *NewInput = &Input[0];
game_input *OldInput = &Input[1];
uint64 LastCounter = mach_absolute_time();
uint64 FlipWallClock = mach_absolute_time();
int DebugTimeMarkerIndex = 0;
mac_debug_time_marker DebugTimeMarkers[30] = {{0}};
uint32 AudioLatencyBytes = 0;
real32 AudioLatencySeconds = 0;
bool32 SoundIsValid = false;
mac_game_code Game = MacLoadGameCode(SourceGameCodeDLLFullPath);
uint32 LoadCounter = 0;
while (GlobalRunning && ([[application windows] count] > 0))
{
@autoreleasepool {
NewInput->dtForFrame = TargetSecondsPerFrame;
time_t NewDLLWriteTime = MacGetLastWriteTime(SourceGameCodeDLLFullPath);
if(NewDLLWriteTime != Game.DLLLastWriteTime)
{
MacUnloadGameCode(&Game);
Game = MacLoadGameCode(SourceGameCodeDLLFullPath);
LoadCounter = 0;
}
game_controller_input *OldKeyboardController = GetController(OldInput, 0);
game_controller_input *NewKeyboardController = GetController(NewInput, 0);
*NewKeyboardController = {};
NewKeyboardController->IsConnected = true;
for(int ButtonIndex = 0;
ButtonIndex < ArrayCount(NewKeyboardController->Buttons);
++ButtonIndex)
{
NewKeyboardController->Buttons[ButtonIndex].EndedDown =
OldKeyboardController->Buttons[ButtonIndex].EndedDown;
}
for(int ButtonIndex = 0;
ButtonIndex < ArrayCount(NewInput->MouseButtons);
++ButtonIndex)
{
NewInput->MouseButtons[ButtonIndex].EndedDown =
OldInput->MouseButtons[ButtonIndex].EndedDown;
}
NewInput->MouseX = OldInput->MouseX;
NewInput->MouseY = OldInput->MouseY;
NewInput->MouseZ = OldInput->MouseZ;
MacProcessPendingMessages(NewKeyboardController);
if(!GlobalPause)
{
{
NSPoint MouseP = window.mouseLocationOutsideOfEventStream;
Input->MouseX = MouseP.x;
Input->MouseY = WindowHeight - MouseP.y;
Input->MouseZ = 0;
bool32 MouseDown = (CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 0));
MacProcessKeyboardMessage(&Input->MouseButtons[0],
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 0));
MacProcessKeyboardMessage(&Input->MouseButtons[1],
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 1));
MacProcessKeyboardMessage(&Input->MouseButtons[2],
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 2));
MacProcessKeyboardMessage(&Input->MouseButtons[3],
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 3));
MacProcessKeyboardMessage(&Input->MouseButtons[4],
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, 4));
// Handle Controllers
}
thread_context Thread = {};
game_offscreen_buffer Buffer = {};
Buffer.Memory = GlobalBackbuffer.Memory;
Buffer.Width = GlobalBackbuffer.Width;
Buffer.Height = GlobalBackbuffer.Height;
Buffer.Pitch = GlobalBackbuffer.Pitch;
Buffer.BytesPerPixel = GlobalBackbuffer.BytesPerPixel;
if(MacState.InputRecordingIndex)
{
// MacRecordInput(&MacState, NewInput);
}
if(MacState.InputPlayingIndex)
{
// MacPlayBackInput(&MacState, NewInput);
}
if(Game.UpdateAndRender)
{
Game.UpdateAndRender(&Thread, &GameMemory, NewInput, &Buffer);
}
if(Game.GetSoundSamples){
uint64 AudioWallClock = mach_absolute_time();
real32 FromBeginToAudioSeconds = MacGetSecondsElapsed(FlipWallClock, AudioWallClock);
/* Sound is still wrong. Poor Sound... */
uint32 ByteToLock = ((SoundOutput.RunningSampleIndex*SoundOutput.BytesPerSample) %
SoundOutput.SecondaryBufferSize);
game_sound_output_buffer SoundBuffer = {};
SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond;
SoundBuffer.SampleCount = BytesToWrite / SoundOutput.BytesPerSample;
SoundBuffer.Samples = (int16 *)GlobalSecondaryBuffer;
Game.GetSoundSamples(&Thread, &GameMemory, &SoundBuffer);
}
uint64 WorkCounter = mach_absolute_time();
real32 WorkSecondsElapsed = MacGetSecondsElapsed(LastCounter, WorkCounter);
real32 SecondsElapsedForFrame = WorkSecondsElapsed;
if(SecondsElapsedForFrame < TargetSecondsPerFrame)
{
useconds_t SleepMS = (useconds_t)(1000.0f * 1000.0f * (TargetSecondsPerFrame -
SecondsElapsedForFrame));
if(SleepMS > 0)
{
usleep(SleepMS);
}
real32 TestSecondsElapsedForFrame = MacGetSecondsElapsed(LastCounter,
mach_absolute_time());
if(TestSecondsElapsedForFrame < TargetSecondsPerFrame)
{
// TODO(casey): LOG MISSED SLEEP HERE
}
while(SecondsElapsedForFrame < TargetSecondsPerFrame)
{
SecondsElapsedForFrame = MacGetSecondsElapsed(LastCounter,
mach_absolute_time());
}
}
else
{
// TODO(casey): MISSED FRAME RATE!
// TODO(casey): Logging
}
uint64 EndCounter = mach_absolute_time();
real32 MSPerFrame = MacGetSecondsElapsed(LastCounter, EndCounter);
LastCounter = EndCounter;
mac_window_dimension Dimension = MacGetWindowDimension(window);
CGContextRef DeviceContext = (CGContextRef)window.graphicsContext.graphicsPort;
MacDisplayBufferInWindow(&GlobalBackbuffer, DeviceContext,
Dimension.Width, Dimension.Height);
FlipWallClock = mach_absolute_time();
#if HANDMADE_INTERNAL
{
// NOTE(casey): This is debug code
// DWORD PlayCursor;
// DWORD WriteCursor;
// if(GlobalSecondaryBuffer->GetCurrentPosition(&PlayCursor, &WriteCursor) == DS_OK)
// {
// Assert(DebugTimeMarkerIndex < ArrayCount(DebugTimeMarkers));
// win32_debug_time_marker *Marker = &DebugTimeMarkers[DebugTimeMarkerIndex];
// Marker->FlipPlayCursor = PlayCursor;
// Marker->FlipWriteCursor = WriteCursor;
// }
}
#endif
game_input *Temp = NewInput;
NewInput = OldInput;
OldInput = Temp;
#if 0
real64 FPS = 0.0f;
NSLog(@"%.03fms/f, %.02ff/s\n", MSPerFrame, FPS);
#endif
#if HANDMADE_INTERNAL
++DebugTimeMarkerIndex;
if(DebugTimeMarkerIndex == ArrayCount(DebugTimeMarkers))
{
DebugTimeMarkerIndex = 0;
}
#endif
}
}
}
return 0;
}