Write a Direct3D12 application (2)
First, before explaining the Direct3D12 program, we will write a program to display only a Window. Basically, all you have to do is create a Window with CreateWindow and process messages in the main loop.
Incidentally, Window applications are message-driven. For example, operations such as moving, resizing, or closing a Window are stored in a queue in the form of messages. The program processes the messages in the main loop. Therefore, when there are no messages, the program is in the same state as if it were in Sleep. This is not suitable for programs such as game programs that process animation and other operations in real time, frame by frame.
#include <windows.h>
// Message Processing
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// WindowGeneration of
WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, hInstance, nullptr, nullptr, nullptr, nullptr, "D3D12SampleWindowClass", nullptr };
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(wcex.lpszClassName, "Direct3D 12 Sample", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, nullptr, nullptr, wcex.hInstance, nullptr);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// main loop
MSG msg = {};
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(wcex.lpszClassName, hInstance);
return 0;
}
It is simple, but there are three things to keep in mind
- difference in entry point (main or WinMain)
- difference between GetMessage and PeekMessage
- character code
1. difference of entry point (main or WinMain)
Although it is possible to display a window whether it is the main function or the WinMain function, I think it is common to use the WinMain function when writing Windows applications.
If you want to switch between the main function and the WinMain function when you build with VisualStudio, you can use the WinMain function,
You can switch the “Linker」→「System」→「Subsystem” setting between “Windows” and “Console”.
2. difference between GetMessage and PeekMessage
In the Window generation sample, the GetMessage function is often used; the GetMessage function is a function that Sleeps when there is no message. Direct3D programs need to draw every frame, so it is necessary to run the main loop even if there is no message. This is why we use the PeekMessage function.
3. character encoding
The standard Windows API is to input character code in UTF16. This has been the case for decades. This is quite unwieldy. Therefore, when I write Windows applications, I change the setting to “Use multibyte character set” in “Configuration Properties」→「Details」→「Character Set”, I type in ASCII code. By the way, the multi-byte character set seems to be SJIS by default for Japanese.
In general, UTF8 (without BOM) is often used as the character code for writing programs, and its compatibility with ASCII code is an advantage. I believe that UTF8 is often used in game programs as well. In order to use UTF16 APIs with strings written in UTF8, it is necessary to convert them in real time.
However, I asked Dr. ChatGPT and he said that Windows API should use UTF16, not multibyte. It’s hard.