Skip to main content
gamedev.net
🔒 Locked

code crashing on execution(DX 8.0)

Started by flame_warrior Apr 22, 2002 at 10:22 AM 3 replies 400+ views
Original Post
flame_warrior
flame_warrior
Hi, I am just starting on Direct3D in Directx 8.0. I have written a simple D3D window(windowed mode). Whenever I seem to execute it -it gives me an access violation. The following is my code :-
  

LPDIRECT3D8          g_d3d;
LPDIRECT3DDEVICE8    g_d3d_device;
HRESULT              g_hr;
D3DXMATRIX mat;

bool Initialize_Everything(void)
{
    D3DDISPLAYMODE display;
    D3DPRESENT_PARAMETERS d3d_params;
    
    g_d3d = Direct3DCreate8(D3D_SDK_VERSION);

    if(g_d3d == NULL) {
        ERROR_BOX(g_Hwnd, "Couldn''t create Direct3D");
        return false;
    }
    
    g_hr = g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display);
    
    if(FAILED(g_hr)) {
        ERROR_BOX(g_Hwnd, "GetAdapterDisplayMode failed");
        return false;
    }
    
    ZeroMemory(&d3d_params, sizeof(d3d_params));
    
    d3d_params.Windowed         = true;
    d3d_params.SwapEffect       = D3DSWAPEFFECT_DISCARD;
    d3d_params.BackBufferFormat = display.Format;
    d3d_params.BackBufferWidth  = display.Width;
    d3d_params.BackBufferHeight = display.Height;
    
    g_hr = g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
                             g_Hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                             &d3d_params, &g_d3d_device);
    
    if(FAILED(g_hr)) {
        ERROR_BOX(g_Hwnd, "Failed to Create Device");
        return false;
    }
    
	
	D3DXMatrixPerspectiveFovLH(&mat, D3DX_PI/6, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 100.0);
	g_d3d_device->SetTransform(D3DTS_PROJECTION, &(D3DMATRIX)mat);

	D3DXMatrixIdentity(&mat);
	g_d3d_device->SetTransform(D3DTS_WORLD, &(D3DMATRIX)mat);

	D3DXMatrixTranslation(&mat, 0, 0, 10.0);
	g_d3d_device->SetTransform(D3DTS_VIEW, &(D3DMATRIX)mat);

	g_d3d_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	
    return true;
}

bool RenderScene(void)
{      
    g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    
    //g_d3d_device->BeginScene();

    
    // draw everything here

    
    //g_d3d_device->EndScene();

    
    g_d3d_device->Present(NULL, NULL, NULL, NULL);
    return true;
}

bool Release_Everything(void)
{
    // release all device in opposite order of its initialization

    if(g_d3d_device)
        g_d3d_device->Release();
    g_d3d_device = NULL;
    
    if(g_d3d)
        g_d3d->Release();
    g_d3d = NULL;
    
    return true;
}

  
I can''t figure out what I am doing wrong. If I keep the RenderScene function empty its ok, but as soon as I have something in that function it gives some kind of illegal operation. If it helps I am using Dev-c++. Thank you.
Hello from my world
Oluseyi
Oluseyi
quote:
Original post by flame_warrior
If I keep the RenderScene function empty its ok, but as soon as I have something in that function it gives some kind of illegal operation.

Well, you''ve localized the error to RenderScene. Now fire upa debugger (Dev-C++ would probably use gdb) and step through the render scene function, checking the runtime values of variables and pointers. The most likely problem I can think of without reading through your code is that the device is no longer valid when it gets to RenderScene.


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Wreakon
Wreakon
I have looked at your code over a few times wondering if I lost my touch. What I think is happenning here is you are calling render at a point when the device is not created. Hence a tip would be to check for the device when you render (might be rendering before you initialize, or after you delete the device.) Other than that I don''t see any potential problems in the code itself.

-------
"Programming is like sex make one mistake, and you have to support it forever."
Homepage: www.ussshrike.com/pcwebvia/
-------Homepage: http://www.pclx.com
tok_junior
tok_junior
Wreakon is definitely on the right track here...
What I''m thinking about is
1: How do you handle a FALSE-return from the setup?
Do you just post a quitmessage? In that case your render-func
will probably be run once before the exit.

2: Direct3D is notorious for returning errors when you create the device. Instead of returning FALSE if the first try fails, try creating again, and if THAT fails, return.

3: Have you tried using the ref-rasterizer?
If it works with that, it''s a problem in your createdevice-call

/T

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
flame_warrior
flame_warrior
quote:
Original post by Wreakon
I have
looked at your code over a few times wondering if I lost my touch. What I think is happenning here is you are calling render at a point when the device is not created. Hence a tip would be to check for the device when you render (might be rendering before you initialize, or after you delete the device.) Other than that I don''t see any potential problems in the code itself.

-------



Hi,

I was just looking at my code again. Weird though as I came across this in my code in WinMain.


if(Initialize_Everything == false)
return 0;


Weird as it looks as it should have been Initialize_Everything() as it is a function, the code as I put in however seems to compile without any errors. Well that was what was causing the problems.

quote:

Do you just post a quitmessage? In that case your render-func
will probably be run once before the exit.



As you can see in my code my RenderScene never actually returns a false. So it should always be running until I encounter a WM_QUIT or WM_DESTROY message.

Thank you everyone.
Hello from my world

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.