Skip to main content
GameDev.net gamedev.net
🔒 Locked

book code problem

Started by Dospro Mar 19, 2005 at 5:35 PM 3 replies 800+ views
Original Post
Dospro
Dospro
Hi. I bought the "Programming Role Playing Games with Dx 2nd edition" book and i found problems with its code. Well. i started reading the book and a new project in MSVC so i could follow the book. I copied all the code in the book to make a single polygon to appear, but i have`t got results. There is also a problem, the book has some undeclared variables and things that you never know what do they do, its strange, so i need you to help me with this please. My code is this: /*Main DirectX objects*/ IDirect3D9 *d3dobj; D3DDISPLAYMODE d3ddm; D3DPRESENT_PARAMETERS d3dpp; IDirect3DDevice9 *d3ddev; IDirect3DVertexBuffer9 *d3dvb=NULL; unsigned char *ptr; D3DXMATRIX matWorld, matView, matProj; D3DXMATRIX matRotX, matRotY, matRotZ; D3DXMATRIX matTrans; D3DXMATRIX matScale; D3DMATERIAL9 d3dm; typedef struct { FLOAT x, y, z; D3DCOLOR diffuse; }sVertex; #define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE) sVertex Verts[4]={ {-100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 255, 255, 255)}, {100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 0, 0, 255) }, {100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 255, 0, 255) }, {-100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 0, 255, 255) } }; d3dobj=Direct3DCreate9(D3D_SDK_VERSION);//Create the object d3ddm.Width=640;//width d3ddm.Height=480;//height d3ddm.RefreshRate=0;//default refresh rate d3ddm.Format=D3DFMT_R5G6B5;//16 bits if(FAILED(d3dobj->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm))) MessageBox(hwnd, "GetAdapterDisplay", "x", MB_OK);//Get the display mode ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));//Reset all memory d3dpp.Windowed=FALSE;//Windowed mode d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;//no swap effect d3dpp.BackBufferFormat=d3ddm.Format;//16bits d3dpp.BackBufferWidth=640;//back buffer d3dpp.BackBufferHeight=480; if(FAILED(d3dobj->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev))) MessageBox(hwnd, "Create Device", "", MB_OK); //d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0); if(FAILED(d3ddev->CreateVertexBuffer(sizeof(sVertex)*4, 0, VertexFVF, D3DPOOL_MANAGED, &d3dvb, NULL))) MessageBox(hwnd, "VertexBuffer", "", MB_OK); if(SUCCEEDED(d3dvb->Lock(0, 0, (void**)&ptr, 0))) { memcpy(ptr, Verts, sizeof(Verts)); d3dvb->Unlock(); } if(FAILED(d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(sVertex)))) MessageBox(hwnd, "SetStrem Source", "", MB_OK); if(FAILED(d3ddev->SetFVF(VertexFVF))) MessageBox(hwnd, "FVF", "", MB_OK); D3DXMatrixRotationX(&matRotX, 0); D3DXMatrixRotationY(&matRotY, 0); D3DXMatrixRotationZ(&matRotZ, 0); D3DXMatrixTranslation(&matTrans, 1.0f, 1.0f, 1.0f); D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f); D3DXMatrixIdentity(&matWorld); D3DXMatrixMultiply(&matWorld, &matWorld, &matScale); D3DXMatrixMultiply(&matWorld, &matWorld, &matRotX); D3DXMatrixMultiply(&matWorld, &matWorld, &matRotY); D3DXMatrixMultiply(&matWorld, &matWorld, &matRotZ); D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans); d3ddev->SetTransform(D3DTS_WORLD, &matWorld); D3DXMatrixRotationX(&matRotX, 0); D3DXMatrixRotationY(&matRotY, 0); D3DXMatrixRotationZ(&matRotZ, 0); D3DXMatrixTranslation(&matTrans, -1, -1, -1); D3DXMatrixIdentity(&matView); D3DXMatrixMultiply(&matView, &matView, &matTrans); D3DXMatrixMultiply(&matView, &matView, &matRotZ); D3DXMatrixMultiply(&matView, &matView, &matRotY); D3DXMatrixMultiply(&matView, &matView, &matRotX); d3ddev->SetTransform(D3DTS_VIEW, &matView); D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, 1.0f, 1000.0f); d3ddev->SetTransform(D3DTS_PROJECTION, &matProj); d3dm.Diffuse.r=d3dm.Ambient.r=1.0f; d3dm.Diffuse.g=d3dm.Ambient.g=1.0f; d3dm.Diffuse.b=d3dm.Ambient.b=0.0f; d3dm.Diffuse.a=d3dm.Ambient.a=1.0f; d3ddev->SetMaterial(&d3dm); d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0); //d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(sVertex)); //d3ddev->SetFVF(VertexFVF); d3ddev->BeginScene(); d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 1); d3ddev->Present( NULL, NULL, NULL, NULL ); d3ddev->EndScene(); I dont know what does it do at all, so i need someone to explain me, becuase the book has poor explanation of this....
Nice Coder
Nice Coder
Use [ code ] and [ source ] tags please.

Also, please attempt to solve the problem by yourself.

I might not do anything, but people get annoyed when people (without doing what they can), as for things. (Like people asking for someone to make them a mmorpg. It aint gonna happen).

Good luck to you, to find an answer.

From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Dospro
Dospro
Yea yea, i understand perfectly what you mean.
But actually i have tried many things, thats why i have to come to this forum...

For example.

D3DXMatrixRotationX(&matRotX, 0);
D3DXMatrixRotationY(&matRotY, 0);
D3DXMatrixRotationZ(&matRotZ, 0);

All those "0" i dont know if they are really zeros. The book doesnt say what values can go there, so i try values like 0.785 and after i finally got that this is just rotation, then i put them to zero..
also the scale thing and trans and the negative 1s.

Please help, i even have to bought the book to lern D3d and i havent succes... please help!
SourceCodeWizzard
SourceCodeWizzard
Hello,

If your programming in C, The following snippets of code are placed in the correct functions.


// GLOBAL VARIABLE DECLARATIONS:

/*Main DirectX objects*/
IDirect3D9 *d3dobj;
D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DDevice9 *d3ddev;
IDirect3DVertexBuffer9 *d3dvb=NULL;
unsigned char *ptr;
D3DXMATRIX matWorld, matView, matProj;
D3DXMATRIX matRotX, matRotY, matRotZ;
D3DXMATRIX matTrans;
D3DXMATRIX matScale;

D3DMATERIAL9 d3dm;


//----------



// STRUCT:

typedef struct {
FLOAT x, y, z;
D3DCOLOR diffuse;
}sVertex;
#define VertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
sVertex Verts[4]={
{-100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 255, 255, 255)},
{100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 0, 0, 255) },
{100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 255, 0, 255) },
{-100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 0, 255, 255) }
};

//-----------



int iGameInitialisation()
{

// CALL: IDirect3D9
d3dobj=Direct3DCreate9(D3D_SDK_VERSION);

// Set the Presentation Parameters structure to windowed
d3ddm.Width=640;//width
d3ddm.Height=480;//height
d3ddm.RefreshRate=0;//default refresh rate
d3ddm.Format=D3DFMT_R5G6B5;//16 bits
if(FAILED(d3dobj->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
MessageBox(hwnd, "GetAdapterDisplay", "x", MB_OK);//Get the display mode

// CALL: ZeroMemory():
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));//Reset all memory

// set the presentation parameters structure to full screen.
d3dpp.Windowed=FALSE;//Windowed mode
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;//no swap effect
d3dpp.BackBufferFormat=d3ddm.Format;//16bits
d3dpp.BackBufferWidth=640;//back buffer
d3dpp.BackBufferHeight=480;
if(FAILED(d3dobj->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev)))




return(S_OK);

}


int iRenderGraphics()
{

//d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
if(FAILED(d3ddev->CreateVertexBuffer(sizeof(sVertex)*4, 0, VertexFVF, D3DPOOL_MANAGED, &d3dvb, NULL)))
MessageBox(hwnd, "VertexBuffer", "", MB_OK);

if(SUCCEEDED(d3dvb->Lock(0, 0, (void**)&ptr, 0)))
{
memcpy(ptr, Verts, sizeof(Verts));
d3dvb->Unlock();
}
if(FAILED(d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(sVertex))))
MessageBox(hwnd, "SetStrem Source", "", MB_OK);
if(FAILED(d3ddev->SetFVF(VertexFVF)))
MessageBox(hwnd, "FVF", "", MB_OK);
D3DXMatrixRotationX(&matRotX, 0);
D3DXMatrixRotationY(&matRotY, 0);
D3DXMatrixRotationZ(&matRotZ, 0);

D3DXMatrixTranslation(&matTrans, 1.0f, 1.0f, 1.0f);
D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f);
D3DXMatrixIdentity(&matWorld);

D3DXMatrixMultiply(&matWorld, &matWorld, &matScale);
D3DXMatrixMultiply(&matWorld, &matWorld, &matRotX);
D3DXMatrixMultiply(&matWorld, &matWorld, &matRotY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matRotZ);
D3DXMatrixMultiply(&matWorld, &matWorld, &matTrans);

d3ddev->SetTransform(D3DTS_WORLD, &matWorld);


D3DXMatrixRotationX(&matRotX, 0);
D3DXMatrixRotationY(&matRotY, 0);
D3DXMatrixRotationZ(&matRotZ, 0);

D3DXMatrixTranslation(&matTrans, -1, -1, -1);
D3DXMatrixIdentity(&matView);

D3DXMatrixMultiply(&matView, &matView, &matTrans);
D3DXMatrixMultiply(&matView, &matView, &matRotZ);
D3DXMatrixMultiply(&matView, &matView, &matRotY);
D3DXMatrixMultiply(&matView, &matView, &matRotX);

d3ddev->SetTransform(D3DTS_VIEW, &matView);

D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, 1.0f, 1000.0f);

d3ddev->SetTransform(D3DTS_PROJECTION, &matProj);
d3dm.Diffuse.r=d3dm.Ambient.r=1.0f;
d3dm.Diffuse.g=d3dm.Ambient.g=1.0f;
d3dm.Diffuse.b=d3dm.Ambient.b=0.0f;
d3dm.Diffuse.a=d3dm.Ambient.a=1.0f;

d3ddev->SetMaterial(&d3dm);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,255), 1.0f, 0);
//d3ddev->SetStreamSource(0, d3dvb, 0, sizeof(sVertex));
//d3ddev->SetFVF(VertexFVF);
d3ddev->BeginScene();

d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 1);
d3ddev->Present( NULL, NULL, NULL, NULL );
d3ddev->EndScene();




return(S_OK)

}



Dont forget, that you have to call these functions, in the main function of the Window program, I.E WinMain() .

Kylotan
Kylotan
I'm sure the DirectX help file will have entries for those rotation functions, telling you what the second parameter is for.

Topic Locked

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

Sign in to reply to this topic.