DirextX9 material 是否不再值得 material 学习 win32 API 编程?

Is DirextX9 material no longer worthy material for learning win32 API programming?

我一直在扩展我的图书馆(这些奇怪的东西的物理图书馆称为 'books'...我知道...我知道)并且我正在阅读 Wendy Jones 的 Beginning DirectX 9.0。正如我过去阅读过一些 'outdated' 的书籍一样,它们背后的逻辑实际上是相同的,如果不是更重要的话(根据我的经验)我读过的 C++ 书籍之类的东西的早期版本。我在这本 DirectX 9 书中遇到的问题是,10/10 练习代码永远不起作用。甚至在这里和 MSDN 上找到的解决方案 没有为我工作。 (相同的问题)。

所以我希望在我去购买有关 DX11 的书籍之前,您是否能告诉我这是否与我的 compiler/vs 或 vs 已于 2015 年更新以及此 DX9 有关obselete/DX11 标准已经出台。

//Include the Windows header file that's needed for all Windows applications 
#include <Windows.h>


HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow);

//forward declerations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT WPARAM, LPARAM);

//This is winmain, the main etry point for Windows applications
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    //Initialize the window
    if (!initWindow(hInstance))
        return false;
    //main message loop: (See page 13, "Adding the Windows Code" - Chapter 2
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    while (msg.message != WM_QUIT);
    {
        //Check the message queue
            while (GetMessage(&msg, wndHandle, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    }
    return(int)msg.wParam;
}


/****************************************************************************** 
* bool initWindow( HINSTANCE hInstance ) 
* initWindow registers the window class for the application, creates the window 
******************************************************************************/

bool initWindow(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    //Fill in the WNDCLASSEX structure. THis describes how the window will look to the system
    wcex.cbSize = sizeof(WNDCLASSEX);                    // the size of the structure
    wcex.style = CS_HREDRAW | CS_VREDRAW;                // the class style
    wcex.lpfnWndProc = (WNDPROC)WndProc;                 // the window procedure callback
    wcex.cbClsExtra = 0;                                 // extra bytes to allocate for this calss
    wcex.cbWndExtra = 0;                                 // extra bytes to allocate for this instance
    wcex.hInstance = hInstance;                          // handle to the application
    wcex.hIcon = 0;                                      // icon to associate with the application
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);          // the default cursor
    wcex.lpszMenuName = NULL;                            // the resource name for the menu
    wcex.lpszClassName = NULL;                           // the class name being created
    wcex.hIconSm = 0;
    RegisterClassEx(&wcex);

    //Create the window
    wndHandle = CreateWindow(
        (LPCWSTR)"DirectXExample",              // the window class to use
        (LPCWSTR)"DirectXExample",              // the title bar text
        WS_OVERLAPPEDWINDOW,                    // the window style
        CW_USEDEFAULT,                          // the starting x coordinate
        CW_USEDEFAULT,                          // the starting y coordinate
        640,                                    //the pixel width of the window
        480,                                    //the pixel height of the window
        NULL,                                   // the parent window; NULL for desktop
        NULL,                                   // the menu for the application; NULL for none
        hInstance,                              // the handle to the apllication instance
        NULL);                                  // no values passed to the window

    //make sure that the window handle that is created is valid
    if (!wndHandle)
        return false;

    //Display the window on the screen
    ShowWindow(wndHandle, SW_SHOW);
    UpdateWindow(wndHandle);
    return true;
}

继续使用 DirectX 9 非常好。但是您在屏幕上显示最小主机 window 的实现有一些简单的错误。它还在 ANSI 和宽字符串之间进行了一些错误的转换。让我们帮你修好:

删除WinMain 的前向声明。这一行,去掉就行了。

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow);

在 WinMain 的实际函数体中,将 lpCmdLine 的类型从 LPTSTR 参数更改为 LPSTR.

//This is winmain, the main etry point for Windows applications
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //Initialize the window
    if (!initWindow(hInstance))

您对 WndProc 的声明也不正确。 WndProc 应该声明如下:

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam);

一旦你修复了上面的WndProc 声明,你就可以在WNDCLASS 初始化中去掉那个错误的转换操作。改变这个:

wcex.lpfnWndProc = (WNDPROC)WndProc;  // the window procedure callback

为此:

wcex.lpfnWndProc = WndProc;  // the window procedure callback

您缺少 WndProc 的定义。您需要自己实现该功能。这是一个最小的实现:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

上面的代码会让你的代码编译,但它仍然会有一些错误,实际上 运行 不会像它应该的那样。让我们解决这些问题。

首先,您的消息泵有一个额外的 ; 阻止它实际 运行 并使您的代码处于无限循环中。这一行:

while (msg.message != WM_QUIT);

应该是(没有分号):

while (msg.message != WM_QUIT)

当我在这里时,您的消息泵实现有点奇怪。 GetMessage only returns FALSE when msg.message==WM_QUIT 所以不需要外循环。改变这个:

while (msg.message != WM_QUIT)
{
    //Check the message queue
        while (GetMessage(&msg, wndHandle, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
}

变成这样:

//Check the message queue until WM_QUIT is received
while (GetMessage(&msg, wndHandle, 0, 0))
{
     TranslateMessage(&msg);
     DispatchMessage(&msg);
}

当您实际为 DX 应用程序实现图形循环时,您可以将 GetMessage 调用更改为 PeekMessage,然后显式检查 WM_QUIT。

您的 initWindow 因多种原因而失败。

您在 WNDCLASSEX 变量中留下了一些垃圾值。更改此行:

 WNDCLASSEX wcex;

变成这样:

WNDCLASSEX wcex = {};

您忘记设置 wcex.lpszClassName。做到这一点:

 wcex.lpszClassName = L"DirectXExample";

然后您将 ANSI 字符串转换为 (LPCWSTR) 是不正确的。为了方便起见,这里有一个固定版本的 initWindow 函数。

bool initWindow(HINSTANCE hInstance)
{
    WNDCLASSEX wcex = {};

    //Fill in the WNDCLASSEX structure. THis describes how the window will look to the system
    wcex.cbSize = sizeof(WNDCLASSEX);                    // the size of the structure
    wcex.style = CS_HREDRAW | CS_VREDRAW;                // the class style
    wcex.lpfnWndProc = (WNDPROC)WndProc;                 // the window procedure callback
    wcex.cbClsExtra = 0;                                 // extra bytes to allocate for this calss
    wcex.cbWndExtra = 0;                                 // extra bytes to allocate for this instance
    wcex.hInstance = hInstance;                          // handle to the application
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);                                      // icon to associate with the application
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);          // the default cursor
    wcex.lpszMenuName = NULL;                            // the resource name for the menu
    wcex.lpszClassName = L"DirectXExample";                           // the class name being created
    wcex.hIconSm = 0;
    RegisterClassEx(&wcex);

    //Create the window
    wndHandle = CreateWindow(
        L"DirectXExample",              // the window class to use
        L"DirectXExample",              // the title bar text
        WS_OVERLAPPEDWINDOW,                    // the window style
        CW_USEDEFAULT,                          // the starting x coordinate
        CW_USEDEFAULT,                          // the starting y coordinate
        640,                                    //the pixel width of the window
        480,                                    //the pixel height of the window
        NULL,                                   // the parent window; NULL for desktop
        NULL,                                   // the menu for the application; NULL for none
        hInstance,                              // the handle to the apllication instance
        NULL);                                  // no values passed to the window

                                                //make sure that the window handle that is created is valid
    if (!wndHandle)
        return false;

    //Display the window on the screen
    ShowWindow(wndHandle, SW_SHOW);
    UpdateWindow(wndHandle);
    return true;
}

这样就可以了。