-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
195 lines (157 loc) · 5.24 KB
/
main.cpp
File metadata and controls
195 lines (157 loc) · 5.24 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
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shlobj.h>
#include <stdint.h>
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#define THIRTEEN_IMPLEMENTATION
#include "external/thirteen.h"
#include "renderer/fps_meter.h"
#include "common/profile.h"
#include "renderer/render_stats.h"
#include "shared.h"
#include "renderer/raster.h"
#include "renderer/texture_loader.h"
#define FPS_METER_WIDTH 200
#define FPS_METER_HEIGHT 70
extern bool SceneInitialize();
extern void SceneDestroy();
extern void SceneRenderFrame();
extern void SceneRenderOverlay2D();
bool WireFrameOverlay = false;
float DeltaTime;
static bool ShowHelp = false;
static bool ShowPerformanceMetrics = true;
static ULONGLONG LastTime;
static char ApplicationDataPath[MAX_PATH];
static void CaptureScreenshot();
typedef struct Command
{
int KeyCode;
const char* Help;
void(*CommandFunc)();
} Command;
static Command Commands[] =
{
{ VK_F1, "Toggle Help", []() { ShowHelp = !ShowHelp; }},
{ 'W', "Toggle Wireframe Overlay", []() { WireFrameOverlay = !WireFrameOverlay; } },
{ 'H', "Toggle Perf Metrics", []() { ShowPerformanceMetrics = !ShowPerformanceMetrics; } },
#if PJD_DEBUG_VIEW_ENABLED
{ '0', "Scene Rendering", []() { SetDebugMode(DM_None); }},
{ '1', "Show Face Mip-Levels", []() { SetDebugMode(DM_FaceMipMapLevel); } },
{ '2', "Show Face Derivatives", []() { SetDebugMode(DM_FaceDerivatives); } },
{ '3', "Show Tile Classification", []() { SetDebugMode(DM_TileClassification); } },
{ '4', "Show Depth", []() { SetDebugMode(DM_DepthBuffer); } },
#endif
{ VK_F11,"Save Screenshot", []() { CaptureScreenshot(); } },
{ 0, 0 }
};
static void HandleUserInput()
{
for (int i = 0; Commands[i].Help; ++i)
{
if (Thirteen::GetKey(Commands[i].KeyCode) && !Thirteen::GetKeyLastFrame(Commands[i].KeyCode)) {
Commands[i].CommandFunc();
}
}
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
{
uint8_t* frameBuffer = Thirteen::Init(FB_WIDTH, FB_HEIGHT, false);
Thirteen::SetVSync(false);
ProfilerInitialize();
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_APPDATA, NULL, 0, ApplicationDataPath)))
{
strcat_s(ApplicationDataPath, "/SoftwareRasterizer");
if (!CreateDirectoryA(ApplicationDataPath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
return 0;
}
}
float* depthBuffer = (float*)_aligned_malloc(FB_WIDTH * FB_HEIGHT * sizeof(float), 32);
RasterizerInitialize({
.BufferDesc = {
.Width = FB_WIDTH,
.Height = FB_HEIGHT
},
.FrameBufferPtr = frameBuffer,
.DepthBufferPtr = depthBuffer
});
if (!SceneInitialize()) {
MessageBox(NULL, L"Failed to load mesh", L"Error", MB_OK | MB_ICONERROR);
return 0;
}
FPSMeterInitialize();
LastTime = GetTickCount64();
while (true)
{
ULONGLONG currentTime = GetTickCount64();
DeltaTime = (currentTime - LastTime) / 1000.0f;
LastTime = currentTime;
HandleUserInput();
ProfilerReset();
#if PJD_USE_RENDER_STATS
ResetRenderStats();
#endif // PJD_USE_RENDER_STATS
{
PROFILE_AUTO("Scene Frame Render");
SceneRenderFrame();
}
ResolveFrameBuffer();
// draw directly to screen after resolve
RasterMode2D(true);
{
PROFILE_AUTO("Scene Overlay 2D");
SceneRenderOverlay2D();
}
if (ShowPerformanceMetrics)
{
FPSMeterUpdate();
FPSMeterDraw(0, FB_HEIGHT - 100 - FPS_METER_HEIGHT, FPS_METER_WIDTH, FPS_METER_HEIGHT);
#if PJD_USE_RENDER_STATS
DrawRenderStats(FB_WIDTH - 200, 10);
#endif // PJD_USE_RENDER_STATS
}
if (ShowHelp)
{
const int width = 300;
const int height = 200;
int top = (FB_HEIGHT - height) / 2;
int left = (FB_WIDTH - width) / 2;
DrawRectangle(left, top, width, height, COLOR(0.4f, 0.4f, 0.4f), CLOSE_DOT_FILL);
int offset = top + 5;
for (int i = 0; Commands[i].Help; ++i)
{
char keyCodeName[128];
UINT scanCode = MapVirtualKey(Commands[i].KeyCode, MAPVK_VK_TO_VSC);
LONG result = GetKeyNameTextA(scanCode << 16, keyCodeName, sizeof(keyCodeName));
WriteString(Format("%-5s - %s", keyCodeName, Commands[i].Help), left + 5, offset), offset += 10;
}
}
if (ShowPerformanceMetrics) {
DrawProfilerStats(10, 10, 300);
}
if (!Thirteen::Render()) {
break;
}
}
SceneDestroy();
if (depthBuffer) {
_aligned_free(depthBuffer);
}
Thirteen::Shutdown();
return 0;
}
static void CaptureScreenshot()
{
char filename[MAX_PATH];
for (int i = 0; ; ++i)
{
sprintf_s(filename, "%s/Screenshot_%d.tga", ApplicationDataPath, i);
DWORD attribs = GetFileAttributesA(filename);
if (attribs == INVALID_FILE_ATTRIBUTES) {
break;
}
}
SaveScreenshot(filename);
}