Sign in to follow this  
SSJCORY

Animation Problem.

Recommended Posts

I have created a class to handle my animations but it isnt working nothing is being displayed :'( any idea what is wrong? Heres my code for my cAnimate class;
class cAnimate{
private:
	vector <HBITMAP>animation;
	bool active;
	int currentframe;
	int frametime;
	int lastupdate;
	int x;
	int y;
	int width;
	int height;
public:
	cAnimate(){
		active = false;
		currentframe = 0;
		frametime = 30;
		lastupdate = GetTickCount();
	}
	int getLastUpdate(){
		return lastupdate;
	}
	int getFrameTime(){
		return frametime;
	}
	void addFrame(HBITMAP newframe){
		animation.push_back(newframe);
	}
	void removeFrame(int index){
		animation.erase(animation.begin() + index);
	}
	void activate(){
		active = true;
	}
	bool isActive(){
		return active;
	}
	int getCurrentFrame(){
		return currentframe;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	int getWidth(){
		return width;
	}
	int getHeight(){
		return height;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,(HBITMAP)animation[currentframe]);
		if(currentframe < animation.size()){
			currentframe++;
		}
		else{
			currentframe = 0;
			active = false;
		}
		BitBlt(hdc,x,y,width,height,idc,0,0,SRCCOPY);
		DeleteDC(idc);
		lastupdate = GetTickCount();
	}
};

I declare cAnimate boom globally outside of main. cAnimate boom; then inside of class cMap there is a method called didcollide and within didcollide it check for player collisions. From the player x's and y's i calculate the middle point for their collision. Then i boom.setX(that) boom.setY(that) then i call boom.activate(); in main i have this:
	boom.addFrame((HBITMAP)LoadImage(0,"boom1.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE));
	boom.addFrame((HBITMAP)LoadImage(0,"boom2.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE));
	boom.addFrame((HBITMAP)LoadImage(0,"boom3.bmp",IMAGE_BITMAP,150,150,LR_LOADFROMFILE));

	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
			if(boom.isActive()){
				int ticker = GetTickCount();
				if(boom.getLastUpdate() + boom.getFrameTime() < ticker){
					boom.display(hwnd);
				}
			}
		}
	}

It should go through the three images and then stop. I dont konw why but it doesnt display any images :'( Here is full source for my project:
#include<windows.h>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class cMap;
class cPlayer;
class cAnimate{
private:
	vector <HBITMAP>animation;
	bool active;
	int currentframe;
	int frametime;
	int lastupdate;
	int x;
	int y;
	int width;
	int height;
public:
	cAnimate(){
		active = false;
		currentframe = 0;
		frametime = 30;
		lastupdate = GetTickCount();
	}
	int getLastUpdate(){
		return lastupdate;
	}
	int getFrameTime(){
		return frametime;
	}
	void addFrame(HBITMAP newframe){
		animation.push_back(newframe);
	}
	void removeFrame(int index){
		animation.erase(animation.begin() + index);
	}
	void activate(){
		active = true;
	}
	bool isActive(){
		return active;
	}
	int getCurrentFrame(){
		return currentframe;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	int getWidth(){
		return width;
	}
	int getHeight(){
		return height;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,(HBITMAP)animation[currentframe]);
		if(currentframe < animation.size()){
			currentframe++;
		}
		else{
			currentframe = 0;
			active = false;
		}
		BitBlt(hdc,x,y,width,height,idc,0,0,SRCCOPY);
		DeleteDC(idc);
		lastupdate = GetTickCount();
	}
};
cAnimate boom;
class cMap{
private:
	int width;
	int height;
	int mousex;
	int mousey;
	vector<cPlayer> players;
	int currentplayer;
public:
	int getwidth(){
		return width;
	}
	int getheight(){
		return height;
	}
	cMap(){
		width = 500;
		height = 500;
		currentplayer = 1; 
	}
	cMap(int newwidth,int newheight){
		width = newwidth;
		height = newheight;
		currentplayer = 1;
	}
	void setMouse(int x, int y){
		mousex = x;
		mousey = y;
	}
	int getMouseX(){
		return mousex;
	}
	int getMouseY(){
		return mousey;
	}
	void moveplayer(int direction);
	void addplayer(cPlayer newplayer);
	void removeplayer(int index);
	void display(HWND hwnd);
	int didcollide(cPlayer player1,cPlayer player2);
	void isClicked(cPlayer player);
	cPlayer getPlayer(int playerid);
};
class cPlayer{
private:
	int x;
	int y;
	int life;
	string name;
	HBITMAP visual;
public:
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	HBITMAP getVisual(){
		return visual;
	}
	string getName(){
		return name;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	void setVisual(HBITMAP newbitmap){
		visual = newbitmap;
	}
	cPlayer(){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(int startx,int starty,string newname){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = startx;
		y = starty;
		name = newname;
		life = 100;
	}
	cPlayer(HBITMAP hbitmap){
		visual = hbitmap;
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(HBITMAP hbitmap,int startx,int starty){
		visual = hbitmap;
		x = startx;
		y = starty;
		name = "default";
		life = 100;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,visual);
		BitBlt(hdc,x,y,50,50,idc,0,0,SRCCOPY);
		ostringstream text;
		text<<life;
		TextOut(hdc,x-15,y-70,"--------------",14);
		TextOut(hdc,x-17,y-70,"+",1);
		TextOut(hdc,x-17,y-40,"+",1);
		TextOut(hdc,x-17,y-60,"+",1);
		TextOut(hdc,x,y-40,text.str().c_str(),text.str().size());
		TextOut(hdc,x,y-60,name.c_str(),name.size());
		DeleteDC(idc);
		SetPixel(hdc,500,500,RGB(255,0,0));
	}
};
cMap cmap;
bool done = false;
cPlayer player4(400,400,"DAVID");
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		return 0;
		break;
	case WM_LBUTTONDOWN:
		cmap.setMouse(LOWORD(lParam),HIWORD(lParam));
		cmap.isClicked(cmap.getPlayer(2));
		break;
	case WM_KEYDOWN:
		RECT rect;
		HDC hdc = GetDC(hwnd);
		GetWindowRect(hwnd,&rect);
		FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
		switch(wParam){
		case VK_LEFT:
			cmap.moveplayer(1);
			break;
		case VK_RIGHT:
			cmap.moveplayer(2);
			break;
		case VK_UP:
			cmap.moveplayer(3);
			break;
		case VK_DOWN:
			cmap.moveplayer(4);
			break;
		}
		cmap.display(hwnd);
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
	HWND hwnd;
	MSG msg;
	WNDCLASS wc;
	wc.cbClsExtra = NULL;
	wc.cbWndExtra = NULL;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "cory";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	hwnd = CreateWindow("cory","window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	cPlayer player(200,200,"Kara");
	cPlayer player2(300,300,"Cory");
	cPlayer player3(400,400,"Alex");
	
	cmap.addplayer(player);
	cmap.addplayer(player2);
	cmap.addplayer(player3);
	boom.addFrame((HBITMAP)LoadImage(0,"boom1.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE));
	boom.addFrame((HBITMAP)LoadImage(0,"boom2.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE));
	boom.addFrame((HBITMAP)LoadImage(0,"boom3.bmp",IMAGE_BITMAP,150,150,LR_LOADFROMFILE));

	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
			if(boom.isActive()){
				int ticker = GetTickCount();
				if(boom.getLastUpdate() + boom.getFrameTime() < ticker){
					boom.display(hwnd);
				}
			}
		}
	}
	UnregisterClass("cory",hInstance);
	return 0;
}
void cMap::display(HWND hwnd){
	HDC hdc = GetDC(hwnd);
	for(int i = 0; i <= players.size()-1;i++){
		for(int j = 0; j <= players.size()-1;j++){
			if(cmap.didcollide(players[i],players[j]) == 1 && i != j){
				int diffx;
				int diffy;
				int boomx;
				int boomy;
				if(players[i].getX() >= players[j].getX()){
					diffx = players[i].getX() - players[j].getX();
					boomx = players[j].getX() + (diffx/2);
					boom.setX(boomx);
				}
				else{
					diffx = players[j].getX() - players[i].getX();
					boomx = players[i].getX() + (diffx/2);
					boom.setX(boomx);
				}
				if(players[i].getY() >= players[j].getY()){
					diffy = players[i].getY() - players[j].getY();
					boomy = players[j].getY() + (diffy/2);
					boom.setY(boomy);
				}
				else{
					diffy = players[j].getY() - players[i].getY();
					boomy = players[i].getY() + (diffy/2);
					boom.setY(boomy);
				}
				cmap.removeplayer(i);
				cmap.removeplayer(j-1);
				boom.activate();
				
			}
		}
	}
	ostringstream os;
	os<<currentplayer+1;
	TextOut(hdc,10,10,"Player ",7);
	TextOut(hdc,60,10,os.str().c_str(),os.str().size());
	int countdown = players.size()-1;
	while(countdown >= 0){
		players[countdown].display(hwnd);
		countdown--;
	}
}
void cMap::addplayer(cPlayer newplayer){
	players.push_back(newplayer);
}
void cMap::moveplayer(int direction){
	if(direction == 1){
		players[currentplayer].setX(players[currentplayer].getX()-10);
	}
	if(direction == 2){
		players[currentplayer].setX(players[currentplayer].getX()+10);
	}
	if(direction == 3){
		players[currentplayer].setY(players[currentplayer].getY()-10);
	}
	if(direction == 4){
		players[currentplayer].setY(players[currentplayer].getY()+10);
	}
	if(currentplayer >= players.size() -1){
		currentplayer = 0;
	}
	else{
		currentplayer++;
	}
}
int cMap::didcollide(cPlayer player1,cPlayer player2){
		int left1,left2;
		int right1,right2;
		int top1,top2;
		int bottom1,bottom2;
		left1 = player1.getX();
		left2 = player2.getX();
		right1 = player1.getX() + 50;
		right2 = player2.getX() + 50;
		top1 = player1.getY();
		top2 = player2.getY();
		bottom1 = player1.getY() + 50;
		bottom2 = player2.getY() + 50;
		if (bottom1 < top2) return(0);
		if (top1 > bottom2) return(0);
		if (right1 < left2) return(0);
		if (left1 > right2) return(0);
		return 1;
}
void cMap::isClicked(cPlayer player){
	if(player.getX() <= mousex && player.getX() + 50 >= mousex){
		if(player.getY() <= mousey && player.getY() + 50 >= mousey){//400,400
			MessageBox(NULL,"OMG CLICKED!","CLICK!",MB_OK);
		}
	}
}
cPlayer cMap::getPlayer(int playerid){
	return players[playerid];
}
void cMap::removeplayer(int index){
	players.erase(players.begin()+index);
}

Thanks a lot guys, Cory

Share this post


Link to post
Share on other sites
OK guys i figured it out now i have a vector for width and height so they can change with the size of the animation.
-Cory

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this