Thread: [DirectX] I cant figure out this error...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    [DirectX] I cant figure out this error...

    I have the following struct that holds the basic data for setting up my window/directx. Everything works good except for my
    Code:
    LPDIRECT3DDEVICE9 d3DDev;
    Here is the struct file if it is needed.
    Code:
    #pragma once
    #include <windows.h>
    #include <windowsx.h>
    
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <dinput.h>
    
    typedef struct sWindowData
    {
        sWindowData()
        {
            ZeroMemory(&wClass, sizeof(WNDCLASSEX));
            iWidth/*--*/= 0;
            iHeight/*-*/= 0;
            fFullscreen = FALSE;
    
            d3D/*-----*/= 0;
            d3DDev/*--*/= 0;
            dInKeyboard = 0;
            dInMouse/**/= 0;
        };
        /*********/
        /*Windows*/
        /*********/
    
        //Window class
        WNDCLASSEX    wClass;
    
        //Window Handle
        HWND        hWnd;
    
        //The window message struct
        MSG            sMsg;
    
        //hInstance handle
        HINSTANCE    hInstance;
    
        //Window Size
        DWORD        iWidth;
        DWORD        iHeight;
    
        //Is application fullscreen?
        BOOL        fFullscreen;
    
        /*********/
        /*DirectX*/
        /*********/
    
        // the pointer to our Direct3D interface
        LPDIRECT3D9 d3D;
    
        // the pointer to the device class
        LPDIRECT3DDEVICE9 d3DDev;
    
        // the pointer to the keyboard device
        LPDIRECTINPUTDEVICE8 dInKeyboard;
    
        // the pointer to the mouse device
        LPDIRECTINPUTDEVICE8 dInMouse;
    }sWindowData;
    I haven't set up the Direct input devices yet, but this function initiates everything.
    Code:
    //Iniatiate
    BOOL cWindow::Initialize(HINSTANCE& hInstance, int p_iWidth, int p_iHeight, BOOL p_fFullscreen)
    {
        //Create sData structure
        sData = new sWindowData();
        if (!sData)
        {
            std::stringstream text;
            std::stringstream caption;
    
            text << "Error: Could not allocate memory.\n" <<
                    " - Failed to allocate sData structure.";
    
            caption << "cWindow: " << this << " Error";
            MessageBoxA(0,text.str().c_str(),caption.str().c_str(), MB_OK|MB_ICONERROR);
            exit(1);
        }
        //Set sData variables required
        sData->hInstance = hInstance;
        sData->iWidth    = p_iWidth;
        sData->iHeight   = p_iHeight;
        sData->fFullscreen = p_fFullscreen;
    
        //Set window class variables
        sData->wClass.cbSize = sizeof(WNDCLASSEX);
        sData->wClass.style = CS_CLASSDC;
        sData->wClass.lpfnWndProc = (WNDPROC)WindowProc;
        sData->wClass.hInstance = sData->hInstance;
        sData->wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        sData->wClass.lpszClassName = "GameClass";
    
        //Register the window class
        RegisterClassEx(&sData->wClass);
    
        //Create the window
        /**** TODO: ENABLE Fullscreen/Windowed toggle ****/
        sData->hWnd = CreateWindowEx(
                              NULL, 
                              "GameClass", 
                              "Our Direct3D Program",
                              WS_OVERLAPPEDWINDOW,
                              0, 0,
                              sData->iWidth, sData->iHeight,
                              NULL,
                              NULL,
                              sData->hInstance,
                              NULL);
    
    
        sData->d3D = Direct3DCreate9(D3D_SDK_VERSION);
        if (!sData->d3D)
        {
            std::stringstream text;
            std::stringstream caption;
    
            text << "Error: Could not initiate Direct3D9.";
    
            caption << "cWindow: " << this << " Error";
            MessageBoxA(0,text.str().c_str(),caption.str().c_str(), MB_OK|MB_ICONERROR);
            delete sData;
            exit(1);
        }
    
        D3DPRESENT_PARAMETERS d3dpp;
    
        ZeroMemory(&d3dpp, sizeof(d3dpp));
    
        /**** TODO: ENABLE Fullscreen/Windowed toggle ****/
        d3dpp.Windowed = TRUE;
    
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow = sData->hWnd;
        d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
        d3dpp.BackBufferWidth = sData->iWidth;
        d3dpp.BackBufferHeight = sData->iHeight;
        d3dpp.EnableAutoDepthStencil = TRUE;
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    
        // create a device class using this information and the info from the d3dpp stuct
        sData->d3D->CreateDevice(D3DADAPTER_DEFAULT,
                          D3DDEVTYPE_HAL,
                          sData->hWnd,
                          D3DCREATE_HARDWARE_VERTEXPROCESSING,
                          &d3dpp,
                          &sData->d3DDev);
    
        // turn off the 3D lighting
        sData->d3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);
    
        // turn on the z-buffer
        sData->d3DDev->SetRenderState(D3DRS_ZENABLE, TRUE);
    
        return true;
    }
    The highlighted(red) text throws an error in the debugger. sData->d3DDev is still 0.

    I have no idea what I am missing. Before I tried cleaning the code up, and putting the data into one struct, and putting the code into a class it was working wonderfully.

    Any assistance would be greatly appreciated. Thank you.

    If you need to know anything else, just let me know.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to check the return result from CreateDevice - maybe you are asking it to do something that it won't do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Matsp- I don't know what I was thinking, but that is the problem.

    Although I can't figure out why it doesn't work.

    I cant use
    Code:
    D3DCREATE_HARDWARE_VERTEXPROCESSING
    //or
    D3DCREATE_MIXED_VERTEXPROCESSING
    I have to use
    Code:
    D3DCREATE_SOFTWARE_VERTEXPROCESSING
    Which does not make since to me. I have a nVidia Go gfx processor. Well much appreciated.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you got the right graphics driver installed?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    perhaps your card doesnt support vertex processing.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typical older cards or built-in cards with standard Windows drivers can't do hardware acceleration.
    Updating drivers for built-in helps.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I thought creating direct3D with mixed processing would automatically decide the needed processing.

    Is there anyway to check if hardware vertex processing is possible prior to creating the device?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would guess that you can check somehow - not sure how tho.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    The help files are not exactly organized well.... I will post if i find it.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can determine what the card supports by using GetDeviceCaps(). If the card does not support hardware T&L it's not much use developing until you get a better card.

    You will also want to check the stencil buffer format as well as the color format to determine if the card supports what you want. It is not wise to assume the card supports a color or depth stencil format without checking the HRESULT of CreateDevice(). You should always have fall backs to account for older hardware.

    There are many callbacks you can make use of which will enumerate devices and all the formats the devices support.

    You can also make use of the FAILED() and SUCEEDED() macros to determine HRESULTs. You can also make use of DXTrace() and other functions in DXErr9 that help you determine why a certain DirectX API call failed. Sometimes they are informative and sometimes not but they usually will give you a hint as to why a call failed.

    You will not just encounter failures like this at init time. You can also experience missing textures, missing models, etc., if a DrawPrimitive() or SetTexture() call fails for one reason or another. Although I don't recommend checking HRESULTs in the main render/update loop I do recommend checking them during init and setup.
    Last edited by VirtualAce; 09-03-2008 at 05:23 PM.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is what you can use to determine what is supported. Though a pain to go through all these device checks, its an important step in initializing your game since not all computers are going to have the same capabilities.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    And GetDeviceCaps() too. Thanks Bubba. Of course GetDeviceCaps() is no miracle function since its primarily designed for regular GDI applications.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    GetDeviceCaps() returns far more information than any GDI application would ever need. Calling GetDeviceCaps() is very important for vertex and pixel shader support. This information will allow you to set the correct pass and technique in your shaders and effect files should you choose to support older cards with fallback shaders.

    Ideally you would call this once and then use the information in the struct that is filled out to assess the functionality of the video card. This structure will tell you a lot about the card. CheckDeviceFormat() is used primarily to determine if a given format or a given vertex processing mode is valid for the current card. You can also call any of the Check.... functions in Direct3D for more information about the card and its capabilities. You can also enumerate all of the video modes that are supported by the card and then you can present the user with an abbreviated list of the ones your game can use. This is very good for a front end options drop down or an in-game drop down that allows the user to choose the video mode. The advantage to doing this outside of the 3D app before the device is initialized is that you don't have to reset the device and thus re-create all of your managed resources when the user wants to change the video mode. Some games require you to restart the game prior to these types of changes taking effect which I've found is a very good approach and is much more reliable and stable. Although the case could be made that if the application is handling lost devices and ALT-TAB correctly it should also be able to handle on the fly video mode changes (since they are essentially the same process) but that is another story.
    Last edited by VirtualAce; 09-03-2008 at 05:32 PM.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was reading the documentation a bit and you are right, you could get by exclusively using GetDeviceCaps(). Good deal.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It should be noted that although this is a pain it is still easier now than it has been in the past. Microsoft has really cleaned up initialization and device caps querying inside of Direct3D. It really used to get ugly and now it's a simple matter of calling the right function with the right params and you have all the information you could ever need or care about. Thank goodness there is little need for CoCreateInstance(), querying for the latest interface, and setting up hardware clippers anymore. That was a nightmare.

    If you look at the DXUT you will see Microsoft's extremely bloated setup and init functions that do just about everything except clean the kitchen sink before they init the device. Of course they wrote it so it will work on just about every known card out there. However you can use some very basic functions to query and test and almost every modern card out there will work. It is not necessary to support every archaic card on the planet and most do quite well with a simple query, altering some fields in D3DPRESENT_PARAMETERS based on the returned data from the query, and calling CreateDevice().

    Also if you are wanting to support windowed Direct3D you MUST set your backbuffer format to D3DFMT_UNKNOWN or the device will not be created.

    Check the SDK for more information.
    Last edited by VirtualAce; 09-03-2008 at 05:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM