forked from AmigurumiShaders/DX11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
247 lines (195 loc) · 7.01 KB
/
Game.cpp
File metadata and controls
247 lines (195 loc) · 7.01 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
#include "Game.h"
#include "Vertex.h"
#include "Input.h"
#include "WICTextureLoader.h"
#include "RaytracingHelper.h"
// Needed for a helper function to read compiled shader files from the hard drive
#pragma comment(lib, "d3dcompiler.lib")
#include <d3dcompiler.h>
// For the DirectX Math library
using namespace DirectX;
// Helper macro for getting a float between min and max
#define RandomRange(min, max) (float)rand() / RAND_MAX * (max - min) + min
// --------------------------------------------------------
// Constructor
//
// DXCore (base class) constructor will set up underlying fields.
// DirectX itself, and our window, are not ready yet!
//
// hInstance - the application's OS-level handle (unique ID)
// --------------------------------------------------------
Game::Game(HINSTANCE hInstance)
: DXCore(
hInstance, // The application's handle
"DirectX Game", // Text for the window's title bar
1280, // Width of the window's client area
720, // Height of the window's client area
true), // Show extra stats (fps) in title bar?
vsync(false)
{
#if defined(DEBUG) || defined(_DEBUG)
// Do we want a console window? Probably only in debug mode
CreateConsoleWindow(500, 120, 32, 120);
printf("Console window created successfully. Feel free to printf() here.\n");
#endif
renderer = std::make_shared<SDFRenderer>();
psData = dynamic_cast<SDFRenderer&>(*renderer).GetPSData();
sdfMaterials = dynamic_cast<SDFRenderer&>(*renderer).GetMaterialBuffer();
}
// --------------------------------------------------------
// Destructor - Clean up anything our game has created:
// - Release all DirectX objects created here
// - Delete any objects to prevent memory leaks
// --------------------------------------------------------
Game::~Game()
{
// Note: Since we're using smart pointers (ComPtr),
// we don't need to explicitly clean up those DirectX objects
// - If we weren't using smart pointers, we'd need
// to call Release() on each DirectX object created in Game
//dx12 - We need to wait here until the GPU is actually done with its work
DX12Helper::GetInstance().WaitForGPU();
}
// --------------------------------------------------------
// Called once per program, after DirectX and the window
// are initialized but before the game loop.
// --------------------------------------------------------
void Game::Init()
{
// Seed random
srand((unsigned int)time(0));
camera = std::make_shared<Camera>(
0.0f, 0.0f, -12.0f, // Position
3.0f, // Move speed
1.0f, // Mouse look
this->width / (float)this->height); // Aspect ratio
sdfEntities = std::make_shared<std::vector<SDFEntity>>();
//sdfMaterials = std::make_shared<std::vector<SDFMaterial>>();
//sdfMaterials->push_back(SDFMaterial{});
InitSDFRenderer();
// I put this in the sdfrenderer init method for now
//// Ensure the command list is closed going into Draw for the first time
//commandList->Close();
}
void Game::InitSDFRenderer()
{
//sdfEntities->
//CreateSDFEntity();
sdfRenderer = renderer; //this is lazy and bad im sorry
sdfRenderer->Init(vsync, camera, sdfEntities);//, *this);
}
void Game::CreateSDFEntity()
{
sdfEntities->push_back(SDFEntity(sdfEntities->size(),psData));
selectedEntityIndex = sdfEntities->size()-1;
}
void Game::CreateSDFMaterial()
{
selectedMaterialIndex = materialCount++;
psData->materialCount++;
}
// --------------------------------------------------------
// Handle resizing DirectX "stuff" to match the new window size.
// For instance, updating our projection matrix's aspect ratio.
// --------------------------------------------------------
void Game::OnResize()
{
// Handle base-level DX resize stuff
//DXCore::OnResize();
sdfRenderer->OnResize(width, height);
}
void Game::UpdateGUI()
{
//io.WantCaptureMouse = true;
static float f = 0.0f;
ImGui_ImplDX12_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Settings", NULL, ImGuiWindowFlags_MenuBar); // Create a window with a name and append into it.
//SDFEntity::GetSDFEntity()->DisplaySDFSettings();
SDFMainGUI();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
void Game::SDFMainGUI()
{
if (ImGui::Button("AddEntity"))
{
CreateSDFEntity();
}
if (ImGui::Button("AddMaterial"))
{
CreateSDFMaterial();
}
if (ImGui::BeginListBox("Entities"))
{
for (int i = 0; i < sdfEntities->size(); i++)
{
const bool is_selected = (selectedEntityIndex == i);
const char* name = sdfEntities->at(i).GetName()->c_str();
if (ImGui::Selectable(name, is_selected))
selectedEntityIndex = i;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
if (ImGui::BeginListBox("Materials"))
{
for (int i = 0; i < materialCount; i++)
{
const bool is_selected = (selectedMaterialIndex == i);
char name[10] ;
sprintf_s(name, "%d", i);
if (ImGui::Selectable(name, is_selected))
selectedMaterialIndex = i;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
ImGui::SeparatorText("Material Settings");
ImGui::ColorEdit3("Diffuse", (float*)&sdfMaterials[selectedMaterialIndex].diffuseColor);
//ImGui::ColorEdit3("Specular", (float*)&sdfMaterials->at(selectedMaterialIndex).specularColor);
//ImGui::SliderFloat3("Position", (float*)&thisEntData.spherePrims[primitives[selectedIndex].idx].Position, -50.0, 50.0);
ImGui::SliderFloat("specularity", &sdfMaterials[selectedMaterialIndex].shininess, 0, 100);
ImGui::Separator();
ImGui::SeparatorText("Light Settings");
ImGui::SliderFloat3("light position", (float*)&(psData->lightPosition), -100, 100);
ImGui::SeparatorText("Procedural Terrain parameters");
ImGui::SliderFloat("height", &(psData->height), -1, 1);
ImGui::SliderFloat("frequency", &(psData->anim), -10, 10);
ImGui::Separator();
if (sdfEntities->size() > selectedEntityIndex)
{
sdfEntities->at(selectedEntityIndex).UpdateGUI();
}
}
// --------------------------------------------------------
// Update your game here - user input, move objects, AI, etc.
// --------------------------------------------------------
void Game::Update(float deltaTime, float totalTime)
{
camera->Update(deltaTime);
//for (int i = 0; i < entities.size(); i++)
//{
// entities[i]->GetTransform()->Rotate(0.0f, 0.3f * deltaTime, 0.0f);
//}
UpdateGUI();
//UIManager::UIUpdate();
// Example input checking: Quit if the escape key is pressed
if (Input::GetInstance().KeyDown(VK_ESCAPE))
Quit();
}
// --------------------------------------------------------
// Clear the screen, redraw everything, present to the user
// --------------------------------------------------------
void Game::Draw(float deltaTime, float totalTime)
{
// Should create a new entity for sdf structures.
sdfRenderer->Render();
//sdfRenderer->RenderEntity(sdfEntities);
DX12Helper& dx12HelperInst = DX12Helper::GetInstance();
}