简体   繁体   English

开始Dir​​ectX 11游戏编程错误

[英]Beginning DirectX 11 Game Programming Error

Here is the code that it gives me on page 33: 以下是它在第33页给出的代码:

#include<Windows.h>

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPWSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( "DX11BookWindowClass", "Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );

    return 0;
}

This code gives me 2 errors- 这段代码给了我2个错误 -

1>------ Build started: Project: BlankWindow, Configuration: Debug Win32 ------ 1> main.cpp 1>c:\\coding\\c++\\visual c++\\directx\\blankwindow\\blankwindow\\main.cpp(10): error C2065: 'WndProc' : undeclared identifier 1>c:\\coding\\c++\\visual c++\\directx\\blankwindow\\blankwindow\\main.cpp(15): error C2440: '=' : cannot convert from 'const char [20]' to 'LPCWSTR' 1> Types pointed to are unrelated; 1> ------ Build build:Project:BlankWindow,Configuration:Debug Win32 ------ 1> main.cpp 1> c:\\ coding \\ c ++ \\ visual c ++ \\ directx \\ blankwindow \\ blankwindow \\ main。 cpp(10):错误C2065:'WndProc':未声明的标识符1> c:\\ coding \\ c ++ \\ visual c ++ \\ directx \\ blankwindow \\ blankwindow \\ main.cpp(15):错误C2440:'=':无法从'转换' const char [20]'到'LPCWSTR'1>指向的类型是不相关的; conversion requires reinterpret_cast, C-style cast or function-style cast ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 转换需要reinterpret_cast,C风格的转换或函数式转换==========构建:0成功,1失败,0最新,0跳过==========

Basically 基本上

'WndProc' : undeclared identifier And '=' : cannot convert from 'const char [20]' to 'LPCWSTR' 'WndProc':未声明的标识符'=':无法从'const char [20]'转换为'LPCWSTR'

What is wrong with this code? 这段代码有什么问题?

Here is a basic working program of your code: 这是您的代码的基本工作程序:

#include<Windows.h>

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

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( L"DX11BookWindowClass", L"Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );
    MSG msg;
    while (GetMessage (&msg, 0, 0, 0))
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
    return msg.wParam;
}

Points: 要点:

  1. I added a WndProc which handles all messages sent to your program. 我添加了一个WndProc ,它处理发送到您程序的所有消息。

  2. I added a message loop to your program. 我在你的程序中添加了一个消息循环 Otherwise, the window would close right away. 否则,窗口会立即关闭。

  3. I added the L prefix as suggested by the_mandrill in the other answer. 我在另一个答案中添加了由the_mandrill建议的L前缀。

This is pretty much your "Hello World" of WinAPI programming. 这几乎是WinAPI编程的“Hello World”。 However, I strongly suggest you learn the WinAPI first before jumping into DirectX programming. 但是,我强烈建议您在进入DirectX编程之前先学习WinAPI。

The problem is that the project is being built as Unicode , but your code is non-Unicode. 问题是该项目是以Unicode身份构建的,但您的代码是非Unicode的。 In other words the Windows API calls are expecting wide (ie 16-bit) strings but your code uses 8-bit strings ('char'). 换句话说,Windows API调用期望宽(即16位)字符串,但您的代码使用8位字符串('char')。 LPCWSTR means long pointer to a constant wide string . LPCWSTR表示指向常量宽字符串的长指针 So the function call is expecting a constant wide string, but you're passing an 8-bit string. 因此函数调用期望一个恒定的宽字符串,但是你传递一个8位字符串。 You have two options: 您有两种选择:

  • Change the project to use the non-Unicode libraries (Properties -> General -> Character Set = 'Use Multi-byte') 更改项目以使用非Unicode库(属性 - >常规 - >字符集='使用多字节')
  • Fix up your code to make it Unicode. 修复代码以使其成为Unicode。 In this case by changing the strings it is complaining about to make them into wide strings. 在这种情况下,通过更改字符串,它抱怨将它们变成宽字符串。 You can do this by prefixing them with L , eg L"DX11BookWindowClass" 您可以通过在L前面添加前缀来实现此目的,例如L"DX11BookWindowClass"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM