Sign in to follow this  

why does DX3D 9 not like my code :-(

This topic is 3461 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

I'm just lost... I'm just messing around with some code (i'm more noob than I realised... ) that's supposed to render several squares (32x32 px) in just white using 2 triangles per... and its just not working... first I was getting a read access violation trying to lock my vertex buffer, which is stored in a global pointer (never mind how evil that is, give me a break) ... in my message pump i had a call to a function called render_frame() that would copy the vertex list to the buffer for rendering. . . the buffer is created in winmain ... debugging just stopped at buffer->lock(0,0,(void**)pVoid,0); (in render_frame() ) with the violation address being pVoid + 0x10 .. ?? .... I moved the contents of render_frame to just be in winmain's message loop, and now i just get first-chance exception - long at memory location. . i think its &pVoid.. I'm just confused by that all. . anyways, here my code. . . note - I currently do get a window which is just black, 640x480 ... that much of my code works (its framework from me following a tut on d3d9 .. )
//we'll include windows and directX headers in luci.h!

#include "luci.h"

#define NUM_BOXES 15

#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct myvertex{ FLOAT x,y,z,rhw; DWORD color;};

struct MyMove { FLOAT x, y; };

struct MyBox{ FLOAT left, up, right, down; MyMove V; DWORD color; } ;
// color=D3DCOLOR_XRGB(0, 0, 255)

MyBox	TheBoxes[NUM_BOXES];
myvertex	BoxPolys[NUM_BOXES][6];

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
	if(LuciCreateMainWindow(hInstance, CmdShow, false,640,480)) return 1;

	initD3D();
	LPDIRECT3DVERTEXBUFFER9 triangle_buf=NULL;

	d3ddev->CreateVertexBuffer(6*NUM_BOXES*sizeof(myvertex),0,CUSTOMFVF,
								D3DPOOL_MANAGED,&triangle_buf,NULL);


	int b=0;
	float screen_x=8; float screen_y=8;

	for(int y=0; y<(NUM_BOXES/15)+1; y++)
	{
		screen_y+=40;
		screen_x=8;

		for(int x=0; x<15; x++)
		{
			screen_x+=40;

			TheBoxes[b].left=screen_x-16.0f;	TheBoxes[b].up=y-16.0f; 
			TheBoxes[b].right=x+16.0f;			TheBoxes[b].down=y+16.0f;
			TheBoxes[b].color=D3DCOLOR_XRGB(255, 255, 255);
			
			b++;
		}
	}

	MSG msg;

	DWORD starting_point;

	bool DeviceLost=false;
	HRESULT DeviceErrCode;

	while( TRUE )
	{
		starting_point = GetTickCount();

		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message == WM_QUIT) 
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if(DeviceLost)
		{
			DeviceErrCode = d3ddev->TestCooperativeLevel();
			if(DeviceErrCode == D3DERR_DEVICENOTRESET)
			{
				if(d3ddev->Reset(&d3dpp)==D3D_OK) DeviceLost=false;
			}
		}
		
		if(!DeviceLost)
		{
			for(int i=0; i<NUM_BOXES; i++)
			{
				BoxPolys[i][0].color=BoxPolys[i][1].color=TheBoxes[i].color;
				BoxPolys[i][2].color=BoxPolys[i][5].color=TheBoxes[i].color;

				BoxPolys[i][0].x=TheBoxes[i].left;	BoxPolys[i][0].y=TheBoxes[i].up;
				BoxPolys[i][1].x=TheBoxes[i].right;	BoxPolys[i][1].y=TheBoxes[i].up;
				BoxPolys[i][2].x=TheBoxes[i].left;	BoxPolys[i][2].y=TheBoxes[i].down;
				BoxPolys[i][3]=BoxPolys[i][2];	BoxPolys[i][4]=BoxPolys[i][1];
				BoxPolys[i][5].x=TheBoxes[i].right;	BoxPolys[i][5].y=TheBoxes[i].down;
			}
			//triangle creation code
			VOID *pVoid;
			triangle_buf->Lock(0,0,(void**)&pVoid,0);
			memcpy(pVoid,BoxPolys,sizeof(BoxPolys));
			triangle_buf->Unlock();

			d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0,0), 1.0f, 0);
			d3ddev->BeginScene();
			d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST,0,NUM_BOXES*2);
			d3ddev->EndScene();

			if(d3ddev->Present(NULL, NULL, NULL, NULL)!=D3D_OK) 
			{
				DeviceLost=true;
			}

		}

		while((GetTickCount() - starting_point) < 1000) ;
	}

	triangle_buf->Release();

	closeD3D();
	
//	LuciMB(L"Exiting Now");

	return msg.wParam;
}

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

	return 0;
}

[Edited by - hstubbs3 on June 21, 2008 8:50:01 PM]

Share this post


Link to post
Share on other sites


I havent played with this for a few years, but you are checking messages then rendering then doing a prorated wait

while((GetTickCount() - starting_point) < 1000) ;


[you might want to loop with a sleep function insetad of simply spinning]


The problem may be that you get alot more messages in than you think but every one of them has to wait one full cycle before it gets processed.




Usually you process inputs in the message loop decoding and caching the actions for when the render/game turn gets executed (triggered by a timer messge or whatever). The message loop should be free running to process all the windows messages quickly.

Share this post


Link to post
Share on other sites
Wouldn't it be helpful to fix the actual error first?

The problem is your outer for loop which loops to NUMBOXES/15+1. That ends up in two loop runs, with i being 0 and 1.
Then there's the inner loop with 15. This increases b up to 30. And that's twice the size of your array.

Share this post


Link to post
Share on other sites
as for the For loop... ok, so i'm an idiot... but even fixing that i still have an issue...

first-chance exception - Microsoft C++ exception: long at memory location 0x0012fd08 ... somewhere in DrawPrimitive ...

the prorated loop was originally intended to limit to about 50fps . . . i was gonna eventually animate these things. . then it was crashing out on an unhandled exception so i put a messagebox in there and it was crazy.. lol. . so yeah.. . 1000 is way too long to wait usually... but still kinda lost..

Oops.. . left out setstreamsource .. . heh... ok. . so now i don't have exceptions,.... still just black.. got to check the winding of the polys or something i guess...

thanks for the help tho ....

Share this post


Link to post
Share on other sites

This topic is 3461 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.

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