当前位置:  开发笔记 > 开发工具 > 正文

DirextX9材料不再是用于学习Win32 API编程的材料吗?

如何解决《DirextX9材料不再是用于学习Win32API编程的材料吗?》经验,为你挑选了1个好方法。

我一直在扩展我的图书馆(这些奇怪的事物的物理图书馆,称为“书” ...我知道...我知道),并且我正在阅读Wendy Jones撰写的Beginning DirectX 9.0。正如我过去浏览过一些过时的书一样,它们背后的逻辑实际上是相同的,即使在较早的版本(以我的经验)中,如我所读的C ++书籍中,如果不是更重要的话。我在DirectX 9本书中遇到的问题是10/10练习代码,永远无法使用。甚至这里找到的解决方案和MSDN都不适合我。(相同的问题)。

因此,我希望您能在购买DX11之前告诉我,是否与我的编译器/ vs有关,或者与vs是2015年更新的事实有关,并且该DX9已过时/ DX11标准已介绍。

//Include the Windows header file that's needed for all Windows applications 
#include 


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;
}

selbie.. 5

继续使用DirectX 9是完全可以的。但是在屏幕上显示最小主机窗口的实现存在一些简单的错误。它还在ANSI和宽字符串之间进行了一些错误的转换。让我们修复一下:

删除WinMain的前向声明。此行,将其删除。

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

在WinMain的实际功能体中,将lpCmdLineLP T STR参数的类型更改为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)

而且,当我在这里时,您的消息泵实现有点奇怪。当msg.message==WM_QUIT 不需要外部循环时,GetMessage仅返回FALSE 。更改此:

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;
}

那应该做到的。



1> selbie..:

继续使用DirectX 9是完全可以的。但是在屏幕上显示最小主机窗口的实现存在一些简单的错误。它还在ANSI和宽字符串之间进行了一些错误的转换。让我们修复一下:

删除WinMain的前向声明。此行,将其删除。

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

在WinMain的实际功能体中,将lpCmdLineLP T STR参数的类型更改为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)

而且,当我在这里时,您的消息泵实现有点奇怪。当msg.message==WM_QUIT 不需要外部循环时,GetMessage仅返回FALSE 。更改此:

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;
}

那应该做到的。

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有