开发者

DirectX and C++

开发者 https://www.devze.com 2023-03-23 01:59 出处:网络
I am having trouble with the following CPP code:http://ideone.com/XZXZJ. I am receiving the following errors:

I am having trouble with the following CPP code: http://ideone.com/XZXZJ .

I am receiving the following errors:

(1) Warning     1       warning LNK4042: object specified more than once; extras ignored        c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\Debug\WinMain.obj    1

(2) Error       2       error LNK1561: entry point must be defined      c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\LINK

No Entry Point setting isn't set. I've tried to set this to WinMain with no luck.

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


Your first problem can be solved by deleting this line:

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

You already have the implementation of the function directly above it. You only need the above if the implementation of the function occurs after the point the function is called or the implementation is in a different translation unit (source file). Generally, try to avoid the 'predeclaration' of functions if you can, use the function itself as the declaration, it will reduce the amount of maintenance required.

The second problem might be because you are compiling a console application (entry point main) rather than a windows application (entry point WinMain).

0

精彩评论

暂无评论...
验证码 换一张
取 消