SHWOO 25 Junior Poster in Training

Hi, I am trying to get a cat walking back and forth between the screen using a left and right tileset of bitmaps. Also, I am supposed to be able to control two caveman sprites that appear to walk left and right as you press the left and right keys.

I am having trouble with the movement of the caveman.

#include "game.h"

LPDIRECT3DTEXTURE9 catleft_image;
LPDIRECT3DTEXTURE9 catright_image;
LPDIRECT3DTEXTURE9 cavemanright_image;
LPDIRECT3DTEXTURE9 cavemanleft_image;
SPRITE catRight;
SPRITE catLeft;
SPRITE cavemanRight;
SPRITE cavemanLeft;

LPDIRECT3DSURFACE9 back;
LPD3DXSPRITE sprite_handler;


HRESULT result;

//timing variable
long start = GetTickCount();

//initialize the game
int Game_Init(HWND hwnd)
{
	//set random number seed
	srand(time(NULL));

	//create sprite handler object
	result = D3DXCreateSprite(d3ddev, &sprite_handler);
	if(result != D3D_OK)
		return 0;

	//load textures
	catleft_image = LoadTexture("catLeft.bmp", D3DCOLOR_XRGB(255,0,255));
	if(catleft_image == NULL)
		return 0;
	
	// initialize the sprite's properties
	// set 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;

	catright_image = LoadTexture("catRight.bmp", D3DCOLOR_XRGB(255,0,255));
	if(catright_image == NULL)
		return 0;

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

	cavemanright_image = LoadTexture("cavemanRight.bmp", D3DCOLOR_XRGB(255,0,255));
	if(cavemanright_image == NULL)
		return 0;

	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;

	cavemanleft_image = LoadTexture("cavemanLeft.bmp", D3DCOLOR_XRGB(255,0,255));
	if(cavemanleft_image == NULL)
		return 0;

	//set cavemanLeft properties
	cavemanLeft.x = 64;
	cavemanLeft.y = 180;
	cavemanLeft.width = 50;
	cavemanLeft.height = 64;
	cavemanLeft.curframe = 0;
	cavemanLeft.lastframe = 11;
	cavemanLeft.animdelay = 3;
	cavemanLeft.animcount = 0;
	cavemanLeft.movex = -5;
	cavemanLeft.movey = 0;

	 back = LoadSurface("background.bmp", NULL );	

	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 the sprite
		catRight.x += catRight.movex;
		catRight.y += catRight.movey;


		if ( catRight.x > SCREEN_WIDTH - catRight.width )
		{
			catRight.x += 0;
			catRight.movex *= -1;
			catLeft.movex *= -1;
		}
		else
			if ( catRight.x < 0 )
			{
				catRight.movex *= -1;
				catRight.x += catRight.movex;
				catLeft.movex *= -1;
				catLeft.x += catLeft.movex;
			}
			

			if(cavemanRight.x > SCREEN_WIDTH - cavemanRight.width)
			{
				cavemanRight.movex = 0;
			}
			else if(cavemanRight.x < 0)
			{
				cavemanRight.movex *= -1;
				cavemanRight.x += cavemanRight.movex;
				cavemanLeft.movex *=-1;
				cavemanLeft.x += cavemanLeft.movex;
			}
		
		//move the sprite
		if(KEY_DOWN(VK_RIGHT)){
			cavemanRight.x += cavemanRight.movex;
			cavemanRight.y += cavemanRight.movey;
		}

		if(KEY_DOWN(VK_LEFT))
		{
			cavemanLeft.x += cavemanLeft.movex;
		}	

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

		//has animation delay reached threshold?
		if (++cavemanLeft.animcount > cavemanLeft.animdelay)
		{
			//reset counter
			cavemanLeft.animcount = 0;

			//animate the sprite
			if (++cavemanLeft.curframe > cavemanLeft.lastframe)
				cavemanLeft.curframe = 0;
		}

		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 = 0;
		}
	}

	//start rendering 
	if (d3ddev->BeginScene())
	{
		//erase the entire background
		d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

		sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);

		//create vector to update sprite position
		D3DXVECTOR3 position((float)cavemanRight.x, (float)cavemanRight.y, 0);
		D3DXVECTOR3 position3((float)catRight.x, (float)catRight.y, 0);

		//this variable will keep cavemanRight from drawing
		bool drawCavemanRight;

		//configure the rect for the source tile
		RECT catSrc;
		RECT caveSrc;
			int caveColumns = 8;
			int catColumns = 6;

			catSrc.left = (catRight.curframe % catColumns) * catRight.width;
			catSrc.top = (catRight.curframe / catColumns) * catRight.height;
			catSrc.right = catSrc.left + catRight.width;
			catSrc.bottom = catSrc.top + catRight.height;
		
			caveSrc.left = (cavemanRight.curframe % caveColumns) * cavemanRight.width;
			caveSrc.top = (cavemanRight.curframe / caveColumns) * cavemanRight.height;
			caveSrc.right = caveSrc.left + cavemanRight.width;
			caveSrc.bottom = caveSrc.top + cavemanRight.height;	


			//if cavemanLeft if being drawn do not draw right caveman

			if(cavemanRight.movex > 0)
			{
				sprite_handler->Draw(
					cavemanright_image,
					&caveSrc,
					NULL,
					&position,
					D3DCOLOR_XRGB(255,255,255));
			}

			else if(cavemanLeft.movex < 0)
			{
				sprite_handler->Draw(
					cavemanleft_image,
					&caveSrc,
					NULL,
					&position,
					D3DCOLOR_XRGB(255,255,255));
			}

		if(catRight.movex > 0)
		{
			sprite_handler->Draw(
				catright_image,
				&catSrc,
				NULL,
				&position3,
				D3DCOLOR_XRGB(255,255,255));
		}

		else if(catRight.movex < 0)
		{
			sprite_handler->Draw(
			catleft_image,
			&catSrc,
			NULL,
			&position3,
			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(cavemanright_image != NULL)
		cavemanright_image->Release();
	if(cavemanleft_image != NULL)
		cavemanleft_image->Release();
	if(catright_image != NULL)
		catright_image->Release();
	if(catleft_image != NULL)
		catleft_image->Release();

	if(back != NULL)
	back->Release();

	if(sprite_handler != NULL)
		sprite_handler->Release();
}