Menu

Microsoft Visual C++ code to work in Dev-C++

2007-07-27
2012-09-26
  • Nobody/Anonymous

    Hi I have the book Beginning DirectX9 I've gotten everything until example2 in the book to compile when typed in. And I've tried to even copy their code thinking I might have typed it in wrong. But is still does not work right the way I've linked it is with this -ld3d9 and -ld3dx9 is there another link I do not know about. That or what is causing it not to compile the box before this was the same. But it did nto rotate like this one I ran the .exe file from the book to know.

    Here is the code below to the project

    include <windows.h>

    include <d3d9.h>

    include <d3dx9tex.h>

    HINSTANCE hInst; // holds the instance for this app
    HWND wndHandle; // global window handle

    LPDIRECT3D9 pD3D;
    LPDIRECT3DDEVICE9 pd3dDevice;
    LPDIRECT3DVERTEXBUFFER9 vertexBuffer;

    // camera variables
    D3DXMATRIX matView; // the view matrix
    D3DXMATRIX matProj; // the projection matrix
    D3DXVECTOR3 cameraPosition; // the position of the camera
    D3DXVECTOR3 cameraLook; // where the camera is pointing

    // A structure for our custom vertex type
    struct CUSTOMVERTEX
    {
    FLOAT x, y, z; // The untransformed, 3D position for the vertex
    DWORD color;
    };

    // Our custom FVF, which describes our custom vertex structure

    define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

    ////////////////////////////////////////////// forward declarations
    bool initWindow(HINSTANCE hInstance);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

    bool initDirect3D(HWND hwnd);
    void shutdownDirect3D(void);
    bool createCube(void);

    void createCamera(float nearClip, float farClip);
    void moveCamera(D3DXVECTOR3 vec);
    void pointCamera(D3DXVECTOR3 vec);

    define SCREEN_WIDTH 640

    define SCREEN_HEIGHT 480

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    // call our function to init and create our window
    if (!initWindow(hInstance))
    {
    MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
    return false;
    }

    if (!initDirect3D(wndHandle))
    {
        MessageBox(NULL, &quot;Unable to init Direct3D&quot;, &quot;ERROR&quot;, MB_OK);
        return false;
    }
    
    if (!createCube())
    {
        MessageBox(NULL, &quot;Cube could not be created&quot;, &quot;ERROR&quot;, MB_OK);
        return false;
    }
    
    createCamera(1.0f, 500.0f);     // near clip plane, far clip plane
    moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
    pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
    
    D3DXMATRIX meshMat, meshScale, meshRotate;
    
    // Main message loop:
    // Enter the message loop
    MSG msg; 
    ZeroMemory( &amp;msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
        // check for messages
        if( PeekMessage( &amp;msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
            TranslateMessage( &amp;msg );
            DispatchMessage( &amp;msg );
        }
        // this is called when no messages are pending
        else
        {
            // Clear the backbuffer to a black color
            pd3dDevice-&gt;Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
    
            pd3dDevice-&gt;BeginScene();
    
            pd3dDevice-&gt;SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
            pd3dDevice-&gt;SetFVF( D3DFVF_CUSTOMVERTEX );
    
            // set meshMat to identity
            D3DXMatrixIdentity(&amp;meshMat);
    
            // set the rotation
            D3DXMatrixRotationY(&amp;meshRotate, timeGetTime()/1000.0f);
    
            // set the scaling
            //D3DXMatrixScaling(&amp;meshScale, 1.0f, 1.0f, 1.0f);
    
            // multiple the scaling and rotation matrices to create the meshMat matrix
            D3DXMatrixMultiply(&amp;meshMat, &amp;meshMat, &amp;meshRotate);
    
            // transform the object in world space
            pd3dDevice-&gt;SetTransform(D3DTS_WORLD, &amp;meshMat);
    
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
            pd3dDevice-&gt;DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
    
            pd3dDevice-&gt;EndScene();
    
            // Present the backbuffer contents to the display
            pd3dDevice-&gt;Present( NULL, NULL, NULL, NULL );
        }
    }
    
    // release and shutdown Direct3D
    shutdownDirect3D();
    
    return (int) msg.wParam;
    

    }

    bool initWindow(HINSTANCE hInstance)
    {
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = 0;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = &quot;DirectXExample&quot;;
    wcex.hIconSm        = 0;
    RegisterClassEx(&amp;wcex);
    
    wndHandle = CreateWindow(&quot;DirectXExample&quot;, 
                             &quot;DirectXExample&quot;, 
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, 
                             CW_USEDEFAULT, 
                             SCREEN_WIDTH, 
                             SCREEN_HEIGHT, 
                             NULL, 
                             NULL, 
                             hInstance, 
                             NULL);
    

    if (!wndHandle)
    return false;

    ShowWindow(wndHandle, SW_SHOW);
    UpdateWindow(wndHandle);

    return true;
    }

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

    bool createCube(void)
    {
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
    // 1
    { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},

        // 2
        { -64.0f,  64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f,  64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    
        // 3
        { -64.0f, 64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f, 64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    
        // 4
        { -64.0f, -64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f, -64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {  64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    
        // 5
        { 64.0f,  64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { 64.0f,  64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        { 64.0f, -64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    
        // 6
        {-64.0f,  64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
        {-64.0f,  64.0f,  64.0f, D3DCOLOR_ARGB(0,255,0,255)},
        {-64.0f, -64.0f,  64.0f, D3DCOLOR_ARGB(0,0,0,255)},
    };
    
    // Create the vertex buffer.
    HRESULT hr;
    
    hr = pd3dDevice-&gt;CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),
                                        0, 
                                        D3DFVF_CUSTOMVERTEX,
                                        D3DPOOL_DEFAULT, 
                                        &amp;vertexBuffer, 
                                        NULL );
    if FAILED (hr)
        return false;
    
    // prepare to copy the vertices into the vertex buffer
    VOID* pVertices;
    // lock the vertex buffer
    hr = vertexBuffer-&gt;Lock(0, sizeof(g_Vertices), (void**)&amp;pVertices, 0);
    
    // check to make sure the vertex buffer can be locked
    if FAILED (hr)
        return false;
    
    // copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    
    // unlock the vertex buffer
    vertexBuffer-&gt;Unlock();
    
    return true;
    

    }
    bool initDirect3D(HWND hwnd)
    {
    if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
    return false;

    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &amp;d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount  = 1;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.BackBufferWidth  = SCREEN_WIDTH;
    d3dpp.hDeviceWindow    = hwnd;
    
    if( FAILED( pD3D-&gt;CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &amp;d3dpp, &amp;pd3dDevice ) ) )
    return false;
    
    pd3dDevice-&gt;SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
    pd3dDevice-&gt;SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
    pd3dDevice-&gt;SetRenderState(D3DRS_LIGHTING, FALSE);
    
    return true;
    

    }

    void shutdownDirect3D(void)
    {
    if( pd3dDevice != NULL)
    {
    pd3dDevice->Release();
    pd3dDevice = NULL;
    }
    if( pD3D != NULL)
    {
    pD3D->Release();
    pD3D = NULL;
    }
    }

    /**********
    * createCamera
    * creates a virtual camera
    **********/
    void createCamera(float nearClip, float farClip)
    {
    //Here we specify the field of view, aspect ration and near and far clipping planes.
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

    }

    /**********
    * moveCamera
    * moves the camera to a position specified by the vector passed as a
    * parameter
    **********/
    void moveCamera(D3DXVECTOR3 vec)
    {
    cameraPosition = vec;
    }

    /**********
    * pointCamera
    * points the camera a location specified by the passed vector
    **********/
    void pointCamera(D3DXVECTOR3 vec)
    {
    cameraLook = vec;

    D3DXMatrixLookAtLH(&amp;matView, &amp;cameraPosition,       //Camera Position
                                 &amp;cameraLook,       //Look At Position
                                 &amp;D3DXVECTOR3(0.0f, 1.0f, 0.0f));       //Up Direction
    
    pd3dDevice-&gt;SetTransform(D3DTS_VIEW, &amp;matView);
    

    }

     
    • Nobody/Anonymous

      Thanks it worked was a link error you were right once I put in -lwinmm the program ran correct.

       
    • Nobody/Anonymous

      Before I forget it stops at this part in the code right at the bottom look.

      void pointCamera(D3DXVECTOR3 vec)
      {
      cameraLook = vec;

      D3DXMatrixLookAtLH(&matView, &cameraPosition, //Camera Position
      &cameraLook, //Look At Position
      &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction I want you to know this line stops

      pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
      }

      Just in case you did not see it this is the line in this function.
      &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
      Stops right here.

       
    • Nobody/Anonymous

      I must have messed up the link in the other code I compiled because I copied the links from stuff I compiled. And it ran past where it stoped before here is the Compile Log below.

      Compiler: Default compiler
      Building Makefile: "C:\Documents and Settings\Mike Neiheisel\My Documents\chapter5\example2\Makefile.win"
      Executing make...
      make.exe -f "C:\Documents and Settings\Mike Neiheisel\My Documents\chapter5\example2\Makefile.win" all
      g++.exe winmain.o -o "example2.exe" -L"C:/Dev-Cpp/lib" -mwindows -ld3d9 -ld3dx9

      winmain.o(.text+0x2e9):winmain.cpp: undefined reference to `timeGetTime@0'
      collect2: ld returned 1 exit status

      make.exe: *** [example2.exe] Error 1

      Execution terminated

       
    • Nobody/Anonymous

      I moved the code to the C: area like you have told me before without spaces. I've run tons of code I've done in the MyDocuments are with spaces and they almost always run fine. I just wanted to let you know this I don't want to stick a bunch of code in the C: area on my compter.

      Here is what the Compile Log says and this code came from the book just they did it in Microsoft Visual C++ 6.0. I thought it might be because there was not #include <time.h> and tried it still did not work correct.

      Compiler: Default compiler
      Building Makefile: "C:\example2\Makefile.win"
      Executing make...
      make.exe -f "C:\example2\Makefile.win" all
      g++.exe winmain.o -o "example2.exe" -L"C:/Dev-Cpp/lib" -mwindows -ld3d9 -ld3dx9

      winmain.o(.text+0x2e9):winmain.cpp: undefined reference to `timeGetTime@0'
      collect2: ld returned 1 exit status

      make.exe: *** [example2.exe] Error 1

      Execution terminated

       
    • Nobody/Anonymous

      timeGetTime is a function in "mmsystem.h"

       
    • Anonymous

      Anonymous - 2007-07-27

      >> timeGetTime is a function in "mmsystem.h"

      This is a linker error not a compiler error, the header file will make no difference.

      The solution is to link libwinmm.s (-lwinmm)

      Clifford

       
    • Nobody/Anonymous

      I know it is a linker problem but what did he really learn from you pointing out what needed to be linked. Sometimes it is best to just give a hint and make people actually do some research, that way in the future they get into the habit of looking for the answer instead of just posting for it. If he was to search the inet for mmsystem.h he would have seen the million of posts that have that info in it(what to link). IMO he was being lazy.

       
      • Anonymous

        Anonymous - 2007-07-27

        Yawn

         
    • Nobody/Anonymous

      ya that is probably what he did while he waited for you to give him the answer. Funny how you bitch about people and their homework and not giving out the answers yet if you do it its all good and dandy. Funny shit

       
      • Anonymous

        Anonymous - 2007-07-27

        Learning how to distinguish between a compiler error and a linker error is a far more important and useful lesson than which Win32 export libraries need linking.

        And this is not homework in any case, no one takes an academic course in DirectX, and even if it were, the aim of a programming course is to learn to program, not how to wield any particular tool-chain.

        If we ask people to post a log and someone actually does the right thing and posts it to simply ignore it would be just dumb and plain rude.

        Clifford

         
    • Nobody/Anonymous

      Thanks for the help and you are right how would I know what is in a linker especially when the book does not say it needs this linker. They tell you about needing the –ld3d9 and –ld3dx9, but nothing about linking this one thank you for the answer. I would have given up and figure that it was a compiler problem not the fact it wanted this linker I kind of did though, which is why I went here for an answer.

      And no I’m not in school making it harder for me I’m just using the books I’ve had Visual Basic 6.0 in Jr. College. But I never had any C++ classes ever just studying it on my own. I just have books on programming in C++ and ones for making Video Games in C++.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.