-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
131 lines (112 loc) · 3.21 KB
/
App.cpp
File metadata and controls
131 lines (112 loc) · 3.21 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
#include "App.h"
#include <memory>
#include "GDIPlusManager.h"
#include "ChiliMath.h"
#include "imgui-1.88/imgui.h"
#include "Box.h"
#include "SkinnedBox.h"
#include "Sheet.h"
#include "Surface.h"
#include "Cylinder.h"
#include "Pyramid.h"
#include "AssetTest.h"
GDIPlusManager gdipm;
App::App():wnd(800, 600, "Direct X Test"), timer(), light(wnd.Gfx()){
class Factory
{
public:
Factory(Graphics& gfx)
:
gfx(gfx)
{}
std::unique_ptr<Drawable> operator()()
{
const DirectX::XMFLOAT3 material = { cdist(rng), cdist(rng), cdist(rng) };
switch (typedist(rng))
{
case 0:
return std::make_unique<Box>(
gfx, rng, adist, ddist,
odist, rdist, bdist,
material
);
case 1:
return std::make_unique<Cylinder>(
gfx, rng, adist, ddist,
odist, rdist, bdist,
tessdist
);
case 2:
return std::make_unique<Pyramid>(
gfx, rng, adist, ddist,
odist, rdist, tessdist);
case 3:
return std::make_unique<SkinnedBox>(
gfx, rng, adist, ddist,
odist, rdist
);
case 4:
return std::make_unique<AssetTest>(
gfx, rng, adist, ddist,
odist, rdist, material, 1.5f);
case 5:
return std::make_unique<Sheet>(
gfx, rng, adist, ddist,
odist, rdist
);
default:
assert(false && "bad drawable type in factory");
return {};
}
}
private:
Graphics& gfx;
std::mt19937 rng{ std::random_device{}() };
std::uniform_real_distribution<float> adist{ 0.0f,PI * 2.0f };
std::uniform_real_distribution<float> ddist{ 0.0f,PI * 0.5f };
std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f };
std::uniform_real_distribution<float> rdist{ 6.0f,20.0f };
std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
std::uniform_real_distribution<float> cdist{ 0.0f,1.0f };
std::uniform_int_distribution<int> tessdist{ 3, 30 };
std::uniform_int_distribution<int> latdist{ 5,20 };
std::uniform_int_distribution<int> longdist{ 10,40 };
std::uniform_int_distribution<int> typedist{ 0,4 };
};
Factory f(wnd.Gfx());
drawables.reserve(nDrawables);
std::generate_n(std::back_inserter(drawables), nDrawables, f);
wnd.Gfx().SetProjecton(DirectX::XMMatrixPerspectiveLH(1.0, 3.0f / 4.0f, 0.5f, 40.0f));
}
App::~App() {}
int App::Go() {
while (true) {
if (const auto ecode = Window::ProcessMessages()) {
return *ecode;
}
DoFrame();
}
}
void App::DoFrame() {
auto dt = timer.Mark() * speed_factor;
wnd.Gfx().BeginFrame(0.07f, 0.0f, 0.12f);
wnd.Gfx().SetCamera(cam.GetMatrix());
light.Bind(wnd.Gfx(), cam.GetMatrix());
for (auto& b : drawables) {
b->Update(wnd.kbd.KeyIsPressed(VK_SPACE) ? 0.0f : dt);
b->Draw(wnd.Gfx());
}
light.Draw(wnd.Gfx());
// Window for Controlling Simulation Speed
if (ImGui::Begin("Simulation Speed")) {
ImGui::SliderFloat("Speed Factor", &speed_factor, 0.0f, 4.0f);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Text("Status %s", wnd.kbd.KeyIsPressed(VK_SPACE) ? "PAUSED" : "RUNNING (hold space to pause)");
}
ImGui::End();
// Window for Controlling Camera Settings
cam.SpawnControlWindow();
// Window for Controlling Light Settings
light.SpawnControlWindow();
wnd.Gfx().EndFrame();
}