#include "game.h"
LPDIRECT3DTEXTURE9 cat_left_image;
LPDIRECT3DTEXTURE9 cat_right_image;
LPDIRECT3DTEXTURE9 caveman_left_image;
LPDIRECT3DTEXTURE9 caveman_right_image;
SPRITE catLeft;
SPRITE catRight;
SPRITE cavemanLeft;
SPRITE cavemanRight;
LPDIRECT3DSURFACE9 back;
LPD3DXSPRITE sprite_handler;
HRESULT result;
//timing variable
long start = GetTickCount();
//initializes the game
int Game_Init(HWND hwnd)
{
//set random number seed
srand(time_t(NULL));
//create sprite handler object
result = D3DXCreateSprite(d3ddev, &sprite_handler);
if (result != D3D_OK)
return 0;
//load the background image
back = LoadSurface("background.bmp", NULL);
//load texture with "pink" as the transparent color
cat_left_image = LoadTexture("catLeft.bmp", D3DCOLOR_XRGB(255,0,255));
if (cat_left_image == NULL)
return 0;
// initialize catLeft's properties
catLeft.x = 96;
catLeft.y = 150;
catLeft.width = 96;
catLeft.height = 96;
catLeft.curframe = 0;
catLeft.lastframe = 5;
catLeft.animdelay = 2;
catLeft.animcount = 0;
catLeft.movex = 8;
catLeft.movey = 0;
//load texture with "pink" as the transparent color
cat_right_image = LoadTexture("catRight.bmp", D3DCOLOR_XRGB(255,0,255));
if (cat_right_image == NULL)
return 0;
// initialize catRight's properties
catRight.x = 96;
catRight.y = 150;
catRight.width = 96;
catRight.height = 96;
catRight.curframe = 0;
catRight.lastframe = 5;
catRight.animdelay = 2;
catRight.animcount = 0;
catRight.movex = 8;
catRight.movey = 0;
//load texture with "pink" as the transparent color
caveman_left_image = LoadTexture("cavemanLeft.bmp", D3DCOLOR_XRGB(255,0,255));
if (caveman_left_image == NULL)
return 0;
//initialize cavemanLeft's properties
cavemanLeft.x = 100;
cavemanLeft.y = 180;
cavemanLeft.width = 50;
cavemanLeft.height = 64;
cavemanLeft.curframe = 1;
cavemanLeft.lastframe = 11;
cavemanLeft.animdelay = 3;
cavemanLeft.animcount = 0;
cavemanLeft.movex = 5;
cavemanLeft.movey = 0;
//load texture with "pink" as the transparent color
caveman_right_image = LoadTexture("cavemanRight.bmp", D3DCOLOR_XRGB(255,0,255));
if (caveman_right_image == NULL)
return 0;
//initialize cavemanLeft's properties
cavemanRight.x = 100;
cavemanRight.y = 180;
cavemanRight.width = 50;
cavemanRight.height = 64;
cavemanRight.curframe = 1;
cavemanRight.lastframe = 11;
cavemanRight.animdelay = 3;
cavemanRight.animcount = 0;
cavemanRight.movex = 5;
cavemanRight.movey = 0;
//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();
//move cat to the left
catLeft.x += catLeft.movex;
catLeft.y += catLeft.movey;
//"warp" the sprite at screen edges
if (catLeft.x > SCREEN_WIDTH - catLeft.width)
catLeft.movex *= -1;
else if (catLeft.x < 0)
{
catLeft.movex *= -1;
catLeft.x += catLeft.movex;
}
//move cat to the right
catRight.x += catRight.movex;
catRight.y += catRight.movey;
//"warp" the sprite at screen edges
if (catRight.x > SCREEN_WIDTH - catRight.width)
catRight.movex *= -1;
else if (catRight.x < 0)
{
catRight.movex *= -1;
catRight.x += catRight.movex;
}
//move caveman to the right
if (KEY_DOWN(VK_RIGHT))
{
cavemanRight.x += cavemanRight.movex;
cavemanRight.y += cavemanRight.movey;
}
//move caveman to the left
if (KEY_DOWN(VK_LEFT))
{
cavemanRight.x -= cavemanRight.movex;
cavemanRight.y -= cavemanRight.movey;
}
//"warp" the sprite at screen edges
if (cavemanRight.x > SCREEN_WIDTH - cavemanRight.width)
{
cavemanRight.x = SCREEN_WIDTH - cavemanRight.width;
}
else if (cavemanRight.x < 0)
{
cavemanRight.x = 0;
}
//has animation delay reached threshold?
if (++cavemanRight.animcount > cavemanRight.animdelay)
{
//reset counter
cavemanRight.animcount = 0;
//animate the sprite
if (++cavemanRight.curframe > cavemanRight.lastframe)
cavemanRight.curframe = 1;
}
if (++cavemanLeft.animcount > cavemanLeft.animdelay)
{
//reset counter
cavemanLeft.animcount = 0;
//animate the sprite
if (++cavemanLeft.curframe > cavemanLeft.lastframe)
cavemanLeft.curframe = 1;
}
if (++catRight.animcount > catRight.animdelay)
{
//reset counter
catRight.animcount = 0;
//animate the sprite
if (++catRight.curframe > catRight.lastframe)
catRight.curframe = 1;
}
if (++catLeft.animcount > catLeft.animdelay)
{
//reset counter
catLeft.animcount = 0;
//animate the sprite
if (++catLeft.curframe > catLeft.lastframe)
catLeft.curframe = 1;
}
}
//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);
//create vector to update caveman position
D3DXVECTOR3 cavemanRightPosition((float)cavemanRight.x, (float)cavemanRight.y, 0);
D3DXVECTOR3 cavemanLeftPosition((float)cavemanLeft.x, (float)cavemanLeft.y, 0);
D3DXVECTOR3 catRightPosition ((float)catRight.x, (float)catRight.y, 0);
D3DXVECTOR3 catLeftPosition ((float)catLeft.x, (float)catLeft.y, 0);
//configure the rect for the source tile
RECT catSrcRect;
RECT cavemanLeftSrcRect;
RECT cavemanRightSrcRect;
int catColumns = 6;
int cavemanColumns = 8;
catSrcRect.left = (catLeft.curframe % catColumns) * catLeft.width;
catSrcRect.top = (catLeft.curframe / catColumns) * catLeft.height;
catSrcRect.right = catSrcRect.left + catLeft.width;
catSrcRect.bottom = catSrcRect.top + catLeft.height;
cavemanLeftSrcRect.left = (cavemanLeft.curframe % cavemanColumns) * cavemanLeft.width;
cavemanLeftSrcRect.top = (cavemanLeft.curframe / cavemanColumns) * cavemanLeft.height;
cavemanLeftSrcRect.right = cavemanLeftSrcRect.left + cavemanLeft.width;
cavemanLeftSrcRect.bottom = cavemanLeftSrcRect.top + cavemanLeft.height;
cavemanRightSrcRect.left = (cavemanRight.curframe % cavemanColumns) * cavemanRight.width;
cavemanRightSrcRect.top = (cavemanRight.curframe / cavemanColumns) * cavemanRight.height;
cavemanRightSrcRect.right = cavemanRightSrcRect.left + cavemanRight.width;
cavemanRightSrcRect.bottom = cavemanRightSrcRect.top + cavemanRight.height;
//draw the sprite
if (cavemanRight.movex > 0)
{
sprite_handler->Draw(
caveman_right_image,
&cavemanRightSrcRect,
NULL,
&cavemanRightPosition,
D3DCOLOR_XRGB(255,255,255));
}
else if (cavemanRight.movex < 0)
{
sprite_handler->Draw(
caveman_left_image,
&cavemanLeftSrcRect,
NULL,
&cavemanLeftPosition,
D3DCOLOR_XRGB(255,255,255));
}
if (catLeft.movex > 0)
{
sprite_handler->Draw(
cat_right_image,
&catSrcRect,
NULL,
&catRightPosition,
D3DCOLOR_XRGB(255,255,255));
}
else if (catLeft.movex < 0)
{
sprite_handler->Draw(
cat_left_image,
&catSrcRect,
NULL,
&catLeftPosition,
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 (caveman_left_image != NULL)
caveman_left_image->Release();
if (caveman_right_image != NULL)
caveman_right_image->Release();
if (cat_left_image != NULL)
cat_left_image->Release();
if (cat_right_image != NULL)
cat_right_image->Release();
if (back != NULL)
back->Release();
if (sprite_handler != NULL)
sprite_handler->Release();
}
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:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
by: psycho_bloodPosted on 2009-04-12 at 23:13:24ID: 24128171
OK, I already fixed most of my problems and now I can move my caveman in all directions across the screen. However, when the caveman moves to the left is not properly drawn/animated. Could anyone take a look at my code below and tell me what I am doing wrong?
Select allOpen in new window