Solved

Trying to animate sprite using DirectX9 and C++

Posted on 2009-04-12
4
7,068 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:psycho_blood
ID: 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?
#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
ID: 24144794
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
ID: 24144906
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
ID: 24301449
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do i run a c++ file? 15 51
Is Dropbox the only way to upload large files? 3 51
C++ help/ Toy problem 19 53
How to get out of play mode in Unity 2 24
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 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