/***************************************************************************/
/*
*/
/* File: game.cpp
*/
/*
*/
/***************************************************************************/
#include
<d3dx8.h>
#include
<d3d8.h>
// Added a camera, as you'll find if you don't you'll be squashed up against
your
// shape that you've loaded in.
void
set_camera()
{
// [1] D3DTS_VIEW
D3DXMATRIX v;
g_pD3DDevice->SetTransform(D3DTS_VIEW, D3DXMatrixLookAtLH(&v,
&D3DXVECTOR3(0,0,-5),
&D3DXVECTOR3(0,0,0),
&D3DXVECTOR3(0,1,0)));
// [2] D3DTS_PROJECTION
D3DXMATRIX p;
g_pD3DDevice->SetTransform( D3DTS_PROJECTION, D3DXMatrixPerspectiveFovLH(
&p,
D3DX_PI/4, 1.0f, 0.0f, 100.0f));
};
/***************************************************************************/
/***
*** ******* *** *** ******* ***** ******
*** *** *** *** *** *** *** *** **
***** ******* *** *** ******* ****** *** **
*** *** *** *** *** *** *** *** **
*** *** *** *** ******* ******* * ***** ******/
/***************************************************************************/
// Okay this function loads the .x file, draws it to the screen and then
tidies up
// before exiting the function! Its only done this way so you can see all
the stages
// in one nice tidy functino! You should really init and de-init the parts
of this
// into a class or seperate functions etc. As everytime its called its
loading and
// destroying the file! At 50fps its not the best way to do it :)
void
DrawXFile()
{
LPD3DXBUFFER pMtrlBuffer = NULL;
DWORD numMaterials;
LPD3DXMESH mesh;
// File Name ---------+
// |
// \|/
// |
D3DXLoadMeshFromX( "square.x", D3DXMESH_SYSTEMMEM, g_pD3DDevice,
NULL, &pMtrlBuffer, &numMaterials,
&mesh);
//Create two arrays. One to hold the materials and
only to hold the textures
D3DMATERIAL8* pMeshMaterials = new
D3DMATERIAL8[numMaterials];
LPDIRECT3DTEXTURE8* pMeshTextures = new
LPDIRECT3DTEXTURE8[numMaterials];
D3DXMATERIAL* matMaterials = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
// Loads of allocation etc here!
for(DWORD i = 0; i < numMaterials; i++)
{
//Copy the material
pMeshMaterials[i] = matMaterials[i].MatD3D;
//Set the ambient color for the material (D3DX
does not do this)
pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse;
//Create the texture
if(FAILED(D3DXCreateTextureFromFile(g_pD3DDevice,
matMaterials[i].pTextureFilename,
&pMeshTextures[i])))
{
pMeshTextures[i] = NULL;
};
}
// Actually drawing something here!
for(DWORD i = 0; i < numMaterials; i++)
{
g_pD3DDevice->SetMaterial(&pMeshMaterials[i]);
g_pD3DDevice->SetTexture(0, pMeshTextures[i]);
mesh->DrawSubset(i);
}
// Tidy up!
for(DWORD i = 0; i < numMaterials; i++)
{
if(pMeshTextures[i] != NULL)
pMeshTextures[i]->Release();
}
delete[] pMeshMaterials;
delete[] pMeshTextures;
pMtrlBuffer->Release();
mesh->Release() // ** BUG FIX! DONT FORGET TO RELEASE RESOURCES
}
// Well if we need to do any drawing or rendering triangles we can do it in
here!
void
Render()
{
if(!g_pD3DDevice)return;
// Some stuff to setup or graphics card!
//Turn off lighting becuase we are specifying that
our vertices have colour
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);
// Clear the back buffer to a blue color
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Just to rotate our shape! See it rotate.
static float
angle = 0.0f;
angle += 0.001f;
//~~~*~~~~ Create a matrix
D3DXMATRIX mx;
//~~~*~~~~ Do somthing to our empty matrix.
D3DXMatrixRotationY(&mx, angle ); // angle in
radians...eg. 1 degree = PI/180
//~~~*~~~~ Use the matrix! No use having a matrix
if we don't use it!
g_pD3DDevice->SetTransform(D3DTS_WORLD, &mx);
set_camera();
//DRAW 3D X FILE DRAWING FILE DRAWING FILE DRAWING 3D FILE HERE.
DrawXFile();
// After rendering the scene we display it.
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
void
mainloop()
{
Render();
} |