-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.h
More file actions
2615 lines (2163 loc) · 86 KB
/
Engine.h
File metadata and controls
2615 lines (2163 loc) · 86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
#pragma once
#define NOMINMAX
//constants
constexpr int MAX_LIGHTS = 1024; // light limit, also change in PixelShader.hlsl;
constexpr int TEXTURE_SLOTS = 32;
constexpr int MAX_QUAD_INSTANCES = 1048576;
constexpr double AU = 149597870700.0; // in meters;
constexpr double NS_IN_SEC = 1000000000.0;
//std
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#include <optional>
#include <variant>
//boost
#include <boost\container\deque.hpp> // must be boost::deque, std::deque does not allow incomplete types which are required for deque<Order> Order::Suborders
//directx
#include <d3d11.h>
#include <dxgi.h>
#include <dinput.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <tchar.h>
#include <D:\Programming\CPP\VS_Solutions\DirectXTK-main\Inc\WICTextureLoader.h>
#include <D:\Programming\CPP\VS_Solutions\FW1FontWrapper-1.1\Source\FW1FontWrapper.h>
#include <D:\Programming\CPP\Sources\rapidjson-master\include\rapidjson\document.h>
// Rect
#include "Rect.h"
// imgui
#include "imgui-master\imgui.h"
#include "imgui-master\imgui_impl_win32.h"
#include "imgui-master\imgui_impl_dx11.h"
// Link libraries (alternative to Properties->Linker->Input->Additional dependencies)
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "d3dcompiler.lib")
#ifdef BUILD_CONFIG_DEBUG
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\FW1FontWrapper-1.1\\Bin\\Debug\\FW1FontWrapper_Debug.lib")
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\DirectXTK-main\\Bin\\Desktop_2022\\x64\\Debug\\DirectXTK.lib")
#elif BUILD_CONFIG_PROFILE
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\FW1FontWrapper-1.1\\Bin\\Profile\\FW1FontWrapper_Profile.lib")
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\DirectXTK-main\\Bin\\Desktop_2022\\x64\\Profile\\DirectXTK.lib")
#elif BUILD_CONFIG_RELEASE
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\FW1FontWrapper-1.1\\Bin\\Release\\FW1FontWrapper_Release.lib")
#pragma comment (lib, "D:\\Programming\\CPP\\VS_Solutions\\DirectXTK-main\\Bin\\Desktop_2022\\x64\\Release\\DirectXTK.lib")
#endif
using std::vector;
using std::array;
using std::string;
using std::wstring;
using std::min;
using std::to_string;
using std::ifstream;
using std::unordered_map;
using std::max;
using std::remove_if;
using std::unordered_set;
using std::get;
using std::optional;
using std::clamp;
using std::variant;
using std::holds_alternative;
using boost::container::deque;
using DirectX::XMFLOAT4;
using DirectX::XMFLOAT3;
using DirectX::XMFLOAT2;
using DirectX::XMMATRIX;
using DirectX::XMVectorGetByIndex;
using DirectX::CreateWICTextureFromFile;
extern "C" {
_declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}
int number_of_active_lights = 0;
// DirectX Object Declarations //
ID3D11Device* D3D11_Device;
ID3D11DeviceContext* D3D11_Device_Context;
IDXGISwapChain* SwapChain;
ID3D11Texture2D* BackBuffer;
ID3D11RenderTargetView* BackBuffer_RTV;
ID3D11DepthStencilState* pDepthdisabledStencilState;
ID3D11InputLayout* D3D11_Vertex_Layout;
ID3D11VertexShader* D3D11_Vertex_Shader;
ID3D11PixelShader* D3D11_Pixel_Shader;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11Buffer* D3D11_PS_Constant_Buffer;
ID3D11Buffer* D3D11_VS_Constant_Buffer;
ID3D11BlendState* Blend_Add;
ID3D11BlendState* Blend_Alpha;
ID3D11RasterizerState* NonecullMode;
ID3D11SamplerState* TextureSamplerState;
ID3D11Buffer* VertexBuffer;
ID3D11Buffer* IndexBuffer;
LPDIRECTINPUT8 DirectInput;
IFW1Factory* pFW1Factory;
IFW1FontWrapper* pFontWrapper;
// *** Enums *** //
enum class Blend_Mode
{
Opaque, Alpha, Additive
};
enum class Topology_Type
{
Trianglelist, Linelist
};
enum class Shader_Lighting_Mode
{
normal_lighting, no_lighting
};
enum class Active_Sprite //used in Drawable component
{
Normal, Icon, None
};
enum class Order_Status
{
Unopened, In_Progress, Finished
};
enum class Draw_Layer
{
Background_and_Universe = 0,
Effects_Lower = 1,
Spacecrafts = 2,
Effects_Higher = 3,
GUI_Lower = 4,
GUI_Higher = 5
};
// *** Classes declarations *** //
class Vertex;
class Light_HLSL;
class VS_Constant_Buffer;
class PS_Constant_Buffer;
class Camera;
class Mouse;
class Linelist;
class Sprite;
class Icon;
class Particle;
class Color;
class Entity_Ref;
class Texture_Atlas;
class Texture_Animated;
class Texture_Atlas_Subrect;
class Animation;
vector<Texture_Atlas> Texture_Atlases;
vector<Texture_Animated> Animated_Textures;
vector<Texture_Atlas_Subrect> Texture_Atlas_Subrects;
vector<Animation> Animations;
unordered_map<string, Texture_Atlas_Subrect*> name_texture_atlas_subrect_map;
unordered_map<string, Animation*> name_animation_map;
int texture_id_counter = 0;
// *** Functions declarations *** //
bool InitializeWindow(HINSTANCE const hInstance, const int ShowWnd, const int width, const int height, const bool windowed);
bool InitDirectInput(HINSTANCE const hInstance);
bool InitializeDirect3d11App();
void Initialize_game();
void Step();
void Render();
void CleanUp();
LRESULT CALLBACK WndProc(const HWND hWnd, const UINT msg, const WPARAM wParam, const LPARAM lParam);
wstring string_to_wstring(const string& input);
string wstring_to_string(const wstring& input);
void UpdateBuffer(ID3D11Buffer* const Buffer, const void* source, const size_t size);
void Process_Input();
Vector2d WorldToSpriteCoords(const Vector2d& point, Sprite& sprite);
Vector2d SpriteToWorldCoords(const Vector2d& point, Sprite& sprite);
Vector2f WorldToScreenCoords(Vector2d point);
Vector2d ScreenToWorldCoords(Vector2f point);
vector<Vector2d> GetCircleVertices(Vector2d position_arg, double radius_arg, int resolution_arg);
void spawn_systems();
void Parse_data();
void spawn_celestial_bodies();
void DrawGUI();
void draw_deferred_strings();
// *** Variables declarations *** //
Stopwatch Stopwatch_FPS_limit;
Stopwatch Second;
const LPCTSTR WndClassName = L"DirectX Game";
HWND hwnd = nullptr;
HRESULT hresult;
MSG msg;
Vector2i screen_resolution;
bool windowed;
bool running = true;
bool paused = false;
bool debug = true;
double camera_scrolling_speed;
double camera_smoothing_factor; //bigger = more camera inertia
const vector<Vector2d> collision_square{Vector2d(0.5, 0.5), Vector2d(0.5, -0.5), Vector2d(-0.5, -0.5), Vector2d(-0.5, 0.5)};
float average_fps;
float fps_cap; // settings.json -- positive: limited fps, 0.0f: no fps limit, negative: vsync
nanoseconds average_frame_time;
boost::circular_buffer<nanoseconds> past_frametimes{60};
nanoseconds target_frame_time;
int num_of_systems;
Vector2d galaxy_size; //determined dynamically based on num_of_systems
const double system_radius{AU};
const double jump_node_radius{1500.0};
const double icon_culling_view_size{AU * 8};
DoubleRect selection_rectangle;
const double minimum_light_intensity = 0.05; // lights weaker than this get culled if outside the camera rectangle
vector<string> magnitudes; //Star magnitudes and constellation names
vector<string> constellations;
// Class representing a color. Channels: Red/Green/Blue/Alpha, represented as floats from 0.0f to 1.0f
class Color
{
public:
Color() : r(1.0f), g(0.0f), b(0.0f), a(1.0f)
{}
// Constructor from individual r, g, b, a values (float values 0.0f - 1.0f)
Color(float red, float green, float blue, float alpha = 1.0f)
: r(red), g(green), b(blue), a(alpha)
{
validate();
}
// Constructor from 0-255 integer values (converts to 0.0f - 1.0f)
Color(unsigned int red, unsigned int green, unsigned int blue, unsigned int alpha = 255)
{
// Clamp integers to 0-255 and convert to 0.0f-1.0f floats
r = clamp(red, 0u, 255u) / 255.0f;
g = clamp(green, 0u, 255u) / 255.0f;
b = clamp(blue, 0u, 255u) / 255.0f;
a = clamp(alpha, 0u, 255u) / 255.0f;
}
// Getters
inline float getR() const { return r; }
inline float getG() const { return g; }
inline float getB() const { return b; }
inline float getA() const { return a; }
inline XMFLOAT4 getXMFLOAT4()
{
return XMFLOAT4(r, g, b, a);
}
inline XMFLOAT3 getXMFLOAT3()
{
return XMFLOAT3(r, g, b);
}
// Setters (with validation)
// Clamp float values to the range [0.0f, 1.0f]
inline void setR(float red) { r = clamp(red, 0.0f, 1.0f); }
inline void setG(float green) { g = clamp(green, 0.0f, 1.0f); }
inline void setB(float blue) { b = clamp(blue, 0.0f, 1.0f); }
inline void setA(float alpha) { a = clamp(alpha, 0.0f, 1.0f); }
inline void print_rgba() const
{
print(runtime("R:{}, G:{}, B:{}, A:{}" + NEWL), r, g, b, a);
}
private:
float r;
float g;
float b;
float a;
inline void validate() const
{
if (r < 0.0f or r > 1.0f or g < 0.0f or g > 1.0f or b < 0.0f or b > 1.0f or a < 0.0f or a > 1.0f)
{
throw std::out_of_range("Color values must be between 0.0 and 1.0.");
}
}
};
namespace Colors
{
const Color AliceBlue = {0.941176534f, 0.972549081f, 1.000000000f};
const Color AntiqueWhite = {0.980392218f, 0.921568692f, 0.843137324f};
const Color Aqua = {0.000000000f, 1.000000000f, 1.000000000f};
const Color Aquamarine = {0.498039246f, 1.000000000f, 0.831372619f};
const Color Azure = {0.941176534f, 1.000000000f, 1.000000000f};
const Color Beige = {0.960784376f, 0.960784376f, 0.862745166f};
const Color Bisque = {1.000000000f, 0.894117713f, 0.768627524f};
const Color Black = {0.000000000f, 0.000000000f, 0.000000000f};
const Color BlanchedAlmond = {1.000000000f, 0.921568692f, 0.803921640f};
const Color Blue = {0.000000000f, 0.000000000f, 1.000000000f};
const Color BlueViolet = {0.541176498f, 0.168627456f, 0.886274576f};
const Color Brown = {0.647058845f, 0.164705887f, 0.164705887f};
const Color BurlyWood = {0.870588303f, 0.721568644f, 0.529411793f};
const Color CadetBlue = {0.372549027f, 0.619607866f, 0.627451003f};
const Color Chartreuse = {0.498039246f, 1.000000000f, 0.000000000f};
const Color Chocolate = {0.823529482f, 0.411764741f, 0.117647067f};
const Color Coral = {1.000000000f, 0.498039246f, 0.313725501f};
const Color CornflowerBlue = {0.392156899f, 0.584313750f, 0.929411829f};
const Color Cornsilk = {1.000000000f, 0.972549081f, 0.862745166f};
const Color Crimson = {0.862745166f, 0.078431375f, 0.235294133f};
const Color Cyan = {0.000000000f, 1.000000000f, 1.000000000f};
const Color DarkBlue = {0.000000000f, 0.000000000f, 0.545098066f};
const Color DarkCyan = {0.000000000f, 0.545098066f, 0.545098066f};
const Color DarkGoldenrod = {0.721568644f, 0.525490224f, 0.043137256f};
const Color DarkGray = {0.662745118f, 0.662745118f, 0.662745118f};
const Color DarkGreen = {0.000000000f, 0.392156899f, 0.000000000f};
const Color DarkKhaki = {0.741176486f, 0.717647076f, 0.419607878f};
const Color DarkMagenta = {0.545098066f, 0.000000000f, 0.545098066f};
const Color DarkOliveGreen = {0.333333343f, 0.419607878f, 0.184313729f};
const Color DarkOrange = {1.000000000f, 0.549019635f, 0.000000000f};
const Color DarkOrchid = {0.600000024f, 0.196078449f, 0.800000072f};
const Color DarkRed = {0.545098066f, 0.000000000f, 0.000000000f};
const Color DarkSalmon = {0.913725555f, 0.588235319f, 0.478431404f};
const Color DarkSeaGreen = {0.560784340f, 0.737254918f, 0.545098066f};
const Color DarkSlateBlue = {0.282352954f, 0.239215702f, 0.545098066f};
const Color DarkSlateGray = {0.184313729f, 0.309803933f, 0.309803933f};
const Color DarkTurquoise = {0.000000000f, 0.807843208f, 0.819607913f};
const Color DarkViolet = {0.580392182f, 0.000000000f, 0.827451050f};
const Color DeepPink = {1.000000000f, 0.078431375f, 0.576470613f};
const Color DeepSkyBlue = {0.000000000f, 0.749019623f, 1.000000000f};
const Color DimGray = {0.411764741f, 0.411764741f, 0.411764741f};
const Color DodgerBlue = {0.117647067f, 0.564705908f, 1.000000000f};
const Color Firebrick = {0.698039234f, 0.133333340f, 0.133333340f};
const Color FloralWhite = {1.000000000f, 0.980392218f, 0.941176534f};
const Color ForestGreen = {0.133333340f, 0.545098066f, 0.133333340f};
const Color Fuchsia = {1.000000000f, 0.000000000f, 1.000000000f};
const Color Gainsboro = {0.862745166f, 0.862745166f, 0.862745166f};
const Color GhostWhite = {0.972549081f, 0.972549081f, 1.000000000f};
const Color Gold = {1.000000000f, 0.843137324f, 0.000000000f};
const Color Goldenrod = {0.854902029f, 0.647058845f, 0.125490203f};
const Color Gray = {0.501960814f, 0.501960814f, 0.501960814f};
const Color Green = {0.000000000f, 0.501960814f, 0.000000000f};
const Color GreenYellow = {0.678431392f, 1.000000000f, 0.184313729f};
const Color Honeydew = {0.941176534f, 1.000000000f, 0.941176534f};
const Color HotPink = {1.000000000f, 0.411764741f, 0.705882370f};
const Color IndianRed = {0.803921640f, 0.360784322f, 0.360784322f};
const Color Indigo = {0.294117659f, 0.000000000f, 0.509803951f};
const Color Ivory = {1.000000000f, 1.000000000f, 0.941176534f};
const Color Khaki = {0.941176534f, 0.901960850f, 0.549019635f};
const Color Lavender = {0.901960850f, 0.901960850f, 0.980392218f};
const Color LavenderBlush = {1.000000000f, 0.941176534f, 0.960784376f};
const Color LawnGreen = {0.486274540f, 0.988235354f, 0.000000000f};
const Color LemonChiffon = {1.000000000f, 0.980392218f, 0.803921640f};
const Color LightBlue = {0.678431392f, 0.847058892f, 0.901960850f};
const Color LightCoral = {0.941176534f, 0.501960814f, 0.501960814f};
const Color LightCyan = {0.878431439f, 1.000000000f, 1.000000000f};
const Color LightGoldenrodYellow = {0.980392218f, 0.980392218f, 0.823529482f};
const Color LightGreen = {0.564705908f, 0.933333397f, 0.564705908f};
const Color LightGray = {0.827451050f, 0.827451050f, 0.827451050f};
const Color LightPink = {1.000000000f, 0.713725507f, 0.756862819f};
const Color LightSalmon = {1.000000000f, 0.627451003f, 0.478431404f};
const Color LightSeaGreen = {0.125490203f, 0.698039234f, 0.666666687f};
const Color LightSkyBlue = {0.529411793f, 0.807843208f, 0.980392218f};
const Color LightSlateGray = {0.466666698f, 0.533333361f, 0.600000024f};
const Color LightSteelBlue = {0.690196097f, 0.768627524f, 0.870588303f};
const Color LightYellow = {1.000000000f, 1.000000000f, 0.878431439f};
const Color Lime = {0.000000000f, 1.000000000f, 0.000000000f};
const Color LimeGreen = {0.196078449f, 0.803921640f, 0.196078449f};
const Color Linen = {0.980392218f, 0.941176534f, 0.901960850f};
const Color Magenta = {1.000000000f, 0.000000000f, 1.000000000f};
const Color Maroon = {0.501960814f, 0.000000000f, 0.000000000f};
const Color MediumAquamarine = {0.400000036f, 0.803921640f, 0.666666687f};
const Color MediumBlue = {0.000000000f, 0.000000000f, 0.803921640f};
const Color MediumOrchid = {0.729411781f, 0.333333343f, 0.827451050f};
const Color MediumPurple = {0.576470613f, 0.439215720f, 0.858823597f};
const Color MediumSeaGreen = {0.235294133f, 0.701960802f, 0.443137288f};
const Color MediumSlateBlue = {0.482352972f, 0.407843173f, 0.933333397f};
const Color MediumSpringGreen = {0.000000000f, 0.980392218f, 0.603921592f};
const Color MediumTurquoise = {0.282352954f, 0.819607913f, 0.800000072f};
const Color MediumVioletRed = {0.780392230f, 0.082352944f, 0.521568656f};
const Color MidnightBlue = {0.098039225f, 0.098039225f, 0.439215720f};
const Color MintCream = {0.960784376f, 1.000000000f, 0.980392218f};
const Color MistyRose = {1.000000000f, 0.894117713f, 0.882353008f};
const Color Moccasin = {1.000000000f, 0.894117713f, 0.709803939f};
const Color NavajoWhite = {1.000000000f, 0.870588303f, 0.678431392f};
const Color Navy = {0.000000000f, 0.000000000f, 0.501960814f};
const Color OldLace = {0.992156923f, 0.960784376f, 0.901960850f};
const Color Olive = {0.501960814f, 0.501960814f, 0.000000000f};
const Color OliveDrab = {0.419607878f, 0.556862772f, 0.137254909f};
const Color Orange = {1.000000000f, 0.647058845f, 0.000000000f};
const Color OrangeRed = {1.000000000f, 0.270588249f, 0.000000000f};
const Color Orchid = {0.854902029f, 0.439215720f, 0.839215755f};
const Color PaleGoldenrod = {0.933333397f, 0.909803987f, 0.666666687f};
const Color PaleGreen = {0.596078455f, 0.984313786f, 0.596078455f};
const Color PaleTurquoise = {0.686274529f, 0.933333397f, 0.933333397f};
const Color PaleVioletRed = {0.858823597f, 0.439215720f, 0.576470613f};
const Color PapayaWhip = {1.000000000f, 0.937254965f, 0.835294187f};
const Color PeachPuff = {1.000000000f, 0.854902029f, 0.725490212f};
const Color Peru = {0.803921640f, 0.521568656f, 0.247058839f};
const Color Pink = {1.000000000f, 0.752941251f, 0.796078503f};
const Color Plum = {0.866666734f, 0.627451003f, 0.866666734f};
const Color PowderBlue = {0.690196097f, 0.878431439f, 0.901960850f};
const Color Purple = {0.501960814f, 0.000000000f, 0.501960814f};
const Color Red = {1.000000000f, 0.000000000f, 0.000000000f};
const Color RosyBrown = {0.737254918f, 0.560784340f, 0.560784340f};
const Color RoyalBlue = {0.254901975f, 0.411764741f, 0.882353008f};
const Color SaddleBrown = {0.545098066f, 0.270588249f, 0.074509807f};
const Color Salmon = {0.980392218f, 0.501960814f, 0.447058856f};
const Color SandyBrown = {0.956862807f, 0.643137276f, 0.376470625f};
const Color SeaGreen = {0.180392161f, 0.545098066f, 0.341176480f};
const Color SeaShell = {1.000000000f, 0.960784376f, 0.933333397f};
const Color Sienna = {0.627451003f, 0.321568638f, 0.176470593f};
const Color Silver = {0.752941251f, 0.752941251f, 0.752941251f};
const Color SkyBlue = {0.529411793f, 0.807843208f, 0.921568692f};
const Color SlateBlue = {0.415686309f, 0.352941185f, 0.803921640f};
const Color SlateGray = {0.439215720f, 0.501960814f, 0.564705908f};
const Color Snow = {1.000000000f, 0.980392218f, 0.980392218f};
const Color SpringGreen = {0.000000000f, 1.000000000f, 0.498039246f};
const Color SteelBlue = {0.274509817f, 0.509803951f, 0.705882370f};
const Color Tan = {0.823529482f, 0.705882370f, 0.549019635f};
const Color Teal = {0.000000000f, 0.501960814f, 0.501960814f};
const Color Thistle = {0.847058892f, 0.749019623f, 0.847058892f};
const Color Tomato = {1.000000000f, 0.388235331f, 0.278431386f};
const Color Turquoise = {0.250980407f, 0.878431439f, 0.815686345f};
const Color Violet = {0.933333397f, 0.509803951f, 0.933333397f};
const Color Wheat = {0.960784376f, 0.870588303f, 0.701960802f};
const Color White = {1.000000000f, 1.000000000f, 1.000000000f};
const Color WhiteSmoke = {0.960784376f, 0.960784376f, 0.960784376f};
const Color Yellow = {1.000000000f, 1.000000000f, 0.000000000f};
const Color YellowGreen = {0.603921592f, 0.803921640f, 0.196078449f};
};
const Color system_circle_color{1.0f, 1.0f, 1.0f, 1.0f};
class Texture_Atlas_Subrect
{
public:
Texture_Atlas* texture_atlas;
Vector2f topleft; // topleft and size are RELATIVE coordinates, from 0.0f to 1.0f!
Vector2f size;
Texture_Atlas_Subrect(Texture_Atlas* atlas, Vector2f topleft_arg, Vector2f size_arg) :
texture_atlas(atlas),
topleft(topleft_arg),
size(size_arg)
{}
};
class Animation
{
public:
Texture_Animated* texture_animated;
float current_frame = 0.0f;
bool looping;
bool to_be_deleted = false;
Animation(Texture_Animated* texture, bool looping_arg);
void advance();
};
class Transformable
{
public:
Transformable(Vector2d position_arg, double orientation_arg, Vector2d scaling_arg) :
position(position_arg),
rotation(orientation_arg),
scaling(scaling_arg),
Transformation(GetTranslationMatrix(position_arg) * GetRotationMatrixY(orientation_arg) * GetScalingMatrix(scaling_arg))
{
InverseTransformation = GetInverse(Transformation);
}
Transformable()
{}
inline const Vector2d& getPosition() const
{
return position;
}
inline double getRotation() const
{
return rotation;
}
inline const Vector2d& getScaling() const
{
return scaling;
}
inline void setPosition(Vector2d pos)
{
position = pos;
TransformationNeedsUpdate = true;
InverseTransformationNeedsUpdate = true;
}
inline void setRotation(double rad)
{
rotation = normalize_angle_rad(rad);
TransformationNeedsUpdate = true;
InverseTransformationNeedsUpdate = true;
}
inline void setScaling(Vector2d scale)
{
scaling = scale;
TransformationNeedsUpdate = true;
InverseTransformationNeedsUpdate = true;
}
const Matrix3x3d& getTransformation()
{
if (TransformationNeedsUpdate)
{
//recompute Transformation
Transformation = GetTranslationMatrix(position) * GetRotationMatrixY(rotation) * GetScalingMatrix(scaling);
TransformationNeedsUpdate = false;
}
return Transformation;
}
const Matrix3x3d& getInverseTransformation()
{
if (InverseTransformationNeedsUpdate)
{
//recompute InverseTransformation
InverseTransformation = GetInverse(GetTranslationMatrix(position) * GetRotationMatrixY(rotation) * GetScalingMatrix(scaling));
InverseTransformationNeedsUpdate = false;
}
return InverseTransformation;
}
private:
Vector2d position; //global world position
double rotation;
Vector2d scaling;
Matrix3x3d Transformation; //translation, rotation, scaling
bool TransformationNeedsUpdate = false;
Matrix3x3d InverseTransformation;
bool InverseTransformationNeedsUpdate = false;
};
// optimized for purely translatable sprites, rotation and scaling is set at construction and fixed, no inverse transformation, direct matrix editing for translation
class Translatable
{
public:
Translatable(Vector2d position_arg, double orientation_arg, Vector2d scaling_arg) :
position(position_arg),
rotation(orientation_arg),
scaling(scaling_arg),
CombinedRotationScaling(GetRotationMatrixY(orientation_arg) * GetScalingMatrix(scaling_arg))
{
Transformation = GetTranslationMatrix(position_arg) * CombinedRotationScaling;
}
Translatable()
{}
inline const Vector2d& getPosition() const
{
return position;
}
inline double getRotation() const
{
return rotation;
}
inline const Vector2d& getScaling() const
{
return scaling;
}
inline void setPosition(Vector2d pos)
{
position = pos;
Transformation.matrix[0][2] = pos.x;
Transformation.matrix[1][2] = pos.y;
}
inline void translate(Vector2d velocity)
{
position += velocity;
Transformation.matrix[0][2] += velocity.x;
Transformation.matrix[1][2] += velocity.y;
}
const Matrix3x3d& getTransformation()
{
return Transformation;
}
private:
Vector2d position; //global world position
double rotation;
Vector2d scaling;
Matrix3x3d Transformation; //translation, rotation, scaling
Matrix3x3d CombinedRotationScaling;
};
class Sprite_Translatable_Only : public Translatable
{
public:
Color color;
Draw_Layer drawing_layer;
variant<Texture_Atlas_Subrect*, Animation> graphics;
Blend_Mode blending_mode;
Shader_Lighting_Mode shader_lighting_mode; // 0: normal lighting, 1: no lighting
Sprite_Translatable_Only(Shader_Lighting_Mode shader_lighting_mode_arg, Color color_arg, Draw_Layer drawing_layer_arg, variant<Texture_Atlas_Subrect*, Animation> graphics_arg, Blend_Mode blending_mode_arg,
Vector2d position_arg, double orientation_arg, Vector2d scaling_arg) :
Translatable(position_arg, orientation_arg, scaling_arg),
shader_lighting_mode(shader_lighting_mode_arg),
color(color_arg),
drawing_layer(drawing_layer_arg),
graphics(graphics_arg),
blending_mode(blending_mode_arg)
{}
Sprite_Translatable_Only()
{}
void Enqueue();
};
class Camera final
{
private:
double zoom = 1.0;
public:
Vector2d position;
Vector2d position_leading;
double zoom_leading = 1.0;
// Projection matrix, initialized as identity matrix
XMMATRIX projection_matrix = DirectX::XMMatrixIdentity();
DoubleRect relative_rectangle;
DoubleRect world_rectangle;
Vector2d scaling;
bool zoom_changed = false;
Camera()
{}
//refresh scaling and relative bounding box
void refresh()
{
scaling = Vector2d(screen_resolution.x * zoom, screen_resolution.y * zoom);
Vector2d cam_scale_half = scaling / 2.0;
Vector2d cam_topleft = cam_scale_half.get_negated();
relative_rectangle = DoubleRect{cam_topleft, scaling};
world_rectangle = DoubleRect{cam_topleft + position, scaling};
}
inline double get_zoom() const
{
return zoom;
}
void set_zoom(double new_zoom)
{
zoom = new_zoom;
zoom_changed = true;
refresh();
update_projection_matrix();
}
void Step();
void update_projection_matrix()
{
// Update projection matrix - uses DirectX function to build an orthogonal projection matrix for a left-handed coordinate system.
projection_matrix = DirectX::XMMatrixOrthographicLH(float(screen_resolution.x) * float(zoom), float(screen_resolution.y) * float(zoom), 0.0f, 1.0f);
}
};
Camera camera;
class Sprite : public Transformable
{
public:
Color color;
Draw_Layer drawing_layer;
variant<Texture_Atlas_Subrect*, Animation> graphics;
Texture_Atlas_Subrect* glowmap_subrect;
Blend_Mode blending_mode;
Shader_Lighting_Mode shader_lighting_mode; // 0 = normal lighting, 1 = no lighting
Sprite(Shader_Lighting_Mode shader_lighting_mode_arg, Color color_arg, Draw_Layer drawing_layer_arg, variant<Texture_Atlas_Subrect*, Animation> graphics_arg, Blend_Mode blending_mode_arg,
Vector2d position_arg, double orientation_arg, Vector2d scaling_arg, Texture_Atlas_Subrect* glowmap_subrect_arg = nullptr) :
Transformable(position_arg, orientation_arg, scaling_arg),
shader_lighting_mode(shader_lighting_mode_arg),
color(color_arg),
drawing_layer(drawing_layer_arg),
graphics(graphics_arg),
glowmap_subrect(glowmap_subrect_arg),
blending_mode(blending_mode_arg)
{}
Sprite()
{}
DoubleRect get_sprite_bounding_box(bool world_coords);
void Enqueue();
};
Sprite background_sprite;
class Icon
{
public:
Sprite sprite;
Vector2d size_on_screen{double(screen_resolution.x) * 0.0125,
double(screen_resolution.x) * 0.0125}; //size of the icon in pixels
Icon(Color color_arg,
Draw_Layer drawing_layer_arg,
Blend_Mode blending_mode_arg,
Texture_Atlas_Subrect* texture_subrect_arg,
Vector2d size_on_screen_arg) :
sprite(Shader_Lighting_Mode::no_lighting, color_arg, drawing_layer_arg, texture_subrect_arg, blending_mode_arg,
Vector2d{0.0,0.0}, 0, size_on_screen_arg * camera.get_zoom()),
size_on_screen(size_on_screen_arg)
{}
};
// A function to convert a line defined by vertices with a linestrip topology (a continuous line connecting all the points)
// to a line defined by vertices with a linelist topology (composed of line segments, two vertices for each line segment).
// Use to render a linestrip using the Linelist class.
vector<Vector2d> linestrip_to_linelist(vector<Vector2d>& linestrip_verts)
{
vector<Vector2d> linelist_verts;
linelist_verts.reserve((linestrip_verts.size() - 1) * 2);
for (int i = 0; i < linestrip_verts.size() - 1; ++i)
{
linelist_verts.emplace_back(linestrip_verts[i]);
linelist_verts.emplace_back(linestrip_verts[i + 1]);
}
return linelist_verts;
}
vector<Color> linestrip_colors_to_linelist(vector<Color>& linestrip_colors)
{
vector<Color> linelist_colors;
linelist_colors.reserve((linestrip_colors.size() - 1) * 2);
for (int i = 0; i < linestrip_colors.size() - 1; ++i)
{
linelist_colors.emplace_back(linestrip_colors[i]);
linelist_colors.emplace_back(linestrip_colors[i + 1]);
}
return linelist_colors;
}
class Linelist final
{
public:
Draw_Layer drawing_layer;
vector<Vector2d> vertices;
Blend_Mode blending_mode;
vector<Color> colors; // contains either a single color for all vertices, or multiple colors - one for each vertex
Linelist(Color color_arg, Draw_Layer drawing_layer_arg, Blend_Mode blending_mode_arg) :
drawing_layer(drawing_layer_arg), blending_mode(blending_mode_arg)
{
colors.emplace_back(color_arg);
}
Linelist(Color color_arg, Draw_Layer drawing_layer_arg, Blend_Mode blending_mode_arg, vector<Vector2d> vertices_arg) :
drawing_layer(drawing_layer_arg), blending_mode(blending_mode_arg), vertices(vertices_arg)
{
colors.emplace_back(color_arg);
}
Linelist(Draw_Layer drawing_layer_arg, Blend_Mode blending_mode_arg, vector<Vector2d> vertices_arg, vector<Color> vertex_colors_arg) :
drawing_layer(drawing_layer_arg), blending_mode(blending_mode_arg), vertices(vertices_arg), colors(vertex_colors_arg)
{}
void Enqueue();
};
Linelist jump_node_lines{Colors::Aquamarine, Draw_Layer::Background_and_Universe, Blend_Mode::Opaque};
enum class Light_Type
{
point, tube, spotlight
};
class Light final
{
public:
Color color;
float intensity;
float falloff_exp;
Vector2d position1;
Vector2d position2; //for tube light
float direction; // for spotlight
float cone_exp;
Light_Type type;
bool disabled;
double culling_threshold_distance;
Light()
{}
// point
Light(bool disabled_arg,
Color color_arg, float intensity_arg, float falloff_exp_arg,
Vector2d position_arg) :
disabled(disabled_arg),
type(Light_Type::point),
color(color_arg),
intensity(intensity_arg),
falloff_exp(falloff_exp_arg),
position1(position_arg)
{
set_culling_threshold_distance();
}
// tube
Light(bool disabled_arg,
Color color_arg, float intensity_arg, float falloff_exp_arg,
Vector2d position1_arg, Vector2d position2_arg) :
disabled(disabled_arg),
type(Light_Type::tube),
color(color_arg),
intensity(intensity_arg),
falloff_exp(falloff_exp_arg),
position1(position1_arg),
position2(position2_arg)
{
set_culling_threshold_distance();
}
// spotlight
Light(bool disabled_arg,
Color color_arg, float intensity_arg, float falloff_exp_arg,
Vector2d position_arg,
float direction_arg, float cone_exp_arg) :
disabled(disabled_arg),
type(Light_Type::spotlight),
color(color_arg),
intensity(intensity_arg),
falloff_exp(falloff_exp_arg),
position1(position_arg),
direction(direction_arg),
cone_exp(cone_exp_arg)
{
set_culling_threshold_distance();
}
inline void set_culling_threshold_distance();
void Enqueue();
};
//color code for use in Deferred String color
unsigned long get_string_color(int a, int b, int g, int r)
{
return ((a & 0xff) << 24) + ((b & 0xff) << 16) + ((g & 0xff) << 8)
+ (r & 0xff);
}
class Particle
{
public:
Sprite_Translatable_Only sprite;
int lifetime;
Vector2d velocity;
Particle(Vector2d position_arg, Vector2d scaling_arg, Color color_arg, Draw_Layer drawing_layer_arg,
int lifetime_arg, Vector2d velocity_arg) :
sprite(Shader_Lighting_Mode::no_lighting, color_arg, drawing_layer_arg, name_texture_atlas_subrect_map["particle"], Blend_Mode::Additive,
position_arg, 0.0, scaling_arg),
lifetime(lifetime_arg),
velocity(velocity_arg)
{}
void Step()
{
sprite.translate(velocity);
lifetime -= 1;
}
};
class Effect
{
public:
Sprite_Translatable_Only sprite;
Vector2d velocity;
Light light;
Effect()
{}
Effect(Vector2d position_arg, double rotation_arg, Vector2d scaling_arg, Color color_arg, Animation* animation_arg, Blend_Mode blending_mode_arg, Draw_Layer draw_layer_arg,
Vector2d velocity_arg, bool light_disabled_arg = true, const float light_falloff_exponent_arg = 0.0f, const Color& light_diffuse_arg = Color{0.0f, 0.0f, 0.0f}, float light_intensity_arg = 0.0f) :
sprite(Shader_Lighting_Mode::no_lighting, color_arg, draw_layer_arg, *animation_arg, blending_mode_arg,
position_arg, rotation_arg, scaling_arg),
velocity(velocity_arg),
light(light_disabled_arg, light_diffuse_arg, light_intensity_arg, light_falloff_exponent_arg, position_arg)
{
Animation& animation = get<Animation>(sprite.graphics);
animation.looping = false;
}
void Step();
};
class Deferred_String
{
public:
wstring str;
float font_size;
Vector2f screen_position;
unsigned int color = 0xff66cd00;
Deferred_String(wstring str_arg,
float font_size_arg,
Vector2f screen_position_arg,
unsigned int color_arg) :
str(str_arg),
font_size(font_size_arg),
screen_position(screen_position_arg),
color(color_arg)
{}
};
vector<Deferred_String> strings_to_draw;
void draw_string_deferred(string& str, float font_size, float screen_pos_x, float screen_pos_y, unsigned int color = 0xff66cd00)
{
strings_to_draw.emplace_back(string_to_wstring(str), font_size, Vector2f(screen_pos_x, screen_pos_y), color);
}
class Vertex final
{
public:
int32_t texture_ID;
int32_t lighting_mode; // 0: normal lighting, 1: no lighting
XMFLOAT2 position;