forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
150 lines (126 loc) · 3.87 KB
/
Main.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
#include <Windows.h>
#include <crtdbg.h>
#include "Window.h"
#include "Graphics.h"
#include "Game.h"
#include "Input.h"
// Annonymous namespace to hold variables
// only accessible in this file
namespace
{
// A "global" game object that
// can be used in WinMain() and
// in our window resize callback
Game* game = 0;
// A simple function to hook up
// to the window for resize
// notifications
void WindowResizeCallback()
{
// Let the game object know
// that the window has been
// resized, if it exists
if(game)
game->OnResize();
}
}
// --------------------------------------------------------
// Entry point for a graphical (non-console) Windows application
// --------------------------------------------------------
int WINAPI WinMain(
_In_ HINSTANCE hInstance, // The handle to this app's instance
_In_opt_ HINSTANCE hPrevInstance, // A handle to the previous instance of the app (always NULL)
_In_ LPSTR lpCmdLine, // Command line params
_In_ int nCmdShow) // How the window should be shown (we ignore this)
{
#if defined(DEBUG) | defined(_DEBUG)
// Enable memory leak detection as a quick and dirty
// way of determining if we forgot to clean something up
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
// Do we also want a console window? Probably only in debug mode
Window::CreateConsoleWindow(500, 120, 32, 120);
printf("Console window created successfully. Feel free to printf() here.\n");
#endif
// Set up app initialization details
unsigned int windowWidth = 1280;
unsigned int windowHeight = 720;
const wchar_t* windowTitle = L"Direct3D11 Game";
bool statsInTitleBar = true;
bool vsync = false;
// The main application object
game = new Game();
// Create the window and verify
HRESULT windowResult = Window::Create(
hInstance,
windowWidth,
windowHeight,
windowTitle,
statsInTitleBar,
WindowResizeCallback);
if (FAILED(windowResult))
return windowResult;
// Initialize the graphics API and verify
HRESULT graphicsResult = Graphics::Initialize(
Window::Width(),
Window::Height(),
Window::Handle(),
vsync);
if (FAILED(graphicsResult))
return graphicsResult;
// Initalize the input system, which requires the window handle
Input::Initialize(Window::Handle());
// Now the game itself can be initialzied
game->Initialize();
// Time tracking
LARGE_INTEGER perfFreq{};
double perfSeconds = 0;
__int64 startTime = 0;
__int64 currentTime = 0;
__int64 previousTime = 0;
// Query for accurate timing information
QueryPerformanceFrequency(&perfFreq);
perfSeconds = 1.0 / (double)perfFreq.QuadPart;
// Performance Counter gives high-resolution time stamps
QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
currentTime = startTime;
previousTime = startTime;
// Windows message loop (and our game loop)
MSG msg = {};
while (msg.message != WM_QUIT)
{
// Determine if there is a message from the operating system
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Translate and dispatch the message
// to our custom WindowProc function
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Calculate up-to-date timing info
QueryPerformanceCounter((LARGE_INTEGER*)¤tTime);
float deltaTime = max((float)((currentTime - previousTime) * perfSeconds), 0.0f);
float totalTime = (float)((currentTime - startTime) * perfSeconds);
previousTime = currentTime;
// Calculate basic fps
Window::UpdateStats(totalTime);
// Input updating
Input::Update();
// Update and draw
game->Update(deltaTime, totalTime);
game->Draw(deltaTime, totalTime);
// Notify Input system about end of frame
Input::EndOfFrame();
#if defined(DEBUG) || defined(_DEBUG)
// Print any graphics debug messages that occurred this frame
Graphics::PrintDebugMessages();
#endif
}
}
// Clean up
delete game;
Input::ShutDown();
Graphics::ShutDown();
return (HRESULT)msg.wParam;
}