-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
2725 lines (2556 loc) · 126 KB
/
code.cpp
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
//One Lone Coder's command prompt engine for reference
//https://www.youtube.com/c/javidx9
//https://github.com/OneLoneCoder/videos/blob/master/olcConsoleGameEngine.h
//https://github.com/OneLoneCoder/CommandLineFPS/blob/master/CommandLineFPS.cpp
#include <Windows.h> //https://docs.microsoft.com/en-us/previous-versions//aa383686(v=vs.85)
#include <chrono>
#include <map>
#define PI 3.14159265f
#define RAD 0.01745329f
#define DEG 57.29577951f
#define FPS60 0.01666666f
#define FPS30 0.03333333f
#define insertws swprintf
#define formatws swprintf
#define appendVector vector.insert(vector.end(), addedVector.begin(), addedVector.end())
#define RALT 0xF001
#define LALT 0xF002
#define RCTRL 0xF003
#define LCTRL 0xF004
#define SHIFT 0xF005
#define MOUSE_DCLICK 0xF009
#define MOUSE_SCROLL 0xF010
#define MOUSE_BTN 0xF011
#define MOUSE_X 0xF058
#define MOUSE_Y 0xF059
//First 4 bits (0x!000)
// (4-7 & C-F) Background takes priority
// (0-3 & 8-B) Foreground takes priority
//Second 4 bits (0x0!00)
// (1,3,5,7,9,B,D,F) Dithering
// (0,2,4,6,8,A,C,E) Plain color
//Third 4 bits (0x00!0)
// (0-F) Background color
//Last 4 bits (0x000!)
// (0-F) Foreground color
//Colors
// 0 Black
// 1 Dark Blue
// 2 Dark Green
// 3 Dark Cyan
// 4 Dark Red
// 5 Dark Purple
// 6 Gold
// 7 Light Grey
// 8 Grey
// 9 Blue
// A Green
// B Cyan
// C Red
// D Purple
// E Yellow
// F White
namespace cmde
{
struct VEC4F
{
float x;
float y;
float z;
float w;
VEC4F() { x = y = z = w = 0; }
VEC4F(float x, float y, float z, float w)
{
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
VEC4F operator* (const float& a) { return VEC4F(x * a, y * a, z * a, w * a); }
VEC4F operator* (const double& a) { return *this * (float)a; }
VEC4F operator* (const int& a) { return *this * (float)a; }
VEC4F operator/ (const float& a) { return VEC4F(x / a, y / a, z / a, w / a); }
VEC4F operator* (const VEC4F& a) { return VEC4F(x * a.x, y * a.y, z * a.z, w * a.w); }
VEC4F operator+ (const VEC4F& a) { return VEC4F(x + a.x, y + a.y, z + a.z, w + a.w); }
VEC4F operator- (const VEC4F& a) { return VEC4F(x - a.x, y - a.y, z - a.z, w - a.w); }
bool operator== (const VEC4F& a) { return (x == a.x && y == a.y && z == a.z && w == a.w); }
};
struct VEC3F : public VEC4F
{
VEC3F() { x = y = z = w = 0; }
VEC3F(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
w = 0;
}
VEC3F(VEC4F v) { x = v.x; y = v.y; z = v.z; w = 0; }
};
struct VEC2F : public VEC4F
{
VEC2F() { x = y = z = w = 0; }
VEC2F(float x, float y)
{
this->x = x;
this->y = y;
z = 0;
w = 0;
}
VEC2F(VEC4F v) { x = v.x; y = v.y; z = 0; w = 0; }
};
struct FUNC
{
virtual VEC4F f(float x) = 0;
virtual VEC4F derivative(float x) = 0;
};
struct LINEAR : public FUNC
{
VEC4F a;
VEC4F b;
LINEAR() { a = b = VEC4F(); }
LINEAR(VEC4F a, VEC4F b)
{
this->a = a;
this->b = b;
}
VEC4F f(float x)
{
return a * x + b;
}
VEC4F derivative(float x)
{
return a;
}
};
struct QUADRATIC : public FUNC
{
VEC4F a;
VEC4F b;
VEC4F c;
QUADRATIC() { a = b = c = VEC4F(); }
QUADRATIC(VEC4F a, VEC4F b, VEC4F c)
{
this->a = a;
this->b = b;
this->c = c;
}
VEC4F f(float x)
{
return a * x * x + b * x + c;
}
VEC4F derivative(float x)
{
return a * 2 * x + b;
}
};
struct FILE
{
OPENFILENAMEW fileName = { 0 };
HANDLE fileHandle = { 0 };
static const unsigned long CHUNK_SIZE = 4096; //4kb
wchar_t name[128] = { 0 };
wchar_t extension[128] = { 0 };
FILE(){}
/// <summary>
/// Reads this file, splits it into lines, and then passes it to the output's processing function. (T's class should be a child of PROCESSEDFILE)
/// </summary>
/// <param name="output">Some type of PROCESSEDFILE where the data read will be stored afterwards. To define UTF encoding override 'static const bool utf16' from PROCESSEDFILE (true -> UTF-16; false -> UTF-8)</param>
template <class T>
bool ProcessFile(T* output)
{
LARGE_INTEGER fileSize = { 0 };
if (!GetFileSizeEx(fileHandle, &fileSize))
return false;
byte buffer[CHUNK_SIZE] = { 0 };
wchar_t lineBuffer[CHUNK_SIZE] = { 0 };
unsigned long lineBufferPos = 0; //The position in lineBuffer where the last line stopped
DWORD readCount = 0;
long tempHigh = fileSize.HighPart;
unsigned long tempLow = fileSize.LowPart;
unsigned long counter = 0;
while (tempHigh > 0 || tempLow > CHUNK_SIZE)
{
if (counter < (ULONG_MAX - CHUNK_SIZE) + 1 || //counter + CHUNK_SIZE can fit within a ulong
tempHigh > 1 || //There's at least 2 * ULONG_MAX bytes (Meaning even if counter overflows into 1 high part, there's still enough extra)
tempLow > CHUNK_SIZE || //There are enough bytes in the low part to cover an entire chunk (For when the high parts run out and start counting down on the low part)
(tempHigh == 1 && CHUNK_SIZE > ((ULONG_MAX - counter) + 1) && tempLow > CHUNK_SIZE - ((ULONG_MAX - counter) + 1)) //The amount of bytes in the low part are enough to hold the overflow
)
{
//There are CHUNK_SIZE bytes remaining
//Reads CHUNK_SIZE at a time
if (ReadFile(fileHandle, &buffer, CHUNK_SIZE, &readCount, NULL))
{
if (!output->utf16)
{
//UTF-8
for (DWORD i = 0; i < readCount; i++)
{
//For every byte that was read, convert it into a wchar and store it at the end of what is currently stored in lineBuffer
lineBuffer[lineBufferPos] = (wchar_t)buffer[i];
lineBufferPos++;
if (lineBuffer[lineBufferPos - 1] == L'\n')
{
//If reached a newline character, lineBuffer now has a full line, and store where that line ends in the wbuffer
output->ProcessLine(lineBuffer, lineBufferPos);
lineBufferPos = 0;
}
if (lineBufferPos >= CHUNK_SIZE)
{
//If the line that is being read is longer than the current CHUNK_SIZE can allow
output = new T();
return false;
}
}
}
else
{
//UTF-16
char wcharBytes[3] = { 0 };
wchar_t wchar[2] = { 0 };
for (DWORD i = 0; i < readCount; i += 2)
{
//For every byte that was read, convert it into a wchar and store it at the end of what is currently stored in lineBuffer
wcharBytes[0] = buffer[i];
wcharBytes[1] = buffer[i + 1];
MultiByteToWideChar(CP_UTF8, 0, wcharBytes, 3, wchar, 2);
lineBuffer[lineBufferPos] = wchar[0];
lineBufferPos++;
if (lineBuffer[lineBufferPos - 1] == L'\n')
{
//If reached a newline character, lineBuffer now has a full line, and store where that line ends in the wbuffer
output->ProcessLine(lineBuffer, lineBufferPos);
lineBufferPos = 0;
}
if (lineBufferPos >= CHUNK_SIZE)
{
//If the line that is being read is longer than the current CHUNK_SIZE can allow
output = new T();
return false;
}
}
}
}
else
{
output = new T();
return false;
}
if (counter < (ULONG_MAX - CHUNK_SIZE) + 1)
{
//If CHUNK_SIZE can be added to counter without overflow
counter += CHUNK_SIZE;
if (tempHigh == 0)
{
//If there're no more high parts
tempLow -= CHUNK_SIZE; //Count down on tempLow the amount of bytes left
}
}
else
{
//If CHUNK_SIZE would make counter overflow
tempHigh--;
counter = CHUNK_SIZE - ((ULONG_MAX - counter) + 1); //Subtracting the leftover bytes remaining in the high part from the ones to be added
if (tempHigh == 0)
{
//If there're no more high parts to take care of the overflow
tempLow -= counter; //Start countdown of bytes left on tempLow by removing the one's taken from the last high part by counter
}
}
}
else
{
//CHUNK_SIZE would go over the file's size
if (tempHigh > 0)
{
//If there're still bytes remaining in a high part
tempLow += (ULONG_MAX - counter) + 1; //Adding the leftover bytes remaining in the high part
}
break;
}
}
//tempLow bytes remain unread in the file (Less than CHUNK_SIZE)
if (ReadFile(fileHandle, &buffer, tempLow, &readCount, NULL))
{
if (!output->utf16)
{
//UTF-8
for (DWORD i = 0; i < readCount; i++)
{
//For every byte that was read, convert it into a wchar and store it at the end of what is currently stored in lineBuffer
lineBuffer[lineBufferPos] = (wchar_t)buffer[i];
lineBufferPos++;
if (lineBuffer[lineBufferPos - 1] == L'\n')
{
//If reached a newline character, lineBuffer now has a full line, and store where that line ends in the wbuffer
output->ProcessLine(lineBuffer, lineBufferPos);
lineBufferPos = 0;
}
}
}
else
{
//UTF-16
char wcharBytes[3] = { 0 };
wchar_t wchar[2] = { 0 };
for (DWORD i = 0; i < readCount; i += 2)
{
//For every byte that was read, convert it into a wchar and store it at the end of what is currently stored in lineBuffer
wcharBytes[0] = buffer[i];
wcharBytes[1] = buffer[i + 1];
MultiByteToWideChar(CP_UTF8, 0, wcharBytes, 3, wchar, 2);
lineBuffer[lineBufferPos] = wchar[0];
lineBufferPos++;
if (lineBuffer[lineBufferPos - 1] == L'\n')
{
//If reached a newline character, lineBuffer now has a full line, and store where that line ends in the wbuffer
output->ProcessLine(lineBuffer, lineBufferPos);
lineBufferPos = 0;
}
}
}
}
else
{
output = new T();
return false;
}
if (lineBufferPos != 0)
{
//If the document doesn't end with a new line character, stick one on the end and pass the line to the function
lineBuffer[lineBufferPos] = L'\n';
lineBufferPos++;
output->ProcessLine(lineBuffer, lineBufferPos);
lineBufferPos = 0;
}
CloseHandle(fileHandle);
return false;
}
/// <summary>
/// Compares this file's extension to the one passed to this function
/// </summary>
/// <param name="extension">The extension to compare against, without the starting '.' (Ex. 'HasExtension(L"png")')</param>
bool HasExtension(const wchar_t* extension)
{
return wcscmp(this->extension, extension) == 0;
}
};
struct PROCESSEDFILE
{
static const bool utf16 = false;
virtual void ProcessLine(wchar_t*, unsigned long) = 0;
/// <summary>Check PROCESSEDFILE struct in cmde for an example, with both UTF-8 and UTF-16 encoding verions</summary>
template <class T>
static bool Export(cmde::FILE exportFile, T exportData) { return false; }
/*
//Example
static bool Export(cmde::FILE& exportFile, Type name)
{
wchar_t lineBuffer[cmde::FILE::CHUNK_SIZE] = { 0 };
//UTF-8 Version
//
DWORD writeCount = swprintf(lineBuffer, cmde::FILE::CHUNK_SIZE, L"(%f)\n", name.value);
char byteBuffer[cmde::FILE::CHUNK_SIZE] = { 0 };
writeCount = WideCharToMultiByte(CP_UTF8, 0, lineBuffer, -1, byteBuffer, cmde::FILE::CHUNK_SIZE, NULL, NULL);
if (!WriteFile(exportFile.fileHandle, &byteBuffer, writeCount - 1, &writeCount, NULL))
{
return false;
}
//
//UTF-16 Version
//
DWORD writeCount = swprintf(lineBuffer, cmde::FILE::CHUNK_SIZE, L"(%f)\n", name.value);
if (!WriteFile(exportFile.fileHandle, &lineBuffer, writeCount * 2, &writeCount, NULL))
{
return false;
}
return true;
//
}
*/
};
class CMDEngine
{
private:
HANDLE console;
HANDLE consoleInput;
HWND window;
SMALL_RECT sr;
bool upscale;
wchar_t _msg[128];
COORD _screenSize;
int _pixelCount;
float _deltaTime;
CHAR_INFO* screen;
public:
const COORD& screenSize = _screenSize;
const int& pixelCount = _pixelCount;
const float& deltaTime = _deltaTime;
bool running;
CHAR_INFO emptyChar;
bool autoClearScreen;
wchar_t title[256];
float fpsLimit;
float* zBuffer;
//More data: float quickest = 100; float sum = 0; float frameAmount = 0;
/// <summary>0 -> nothing ; 1 -> released ; 2 -> pressed ; 3 -> held ; MOUSE_X and MOUSE_Y -> point on the command prompt</summary>
std::map<wchar_t, short> inputs;
/// <summary>
/// Constructor for the command prompt engine
/// </summary>
/// <param name="screenWidth">The width of the command prompt in characters</param>
/// <param name="screenHeight">The height of the command prompt in characters</param>
/// <param name="fontWidth">The width of the characters in pixels</param>
/// <param name="fontHeight">The height of the characters in pixels (Less than 3 may cause lines between pixels; 1 may cause distortions for some resolutions)</param>
/// <param name="autoUpscale">Whether to automatically scale the font up so that it occupies the most amount of screen space</param>
/// <param name="clearScreen">Whether to automatically clear the screen at the beginning of 'Update()' (You can do so manually with the 'ClearFrame()' function)</param>
/// <param name="maxFPS">The minimum amount of time before the next frame is drawn (1/fps). Setting this to 0 or less will allow for infinte framerate</param>
CMDEngine(short screenWidth, short screenHeight, short fontWidth = 1, short fontHeight = 1, bool autoUpscale = true, bool clearScreen = true, float maxFPS = FPS60)
{
upscale = autoUpscale;
_screenSize = { (short)screenWidth, (short)screenHeight };
_pixelCount = screenWidth * screenHeight;
autoClearScreen = clearScreen;
running = true;
if (fontWidth > 30 || fontHeight > 30)
{
ThrowError(L"FontTooBig (30)");
return;
}
if (screenWidth < 15 || screenHeight < 2 || fontWidth < 1 || fontHeight < 1)
{
ThrowError(L"SmallerThanMin (15, 2, 1, 1)");
return;
}
screen = new CHAR_INFO[_pixelCount];
zBuffer = new float[pixelCount];
_deltaTime = 0;
emptyChar.Char.UnicodeChar = 0x2588;
emptyChar.Attributes = 0x0000;
inputs.insert(std::pair<wchar_t, short>(RALT, 0));
inputs.insert(std::pair<wchar_t, short>(LALT, 0));
inputs.insert(std::pair<wchar_t, short>(RCTRL, 0));
inputs.insert(std::pair<wchar_t, short>(LCTRL, 0));
inputs.insert(std::pair<wchar_t, short>(SHIFT, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_DCLICK, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_SCROLL, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 0, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 1, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 2, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 3, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 4, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 5, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 6, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_BTN + 7, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_X, 0));
inputs.insert(std::pair<wchar_t, short>(MOUSE_Y, 0));
SetTitle(L"CMDEngine Program");
if (maxFPS > 0)
{
fpsLimit = maxFPS;
}
//Setting up the console window (Not entirely sure about what everything here does, had to copy most of it due to the complexity)
#pragma region ConsoleWindowSetup
//Creates an object that then basically functions as the console
console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
//Gets a reference to the input handler
consoleInput = GetStdHandle(STD_INPUT_HANDLE);
//Changes some of the input handler's settings (Prevents highlighting and permits reading mouse input)
SetConsoleMode(consoleInput, ENABLE_EXTENDED_FLAGS | ENABLE_MOUSE_INPUT);
//Set the window size to the smallest possible
sr = { 0, 0, 1, 1 };
SetConsoleWindowInfo(console, true, &sr);
//Set the buffer size to the wanted size
if (!SetConsoleScreenBufferSize(console, _screenSize))
{
ThrowError(L"SetConsoleScreenBufferSize");
return;
}
//Assign the console object to the actual console (From now on changes to the console object will affect the actual console)
if (!SetConsoleActiveScreenBuffer(console))
{
ThrowError(L"SetConsoleActiveScreenBuffer");
return;
}
//Setting the cursor visibility
CONSOLE_CURSOR_INFO cci = CONSOLE_CURSOR_INFO();
cci.bVisible = false;
cci.dwSize = 3;
SetConsoleCursorInfo(console, &cci);
//Setting the character size and font
CONSOLE_FONT_INFOEX cfi = CONSOLE_FONT_INFOEX();
cfi.cbSize = sizeof(cfi);
cfi.dwFontSize.X = fontWidth;
cfi.dwFontSize.Y = fontHeight;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
//Don't know why, but ' cfi.FaceName = L"Liberation Mono"; ' doesn't work, so instead 'wcscpy_s' has to be used
// this copies the wide character string in the 2nd parameter into the variable in the 1st parameter
//wcscpy_s(cfi.FaceName, L"Liberation Mono");
if (!SetCurrentConsoleFontEx(console, false, &cfi))
{
ThrowError(L"SetConsoleActiveScreenBuffer");
return;
}
//Check to see if the wanted size is too big for the screen
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(console, &csbi))
{
ThrowError(L"GetConsoleScreenBufferInfo");
return;
}
if (_screenSize.Y > csbi.dwMaximumWindowSize.Y)
{
swprintf(_msg, 128, L"Screen Height / Font Height Too Big (%d * %d)", csbi.dwMaximumWindowSize.Y, fontHeight);
ThrowError(_msg);
return;
}
if (_screenSize.X > csbi.dwMaximumWindowSize.X)
{
swprintf(_msg, 128, L"Screen Width / Font Width Too Big (%d * %d)", csbi.dwMaximumWindowSize.X, fontWidth);
ThrowError(_msg);
return;
}
//Tries to scale the font up to make the window occupy the most screen space possible
if (autoUpscale)
{
float fontRatio = ((float)fontWidth) / ((float)fontHeight);
short tFW = fontWidth;
short tFH = fontHeight;
short i = 1;
while (true)
{
if ((fontRatio * i) == (trunc(fontRatio * i)))
{
cfi.dwFontSize.X = fontWidth + (short)(fontRatio * i);
cfi.dwFontSize.Y = fontHeight + i;
SetCurrentConsoleFontEx(console, false, &cfi);
GetConsoleScreenBufferInfo(console, &csbi);
if (_screenSize.X > csbi.dwMaximumWindowSize.X || _screenSize.Y > csbi.dwMaximumWindowSize.Y)
{
break;
}
tFW = fontWidth + (short)(fontRatio * i);
tFH = fontHeight + i;
}
i++;
}
cfi.dwFontSize.X = tFW;
cfi.dwFontSize.Y = tFH;
SetCurrentConsoleFontEx(console, false, &cfi);
GetConsoleScreenBufferInfo(console, &csbi);
}
//Set the window size to the wanted screen size
sr = { 0, 0, (short)(screenWidth - 1), (short)(screenHeight - 1) };
if (!SetConsoleWindowInfo(console, true, &sr))
{
ThrowError(L"SetConsoleWindowInfo");
return;
}
#pragma endregion
//Gets a reference to the program window and changes some settings
#pragma region WindowSettings
//Sets the title of the program to something random in order to distinguish it from the other programs on the computer
int temp = rand();
swprintf(_msg, 128, L"%d", temp);
SetConsoleTitle(_msg);
Sleep(40);
//Finds the window with that random name (Don't have to set it back to normal cause the 'UpdateTitle()' is run every frame anyways)
window = FindWindowW(NULL, _msg);
//Changes the window's settings (Prevents maximizing, minimizing, and resizing)
SetWindowLongPtrW(window, GWL_STYLE, WS_OVERLAPPEDWINDOW & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX));
::SetWindowPos(window, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOREPOSITION);
#pragma endregion
}
/// <summary>
/// Prints an error message to the console (Returns 0 so that you can just do 'return ThrowError(...);')
/// </summary>
/// <param name="msg">The message to print to the console</param>
int ThrowError(const wchar_t* msg)
{
wchar_t _error[128];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _error, 128, NULL);
wprintf(L"ERROR: %s\n\t%s\n", msg, _error);
running = false;
return 0;
}
bool OnScreen(short x, short y) { return (x >= 0 && y >= 0 && x < screenSize.X&& y < screenSize.Y); }
bool OnScreen(VEC2F p) { return (p.x >= 0 && p.y >= 0 && p.x < screenSize.X&& p.y < screenSize.Y); }
#pragma region DrawFunctions
//Simply modifies the value of the specified position in the screen array
#pragma region Draw
/// <summary>
/// Draws to a specific point on the command prompt
/// </summary>
/// <param name="x">The x position of the point (Leftmost is 0; Rightmost is screenSize.X)</param>
/// <param name="y">The y position of the point (Topmost is 0; Bottommost is screenSize.Y)</param>
/// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param>
/// <param name="cha">The character with which to draw to that point</param>
/// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void Draw(short x, short y, short col = 0x000F, short cha = 0x2588, float depth = -1)
{
if (OnScreen(x, y) && depth < zBuffer[y * screenSize.X + x])
{
screen[y * screenSize.X + x].Char.UnicodeChar = cha;
screen[y * screenSize.X + x].Attributes = col;
zBuffer[y * screenSize.X + x] = depth;
}
}
/// <summary>Draws to a specific point on the command prompt</summary> /// <param name="x">The x position of the point (Leftmost is 0; Rightmost is screenSize.X)</param> /// <param name="y">The y position of the point (Topmost is 0; Bottommost is screenSize.Y)</param> /// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw to that point</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void Draw(int x, int y, short col = 0x000F, short cha = 0x2588, float depth = -1) { Draw((short)x, (short)y, col, cha, depth); }
//Due to issues with numbers between 0-1 when casting, '(short)((short)(x + 2) - 2)' is needed, since numbers between 2-3 convert correctly
/// <summary>Draws to a specific point on the command prompt</summary> /// <param name="x">The x position of the point (Leftmost is 0; Rightmost is screenSize.X)</param> /// <param name="y">The y position of the point (Topmost is 0; Bottommost is screenSize.Y)</param> /// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw to that point</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void Draw(float x, float y, short col = 0x000F, short cha = 0x2588, float depth = -1) { Draw((short)((short)(x + 2) - 2), (short)((short)(y + 2) - 2), col, cha, depth); }
/// <summary>Draws to a specific point on the command prompt</summary> /// <param name="p">The position of the point (Leftmost is 0; Rightmost is screenSize.X; Topmost is 0; Bottommost is screenSize.Y)</param> /// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw to that point</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void Draw(VEC2F p, short col = 0x000F, short cha = 0x2588, float depth = -1) { Draw((short)((short)(p.x + 2) - 2), (short)((short)(p.y + 2) - 2), col, cha, depth); }
#pragma endregion
//Gets the step size for the reaction in 1 axis when moving 1 unit in the other
// by going from point 1 to point 2 like this in both axis, you can find every space through which the line crosses
// but because they're floats, the points may not be exactly in a space, so for the first step you have to move less than 1 in the axis
// after the first movement in each axis, the movements can be by 1 unit
//If the points are whole numbers, the first step can be skipped due to knowing that the points will always be exactly in a space
//Using relative screen space for the points is the same as normal, but with the added step of converting the coordinates first
#pragma region DrawLine
/// <summary>
/// Draws a line on the command prompt from a point to another point
/// </summary>
/// <param name="x1">The x position of the first point (Leftmost is 0; Rightmost is screenSize.X)</param>
/// <param name="y1">The y position of the first point (Topmost is 0; Bottommost is screenSize.Y)</param>
/// <param name="x2">The x position of the second point (Leftmost is 0; Rightmost is screenSize.X)</param>
/// <param name="y2">The y position of the second point (Topmost is 0; Bottommost is screenSize.Y)</param>
/// <param name="col">The color with which to draw the line (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param>
/// <param name="cha">The character with which to draw the line</param>
/// <param name="depth1">How far away from the camera the first point is (For rendering things on top of each other) (Negative values are always drawn)</param>
/// <param name="depth2">How far away from the camera the second point is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawLine(float x1, float y1, float x2, float y2, short col = 0x000F, short cha = 0x2588, float depth1 = -1, float depth2 = -1)
{
short ux = (x1 < x2 ? 1 : -1), uy = (y1 < y2 ? 1 : -1);
float tx = x2 - x1, ty = y2 - y1;
float sx = (ty != 0 ? ux * abs(tx / ty) : 0), sy = (tx != 0 ? uy * abs(ty / tx) : 0);
Draw(x1, y1, col, cha, depth1);
Draw(x2, y2, col, cha, depth2);
tx = (float)fmod(ux - fmod(x1, 1.0f), 1.0f);
ty = (float)fmod(uy - fmod(y1, 1.0f), 1.0f);
LINEAR depthFunc = LinearFunction(x1, depth1, x2, depth2);
for (float x = x1 + tx, y = y1 + uy * abs(tx * sy); x * ux < x2 * ux; x += ux, y += sy)
{
Draw(x, y, col, cha, depthFunc.f(x).y);
}
depthFunc = LinearFunction(y1, depth1, y2, depth2);
for (float y = y1 + ty, x = x1 + ux * abs(ty * sx); y * uy < y2 * uy; y += uy, x += sx)
{
Draw(x, y, col, cha, depthFunc.f(y).y);
}
}
/// <summary>Draws a line on the command pront from a point to another point</summary> /// <param name="p1">The position of the first point (Leftmost is 0; Rightmost is screenSize.X; Topmost is 0; Bottommost is screenSize.Y)</param> /// <param name="p2">The position of the second point (Leftmost is 0; Rightmost is screenSize.X; Topmost is 0; Bottommost is screenSize.Y)</param> /// <param name="col">The color with which to draw the line (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the line</param> /// <param name="depth1">How far away from the camera the first point is (For rendering things on top of each other) (Negative values are always drawn)</param> /// <param name="depth2">How far away from the camera the second point is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawLine(VEC2F p1, VEC2F p2, short col = 0x000F, short cha = 0x2588, float depth1 = -1, float depth2 = -1) { DrawLine(p1.x, p1.y, p2.x, p2.y, col, cha, depth1, depth2); }
/// <summary>Draws a line on the command pront from a point in relative screen space to another point in relative screen space</summary> /// <param name="x1">The x position of the first point in relative screen space (Leftmost is 0.0; Rightmost is 1.0)</param> /// <param name="y1">The y position of the first point in relative screen space (Topmost is 0.0; Bottommost is 1.0)</param> /// <param name="x2">The x position of the second point in relative screen space (Leftmost is 0.0; Rightmost is 1.0)</param> /// <param name="y2">The y position of the second point in relative screen space (Topmost is 0.0; Bottommost is 1.0)</param> /// <param name="col">The color with which to draw the line (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the line</param> /// <param name="depth1">How far away from the camera the first point is (For rendering things on top of each other) (Negative values are always drawn)</param> /// <param name="depth2">How far away from the camera the second point is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawLineS(float x1, float y1, float x2, float y2, short col = 0x000F, short cha = 0x2588, float depth1 = -1, float depth2 = -1) { DrawLine(ScreenPosToPoint(x1, y1), ScreenPosToPoint(x2, y2), col, cha, depth1, depth2); }
/// <summary>Draws a line on the command pront from a point in relative screen space to another point in relative screen space</summary> /// <param name="p1">The position of the first point in relative screen space (Leftmost is 0.0; Rightmost is 1.0; Topmost is 0.0; Bottommost is 1.0)</param> /// <param name="p2">The position of the second point in relative screen space (Leftmost is 0.0; Rightmost is 1.0; Topmost is 0.0; Bottommost is 1.0)</param> /// <param name="col">The color with which to draw the line (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the line</param> /// <param name="depth1">How far away from the camera the first point is (For rendering things on top of each other) (Negative values are always drawn)</param> /// <param name="depth2">How far away from the camera the second point is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawLineS(VEC2F p1, VEC2F p2, short col = 0x000F, short cha = 0x2588, float depth1 = -1, float depth2 = -1) { DrawLine(ScreenPosToPoint(p1), ScreenPosToPoint(p2), col, cha, depth1, depth2); }
#pragma endregion
//Uses trigonometry to get the vertices of a regular polygon of 'edges' sides and then connects them with lines
//Using relative screen space for the polygon requires the conversion of the center point and the radius
// but due to the radius not being a point, it must be defined relative to the width or height of the screen
#pragma region DrawRPoly
/// <summary>
/// Draws a regular polygon to the screen
/// </summary>
/// <param name="cx">The x position of the center of the polygon</param>
/// <param name="cy">The y position of the center of the polygon</param>
/// <param name="edges">The amount of edges the polygon has</param>
/// <param name="rad">The distance from the center point to each of the vertices</param>
/// <param name="rot">The angle at which to draw the polygon in degrees</param>
/// <param name="col">The color with which to draw the polygon (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param>
/// <param name="cha">The character with which to draw the polygon</param>
/// <param name="depth">How far away from the camera the shape is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawRPoly(float cx, float cy, short edges, float rad, float rot = 0, short col = 0x000F, short cha = 0x2588, float depth = -1)
{
float as = (360.0f / edges) * RAD;
float max = (360.0f + rot) * RAD;
for (float a = rot * RAD; a < max; a += as)
{
DrawLine((float)cos(a) * rad + cx, (float)sin(a) * rad + cy, (float)cos(a + as) * rad + cx, (float)sin(a + as) * rad + cy, col, cha, depth, depth);
}
}
/// <summary>Draws a regular polygon to the screen</summary> /// <param name="p">The position of the center of the polygon</param> /// <param name="edges">The amount of edges the polygon has</param> /// <param name="rad">The distance from the center point to each of the vertices</param> /// <param name="rot">The angle at which to draw the polygon in degrees</param> /// <param name="col">The color with which to draw the polygon (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the polygon</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawRPoly(VEC2F p, short edges, float rad, float rot = 0, short col = 0x000F, short cha = 0x2588, float depth = -1) { DrawRPoly(p.x, p.y, edges, rad, rot, col, cha, depth); }
/// <summary>Draws a regular polygon to the screen using relative screen space</summary> /// <param name="cx">The x position of the center of the polygon in relative screen space</param> /// <param name="cy">The y position of the center of the polygon in relative screen space</param> /// <param name="edges">The amount of edges the polygon has</param> /// <param name="rad">The distance from the center point to each of the vertices in relative screen space</param> /// <param name="useY">Whether the radius' size is defined by the screen's height or width ('true' means height is used; 'false' for width)</param> /// <param name="rot">The angle at which to draw the polygon in degrees</param> /// <param name="col">The color with which to draw the polygon (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the polygon</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawRPolyS(float cx, float cy, short edges, float rad, bool useY, float rot = 0, short col = 0x000F, short cha = 0x2588, float depth = -1) { DrawRPoly(ScreenPosToPoint(cx, cy), edges, (useY ? ScreenPosToPoint(0, rad).y : ScreenPosToPoint(rad, 0).x), rot, col, cha, depth); }
/// <summary>Draws a regular polygon to the screen using relative screen space</summary> /// <param name="p">The position of the center of the polygon in relative screen space</param> /// <param name="edges">The amount of edges the polygon has</param> /// <param name="rad">The distance from the center point to each of the vertices in relative screen space</param> /// <param name="useY">Whether the radius' size is defined by the screen's height or width ('true' means height is used; 'false' for width)</param> /// <param name="rot">The angle at which to draw the polygon in degrees</param> /// <param name="col">The color with which to draw the polygon (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param> /// <param name="cha">The character with which to draw the polygon</param> /// <param name="depth">How far away from the camera the pixel is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawRPolyS(VEC2F p, short edges, float rad, bool useY, float rot = 0, short col = 0x000F, short cha = 0x2588, float depth = -1) { DrawRPolyS(p.x, p.y, edges, rad, useY, rot, col, cha, depth); }
#pragma endregion
/// <summary>
/// Writes a line of text starting from a point on the command prompt
/// </summary>
/// <param name="x">The x position of the leftmost character</param>
/// <param name="y">The y position on which to write the line</param>
/// <param name="text">The text to write</param>
/// <param name="length">The amount of characters in the text</param>
/// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param>
/// <param name="depth">How far away from the camera the text is (For rendering things on top of each other) (Negative values are always drawn)</param>
void WriteText(short x, short y, wchar_t* text, short length, short col = 0x000F, float depth = -1)
{
for (short i = 0; i < length; i++)
{
Draw((short)(i + x), y, col, text[i], depth);
}
}
/// <summary>
/// Draws a filled in triangle to the command prompt from 3 vertices
/// </summary>
/// <param name="v1">The first vertice of the triangle</param>
/// <param name="v2">The second vertice of the triangle</param>
/// <param name="v3">The third vertice of the triangle</param>
/// <param name="col">The color with which to draw to that point (16 available colors (0-F); Must be inputted as Hex 0x0000; The last 2 zeros determine the background and foreground colors respectively (0x00BF))</param>
/// <param name="cha">The character with which to draw to that point</param>
/// <param name="depth1">How far away from the camera the first vertice is (For rendering things on top of each other) (Negative values are always drawn)</param>
/// <param name="depth2">How far away from the camera the second vertice is (For rendering things on top of each other) (Negative values are always drawn)</param>
/// <param name="depth3">How far away from the camera the third vertice is (For rendering things on top of each other) (Negative values are always drawn)</param>
void DrawTriangle(VEC2F v1, VEC2F v2, VEC2F v3, short col = 0x000F, short cha = 0x2588, float depth1 = -1, float depth2 = -1, float depth3 = -1)
{
VEC3F list[3] = { VEC3F(v1.x, v1.y, depth1), VEC3F(v2.x, v2.y, depth2), VEC3F(v3.x, v3.y, depth3) };
for (short i = 0; i < 2; i++)
{
if (list[i].y < list[0].y)
{
std::swap(list[i], list[0]);
}
if (list[i].y > list[2].y)
{
std::swap(list[i], list[2]);
}
}
VEC3F b1, b2, t1;
if ((short)list[0].y == (short)list[2].y)
{
//Line
DrawLine(list[0], list[1], col, cha, list[0].z, list[1].z);
DrawLine(list[1], list[2], col, cha, list[1].z, list[2].z);
DrawLine(list[2], list[0], col, cha, list[2].z, list[0].z);
}
else
{
if ((short)list[0].y == (short)list[1].y)
{
b1 = list[0];
b2 = list[1];
t1 = list[2];
}
else
{
if ((short)list[1].y == (short)list[2].y)
{
b1 = list[1];
b2 = list[2];
t1 = list[0];
}
else
{
//Turn into 2 triangles with a flat side and then pass them both through this function
b1 = list[1];
b2 = VEC3F(LinearFunction(list[0].y, list[0].x, list[2].y, list[2].x, b1.y), b1.y, LinearFunction(list[0].y, list[0].z, list[2].y, list[2].z, b1.y));
DrawTriangle(list[0], b1, b2, col, cha, list[0].z, b1.z, b2.z);
DrawTriangle(b1, b2, list[2], col, cha, b1.z, b2.z, list[2].z);
return;
}
}
}
//The 'DrawLine()' code but slightly modified so that 2 lines can be done at once
//If you draw 2 lines to the same point, both starting at the same Y level and filling in the space between them every time the Y level changes
//then you've drawn a triangle
short ux1 = (b1.x < t1.x ? 1 : -1), ux2 = (b2.x < t1.x ? 1 : -1), uy = (b1.y < t1.y ? 1 : -1);
float tx1 = t1.x - b1.x, tx2 = t1.x - b2.x, ty = t1.y - b1.y;
float sx1 = (ty != 0 ? ux1 * abs(tx1 / ty) : 0), sy1 = (tx1 != 0 ? uy * abs(ty / tx1) : 0), sx2 = (ty != 0 ? ux2 * abs(tx2 / ty) : 0), sy2 = (tx2 != 0 ? uy * abs(ty / tx2) : 0);
DrawLine(b1.x, b1.y, b2.x, b2.y, col, cha, b1.z, b2.z);
Draw(t1.x, t1.y, col, cha, t1.z);
tx1 = (float)fmod(ux1 - fmod(b1.x, 1.0f), 1.0f);
tx2 = (float)fmod(ux2 - fmod(b2.x, 1.0f), 1.0f);
ty = (float)fmod(uy - fmod(b1.y, 1.0f), 1.0f);
LINEAR b1Depth = LinearFunction(b1.y, b1.z, t1.y, t1.z);
//+1 on x for line 1 & +? on y for line 1
for (float x = b1.x + tx1, y = b1.y + uy * abs(tx1 * sy1); x * ux1 < t1.x * ux1; x += ux1, y += sy1)
{
Draw(x, y, col, cha, b1Depth.f(y).y);
}
LINEAR b2Depth = LinearFunction(b2.y, b2.z, t1.y, t1.z);
//+1 on x for line 2 & +? on y for line 2
for (float x = b2.x + tx2, y = b2.y + uy * abs(tx2 * sy2); x * ux2 < t1.x * ux2; x += ux2, y += sy2)
{
Draw(x, y, col, cha, b2Depth.f(y).y);
}
//+1 on y for line 1 & 2, & +? on x for line 1 & 2
//DEBUG
for (float y = b1.y + ty, x1 = b1.x + ux1 * abs(ty * sx1), x2 = b2.x + ux2 * abs(ty * sx2); y * uy < t1.y * uy; y += uy, x1 += sx1, x2 += sx2)
{
DrawLine(x1, y, x2, y, col, cha, b1Depth.f(y).y, b2Depth.f(y).y);
}
}
#pragma endregion
/// <summary>
/// Converts a relative screen space position ('0.0 - 1.0' is 'Left - Right' or 'Top - Bottom') to a point in the pixel grid ('0.0 - screenSize.X' is 'Left - Right' and '0.0 - screenSize.Y' is 'Top - Bottom')
/// </summary>
/// <param name="x">The x value (If outside of range [0.0; 1.0] the resulting pixel will be out of the screen)</param>
/// <param name="y">The y value (If outside of range [0.0; 1.0] the resulting pixel will be out of the screen)</param>
VEC2F ScreenPosToPoint(float x, float y)
{
return { x * screenSize.X, y * screenSize.Y };
}
///<summary>Converts a relative screen space position ('0.0 - 1.0' is 'Left - Right' or 'Top - Bottom') to a point in the pixel grid ('0.0 - screenSize.X' is 'Left - Right' and '0.0 - screenSize.Y' is 'Top - Bottom')</summary> /// <param name="p">The value (If x or y is outside of range [0.0; 1.0] the resulting pixel will be out of the screen)</param>
VEC2F ScreenPosToPoint(VEC2F p) { return { p.x * screenSize.X, p.y * screenSize.Y }; }
/// <summary>Fills the entire console with the character and color in the 'empty' variable</summary>
void ClearFrame()
{
for (int i = 0; i < pixelCount; i++)
{
screen[i] = emptyChar;
zBuffer[i] = 1;
}
}
void DrawFrame()
{
WriteConsoleOutput(console, screen, { screenSize.X, screenSize.Y }, { 0, 0 }, &sr);
}
void ResizeWindow(short screenWidth, short screenHeight, short fontWidth = 1, short fontHeight = 1)
{
if (fontWidth > 30 || fontHeight > 30)
{
ThrowError(L"FontTooBig (30)");
return;
}
if (screenWidth < 15 || screenHeight < 2 || fontWidth < 1 || fontHeight < 1)
{
ThrowError(L"SmallerThanMin (15, 2, 1, 1)");
return;
}
_screenSize = { (short)screenWidth, (short)screenHeight };
_pixelCount = screenWidth * screenHeight;
screen = new CHAR_INFO[pixelCount];
//Setting up the console window (Not entirely sure about what everything here does, had to copy most of it due to the complexity)
#pragma region ConsoleWindowSetup
//Creates an object that then basically functions as the console
console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
//Set the window size to the smallest possible
sr = { 0, 0, 1, 1 };
SetConsoleWindowInfo(console, true, &sr);
//Set the buffer size to the wanted size
if (!SetConsoleScreenBufferSize(console, _screenSize))
{
ThrowError(L"SetConsoleScreenBufferSize");
return;
}
//Assign the console object to the actual console (From now on changes to the console object will affect the actual console)
if (!SetConsoleActiveScreenBuffer(console))
{
ThrowError(L"SetConsoleActiveScreenBuffer");
return;
}
//Setting the cursor visibility
CONSOLE_CURSOR_INFO cci = CONSOLE_CURSOR_INFO();
cci.bVisible = false;
cci.dwSize = 3;
SetConsoleCursorInfo(console, &cci);
//Setting the character size and font
CONSOLE_FONT_INFOEX cfi = CONSOLE_FONT_INFOEX();
cfi.cbSize = sizeof(cfi);
cfi.dwFontSize.X = fontWidth;
cfi.dwFontSize.Y = fontHeight;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
//Don't know why, but ' cfi.FaceName = L"Liberation Mono"; ' doesn't work, so instead 'wcscpy_s' has to be used
// this copies the wide character string in the 2nd parameter into the variable in the 1st parameter
//wcscpy_s(cfi.FaceName, L"Liberation Mono");
if (!SetCurrentConsoleFontEx(console, false, &cfi))
{
ThrowError(L"SetConsoleActiveScreenBuffer");
return;
}
//Check to see if the wanted size is too big for the screen
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(console, &csbi))
{
ThrowError(L"GetConsoleScreenBufferInfo");
return;
}
if (_screenSize.Y > csbi.dwMaximumWindowSize.Y)
{
wchar_t msg[128];
swprintf(msg, 128, L"Screen Height / Font Height Too Big (%d * %d)", csbi.dwMaximumWindowSize.Y, fontHeight);
ThrowError(msg);
return;
}
if (_screenSize.X > csbi.dwMaximumWindowSize.X)
{
wchar_t msg[128];
swprintf(msg, 128, L"Screen Width / Font Width Too Big (%d * %d)", csbi.dwMaximumWindowSize.X, fontWidth);
ThrowError(msg);
return;
}
//Tries to scale the font up to make the window occupy the most screen space possible
if (upscale)
{
float fontRatio = ((float)fontWidth) / ((float)fontHeight);
short tFW = fontWidth;
short tFH = fontHeight;
short i = 1;
while (true)
{
if ((fontRatio * i) == (trunc(fontRatio * i)))
{
cfi.dwFontSize.X = fontWidth + (short)(fontRatio * i);
cfi.dwFontSize.Y = fontHeight + i;
SetCurrentConsoleFontEx(console, false, &cfi);
GetConsoleScreenBufferInfo(console, &csbi);
if (_screenSize.X > csbi.dwMaximumWindowSize.X || _screenSize.Y > csbi.dwMaximumWindowSize.Y)
{
break;
}
tFW = fontWidth + (short)(fontRatio * i);
tFH = fontHeight + i;
}
i++;
}
cfi.dwFontSize.X = tFW;
cfi.dwFontSize.Y = tFH;
SetCurrentConsoleFontEx(console, false, &cfi);
GetConsoleScreenBufferInfo(console, &csbi);
}
//Set the window size to the wanted screen size
sr = { 0, 0, (short)(screenWidth - 1), (short)(screenHeight - 1) };
if (!SetConsoleWindowInfo(console, true, &sr))
{
ThrowError(L"SetConsoleWindowInfo");
return;
}
#pragma endregion
}
//Used the same name as a function in 'Windows.h', this is horrible practice
/// <summary>
/// Moves the command prompt window to a specific point on the screen (This function has the same name as one in 'Windows.h', if by any chance you meant to use that one, use '::SetWindowPos')
/// </summary>
/// <param name="x">Moves the window so that there are this many pixels between the edge of the screen and the left side of the window (0 pixels seems to actually be -7)</param>
/// <param name="y">Moves the window so that there are this many pixels between the edge of the screen and the top side of the window (In order to hide the Windows top bar use -29)</param>
void SetWindowPos(short x, short y)
{
::SetWindowPos(window, NULL, x, y, NULL, NULL, SWP_NOSIZE);
}
/// <summary>Moves the command prompt window to a specific point on the screen (This function has the same name as one in 'Windows.h', if by any chance you meant to use that one, use '::SetWindowPos')</summary> /// <param name="x">Moves the window so that there are this many pixels between the edge of the screen and the left side of the window (0 pixels seems to actually be -7)</param> /// <param name="y">Moves the window so that there are this many pixels between the edge of the screen and the top side of the window (In order to hide the Windows top bar use -29)</param>
void SetWindowPos(VEC2F v) { ::SetWindowPos(window, NULL, (int)v.x, (int)v.y, NULL, NULL, SWP_NOSIZE); }
//Used the same name as a function in 'Windows.h', this is horrible practice
/// <summary>
/// Moves the command prompt window by a number of pixels on the screen (This function has the same name as one in 'Windows.h', if by any chance you meant to use that one, use '::MoveWindow')
/// </summary>
/// <param name="x">Moves the window this many pixels to the right (Negative numbers go to the left)</param>
/// <param name="y">Moves the window this many pixels down (Negative numbers go up)</param>