/***************************************************************************/
/*
*/
/* File: game.cpp
*/
/*
*/
/***************************************************************************/
#include
<d3dx8.h>
#include
<d3d8.h>
// These two items, the struct and the UINT, are the defentions of a single
// vertex point in space!.. the UINT is for directX so it knows what we
want.
struct
my_vertex
{
FLOAT x, y, z; // D3DFVF_XYZ
DWORD colour; // D3DFVF_DIFFUSE
};
UINT
my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
// Well if we need to do any drawing or rendering triangles we can do it in
here!
void
Render()
{
if(!g_pD3DDevice)return;
my_vertex v[] =
{
// Three points make up the face of the triangle.
{-1.0f,-1.0f, 0.0f, 0xffffff00},
//[1]
{0.0f, 1.0f, 0.0f, 0xffff2200},
//[2]
{1.0f, -1.0f, 0.0f, 0xffffff00}
//[3]
};
WORD my_indexs[] =
{
0, 1, 2
};
IDirect3DVertexBuffer8 * DX_vb;
g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex),
0, my_vertex_description,
D3DPOOL_MANAGED,
&DX_vb );
IDirect3DIndexBuffer8 * DX_ib;
g_pD3DDevice->CreateIndexBuffer( sizeof(my_indexs),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&DX_ib);
void *t;
DX_ib->Lock(0,0, (BYTE**)&t, 0);
memcpy(t, my_indexs, sizeof(my_indexs));
DX_ib->Unlock();
// Copy our array which is in computer memory over
to the directX memory.. using that pointer we
// just created etc.
unsigned char
*temp_pointer_vb;
DX_vb->Lock(0,0, &temp_pointer_vb, 0);
memcpy(temp_pointer_vb, v, sizeof(v) );
DX_vb->Unlock();
// So we've created our vertices and our index
array on the graphics card you might
// say, so now we can tell our graphics card which
memory to use and how much etc..
// 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 );
// Draw our triangle.
g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));
g_pD3DDevice->SetVertexShader(my_vertex_description);
g_pD3DDevice->SetIndices(DX_ib, 0);
// The ending point in the index buffer (e.g.
which triangle)------
// The starting point in the index
buffer----------------------- |
// | |
// The end in the vertex buffer (e.g. how many
points)--- | |
// Starting vertex in the vertex buffer (e.g.
x,y,z)-- | | |
// | | | |
// \ /\ / \ /\ /
//
| | | |
g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 3, 0, 1);
// After rendering the scene we display it.
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
// As where calling the functino over and over
again.. we'd be constantly creating new memory
// without releasing the old if not for these
line!
DX_vb->Release();
DX_ib->Release();
}
void
mainloop()
{
Render();
} |