1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include "game.h"
using namespace std;
// these 2 are not used
static Timer Update;
static Timer Move;
LPDIRECT3DTEXTURE9 tower_image;
Sprite sTower[MAX_TOWERS];
LPDIRECT3DTEXTURE9 enemy_image;
Sprite sEnemy[MAX_ENEMIES];
LPDIRECT3DTEXTURE9 bullet_image;
Sprite sBullits[MAX_BULLITS];
LPDIRECT3DSURFACE9 back;
LPD3DXSPRITE sprite_handler;
bool fired;
vector<Sprite> vecEnemySprites;
HRESULT result;
//timing variable
long start = GetTickCount();
long gap = GetTickCount();
Timer update;
//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand((unsigned int)time(NULL));
//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;
//load textures with "pink" as the transparent color
tower_image = LoadTexture("Graphics/Goodie.bmp", D3DCOLOR_XRGB(255,0,255));
if (tower_image == NULL)
return 0;
enemy_image = LoadTexture("Graphics/Baddie.bmp", D3DCOLOR_XRGB(255,0,255));
if (enemy_image == NULL)
return 0;
bullet_image = LoadTexture("Graphics/Bullet.bmp", D3DCOLOR_XRGB(255,0,255));
if (bullet_image == NULL)
return 0;
//load the background image
back = LoadSurface("Graphics/Background.bmp", NULL);
//initialize the sprite's properties
// x = 65
for (int i = 0; i < LEVEL_ONE_ENEMIES; ++i)
{
sEnemy[i] = Sprite(65, 0, 32, 32, 0, 2, 100, 50);
vecEnemySprites.push_back(sEnemy[i]);
}
//return okay
return 1;
}
//the main game loop
void Game_Run(HWND hwnd)
{
//make sure the Direct3D device is valid
if (d3ddev == NULL)
return;
//after short delay, ready for next frame?
//this keeps the game running at a steady frame rate
if (GetTickCount() - start >= 30)
{
//reset timing
start = GetTickCount();
for (int i = 0; i < 10; i++)
{
//sEnemy[i].mIsAlive = true;
sEnemy[i].MoveEnemy();
}
}
//create vector to update sprite position
D3DXVECTOR3* positions = 0; // create a pointer to D3DXVECTOR3
// positions now points to start of the array of D3DXVECTOR3's
positions = new D3DXVECTOR3[LEVEL_ONE_ENEMIES];
for (int i = 0; i < LEVEL_ONE_ENEMIES; i++)
{
positions[i].x = sEnemy[i].getX();
positions[i].y = sEnemy[i].getY();
positions[i].z = 0;
}
//start rendering
if (d3ddev->BeginScene())
{
//erase the entire background
d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
//start sprite handler
sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);
for (int i = 0; i < LEVEL_ONE_ENEMIES; i++)
{
if (sEnemy[i].mIsAlive == true)
{
sprite_handler->Draw(
enemy_image,
NULL, NULL,
&positions[i],
D3DCOLOR_XRGB(255,255,255));
}
}
//stop drawing
sprite_handler->End();
//stop rendering
d3ddev->EndScene();
//display the back buffer on the screen
d3ddev->Present(NULL, NULL, NULL, NULL);
}
//check for escape key (to exit program)
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
}
//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{
if (tower_image != NULL)
tower_image->Release();
if (enemy_image != NULL)
enemy_image->Release();
if (back != NULL)
back->Release();
if (sprite_handler != NULL)
sprite_handler->Release();
}
|