Skip to main content
gamedev.net
🔒 Locked

Help with D3D.

Started by SSJCORY 9 replies 950+ views
Original Post
SSJCORY
SSJCORY
This is one of my first programs with directx. The only thing thats not my code and is directly copied is things dealing with Triangles and Matrices. I dont understand what this stuff does and I need an explanation I tried msdn but it was too complicated. First i''ll display the whole thing then i''ll display the parts i need help with.
#define WIN32_LEAN_AND_MEAN
#include<d3dx8.h>
#include<d3d8.h>
#include<stdio.h>
#pragma comment(lib,"d3dx8.lib")
#pragma comment(lib,"d3d8.lib")
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void initWindow();
void killWindow();
void initD3D();
void killD3D();
void initGame();
void killGame();
void askFullscreen();
void messageHandler();
void render();
void drawStuff();
HWND ghwnd = NULL;
HINSTANCE gInstance = NULL;
LPDIRECT3D8 pd3d;
IDirect3DDevice8 *pd3dd = NULL;
const int gheight = 480;
const int gwidth = 640;
const char gamename[] = "Cool Game 1.1";
bool fullscreen = true;
bool gameover = false;
class CVertex{
public:
	float fX,fY,fZ;
	DWORD color;
	void Create(float fx,float fy,float fz,float r,float g,float b){
		fX=fx;
		fY=fy;
		fZ=fz;
		color = D3DCOLOR_XRGB((BYTE)(r*255),(BYTE)(g*255),(BYTE)(b*255));
	}
};
const DWORD D3DFVF_CVertex = D3DFVF_XYZ|D3DFVF_DIFFUSE;
CVertex vtxTriangle[3];
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR CmdLine,int iCmd){
	gInstance = hInstance;
	askFullscreen();
	initWindow();
	initD3D();
	initGame();
	while(!gameover){
		messageHandler();
		render();
	}
	killGame();
	killD3D();
	killWindow();
	return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
	switch(msg){
	case WM_DESTROY:
		gameover = true;
		PostQuitMessage(0);
		return 0;
		break;
	case WM_LBUTTONDOWN:
		gameover = true;
		PostQuitMessage(0);
		return 0;
		break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}
void initWindow(){
	ULONG width,height;
	DWORD style;
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = gInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Class";
	wc.lpszMenuName = NULL;
	wc.style = CS_OWNDC;
	RegisterClass(&wc);
	if(fullscreen){
		height = GetSystemMetrics(SM_CYSCREEN);
		width = GetSystemMetrics(SM_CXSCREEN);
		style = WS_POPUP;
	}
	else{
		height = gheight;
		width = gwidth;
		style = WS_OVERLAPPED|WS_SYSMENU;
	}
	ghwnd = CreateWindow("Class",gamename,style,0,0,width,height,NULL,NULL,gInstance,NULL);
	ShowWindow(ghwnd,SW_SHOW);
	UpdateWindow(ghwnd);
	SetFocus(ghwnd);
}
void killWindow(){
	if(ghwnd){
		if(!DestroyWindow(ghwnd)){
		}
		else{
			MSG msg;
			while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
				DispatchMessage(&msg);
			}
		}
		ghwnd = NULL;
	}
}
void initD3D(){
	D3DPRESENT_PARAMETERS d3dpp;
	D3DDISPLAYMODE displaymode;
	HRESULT hr;
	pd3d = Direct3DCreate8(D3D_SDK_VERSION);
	hr = pd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displaymode);
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.BackBufferCount = 1;
	d3dpp.hDeviceWindow = ghwnd;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	if(fullscreen){
		d3dpp.BackBufferHeight = gheight;
		d3dpp.BackBufferWidth = gwidth;
		d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
		d3dpp.Windowed = FALSE;
	}
	else{
		d3dpp.BackBufferFormat = displaymode.Format;
		d3dpp.Windowed = TRUE;
	}
	hr = pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,ghwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dd);
	D3DXMATRIX mat;
	D3DXMatrixPerspectiveFovLH(&mat,D3DX_PI/10,gwidth/gheight,1.0,100.0);
	pd3dd->SetTransform(D3DTS_PROJECTION,&(D3DMATRIX)mat);
	D3DXMatrixIdentity(&mat);
	pd3dd->SetTransform(D3DTS_WORLD,&(D3DMATRIX)mat);
	D3DXMatrixTranslation(&mat,0,0,10.0);
	pd3dd->SetTransform(D3DTS_VIEW,&(D3DMATRIX)mat);
	vtxTriangle[0].Create(0,0,0,1,0,0);
	vtxTriangle[1].Create(0,1,0,0,1,0);
	vtxTriangle[2].Create(1,0,0,0,0,1);
	pd3dd->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	pd3dd->SetRenderState(D3DRS_LIGHTING,FALSE);
}
void killD3D(){
	if(pd3d){
		pd3d->Release();
		pd3d = NULL;
	}
	if(pd3dd){
		pd3dd->Release();
		pd3dd = NULL;
	}
}
void initGame(){
}
void killGame(){
}
void render(){
	pd3dd->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
	if(SUCCEEDED(pd3dd->BeginScene())){
		pd3dd->SetVertexShader(D3DFVF_CVertex);
		pd3dd->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,vtxTriangle,sizeof(CVertex));
		pd3dd->EndScene();
	}
	pd3dd->Present(NULL,NULL,NULL,NULL);
}
void messageHandler(){
	MSG msg;
	if(PeekMessage(&msg,ghwnd,0,0,PM_REMOVE)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}
void askFullscreen(){
	int full;
	full = MessageBox(NULL,"FullScreen mode?",gamename,MB_YESNOCANCEL);
	switch(full){
	case IDYES:
		fullscreen = true;
		break;
	case IDNO:
		fullscreen = false;
		break;
	case IDCANCEL:
		exit(5);
		break;
	}
}
PART 1 located in the globals
const DWORD D3DFVF_CVertex = D3DFVF_XYZ|D3DFVF_DIFFUSE;
//What does this mean/do?

PART 2 in initD3D()
D3DXMATRIX mat;
	D3DXMatrixPerspectiveFovLH(&mat,D3DX_PI/10,gwidth/gheight,1.0,100.0);
	pd3dd->SetTransform(D3DTS_PROJECTION,&(D3DMATRIX)mat);
	D3DXMatrixIdentity(&mat);
	pd3dd->SetTransform(D3DTS_WORLD,&(D3DMATRIX)mat);
	D3DXMatrixTranslation(&mat,0,0,10.0);
	pd3dd->SetTransform(D3DTS_VIEW,&(D3DMATRIX)mat);
	vtxTriangle[0].Create(0,0,0,1,0,0);
	vtxTriangle[1].Create(0,1,0,0,1,0);
	vtxTriangle[2].Create(1,0,0,0,0,1);
	pd3dd->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
	pd3dd->SetRenderState(D3DRS_LIGHTING,FALSE);
//I dont understand D3DXMatrixPerspectiveFovLH and it''s parameters.

//I also dont understand set transform matrix identity and MatrixTranslation.

Someone please help me, I really need help. I tried msdn but there were lots of big words i didn''t understand, someone please break it down for me, it will be greatly appreciated. Thanks
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Cypher19
Cypher19
I would REALLY suggest downloading the DX9 SDK if you haven''t already and go through the tutorials they have there. Just remember, if there''s a word you don''t understand, nothing''s stopping you from looking it up in the dictionary or going to MSDN''s glossary.
Sfpiano
Sfpiano
FVF stands for Flexible Vertex Format. You need to tell Directx what type of data you''re vertices will hold, and this is how you do that. In your example the D3DFVF_XYZ parameter means your vertices will have an x, y, and z coordinate, and the D3DFVF_DIFFUSE means you''re vertex will have a color component.

As for D3DXMatrixPerspectiveFovLH, I believe that builds your Field of View matrix. (the matrix that lets you see) The LH is in reference to the fact that you''re using Left-handed coorinates in yoru program.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
SSJCORY
SSJCORY
Thanks man, i would download the directx 9 sdk but one thing 56k ahhh. and the tutorials that i''m reading right now are in directx 8.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Sfpiano
Sfpiano
Well for basic functions, DX8 and 9 aren''t too different. A lot of the time all you have to do to convert code is change the 8 to a 9 and call SetFVF() instead of the DX8 call.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
azn_elliot
azn_elliot
you should still download the sdk, I don''t know how I would have learned without it
DrunkenHyena
DrunkenHyena
Hmm, that code looks familiar. I''d bet it was based on my tutorial code. I''d recommend you try my tutorial series. I have one covering D3D8 and one covering D3D9. The DX8 version covers more ground, but the DX9 series covers it better. You can find them on my site (www.drunkenhyena.com, or just click my sig).


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena</FON
SSJCORY
SSJCORY
Yep thats what i''m learning directx from, that and some other tutorials. Your tutorials are great but they dont go into simple terms for the matrix functions.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Jawbreaker
Jawbreaker
I recommend going out and buying a basic Direct3d book, I learned from the awesome book "2d in Direct3d", goes over all the basics.

Topic Locked

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

Sign in to reply to this topic.