Jump to content
  • Advertisement
Sign in to follow this  

DirectX Rendering

This topic is 2349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, this is my first time posting on this forum. I have seen this site pop up several times while searching on google and a lot of people seem to get there questions answered. Not sure if this thread is in the right place, but I will just let this out and hopefully you can redirect me to the right forum for next time.

I am currently working through a DirectX book, my second book. The first book I was working through wasn't doing me much good to be honest, wasn't explaining much properly and pretty much dropped me in the middle of nowhere with some pretty advanced code. The book was part of a series and I followed the series, but the DirectX book just seemed to go off track. Anyway, this book I am currently working on "Beginning DirectX by Wendy Jones" is really helping me, I am learning a hell of a lot more.

I am currently on Chapter 4 which is to do with 3D Primer and rendering primitives in DirectX. I always write down the notes for each function, the terminology and try and go over it every so often to keep my mind refreshed. I just wrote a piece of code out which simply draws a triangle, using some of the DirectX functions onto the screen. I don't get any error codes, but when I do Debug my program it seems to stop at the Unlock function, a window pops up saying:

"Unhandled exception at 0x00141c53 in Vertices.exe: 0xC0000005: Access violation reading location 0x43a00030."

I didn't know what it was to begin with, but I looked at unhandled exception and tried storing my Unlock function call into a HRESULT variable and checking to see if has failed or not, like so:

hResult = pVertexBuffer->Unlock()
if(FAILED(hResult))
return false;


pVertexBuffer is an LPDIRECT3DVERTEXBUFFER9 object. The hResult is the HRESULT. Obviously, this didn't work and I couldn't see why. I checked the example code that came with the CD and compared it to mine. There's was quite messy, so I decided to create a separate class for rendering anything to do with DirectX. This is my custom DirectX rendering class:

#include "dxManager.h"

#define CUSTOMFVF D3DFVF_XYZRHW | D3DFVF_DIFFUSE

dxManager::dxManager()
{
pD3D = NULL;
pD3DDevice = NULL;
pVertexBuffer = NULL;
}

dxManager::~dxManager()
{
}

void dxManager::cleanUp(void)
{
if(pD3D != NULL)
{
pD3D->Release();
pD3D = NULL;
}

if(pD3DDevice != NULL)
{
pD3DDevice->Release();
pD3DDevice = NULL;
}

if(pVertexBuffer != NULL)
{
pVertexBuffer->Release();
pVertexBuffer = NULL;
}
}

void dxManager::clearScreen(void)
{
pBackBuffer = NULL;
hResult = NULL;

if(pD3DDevice == NULL)
return;

pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
}

void dxManager::endRender(void)
{
pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

bool dxManager::initDirect3D(HWND hWnd)
{
if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
{
hResult = E_FAIL;
return false;
}

D3DPRESENT_PARAMETERS pD3DPresent;
ZeroMemory(&pD3DPresent, sizeof(pD3DPresent));

pD3DPresent.BackBufferWidth = 640;
pD3DPresent.BackBufferHeight = 480;
pD3DPresent.BackBufferFormat = D3DFMT_X8R8G8B8;
pD3DPresent.BackBufferCount = 0;
pD3DPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
pD3DPresent.hDeviceWindow = hWnd;
pD3DPresent.Windowed = true;

hResult = pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&pD3DPresent, &pD3DDevice);
if(FAILED(hResult))
return NULL;

return true;
}

LPDIRECT3DVERTEXBUFFER9 dxManager::createVertexBuffer()
{
VOID* pVertex;

CUSTOMVERTEX pVertices[3] =
{
{320.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(0,255,0,0)},
{250.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
{50.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(0,0,0,255)}
};

hResult = pD3DDevice->CreateVertexBuffer(
3*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_DEFAULT,
&pVertexBuffer,
NULL);
if(FAILED(hResult))
return NULL;

hResult = pVertexBuffer->Lock(0, sizeof(pVertices),
(void**) &pVertex, 0);
if(FAILED(hResult))
return false;

memcpy(pVertexBuffer, pVertices, sizeof(pVertices));

pVertexBuffer->Unlock();

return S_OK;
}

void dxManager::renderScene()
{
pD3DDevice->BeginScene();

pD3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(CUSTOMVERTEX));

pD3DDevice->SetFVF(CUSTOMFVF);

pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);

pD3DDevice->EndScene();
}


It always put's a Call Stack on the Unlock function when I Debug. Now I haven't studied much into Debugging and such, so don't be afraid to explain to me what is actually going wrong.

Share this post


Link to post
Share on other sites
Advertisement
You should init your pVertex pointer to NULL before locking, and your memcpy destination needs to be pVertex instead of pVertexBuffer.

Also, you're Releasing your D3D objects in the wrong order - you want to reverse what you've currently got.

Share this post


Link to post
Share on other sites
Hello, thanks for the answer. My program is up and running, my shape is being displayed.

I just had one more question, why is that I have to release my D3D objects in the opposite order? Can it cause problems??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!