Sign in to follow this  

Can someone help me?

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

Im learning D3D.. But now I dont know what I made wrong =//// Its difficult to learn this way.. can someone check my code and see whats wrong?
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib") 
#pragma comment(lib,"d3dx9.lib")
#define FVF (D3DFVF_XYZ|D3DFVF_TEX1|D3DFVF_DIFFUSE)
struct vertex{
	float x,y,z;
	float u,v;
	D3DCOLOR diffuse;
};


HRESULT starting(HWND);
void settings(void);
void draw(void);
void erase(void);
HRESULT createVb(void);
HRESULT createIb(void);

IDirect3D9* d3d = NULL;
IDirect3DDevice9* device = NULL;
IDirect3DVertexBuffer9* vb = NULL;
IDirect3DIndexBuffer9* ib = NULL;
IDirect3DTexture9* texture = NULL;

HWND* window_pointer;
bool running=true;



LRESULT WINAPI MsgProc(HWND handle,UINT msg, WPARAM anterior, LPARAM posterior){

	switch(msg){
	case WM_DESTROY:
		running=false;
		PostQuitMessage(0);
		break;
		return 0;
	case WM_KEYDOWN:
		
		break;
	
	}
	return DefWindowProc(handle,msg,anterior,posterior);
		
}


INT WINAPI WinMain(HINSTANCE instancia,HINSTANCE prev,LPSTR linha,int show){


	WNDCLASSEX classe;
	classe.cbClsExtra=NULL;
	classe.cbSize=sizeof(classe);
	classe.cbWndExtra=0;
	classe.hbrBackground=NULL;
	classe.hCursor=NULL;
	classe.hIcon=NULL;
	classe.hIconSm=NULL;
	classe.hInstance=instancia;
	classe.lpfnWndProc=MsgProc;
	classe.lpszClassName="Classe";
	classe.lpszMenuName=NULL;
	classe.style=CS_CLASSDC;

	RegisterClassEx(&classe);
	
	HWND janela = CreateWindow("Classe","..::.. Window .::..",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,instancia,NULL);
	
	window_pointer = &janela;
	if(SUCCEEDED(starting(janela))){
		settings();
	} else {return 0;}
	
	if(FAILED(createVb())){
		return 0;
	}

	if(FAILED(createIb())){
		return 0;
	}

	ShowWindow(janela,show);
	UpdateWindow(janela);

	MSG msg;
	ZeroMemory(&msg,sizeof(msg));
	while(running){
		draw();
		PeekMessage(&msg,janela,0,0,PM_REMOVE);
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	erase();

	UnregisterClass("Classe",classe.hInstance);
	return 0;
}

HRESULT starting(HWND janela){

	if(NULL==(d3d=Direct3DCreate9(D3D_SDK_VERSION))){	
	return E_FAIL;
	}


	D3DPRESENT_PARAMETERS parametros;
	ZeroMemory(¶metros,sizeof(parametros));
    parametros.BackBufferWidth=640;
    parametros.BackBufferHeight=460;
    parametros.BackBufferFormat=D3DFMT_UNKNOWN;
    parametros.SwapEffect=D3DSWAPEFFECT_DISCARD;

    parametros.hDeviceWindow=janela;
    parametros.Windowed=TRUE;
    parametros.EnableAutoDepthStencil=TRUE;
    parametros.AutoDepthStencilFormat=D3DFMT_D16;

if(D3DXCreateTextureFromFile(device,"tex.jpg",&texture)){
		return E_FAIL;
	}
	
	if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,janela,D3DCREATE_SOFTWARE_VERTEXPROCESSING,¶metros,&device))){
	
	return E_FAIL;
	
	}

	return S_OK;
}

void settings(){

	device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
	device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	device->SetRenderState(D3DRS_LIGHTING,FALSE);
    D3DXMATRIX matrix;
	D3DXMatrixPerspectiveFovLH(&matrix,(D3DX_PI/4),1.5f,1.0f,100.0f);
	device->SetTransform(D3DTS_PROJECTION,&matrix);
	


}

void draw(){

	device->Clear(0,NULL,D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
	if(SUCCEEDED(device->BeginScene())){

	device->SetStreamSource(0,vb,0,sizeof(vertex));
	device->SetIndices(ib);
	device->SetFVF(FVF);
	device->SetTexture(0,texture);
	
	device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,6);

	device->EndScene();
	}
	device->Present(NULL,NULL,NULL,NULL);

static float valor = (D3DX_PI/4);
    D3DXMATRIX matrix;
	D3DXMatrixPerspectiveFovLH(&matrix,valor,1.5f,1.0f,100.0f);
	device->SetTransform(D3DTS_PROJECTION,&matrix);
	valor+=0.01;

}

HRESULT createVb(){

	vertex triangulo[] = {
        //  X      Y     Z              RGB
        {  -1.0f, -1.0f, 5.0f,0.0f,0.0f, D3DCOLOR_XRGB(255,0,0), },
        {	1.0f, -1.0f, 5.0f,0.0f,1.0f, D3DCOLOR_XRGB(255,255,0), },
        {   1.0f,  1.0f, 5.0f,1.0f,0.0f, D3DCOLOR_XRGB(255,255,255), },
		{  -1.0f,  1.0f, 5.0f,1.0f,1.0f, D3DCOLOR_XRGB(0,255,0), },
		{  -1.0f, -1.0f, 7.0f,0.0f,0.0f, D3DCOLOR_XRGB(0,0,255), },
        {	1.0f, -1.0f, 7.0f,0.0f,1.0f, D3DCOLOR_XRGB(0,255,255), },
        {   1.0f,  1.0f, 7.0f,1.0f,0.0f, D3DCOLOR_XRGB(255,0,255), },
		{  -1.0f,  1.0f, 7.0f,1.0f,1.0f, D3DCOLOR_XRGB(0,0,0), },
};


	//Criando Vertex Buffer
	if(FAILED(device->CreateVertexBuffer(8*sizeof(vertex),D3DUSAGE_WRITEONLY,VERTICE,D3DPOOL_DEFAULT,&vb,NULL))){
	return E_FAIL;
	
	}

	void *algo;
	vb->Lock(0,0,&algo,0);
	memcpy(algo,triangulo,sizeof(triangulo));
	vb->Unlock();

	return S_OK;
	
}

HRESULT createIb(){
	WORD faces[]={
		0,1,2,
		0,2,3,
		0,4,1,
    	1,4,5,
		4,6,7,
		4,5,6,
		4,0,3,
		4,7,3,
		1,5,6,
		1,2,6,
		3,6,7,
		3,6,2,	
	};

	if(FAILED(device->CreateIndexBuffer(sizeof(vertex),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,NULL))){
	return E_FAIL;
	}
	void* algo;
	ib->Lock(0,0,&algo,0);
	memcpy(algo,faces,sizeof(faces));
	ib->Unlock();

return S_OK;
}

void erase(){
	if(!device==NULL)device->Release();
	if(!d3d==NULL)d3d->Release();
	if(!vb==NULL)vb->Release();
	if(!ib==NULL)ib->Release();
	if(!texture==NULL)texture->Release();

}

Share this post


Link to post
Share on other sites
You're gonna have say what is actually happening... Are you getting compiler errors? Is the program crashing when you run it? Do you just get a black window?

In addition, what IDE do you use? If it has a nice debugger, then [if you don't know how to already,] learning to use it will help you out tons.

Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
You're gonna have say what is actually happening... Are you getting compiler errors? Is the program crashing when you run it? Do you just get a black window?

In addition, what IDE do you use? If it has a nice debugger, then [if you don't know how to already,] learning to use it will help you out tons.


Im not getting compilers errors, its crashing when I run it, I dont get a black window. Im using visual c++ 6.0.. The debug just say strange messages..

I setup my directX to stop running when crash and it stops running.. If I setup it to dont stop running it doesnt create the Texture.. it returns "E_FAIL" to create it.

Share this post


Link to post
Share on other sites
Maybe your program crashes cause you try to load a texture before you create your device (shouldn't cause your program to crash).

Also, why using the WNDCLASSEX structure if you are using the CreateWindow function? This has no effect, and you can just use the WNDCLASS structure.

But to find your problem exactly I suggest you step through the code with the debugger. That way, you can determine exactly where it's going wrong.

Share this post


Link to post
Share on other sites
This particular problem is almost definitely because you're trying to create your texture before you create your device. You'll notice that D3DXCreateTextureFromFile() takes a pointer to a device as the first parameter, but device at that point is NULL. The function can't create a texture without a device. You'll need to move that line farther down, so that it is called after you have created your device.

Share this post


Link to post
Share on other sites

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