gpt4 book ai didi

winapi - DirextX9 Material 不再是用于学习Win32 API编程的 Material 吗?

转载 作者:行者123 更新时间:2023-12-02 10:43:45 26 4
gpt4 key购买 nike

我一直在扩展我的图书馆(这些奇怪的事物的物理图书馆,称为“书” ...我知道...我知道),并且我正在阅读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 <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很好。但是在屏幕上显示最小主机窗口的实现存在一些简单的错误。它还在ANSI和宽字符串之间进行了一些错误的转换。让我们修复一下:

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

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

在WinMain的实际函数主体中,将 lpCmdLine的类型从LP 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)

当我在这里时,您的消息泵实现有点奇怪。 GetMessage仅在 msg.message==WM_QUIT时返回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;
}

那应该做到的。

关于winapi - DirextX9 Material 不再是用于学习Win32 API编程的 Material 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com