[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

04/12/2009 at 04:48PM PDT, ID: 24316226
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Trying to animate sprite using DirectX9 and C++

Asked by psycho_blood in DirectX Graphics & Game Programming, Microsoft Visual C++.Net, C++ Programming Language

Tags: directx, sprite, animation

In this program, I create a window and display a cat walking back and forth between the screen using a left and right tileset of bitmaps. Also, I am trying to control two caveman sprites as follows:
- If you press the Right key, cavemanRight.bmp is loaded and the caveman runs to the right.
- If you press the Left key, cavemanLeft.bmp is loaded and the caveman runs to the left.
 
I would like to know why am I only able to display cavemanRight.bmp.
Thanks!
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:
#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();
 
}
Attachments:
 
Background bitmap
Background bitmap
 
 
catLeft bitmap
catLeft bitmap
 
 
catRight bitmap
catRight bitmap
 
 
cavemanLeft bitmap
cavemanLeft bitmap
 
 
cavemanRight bitmap
cavemanRight bitmap
 
[+][-]04/12/09 11:13 PM, ID: 24128171

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/14/09 09:56 PM, ID: 24144794

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/14/09 10:28 PM, ID: 24144906

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/04/09 08:56 PM, ID: 24301449

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: DirectX Graphics & Game Programming, Microsoft Visual C++.Net, C++ Programming Language
Tags: directx, sprite, animation
Sign Up Now!
Solution Provided By: psycho_blood
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625