-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCodeScrap.txt
1204 lines (827 loc) · 34.6 KB
/
CodeScrap.txt
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
This text file contains "scrap code" that we aren't using anymore, but don't want to delete forever just yet.
============================
============================
void BlitStringToBuffer(_In_ char* String, _In_ GAMEBITMAP* FontSheet, _In_ PIXEL32* Color, _In_ uint16_t x, _In_ uint16_t y)
{
// Map any char value to an offset dictated by the g6x7Font ordering.
// 0xab and 0xbb are extended ASCII characters that look like double angle brackets.
// We use them as a cursor in menus.
static int32_t FontCharacterPixelOffset[] = {
// .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,
// ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
94,64,87,66,67,68,70,85,72,73,71,77,88,74,91,92,52,53,54,55,56,57,58,59,60,61,86,84,89,75,90,93,
// @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
65,0, 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,80,78,81,69,76,
// ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ..
62,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,82,79,83,63,93,
// .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,
// .. .. .. .. .. .. .. .. .. .. .. bb .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ab .. .. .. ..
93,93,93,93,93,93,93,93,93,93,93,96,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,95,93,93,93,93,
// .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,
// .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. F2 .. .. .. .. .. .. .. .. .. .. .. .. ..
93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,93,97,93,93,93,93,93,93,93,93,93,93,93,93,93
};
uint16_t CharWidth = (uint16_t)FontSheet->BitmapInfo.bmiHeader.biWidth / FONT_SHEET_CHARACTERS_PER_ROW;
uint16_t CharHeight = (uint16_t)FontSheet->BitmapInfo.bmiHeader.biHeight;
uint16_t BytesPerCharacter = (CharWidth * CharHeight * (FontSheet->BitmapInfo.bmiHeader.biBitCount / 8));
uint16_t StringLength = (uint16_t)strlen(String);
GAMEBITMAP StringBitmap = { 0 };
StringBitmap.BitmapInfo.bmiHeader.biBitCount = GAME_BPP;
StringBitmap.BitmapInfo.bmiHeader.biHeight = CharHeight;
StringBitmap.BitmapInfo.bmiHeader.biWidth = CharWidth * StringLength;
StringBitmap.BitmapInfo.bmiHeader.biPlanes = 1;
StringBitmap.BitmapInfo.bmiHeader.biCompression = BI_RGB;
StringBitmap.Memory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((size_t)BytesPerCharacter * (size_t)StringLength));
for (int Character = 0; Character < StringLength; Character++)
{
int StartingFontSheetPixel = 0;
int FontSheetOffset = 0;
int StringBitmapOffset = 0;
PIXEL32 FontSheetPixel = { 0 };
StartingFontSheetPixel = (FontSheet->BitmapInfo.bmiHeader.biWidth * FontSheet->BitmapInfo.bmiHeader.biHeight) - \
FontSheet->BitmapInfo.bmiHeader.biWidth + (CharWidth * FontCharacterPixelOffset[(uint8_t)String[Character]]);
for (int YPixel = 0; YPixel < CharHeight; YPixel++)
{
for (int XPixel = 0; XPixel < CharWidth; XPixel++)
{
FontSheetOffset = StartingFontSheetPixel + XPixel - (FontSheet->BitmapInfo.bmiHeader.biWidth * YPixel);
StringBitmapOffset = (Character * CharWidth) + ((StringBitmap.BitmapInfo.bmiHeader.biWidth * StringBitmap.BitmapInfo.bmiHeader.biHeight) - \
StringBitmap.BitmapInfo.bmiHeader.biWidth) + XPixel - (StringBitmap.BitmapInfo.bmiHeader.biWidth) * YPixel;
// NOTE: memcpy_s is safer but is much slower.
//memcpy_s(&FontSheetPixel, sizeof(PIXEL32), (PIXEL32*)FontSheet->Memory + FontSheetOffset, sizeof(PIXEL32));
memcpy(&FontSheetPixel, (PIXEL32*)FontSheet->Memory + FontSheetOffset, sizeof(PIXEL32));
if (FontSheetPixel.Colors.Alpha == 255)
{
FontSheetPixel.Colors.Red = Color->Colors.Red;
FontSheetPixel.Colors.Green = Color->Colors.Green;
FontSheetPixel.Colors.Blue = Color->Colors.Blue;
// NOTE: memcpy_s is safer but is much slower.
//memcpy_s((PIXEL32*)StringBitmap.Memory + StringBitmapOffset, sizeof(PIXEL32), &FontSheetPixel, sizeof(PIXEL32));
memcpy((PIXEL32*)StringBitmap.Memory + StringBitmapOffset, &FontSheetPixel, sizeof(PIXEL32));
}
}
}
}
Blit32BppBitmapToBuffer(&StringBitmap, x, y, 0);
if (StringBitmap.Memory)
{
HeapFree(GetProcessHeap(), 0, StringBitmap.Memory);
}
}
// This function draws any sized bitmap onto the global backbuffer. Sprites, text strings, etc.
// WARNING: Currently there is no safeguard preventing you from trying to draw pixels outside
// of the screen area, and attempting to do so will crash the game if the area to be
// drawn to falls outside of valid gBackBuffer memory!
void Blit32BppBitmapToBuffer(_In_ GAMEBITMAP* GameBitmap, _In_ int16_t x, _In_ int16_t y, _In_ int16_t BrightnessAdjustment)
{
int32_t StartingScreenPixel = ((GAME_RES_WIDTH * GAME_RES_HEIGHT) - GAME_RES_WIDTH) - (GAME_RES_WIDTH * y) + x;
int32_t StartingBitmapPixel = ((GameBitmap->BitmapInfo.bmiHeader.biWidth * GameBitmap->BitmapInfo.bmiHeader.biHeight) - \
GameBitmap->BitmapInfo.bmiHeader.biWidth);
int32_t MemoryOffset = 0;
int32_t BitmapOffset = 0;
PIXEL32 BitmapPixel = { 0 };
#ifdef __AVX2__
// We go 8 pixels at a time SIMD-style, until there are fewer than 8 pixels left
// on the current row, then finish the remainder of the row one pixel at a time.
__m256i BitmapOctoPixel;
for (int16_t YPixel = 0; YPixel < GameBitmap->BitmapInfo.bmiHeader.biHeight; YPixel++)
{
int16_t PixelsRemainingOnThisRow = (int16_t)GameBitmap->BitmapInfo.bmiHeader.biWidth;
int16_t XPixel = 0;
while (PixelsRemainingOnThisRow >= 8)
{
MemoryOffset = StartingScreenPixel + XPixel - (GAME_RES_WIDTH * YPixel);
BitmapOffset = StartingBitmapPixel + XPixel - (GameBitmap->BitmapInfo.bmiHeader.biWidth * YPixel);
// Load 256 bits (8 pixels) from memory into register YMMx
// WARNING: The buffer MUST be 32-byte aligned or else this intrinsic will crash the program!
// If unable to guarantee 32-byte alignment, use _mm256_loadu_si256 instead.
BitmapOctoPixel = _mm256_load_si256((const __m256i*)((PIXEL32*)GameBitmap->Memory + BitmapOffset));
// AARRGGBBAARRGGBB-AARRGGBBAARRGGBB-AARRGGBBAARRGGBB-AARRGGBBAARRGGBB
// YMM0 = FF5B6EE1FF5B6EE1-FF5B6EE1FF5B6EE1-FF5B6EE1FF5B6EE1-FF5B6EE1FF5B6EE1
// Blow the 256-bit vector apart into two separate 256-bit vectors Half1 and Half2,
// each containing 4 pixels, where each pixel is now 16 bits instead of 8.
__m256i Half1 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256(BitmapOctoPixel, 0));
// AAAARRRRGGGGBBBB-AAAARRRRGGGGBBBB-AAAARRRRGGGGBBBB-AAAARRRRGGGGBBBB
// YMM0 = 00FF005B006E00E1-00FF005B006E00E1-00FF005B006E00E1-00FF005B006E00E1
// Add the brightness adjustment to each 16-bit element, except alpha.
Half1 = _mm256_add_epi16(Half1, _mm256_set_epi16(
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment));
// Do the same for Half2 that we just did for Half1.
__m256i Half2 = _mm256_cvtepu8_epi16(_mm256_extracti128_si256(BitmapOctoPixel, 1));
Half2 = _mm256_add_epi16(Half2, _mm256_set_epi16(
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment,
0, BrightnessAdjustment, BrightnessAdjustment, BrightnessAdjustment));
// Now we need to reassemble the two halves back into a single 256-bit group of 8 pixels.
// _mm256_packus_epi16(a,b) takes the 16-bit signed integers in the 256-bit vectors a and b
// and converts them to a 256-bit vector of 8-bit unsigned integers. The result contains the
// first 8 integers from a, followed by the first 8 integers from b, followed by the last 8
// integers from a, followed by the last 8 integers from b.
// Values that are out of range are set to 0 or 255.
__m256i Recombined = _mm256_packus_epi16(Half1, Half2);
BitmapOctoPixel = _mm256_permute4x64_epi64(Recombined, _MM_SHUFFLE(3, 1, 2, 0));
// Create a mask that selects only the pixels that have an Alpha == 255.
__m256i Mask = _mm256_cmpeq_epi8(BitmapOctoPixel, _mm256_set1_epi8(-1));
// Conditionally store the result to the global back buffer, based on the mask
// we just created that selects only the pixels where Alpha == 255.
_mm256_maskstore_epi32((int*)((PIXEL32*)gBackBuffer.Memory + MemoryOffset), Mask, BitmapOctoPixel);
PixelsRemainingOnThisRow -= 8;
XPixel += 8;
}
while (PixelsRemainingOnThisRow > 0)
{
MemoryOffset = StartingScreenPixel + XPixel - (GAME_RES_WIDTH * YPixel);
BitmapOffset = StartingBitmapPixel + XPixel - (GameBitmap->BitmapInfo.bmiHeader.biWidth * YPixel);
memcpy_s(&BitmapPixel, sizeof(PIXEL32), (PIXEL32*)GameBitmap->Memory + BitmapOffset, sizeof(PIXEL32));
if (BitmapPixel.Colors.Alpha == 255)
{
// Clamp between 0 and 255
// min(upper, max(x, lower))
BitmapPixel.Colors.Red = (uint8_t)min(255, max((BitmapPixel.Colors.Red + BrightnessAdjustment), 0));
BitmapPixel.Colors.Green = (uint8_t)min(255, max((BitmapPixel.Colors.Green + BrightnessAdjustment), 0));
BitmapPixel.Colors.Blue = (uint8_t)min(255, max((BitmapPixel.Colors.Blue + BrightnessAdjustment), 0));
memcpy_s((PIXEL32*)gBackBuffer.Memory + MemoryOffset, sizeof(PIXEL32), &BitmapPixel, sizeof(PIXEL32));
}
PixelsRemainingOnThisRow--;
XPixel++;
}
}
#else
for (int16_t YPixel = 0; YPixel < GameBitmap->BitmapInfo.bmiHeader.biHeight; YPixel++)
{
for (int16_t XPixel = 0; XPixel < GameBitmap->BitmapInfo.bmiHeader.biWidth; XPixel++)
{
MemoryOffset = StartingScreenPixel + XPixel - (GAME_RES_WIDTH * YPixel);
BitmapOffset = StartingBitmapPixel + XPixel - (GameBitmap->BitmapInfo.bmiHeader.biWidth * YPixel);
memcpy_s(&BitmapPixel, sizeof(PIXEL32), (PIXEL32*)GameBitmap->Memory + BitmapOffset, sizeof(PIXEL32));
if (BitmapPixel.Colors.Alpha == 255)
{
// Clamp between 0 and 255
// min(upper, max(x, lower))
BitmapPixel.Colors.Red = (uint8_t)min(255, max((BitmapPixel.Colors.Red + BrightnessAdjustment), 0));
BitmapPixel.Colors.Green = (uint8_t)min(255, max((BitmapPixel.Colors.Green + BrightnessAdjustment), 0));
BitmapPixel.Colors.Blue = (uint8_t)min(255, max((BitmapPixel.Colors.Blue + BrightnessAdjustment), 0));
memcpy_s((PIXEL32*)gBackBuffer.Memory + MemoryOffset, sizeof(PIXEL32), &BitmapPixel, sizeof(PIXEL32));
}
}
}
#endif
}
==============================================
==============================================
// We no longer need these functions, because these functions load individual BMPX or WAV or OGG files from disk.
// However we no longer do that. Now we are decompressing these files from a zip archive.
DWORD Load32BppBitmapFromFile(_In_ char* FileName, _Inout_ GAMEBITMAP* GameBitmap);
DWORD LoadWavFromFile(_In_ char* FileName, _Inout_ GAMESOUND* GameSound);
DWORD LoadTilemapFromFile(_In_ char* FileName, _Inout_ TILEMAP* TileMap);
DWORD LoadOggFromFile(_In_ char* FileName, _Inout_ GAMESOUND* GameSound);
DWORD Load32BppBitmapFromFile(_In_ char* FileName, _Inout_ GAMEBITMAP* GameBitmap)
{
DWORD Error = ERROR_SUCCESS;
HANDLE FileHandle = INVALID_HANDLE_VALUE;
WORD BitmapHeader = 0;
DWORD PixelDataOffset = 0;
DWORD NumberOfBytesRead = 2;
if ((FileHandle = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
goto Exit;
}
if (ReadFile(FileHandle, &BitmapHeader, 2, &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
goto Exit;
}
if (BitmapHeader != 0x4d42) // "BM" backwards
{
Error = ERROR_FILE_INVALID;
goto Exit;
}
if (SetFilePointer(FileHandle, 0xA, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
goto Exit;
}
if (ReadFile(FileHandle, &PixelDataOffset, sizeof(DWORD), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
goto Exit;
}
if (SetFilePointer(FileHandle, 0xE, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
goto Exit;
}
if (ReadFile(FileHandle, &GameBitmap->BitmapInfo.bmiHeader, sizeof(BITMAPINFOHEADER), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
goto Exit;
}
if ((GameBitmap->Memory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, GameBitmap->BitmapInfo.bmiHeader.biSizeImage)) == NULL)
{
Error = ERROR_NOT_ENOUGH_MEMORY;
goto Exit;
}
if (SetFilePointer(FileHandle, PixelDataOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
goto Exit;
}
if (ReadFile(FileHandle, GameBitmap->Memory, GameBitmap->BitmapInfo.bmiHeader.biSizeImage, &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
goto Exit;
}
Exit:
if (FileHandle && (FileHandle != INVALID_HANDLE_VALUE))
{
CloseHandle(FileHandle);
}
if (Error == ERROR_SUCCESS)
{
LogMessageA(LL_INFO, "[%s] Loading successful: %s", __FUNCTION__, FileName);
}
else
{
LogMessageA(LL_ERROR, "[%s] Loading failed: %s! Error 0x%08lx!", __FUNCTION__, FileName, Error);
}
return(Error);
}
// Loads and decodes an Ogg Vorbis music file using Sean Barrett's stb_vorbis.c
DWORD LoadOggFromFile(_In_ char* FileName, _Inout_ GAMESOUND* GameSound)
{
DWORD Error = ERROR_SUCCESS;
HANDLE FileHandle = INVALID_HANDLE_VALUE;
LARGE_INTEGER FileSize = { 0 };
DWORD BytesRead = 0;
void* FileBuffer = NULL;
int SamplesDecoded = 0;
int Channels = 0;
int SampleRate = 0;
short* DecodedAudio = NULL;
if ((FileHandle = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] CreateFileA failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (GetFileSizeEx(FileHandle, &FileSize) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] GetFileSizeEx failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
LogMessageA(LL_INFO, "[%s] Size of file %s is %lu bytes.", __FUNCTION__, FileName, FileSize.QuadPart);
FileBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FileSize.QuadPart);
if (FileBuffer == NULL)
{
Error = ERROR_OUTOFMEMORY;
LogMessageA(LL_ERROR, "[%s] HeapAlloc failed with 0x%08lx on %s!", __FUNCTION__, Error, FileName);
goto Exit;
}
if (ReadFile(FileHandle, FileBuffer, (DWORD)FileSize.QuadPart, &BytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx on %s!", __FUNCTION__, Error, FileName);
goto Exit;
}
SamplesDecoded = stb_vorbis_decode_memory(FileBuffer, (int)FileSize.QuadPart, &Channels, &SampleRate, &DecodedAudio);
if (SamplesDecoded < 1)
{
Error = ERROR_BAD_COMPRESSION_BUFFER;
LogMessageA(LL_ERROR, "[%s] stb_vorbis_decode_memory failed with 0x%08lx on %s!", __FUNCTION__, Error, FileName);
goto Exit;
}
GameSound->WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
GameSound->WaveFormat.nChannels = (WORD)Channels;
GameSound->WaveFormat.nSamplesPerSec = SampleRate;
GameSound->WaveFormat.nAvgBytesPerSec = GameSound->WaveFormat.nSamplesPerSec * GameSound->WaveFormat.nChannels * 2;
GameSound->WaveFormat.nBlockAlign = GameSound->WaveFormat.nChannels * 2;
GameSound->WaveFormat.wBitsPerSample = 16;
GameSound->Buffer.Flags = XAUDIO2_END_OF_STREAM;
GameSound->Buffer.AudioBytes = SamplesDecoded * GameSound->WaveFormat.nChannels * 2;
GameSound->Buffer.pAudioData = (const BYTE*)DecodedAudio;
Exit:
if (FileHandle && (FileHandle != INVALID_HANDLE_VALUE))
{
CloseHandle(FileHandle);
}
if (FileBuffer)
{
HeapFree(GetProcessHeap(), 0, FileBuffer);
}
return(Error);
}
DWORD LoadTilemapFromFile(_In_ char* FileName, _Inout_ TILEMAP* TileMap)
{
DWORD Error = ERROR_SUCCESS;
HANDLE FileHandle = INVALID_HANDLE_VALUE;
LARGE_INTEGER FileSize = { 0 };
DWORD BytesRead = 0;
void* FileBuffer = NULL;
char* Cursor = NULL;
char TempBuffer[16] = { 0 };
uint16_t Rows = 0;
uint16_t Columns = 0;
if ((FileHandle = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] CreateFileA failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (GetFileSizeEx(FileHandle, &FileSize) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] GetFileSizeEx failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
LogMessageA(LL_INFO, "[%s] Size of file %s is %lu bytes.", __FUNCTION__, FileName, FileSize.QuadPart);
if (FileSize.QuadPart < 300)
{
Error = ERROR_FILE_INVALID;
LogMessageA(LL_ERROR, "[%s] File %s is too small to be a valid tile map! 0x%08lx!", __FUNCTION__, FileName, Error);
goto Exit;
}
FileBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FileSize.QuadPart);
if (FileBuffer == NULL)
{
Error = ERROR_OUTOFMEMORY;
LogMessageA(LL_ERROR, "[%s] HeapAlloc failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, FileBuffer, (DWORD)FileSize.QuadPart, &BytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if ((Cursor = strstr(FileBuffer, "width=")) == NULL)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not locate the width attribute! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
BytesRead = 0;
for (;;)
{
if (BytesRead > 8)
{
// We should have found the opening quotation mark by now.
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not locate the opening quotation mark before the width attribute! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (*Cursor == '\"')
{
Cursor++;
break;
}
else
{
Cursor++;
}
BytesRead++;
}
BytesRead = 0;
for (uint8_t Counter = 0; Counter < 6; Counter++)
{
if (*Cursor == '\"')
{
Cursor++;
break;
}
else
{
TempBuffer[Counter] = *Cursor;
Cursor++;
}
}
TileMap->Width = (uint16_t)atoi(TempBuffer);
if (TileMap->Width == 0)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Width attribute was 0! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
memset(TempBuffer, 0, sizeof(TempBuffer));
if ((Cursor = strstr(FileBuffer, "height=")) == NULL)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not locate the height attribute! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
BytesRead = 0;
for (;;)
{
if (BytesRead > 8)
{
// We should have found the opening quotation mark by now.
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not locate the opening quotation mark before the height attribute! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (*Cursor == '\"')
{
Cursor++;
break;
}
else
{
Cursor++;
}
BytesRead++;
}
BytesRead = 0;
for (uint8_t Counter = 0; Counter < 6; Counter++)
{
if (*Cursor == '\"')
{
Cursor++;
break;
}
else
{
TempBuffer[Counter] = *Cursor;
Cursor++;
}
}
TileMap->Height = (uint16_t)atoi(TempBuffer);
if (TileMap->Height == 0)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Height attribute was 0! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
LogMessageA(LL_INFO, "[%s] %s TileMap dimensions: %dx%d.", __FUNCTION__, FileName, TileMap->Width, TileMap->Height);
Rows = TileMap->Height;
Columns = TileMap->Width;
TileMap->Map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Rows * sizeof(void*));
if (TileMap->Map == NULL)
{
Error = ERROR_OUTOFMEMORY;
LogMessageA(LL_ERROR, "[%s] HeapAlloc failed! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
for (uint16_t Counter = 0; Counter < TileMap->Height; Counter++)
{
TileMap->Map[Counter] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Columns * sizeof(void*));
if (TileMap->Map[Counter] == NULL)
{
Error = ERROR_OUTOFMEMORY;
LogMessageA(LL_ERROR, "[%s] HeapAlloc failed! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
}
BytesRead = 0;
memset(TempBuffer, 0, sizeof(TempBuffer));
if ((Cursor = strstr(FileBuffer, ",")) == NULL)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not find a comma character in the file %s! 0x%08lx!", __FUNCTION__, FileName, Error);
goto Exit;
}
while (*Cursor != '\r' && *Cursor != '\n')
{
if (BytesRead > 4)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] Could not find a new line character at the beginning of the tile map data in the file %s! 0x%08lx!", __FUNCTION__, FileName, Error);
goto Exit;
}
BytesRead++;
Cursor--;
}
Cursor++;
for (uint16_t Row = 0; Row < Rows; Row++)
{
for (uint16_t Column = 0; Column < Columns; Column++)
{
memset(TempBuffer, 0, sizeof(TempBuffer));
Skip:
if (*Cursor == '\r' || *Cursor == '\n')
{
Cursor++;
goto Skip;
}
for (uint8_t Counter = 0; Counter < 8; Counter++)
{
if (*Cursor == ',' || *Cursor == '<')
{
if (((TileMap->Map[Row][Column]) = (uint8_t)atoi(TempBuffer)) == 0)
{
Error = ERROR_INVALID_DATA;
LogMessageA(LL_ERROR, "[%s] atoi failed while converting tile map data in the file %s! 0x%08lx!", __FUNCTION__, FileName, Error);
goto Exit;
}
Cursor++;
break;
}
TempBuffer[Counter] = *Cursor;
Cursor++;
}
}
}
Exit:
if (FileHandle && (FileHandle != INVALID_HANDLE_VALUE))
{
CloseHandle(FileHandle);
}
if (FileBuffer)
{
HeapFree(GetProcessHeap(), 0, FileBuffer);
}
return(Error);
}
DWORD LoadWavFromFile(_In_ char* FileName, _Inout_ GAMESOUND* GameSound)
{
DWORD Error = ERROR_SUCCESS;
DWORD NumberOfBytesRead = 0;
DWORD RIFF = 0;
uint16_t DataChunkOffset = 0;
DWORD DataChunkSearcher = 0;
BOOL DataChunkFound = FALSE;
DWORD DataChunkSize = 0;
HANDLE FileHandle = INVALID_HANDLE_VALUE;
void* AudioData = NULL;
if ((FileHandle = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] CreateFileA failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, &RIFF, sizeof(DWORD), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (RIFF != 0x46464952) // "RIFF" backwards
{
Error = ERROR_FILE_INVALID;
LogMessageA(LL_ERROR, "[%s] First four bytes of this file are not 'RIFF'!", __FUNCTION__, Error);
goto Exit;
}
if (SetFilePointer(FileHandle, 20, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] SetFilePointer failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, &GameSound->WaveFormat, sizeof(WAVEFORMATEX), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (GameSound->WaveFormat.nBlockAlign != ((GameSound->WaveFormat.nChannels * GameSound->WaveFormat.wBitsPerSample) / 8) ||
(GameSound->WaveFormat.wFormatTag != WAVE_FORMAT_PCM) ||
(GameSound->WaveFormat.wBitsPerSample != 16))
{
Error = ERROR_DATATYPE_MISMATCH;
LogMessageA(LL_ERROR, "[%s] This wav file did not meet the format requirements! Only PCM format, 44.1KHz, 16 bits per sample wav files are supported. 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
while (DataChunkFound == FALSE)
{
if (SetFilePointer(FileHandle, DataChunkOffset, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] SetFilePointer failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, &DataChunkSearcher, sizeof(DWORD), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (DataChunkSearcher == 0x61746164) // 'data', backwards
{
DataChunkFound = TRUE;
break;
}
else
{
DataChunkOffset += 4;
}
if (DataChunkOffset > 256)
{
Error = ERROR_DATATYPE_MISMATCH;
LogMessageA(LL_ERROR, "[%s] Data chunk not found within first 256 bytes of this file! 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
}
if (SetFilePointer(FileHandle, DataChunkOffset + 4, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] SetFilePointer failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, &DataChunkSize, sizeof(DWORD), &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
AudioData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, DataChunkSize);
if (AudioData == NULL)
{
Error = ERROR_NOT_ENOUGH_MEMORY;
LogMessageA(LL_ERROR, "[%s] HeapAlloc failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
GameSound->Buffer.Flags = XAUDIO2_END_OF_STREAM;
GameSound->Buffer.AudioBytes = DataChunkSize;
if (SetFilePointer(FileHandle, DataChunkOffset + 8, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] SetFilePointer failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
if (ReadFile(FileHandle, AudioData, DataChunkSize, &NumberOfBytesRead, NULL) == 0)
{
Error = GetLastError();
LogMessageA(LL_ERROR, "[%s] ReadFile failed with 0x%08lx!", __FUNCTION__, Error);
goto Exit;
}
GameSound->Buffer.pAudioData = AudioData;
Exit:
if (Error == ERROR_SUCCESS)
{
LogMessageA(LL_INFO, "[%s] Successfully loaded %s.", __FUNCTION__, FileName);
}
else
{
LogMessageA(LL_ERROR, "[%s] Failed to load %s! Error: 0x%08lx!", __FUNCTION__, FileName, Error);
}
if (FileHandle && (FileHandle != INVALID_HANDLE_VALUE))
{
CloseHandle(FileHandle);
}
return(Error);
}
///
///
/// No longer need these functions because they are the same as rep stosd, etc.
///
///
#ifdef AVX
__forceinline void ClearScreen(_In_ __m256i* Color)