/***************************************************************************/
/*
*/
/* File: game.cpp
*/
/*
*/
/***************************************************************************/
#include
<d3dx8.h>
#include
<d3d8.h>
// Okay now, this function will only return true if it is called and at
least
// two seconds has passed, else it will return false.
bool
DelayTime()
{
static DWORD prev_time = 0;
// Convert our GetTickCount value to seconds
instead of milliseconds.
DWORD cur_time = 0.001 * GetTickCount();
if(prev_time < cur_time)
{
// If where in here 2 seconds has
passed. Change the 2 to 0.5 etc for a
// a less time etc.
prev_time = cur_time + 2;
return
true;
}
// If where here 2 seconds hasn't passed!
return false;
}
void
draw_triangle(float x,
float y, float
z)
{
struct my_vertex
{
FLOAT x, y, z; // D3DFVF_XYZ
DWORD colour; // D3DFVF_DIFFUSE
};
my_vertex v[] =
{
// Three points make up the face of the triangle.
{-0.5f + x, -0.5f + y, 0.0f +z, 0xffff0000},
//[1]
{ 0.0f + x, 0.5f + y, 0.0f +z, 0xff00222f},
//[2]
{ 0.5f + x, -0.5f + y, 0.0f +z, 0xff0aff00}
//[3]
};
UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
IDirect3DVertexBuffer8 * DX_vb;
g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex),
0, my_vertex_description, D3DPOOL_MANAGED, &DX_vb );
// 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();
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);
// Draw our triangle.
g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));
g_pD3DDevice->SetVertexShader(my_vertex_description);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
DX_vb->Release();
}
// Well if we need to do any drawing or rendering triangles we can do it in
here!
void
Render()
{
if(!g_pD3DDevice)return;
// 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 );
float static
move = 0;
// If 2 seconds has passed DelayTime will return
true! and so our triangle will move a little bit
// up.
if( DelayTime() )
move = move + 0.1;
draw_triangle(0.0f, move, 0.0f);
// After rendering the scene we display it.
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
void
mainloop()
{
Render();
} |