Solved

Trying to animate sprite using DirectX9 and C++

Posted on 2009-04-12
4
7,037 Views
Last Modified: 2013-12-08
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!

#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();
 

}

Open in new window

background.bmp
catLeft.bmp
catRight.bmp
cavemanLeft.bmp
cavemanRight.bmp
0
Comment
Question by:psycho_blood
  • 3
4 Comments
 

Author Comment

by:psycho_blood
Comment Utility
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?
#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;

		}
 

		//move caveman up

		if (KEY_DOWN(VK_UP))

		{

			cavemanRight.height += cavemanRight.movey;

			cavemanRight.y--;

		}
 

		//move caveman down

		if (KEY_DOWN(VK_DOWN))

		{

			cavemanRight.height -= cavemanRight.movey;

			cavemanRight.y++;

		}
 

		//"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;

		}
 

		if (cavemanRight.y > SCREEN_HEIGHT - cavemanRight.height)

		{

			cavemanRight.y = SCREEN_HEIGHT - cavemanRight.height;

		}

		else if (cavemanRight.y < 0)

		{

			cavemanRight.y = 0;

		}
 

		//move caveman to the right

		if (KEY_DOWN(VK_RIGHT))

		{

			cavemanLeft.x += cavemanLeft.movex;

			cavemanLeft.y += cavemanLeft.movey;

		}
 

		//move caveman to the left

		if (KEY_DOWN(VK_LEFT))

		{

			cavemanLeft.x -= cavemanLeft.movex;

			cavemanLeft.y -= cavemanLeft.movey;

		}
 

		//move caveman up

		if (KEY_DOWN(VK_UP))

		{

			cavemanLeft.height += cavemanLeft.movey;

			cavemanLeft.y--;

		}
 

		//move caveman down

		if (KEY_DOWN(VK_DOWN))

		{

			cavemanLeft.height -= cavemanLeft.movey;

			cavemanLeft.y++;

		}
 

		//"warp" the sprite at screen edges

		if (cavemanLeft.x > SCREEN_WIDTH - cavemanLeft.width)

		{

			cavemanLeft.x = SCREEN_WIDTH - cavemanLeft.width;

		}

		else if (cavemanLeft.x < 0)

		{

			cavemanLeft.x = 0;

		}
 

		if (cavemanLeft.y > SCREEN_HEIGHT - cavemanLeft.height)

		{

			cavemanLeft.y = SCREEN_HEIGHT - cavemanLeft.height;

		}

		else if (cavemanLeft.y < 0)

		{

			cavemanLeft.y = 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)

		if (KEY_DOWN(VK_RIGHT)) //I need a better condition than this!

		{

			sprite_handler->Draw(

				caveman_right_image,

				&cavemanRightSrcRect,

				NULL,

				&cavemanRightPosition,

				D3DCOLOR_XRGB(255,255,255));

		}

		//else if (cavemanRight.movex < 0)

		else if (KEY_DOWN(VK_LEFT)) //I need a better condition than this!

		{

			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();
 

}

Open in new window

0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
I can see two times  'move caveman to the left" is this needed?

		//move caveman to the left

		if (KEY_DOWN(VK_LEFT))

		{

			cavemanRight.x -= cavemanRight.movex;

			cavemanRight.y -= cavemanRight.movey;

		}
 

		//move caveman to the left

		if (KEY_DOWN(VK_LEFT))

		{

			cavemanLeft.x -= cavemanLeft.movex;

			cavemanLeft.y -= cavemanLeft.movey;

		}

Open in new window

0
 

Author Comment

by:psycho_blood
Comment Utility
I am sure duplicating code is not the most efficient way to program, considering all possible cases worked for me. So far, my code is still duplicated, but I might change it to a switch instead of nested if. What do you think?
0
 

Accepted Solution

by:
psycho_blood earned 0 total points
Comment Utility
The attached code shows how I fixed my original problem. The program is not perfect but it works.
#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;

		}
 

		//move caveman up

		if (KEY_DOWN(VK_UP))

		{

			cavemanRight.height += cavemanRight.movey;

			cavemanRight.y--;

		}
 

		//move caveman down

		if (KEY_DOWN(VK_DOWN))

		{

			cavemanRight.height -= cavemanRight.movey;

			cavemanRight.y++;

		}
 

		//"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;

		}
 

		if (cavemanRight.y > SCREEN_HEIGHT - cavemanRight.height)

		{

			cavemanRight.y = SCREEN_HEIGHT - cavemanRight.height;

		}

		else if (cavemanRight.y < 0)

		{

			cavemanRight.y = 0;

		}
 

		//move caveman to the right

		if (KEY_DOWN(VK_RIGHT))

		{

			cavemanLeft.x += cavemanLeft.movex;

			cavemanLeft.y += cavemanLeft.movey;

		}
 

		//move caveman to the left

		if (KEY_DOWN(VK_LEFT))

		{

			cavemanLeft.x -= cavemanLeft.movex;

			cavemanLeft.y -= cavemanLeft.movey;

		}
 

		//move caveman up

		if (KEY_DOWN(VK_UP))

		{

			cavemanLeft.height += cavemanLeft.movey;

			cavemanLeft.y--;

		}
 

		//move caveman down

		if (KEY_DOWN(VK_DOWN))

		{

			cavemanLeft.height -= cavemanLeft.movey;

			cavemanLeft.y++;

		}
 

		//"warp" the sprite at screen edges

		if (cavemanLeft.x > SCREEN_WIDTH - cavemanLeft.width)

		{

			cavemanLeft.x = SCREEN_WIDTH - cavemanLeft.width;

		}

		else if (cavemanLeft.x < 0)

		{

			cavemanLeft.x = 0;

		}
 

		if (cavemanLeft.y > SCREEN_HEIGHT - cavemanLeft.height)

		{

			cavemanLeft.y = SCREEN_HEIGHT - cavemanLeft.height;

		}

		else if (cavemanLeft.y < 0)

		{

			cavemanLeft.y = 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)

		if (KEY_DOWN(VK_RIGHT)) //I need a better condition than this!

		{

			sprite_handler->Draw(

				caveman_right_image,

				&cavemanRightSrcRect,

				NULL,

				&cavemanRightPosition,

				D3DCOLOR_XRGB(255,255,255));

		}

		//else if (cavemanRight.movex < 0)

		else if (KEY_DOWN(VK_LEFT)) //I need a better condition than this!

		{

			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();
 

}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now