Direct3d noob woes

Started by
12 comments, last by Adams555 18 years, 9 months ago
Ok right now i'm just trying to init d3d and clear the screen to red. It is not creating the device right i dont understand why heres my source

#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
bool done = false;
IDirect3D9 *pd3d = NULL;
IDirect3DDevice9 *pdevice = NULL;
IDirect3DVertexBuffer9 *pvb = NULL;
LRESULT CALLBACK wndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
void d3dinit(HWND hwnd){
	HRESULT hr;
	pd3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(!pd3d){
		MessageBox(NULL,"create failed","create failed",MB_OK);
	}
	D3DDISPLAYMODE displaymode;
	displaymode.Format = D3DFMT_R5G6B5;
	displaymode.Height = 480;
	displaymode.Width = 640;
	displaymode.RefreshRate = 0;
	pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode);
	D3DPRESENT_PARAMETERS parameters;
	ZeroMemory(&parameters,sizeof(D3DPRESENT_PARAMETERS));
	parameters.Windowed = true;
	parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	parameters.BackBufferFormat = displaymode.Format;
	parameters.BackBufferCount = 1;
	parameters.hDeviceWindow = hwnd;
	
	hr= pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd
		,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&parameters,&pdevice);
	if(FAILED(hr)){
		MessageBox(NULL,"DEVICE FAILED","BUSTED",MB_OK);
	}
}
void destroyd3d(){
	if(pd3d){
		pd3d->Release();
	}
	if(pdevice){
		pdevice->Release();
	}
	pd3d = NULL;
	pdevice = NULL;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){
	WNDCLASS wc;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = wndProc;
	wc.lpszClassName = "class";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	HWND hwnd = CreateWindow("class","NAME",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	d3dinit(hwnd);
	MSG msg;
	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		pdevice->Clear(0,NULL,D3DCLEAR_TARGET,0,1.0f,0);
		pdevice->BeginScene();
		pdevice->EndScene();
		pdevice->Present(NULL,NULL,NULL,NULL);
	}
	destroyd3d();
	return 0;
}

Thanks, Cory p.s. if those of you who recognize me are wondering why i am doin dx its bc i want to port what i had made in win32 to directx.
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
Check the hr error code - it will probably give you some good hints about the error.

I tried to reproduce your problem here, and I got a D3DERR_INVALIDCALL. Looking the stuff more closely, I saw that hwnd was NULL.

The problem comes from the way you are initializing the WNDCLASS structure - some fields are set to values that are not coherant - because you forgot to ZeroMemory() it.

Thus adding the single line
	WNDCLASS wc;	ZeroMemory(&wc, sizeof(wc));	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

Seems to correct the problem.

Now, if you want to clear you window with the red color, you should modify your Clear() call accordingly:

pdevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0xff,0,0),1.0f,0);


As a rule of thumb: when a system function returns an error code, check it. RegisterClass() returns an ATOM which is equal to 0 if it fails. Another point: if you detect an error, stop your treatment - detecting an error and continue as if everuthing was OK will going to give big headaches...

HTH
Wow i never zeroed the memory on the windowclass before and it worked hmm wierd. Ok thanks a lot ed ur always on it :-)
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
NEW PROBLEM!
CreateTextFromFile is not working >.<
heres source
#include<windows.h>#include<d3d9.h>#include<d3dx9.h>#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")bool done = false;IDirect3D9 *pd3d = NULL;IDirect3DDevice9 *pdevice = NULL;IDirect3DVertexBuffer9 *pvb = NULL;typedef struct{	int x;	int y;	int z;}myvertex;#define vtxfmt (D3DFVF_XYZ)LRESULT CALLBACK wndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){	switch(iMsg){	case WM_DESTROY:		PostQuitMessage(0);		done = true;		break;	}	return DefWindowProc(hwnd,iMsg,wParam,lParam);}void d3dinit(HWND hwnd){	HRESULT hr;	pd3d = Direct3DCreate9(D3D_SDK_VERSION);	if(!pd3d){		MessageBox(NULL,"create failed","create failed",MB_OK);	}	D3DDISPLAYMODE displaymode;	displaymode.Format = D3DFMT_R5G6B5;	displaymode.Height = 480;	displaymode.Width = 640;	displaymode.RefreshRate = 0;	pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode);	D3DPRESENT_PARAMETERS parameters;	ZeroMemory(&parameters,sizeof(parameters));	parameters.Windowed = true;	parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;	parameters.BackBufferFormat = displaymode.Format;	parameters.BackBufferCount = 1;	parameters.hDeviceWindow = hwnd;		hr= pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd		,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&parameters,&pdevice);	if(FAILED(hr)){		MessageBox(NULL,"DEVICE FAILED","BUSTED",MB_OK);	}	pdevice->CreateVertexBuffer(4*sizeof(myvertex),D3DCREATE_SOFTWARE_VERTEXPROCESSING,vtxfmt,D3DPOOL_MANAGED,&pvb,NULL);	myvertex verts[4] = {		{100,200,0},		{100,100,0},		{200,100,0},		{200,200,0}	};	void *ptr;	pvb->Lock(0,0,&ptr,0);	memcpy(ptr,verts,sizeof(verts));	pvb->Unlock();}void destroyd3d(){	if(pd3d){		pd3d->Release();	}	if(pdevice){		pdevice->Release();	}	pd3d = NULL;	pdevice = NULL;}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdline,int iCmd){	WNDCLASS wc;	ZeroMemory(&wc,sizeof(wc));	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	wc.hCursor = LoadCursor(NULL,IDC_ARROW);	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);	wc.hInstance = hInstance;	wc.lpfnWndProc = wndProc;	wc.lpszClassName = "class";	wc.lpszMenuName = NULL;	wc.style = CS_VREDRAW|CS_HREDRAW;	RegisterClass(&wc);	HWND hwnd = CreateWindow("class","NAME",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstance,NULL);	ShowWindow(hwnd,iCmd);	UpdateWindow(hwnd);	d3dinit(hwnd);	IDirect3DTexture9 *ptexture = NULL;	if(FAILED(D3DXCreateTextureFromFile(pdevice,"pic.bmp",&ptexture))){		MessageBox(NULL,"createtextfail","failed",MB_OK);	}	pdevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);    pdevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);    pdevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);	pdevice->SetTexture(0,ptexture);	pdevice->SetStreamSource(0,pvb,sizeof(myvertex),0);	MSG msg;	while(!done){		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){			TranslateMessage(&msg);			DispatchMessage(&msg);		}		pdevice->Clear(0,NULL,D3DCLEAR_TARGET,0,1.0f,0);		pdevice->BeginScene();		pdevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);		pdevice->EndScene();		pdevice->Present(NULL,NULL,NULL,NULL);	}	destroyd3d();	return 0;}

Whats wrong with it. I dont et an error but it doesnt work :'(
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
OK createtextfromfile is working but its not displaying an image wtf is wrong with it?
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
GOD DANGIT ITS NOT WORKING>
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
calm down dude,

there are some people on here that actually work at game companies. Acting unprofessional could cast you in a bad light, and down the road that might bite you as somebody remembers you as that "hotheaded guy who threw a tantrum when his code didn't work."

stay cool man.

we're here to help, but nothing requires somebody to pour through your code and tell you what's wrong right away.

I understand your frustration, but I want to warn you that if you eventually want to work in the industry (like a lot of people on here do), you might want to keep yourself under control.
patience...

Your verteces are probably not in the right order. Right now it looks like they form a box:

1 -> -> -> -> -> 2^                v^                v ^                v^                v^                v^                v0 <- <- <- <- <- 3


So if you make 2 triangles from 0,1,2 and 1,2,3 does it make a box?

No, it makes this:

--------------|            ||            | |      ^     ||     / \    ||    -   -   ||  /       \ || -         \|


And that's even assuming you're using TRIANGLESTRIPs, and that the verteces were in the right order. Triange lists require 3 verteces -per triangle-. Don't worry though, that sort of stuff is tricky for beginners, and not very well documented!

Do this.

Change the TRIANGLELIST to whatever triangle strips are [just TRIANGLESTRIP I think...]

And place the vertexes into the buffer like this:
0 -> 1  /2 -> 3


Like a Z. That will ensure they face the right way, and the 2 triangles you make end up like a box.

That should at least render a box to the screen. I don't think the box will have the texture you want, or even be colored properly without adding those entries to your vertex. Not sure though.

And I'm probably forgetting/missing something. Thankfully this code is something you can largely write once or twice and then never look at it again if you don't want to.

The best beginner tutorial to do this I know of is by drunken hyena here.

[edit: looking over my old code, it appears as though I might be completely off my rocker!

Anyways, the link should be good at least :D

]
Your MyVertex struct only has position coordinates for each vertex, it also needs texture (UV) coordinates. If you don't have texture coordinates for each of your vertics, Direct3D won't know how to apply the texture. So,

typedef struct{
int x;
int y;
int z;
}myvertex;
#define vtxfmt (D3DFVF_XYZ)

should be changed to:

typedef struct{
float x;
float y;
float z;
float u;
float v;
}myvertex;
#define vtxfmt (D3DFVF_XYZ | D3DFVF_TEX0)

And the UV coordinates for each vertex in your rectangle should look like this:

0,0--1,0
| / |
0,1--1,1

EDIT: darn forum is condensing all my spaces!
i wanted to make a box so i can put a texture on the box. My goal is to be able to do 2d. The uv coords are coorect and order is coorect too.
-Cory
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|

This topic is closed to new replies.

Advertisement