Sign in to follow this  
caldiar

OpenGL Bad first experience with DirectX.... any help?

Recommended Posts

yes, first time with DirectX... using DX9 right now and tried to create a simple window.... well turns out it isn't so simple for me. Here's the code:
#include <windows.h>
#include "directx/d3d9.h"

HWND hWnd;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pDevice; 

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

bool CreateSimpWindow(int width, int height){

    // Create A Window Class Structure 
    WNDCLASSEX wc;
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(wc); 
    wc.cbWndExtra = 0; 
    wc.hbrBackground = NULL;
    wc.hCursor = NULL; 
    wc.hIcon = NULL; 
    wc.hIconSm = NULL; 
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "X3D"; 
    wc.lpszMenuName = NULL;
    wc.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;

    // Register Window Class
    RegisterClassEx(&wc); 

    // Create Window 
    hWnd = CreateWindowEx(0, 
       "X3D", "DX9 Init Test", 
       WS_OVERLAPPEDWINDOW|WS_VISIBLE, 100,100,width,height,
       NULL,NULL, wc.hInstance, 0);

    return true;
}

bool InitD3D(){
    if((pD3D = Direct3DCreate9(D3D_SDK_VERSION))==NULL){
        return false;
    }
    D3DDISPLAYMODE d3ddm;
    pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.Windowed = true;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    if(FAILED(pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
    hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice))){
        return false;
    }
    return true;
} 

void RenderD3DScene(){
    pDevice->Clear(0,NULL,D3DCLEAR_TARGET,0,1,0);
    pDevice->Present(NULL,NULL,NULL,NULL);
} 

void MessageLoop(){
    MSG msg; 
    while(true){
        RenderD3DScene();
        if(PeekMessage(&msg,hWnd,0,0,PM_REMOVE)){
             if(msg.message==WM_QUIT)return;
             TranslateMessage(&msg); 
             DispatchMessage(&msg); 
        }
    } 
}

INT WINAPI WinMain( HINSTANCE , HINSTANCE , LPSTR , INT ){

    if(!CreateSimpWindow(400, 400))return 1;

    if(!InitD3D())return 2; 

    MessageLoop(); 

    return 0;
} 


well apparently my compiler is angry with me and spits out invalid conversion from 'void*' to 'HINSTANCE_*' on line 42 in function bool CreateSimpWindow here's the line its referring to NULL,NULL, wc.hInstance, 0); any ideas why it's upset and doesn't want to compile? Any help is greatly appreciated. Maybe I should stick with OpenGL and SDL lol =) Thanks! Brandon [edit by k2 : added source tags] [Edited by - kSquared on September 8, 2005 6:39:23 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What compiler are you using?

I just pasted your code into Code::Blocks and it ran perfectly fine as is.

I don't see any problems with what you have.

Share this post


Link to post
Share on other sites
Have you got the various library/include paths configured? in my experience these sorts of errors are usually born from having a messed up Platform SDK (either outdated or badly configured).

hth
Jack

Share this post


Link to post
Share on other sites
interesting to hear.
Im using Dev-C++
so now we know that my compiler is giving me the issues. Maybe it is misconfigured libraries.... hmm..

Thanks guys =)

Share this post


Link to post
Share on other sites
Quote:
Original post by caldiar
Maybe it is misconfigured libraries.... hmm..


More likely it is misconfigured include directories. The linker hasn't come into play yet, so it can't have anything to do with the libraries.

Share this post


Link to post
Share on other sites
shouldn't this line

Quote:
wc.hInstance = GetModuleHandle(NULL);


actually get the hInstance thats passed into WinMain?

I'm fairly new to windows programming but thats how i was tought

Share this post


Link to post
Share on other sites
decided to take a peek in a DirectX 9 book I found laying in my room and copied the code from that book. A little different and this time I get a linker error! heh =)


Here's the code....


#include &lt;windows.h&gt;
#include "directx/d3d9.h"

HINSTANCE hInst;
// application instance
HWND wndHandle;
// application window handle

LPDIRECT3D9 pD3D; // the Direct3D Object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D Device

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

// DirectX functions
bool initDirect3D();
void render(void);

// WinMain

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

if (!initDirect3D())
{
MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
return false;
}

// Main message loop:
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
render();
}
}

// release the device and the direct3D object
if( pd3dDevice != NULL)
pd3dDevice-&gt;Release();

if( pD3D != NULL)
pD3D-&gt;Release();

return (int) msg.wParam;
}

// initWindow

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 = "DirectX Init";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);

// create the window
wndHandle = CreateWindow("DirectX Init",
"DirectX Init",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
return false;

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

return true;
}

// WndProc

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

//initializes direct3D

bool initDirect3D()
{
pD3D = NULL;
pd3dDevice = NULL;

// create the directX object
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}

// fill the presentation parameters structure
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferWidth = 640;
d3dpp.hDeviceWindow = wndHandle;

// create a default directx device
if( FAILED( pD3D-&gt;CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pd3dDevice ) ) )
{
return false;
}
return true;
}

//render

void render(void)
{
// check to make sure we have a valid Direct3D Device
if( NULL == pd3dDevice )
return;

// Clear the backbuffer to a blue color
pd3dDevice-&gt;Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

// Present the backbuffer contents to the display
pd3dDevice-&gt;Present( NULL, NULL, NULL, NULL );
}


(btw if anybody can tell me how to stick the code in one of the small boxes on these forums I'd be really grateful. Can't remember how to do it lol)

so I get this linker error from my compiler (Dev-C++)

[linker error] undefined reference to 'Direct3DCreate9@4'

*rolls his eyes*
I remember running in to something similar to this a long time ago and it was fixed with a simple line in the compiler options. And once again I can't seem to remember what it was! =
Again, input is appreciated =)

[Edited by - Sander on September 8, 2005 6:47:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by NovaCaine
Check you have all the lib files in your project settings under "Linker"

Also the code box commands are "*** Source Snippet Removed ***"


[source] [/source]

To paste that in you must use HTML character codes: &#91source&#93 &#91/source&#93

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this