DirectX and C++

DirectX and C++

本文关键字:C++ and DirectX      更新时间:2023-10-16

我有以下CPP代码的问题:http://ideone.com/XZXZJ。

我收到以下错误:

(1) Warning     1       warning LNK4042: object specified more than once; extras ignored        c:Usersjabbottdocumentsvisual studio 2010ProjectsDirectX9_TutorialDirectX9_TutorialDebugWinMain.obj    1
(2) Error       2       error LNK1561: entry point must be defined      c:Usersjabbottdocumentsvisual studio 2010ProjectsDirectX9_TutorialDirectX9_TutorialLINK

No入口点设置未设置。我试着把这个设置为WinMain,但没有运气。

// WinMain.h
#ifndef APP_HPP_
#define APP_HPP_
#include <Windows.h>    // Windows API Library
#include <d3d9.h>               // Include DirectX 9  Library
#include <d3d10_1.h>    // Include DirectX 10 Library
#include <d3d11.h>              // Include DirectX 11 Library

/* Global Functions */
// Handle any messages from MS Windows
LRESULT CALLBACK WndProc(HWND hWnd,
                        UINT uMsg,
                        WPARAM wParam,
                        LPARAM lParam);
// Main Entry Point
int WINAPI WinMain( HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpCmdLine,
                int nShowCmd);
/* Global Variables */
IDirect3D9              *g_pD3D;                // DirectX 3D v9 Instance
IDirect3DDevice9        *g_pD3DDevice;  // DX3D9:  Representation of Graphics Card
LPCTSTR ClsName = "g_szInterfaceClass";
//IDirect3D10   *g_pD3D10;      // DirectX 3D v10 Instance
//IDirect3DDevice10     *g_pD3DDevice10;        // DX3D10:  Representation of Graphics Card
//IDirect3D11   *g_pD3D11;      // DirectX 3D v11 Instance
//IDirect3DDevice11     *g_pD3DDevice11;        // DX3D11:  Representation of Graphics Card
#define WINDOW_WIDTH    200
#define WINDOW_HEIGHT   200
/* Error Macro for Debugging */
//#define ERROR(msg) { MessageBox(NULL, msg, L"Error", MB_OK|MB_ICONEXCLAMATION); }
#endif // WINMAIN_H

WinMain.cpp:

// WinMain.cpp
#include "WinMain.h"

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

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */
int WINAPI WinMain( HINSTANCE hInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpCmdLine,
                int nShowCmd)
{
        // The WNDCLASSEX structure contains window class information
        WNDCLASSEX      wc;
                wc.cbSize        = sizeof(WNDCLASSEX);
                wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
                wc.lpfnWndProc   = (WNDPROC) WndProc;
                wc.cbClsExtra    = 0;
                wc.cbWndExtra    = 0;
                wc.hInstance     = hInstance;
                wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
                wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
                wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
                wc.lpszMenuName  = NULL;
                wc.lpszClassName = ClsName;
                wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
        // rect structure to define the window dimensions
        RECT rect;
        rect.top                =       (long) 0;
        rect.left               =       (long) 0;
        rect.right              =       (long) WINDOW_WIDTH;
        rect.bottom             =       (long) WINDOW_HEIGHT;
        // Register the window to MS Windows
        if(!RegisterClassEx(&wc))
        {
                MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
                return 0;
        }
        // calculate the required size of the window rectangle, based on the desired size of the client rectangle
        AdjustWindowRectEx(     &rect,
                                                WS_OVERLAPPEDWINDOW,
                                                FALSE,
                                                WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
        // create the target window of the application
        HWND hWindow = CreateWindowEx(NULL,
                                                        ClsName,
                                                        "Name of Window",
                                                        WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                                                        0, 0,
                                                        rect.right-rect.left,
                                                        rect.bottom-rect.top,
                                                        NULL, NULL,
                                                        hInstance,
                                                        NULL);
      if (!hWindow)
      {
                DestroyWindow(hWindow);
               UnregisterClass(ClsName, hInstance);
               MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
               return 0;
     }
    ShowWindow(hWindow, SW_SHOW);
    UpdateWindow(hWindow);
    SetForegroundWindow(hWindow);
    SetFocus(hWindow);
    D3DPRESENT_PARAMETERS   d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed                  = true;
    d3dpp.hDeviceWindow     = hWindow;
    d3dpp.BackBufferCount   = 1;
    d3dpp.BackBufferWidth   = WINDOW_WIDTH;
    d3dpp.BackBufferHeight  = WINDOW_HEIGHT;
    d3dpp.BackBufferFormat  = D3DFMT_X8R8G8B8;
    d3dpp.SwapEffect                = D3DSWAPEFFECT_DISCARD;

    // Identify current version of Direct3D
    g_pD3D =Direct3DCreate9(D3D_SDK_VERSION);
    // Create the Direct3D Interface Device.  Thisisanabstracted version of
    if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
                                                    D3DDEVTYPE_HAL,
                                                    hWindow,
                                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                                    &d3dpp,
                                                    &g_pD3DDevice)))
    {
            MessageBox(NULL, "Failed to create Direct3D InterfaceDevice.", "Error!", MB_ICONEXCLAMATION | MB_OK);
    }
    /* Main Game Boot */
    // Enter the  message loop
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
            // Clear the target render buffer black
            g_pD3DDevice->Clear(    0,
                                            0,
                                            D3DCLEAR_TARGET,
                                            D3DCOLOR_XRGB(0, 0, 0),
                                            1.0f,
                                            (DWORD) 0.0f);
            // Send the buffer to the computer monitor
            g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
            //Send any messages to WndProc function
            TranslateMessage(&msg);
            DispatchMessage(&msg);
    }

    g_pD3D->Release();
    return 1;
}

你的第一个问题可以通过删除这一行来解决:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */

在它的正上方已经有了函数的实现。只有当函数的实现发生在函数调用点之后,或者实现在不同的翻译单元(源文件)中时,才需要上述内容。一般来说,如果可以的话,尽量避免函数的"预声明",使用函数本身作为声明,这将减少所需的维护量。

第二个问题可能是因为您正在编译控制台应用程序(入口点main)而不是windows应用程序(入口点WinMain)。