Sign in to follow this  
SSJCORY

Is this a good start?

Recommended Posts

I am starting on a game is this like a good start am i going about things correctly? Heres my code.
#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;
	vector <int>width;
	vector <int>height;
public:
	cAnimate(){
		active = false;
		currentframe = 0;
		frametime = 300;
		lastupdate = GetTickCount();
	}
	int getLastUpdate(){
		return lastupdate;
	}
	int getFrameTime(){
		return frametime;
	}
	void addFrame(HBITMAP newframe,int newwidth,int newheight){
		animation.push_back(newframe);
		width.push_back(newwidth);
		height.push_back(newheight);
	}
	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(int index){
		return width[index];
	}
	int getHeight(int index){
		return height[index];
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,(HBITMAP)animation[currentframe]);
		BitBlt(hdc,x,y,width[currentframe],height[currentframe],idc,0,0,SRCCOPY);
		DeleteDC(idc);
		lastupdate = GetTickCount();
		if(currentframe < animation.size()){
			currentframe++;
		}
		else{
			currentframe = 0;
			active = false;
		}
	}
};
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),50,50);
	boom.addFrame((HBITMAP)LoadImage(0,"boom2.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE),100,100);
	boom.addFrame((HBITMAP)LoadImage(0,"boom3.bmp",IMAGE_BITMAP,150,150,LR_LOADFROMFILE),150,150);

	while(!done){
		if(boom.isActive()){
			int ticker = GetTickCount();
			if(boom.getLastUpdate() + boom.getFrameTime() < ticker){
				boom.display(hwnd);
			}
		}
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
		}
	}
	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 for your input, -CORY

Share this post


Link to post
Share on other sites
Ok yikes i tried to do what you told me and i got errors >.< At first i had 200+ errors i got it down to 11. Do you think you can help me? Heres the sources:
player.h:

#include<string>
#include<windows.h>
#include<sstream>
using namespace std;
class cPlayer{
private:
int x;
int y;
int life;
string name;
HBITMAP visual;
public:
int getX();
int getY();
HBITMAP getVisual();
string getName();
void setX(int newx);
void setY(int newy);
void setVisual(HBITMAP newbitmap);
cPlayer();
cPlayer(int startx,int starty,string newname);
cPlayer(HBITMAP hbitmap);
cPlayer(HBITMAP hbitmap,int startx,int starty);
void display(HWND hwnd);
};


player.cpp:

#include"Player.h"
void cPlayer::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));
}
cPlayer::cPlayer(HBITMAP hbitmap,int startx,int starty){
visual = hbitmap;
x = startx;
y = starty;
name = "default";
life = 100;
}
cPlayer::cPlayer(HBITMAP hbitmap){
visual = hbitmap;
x = 0;
y = 0;
name = "default";
life = 100;
}
cPlayer::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::cPlayer(){
visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
x = 0;
y = 0;
name = "default";
life = 100;
}
void cPlayer::setVisual(HBITMAP newbitmap){
visual = newbitmap;
}
int cPlayer::getX(){
return x;
}
int cPlayer::getY(){
return y;
}
HBITMAP cPlayer::getVisual(){
return visual;
}
string cPlayer::getName(){
return name;
}
void cPlayer::setX(int newx){
x = newx;
}
void cPlayer::setY(int newy){
y = newy;
}


map.h:

#include<vector>
#include"player.h"
using namespace std;
class cMap{
private:
int width;
int height;
int mousex;
int mousey;
vector<cPlayer> players;
int currentplayer;
public:
int getwidth();
int getheight();
cMap();
cMap(int newwidth,int newheight);
void setMouse(int x, int y);
int getMouseX();
int getMouseY();
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);
};


map.cpp:

#include"map.h"
int cMap::getwidth(){
return width;
}
int cMap::getheight(){
return height;
}
cMap::cMap(){
width = 500;
height = 500;
currentplayer = 1;
}
cMap::cMap(int newwidth,int newheight){
width = newwidth;
height = newheight;
currentplayer = 1;
}
void cMap::setMouse(int x, int y){
mousex = x;
mousey = y;
}
int cMap::getMouseX(){
return mousex;
}
int cMap::getMouseY(){
return mousey;
}
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);
}


animate.h:

#include<vector>
#include<windows.h>
using namespace std;
class cAnimate{
private:
vector <HBITMAP>animation;
bool active;
int currentframe;
int frametime;
int lastupdate;
int x;
int y;
vector <int>width;
vector <int>height;
public:
cAnimate();
int getLastUpdate();
int getFrameTime();
void addFrame(HBITMAP newframe,int newwidth,int newheight);
void removeFrame(int index);
void activate();
bool isActive();
int getCurrentFrame();
void setX(int newx);
void setY(int newy);
int getX();
int getY();
int getWidth(int index);
int getHeight(int index);
void display(HWND hwnd);
};


animate.cpp:

#include"animate.h"
cAnimate::cAnimate(){
active = false;
currentframe = 0;
frametime = 300;
lastupdate = GetTickCount();
}
int cAnimate::getLastUpdate(){
return lastupdate;
}
int cAnimate::getFrameTime(){
return frametime;
}
void cAnimate::addFrame(HBITMAP newframe,int newwidth,int newheight){
animation.push_back(newframe);
width.push_back(newwidth);
height.push_back(newheight);
}
void cAnimate::removeFrame(int index){
animation.erase(animation.begin() + index);
}
void cAnimate::activate(){
active = true;
}
bool cAnimate::isActive(){
return active;
}
int cAnimate::getCurrentFrame(){
return currentframe;
}
void cAnimate::setX(int newx){
x = newx;
}
void cAnimate::setY(int newy){
y = newy;
}
int cAnimate::getX(){
return x;
}
int cAnimate::getY(){
return y;
}
int cAnimate::getWidth(int index){
return width[index];
}
int cAnimate::getHeight(int index){
return height[index];
}
void cAnimate::display(HWND hwnd){
HDC hdc = GetDC(hwnd);
HDC idc = CreateCompatibleDC(hdc);
SelectObject(idc,(HBITMAP)animation[currentframe]);
BitBlt(hdc,x,y,width[currentframe],height[currentframe],idc,0,0,SRCCOPY);
DeleteDC(idc);
lastupdate = GetTickCount();
if(currentframe < animation.size()){
currentframe++;
}
else{
currentframe = 0;
active = false;
}
}


main.cpp:

#include<windows.h>
#include<vector>
#include<string>
#include<sstream>
#include"animate.h"
#include"map.h"
#include"player.h"
using namespace std;
cAnimate boom;
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),50,50);
boom.addFrame((HBITMAP)LoadImage(0,"boom2.bmp",IMAGE_BITMAP,100,100,LR_LOADFROMFILE),100,100);
boom.addFrame((HBITMAP)LoadImage(0,"boom3.bmp",IMAGE_BITMAP,150,150,LR_LOADFROMFILE),150,150);

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


Finally the 11 errors:

--------------------Configuration: New - Win32 Debug--------------------
Compiling...
main.cpp
c:\program files\microsoft visual studio\myprojects\new\player.h(5) : error C2011: 'cPlayer' : 'class' type redefinition
Map.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(32) : error C2065: 'cmap' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(32) : error C2228: left of '.didcollide' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(40) : error C2065: 'boom' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(40) : error C2228: left of '.setX' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(45) : error C2228: left of '.setX' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(50) : error C2228: left of '.setY' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(55) : error C2228: left of '.setY' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(57) : error C2228: left of '.removeplayer' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(58) : error C2228: left of '.removeplayer' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\New\Map.cpp(59) : error C2228: left of '.activate' must have class/struct/union type
Error executing cl.exe.

New.exe - 11 error(s), 0 warning(s)


Thanks a lot for whatever help u can give me.
-Cory Fisher

[Edited by - SSJCORY on June 24, 2005 10:57:22 AM]

Share this post


Link to post
Share on other sites
Your code is very hard to read. Try putting some space between lines in appropriate places (such as between functions).

Also, in your cMap::display function, you do the following:
if(cmap.didcollide(...
and..
cmap.removeplayer(i);
cmap.removeplayer(j-1);

Clearly you really shouldn't be doing that [smile]
You'd probably have found that one yourself when you seperate your code in to different files.

Share this post


Link to post
Share on other sites
Sorry for double post but the one above is rather cluttered. I got it down to 1 error by taking cMap::display and putting it after main bc it has an instance of the cmap class in it.
The only error i get now is class cPlayer class redefenition in player.h and map.h
-Cory Fisher
EDIT heh i didnt double post but ya i figured that out just as u were posting i was posting lol. Now the only error is redefenition cuz i have to #include"player.h" in both player.cpp and map.h any ideas?

Share this post


Link to post
Share on other sites
You'll need to put inclusion guards in your header files. The link provided to you by load_bitmap_file does cover them on this page.

Oh and your cMap::display function shouldn't have had a reference to an instance of the cMap class in it because display would be called by that instance of the class anyway.

Instead of
if(cmap.didcollide(...
that I pointed out earlier it should just be
if(didcollide(...
and the same for all the other references to cmap in that function.

Share this post


Link to post
Share on other sites
Thanks for all your help mrp :-D u helped me greatly now i'm all organized :-D It took me a few to figure out inclusion guards but they are very usefull :-D
Thanks a lot bro,
COry

Share this post


Link to post
Share on other sites
Now i'm trying to make it so that my cMap::display method can be defined in map.cpp instead of main. But my animation boom. is created in main which is used in cmap::display. I tried putting inside animation.h }boom; and now i get weird evil looking linker errors.
here they are:

--------------------Configuration: New - Win32 Debug--------------------
Compiling...
Map.cpp
main.cpp
Linking...
Map.obj : error LNK2005: "class cAnimate boom" (?boom@@3VcAnimate@@A) already defined in main.obj
Animate.obj : error LNK2005: "class cAnimate boom" (?boom@@3VcAnimate@@A) already defined in main.obj
Map.obj : error LNK2005: "class cAnimate boom" (?boom@@3VcAnimate@@A) already defined in main.obj
Animate.obj : error LNK2005: "class cAnimate boom" (?boom@@3VcAnimate@@A) already defined in main.obj
Debug/New.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

New.exe - 5 error(s), 0 warning(s)


i'm scared lol
-Cory

Share this post


Link to post
Share on other sites
Where are your include guards for your header files? Remember what happens when you use header files that declare classes/structs/variables more than once and do not have include guards [wink]

Share this post


Link to post
Share on other sites
I have inclusion guards on all my headers now so thats not the problem.
I need to use the instance of cAnimate boom in main and in my cMap class. Is there a way to do this?
-Cory

Share this post


Link to post
Share on other sites
Just a couple of things I found to note (not specific to your current problem)
But, your variable naming seems pretty good (easy to follow without having to lookup what the method of member is doing)

But, well... Comments. The program is small now, so it's easy to follow and keep it all in your head, but when it gets ten times this size, and you put it down for a month or two to work on something else (not saying you will, but if you do) then you're -really- going to welcome some comments explaining what things are and why you did them a certain way. Yes, you can probably read through the code and figure it out, but a few well placed comments could save you a lot of time in that figuring process.

As for your current problem, without seeing where you changed the code, I'd say either you missed a header guard, or you forgot to take the definition out of Main.cpp when you added it to map.cpp

Share this post


Link to post
Share on other sites
Ah sorry, just caught your last post. Your sharing a global instance of the class acrossed multiple files? I think what your looking for is an "extern" reference.

Basically, use cAnimate boom in main.cpp like normal, then in the header files for map, or animate declare:

extern class cAnimate boom;

And that should allow you to use the single reference acrossed those files.

(I think I have that syntax correct, its been a month or two since I touched C++)

Share this post


Link to post
Share on other sites
i use boom.addFrame in main.
also i use boom.setx,y,activate in map.
How do i make it so i can use it in botH?
i have all the inclusion guards here i'll show you
player.h

#include<string>
#include<windows.h>
#include<sstream>
#ifndef PLAYER_H
#define PLAYER_H
using namespace std;
class cPlayer{
private:
int x;
int y;
int life;
string name;
HBITMAP visual;
public:
int getX();
int getY();
HBITMAP getVisual();
string getName();
void setX(int newx);
void setY(int newy);
void setVisual(HBITMAP newbitmap);
cPlayer();
cPlayer(int startx,int starty,string newname);
cPlayer(HBITMAP hbitmap);
cPlayer(HBITMAP hbitmap,int startx,int starty);
void display(HWND hwnd);
};
#endif


animate.h

#include<vector>
#include<windows.h>
using namespace std;
#ifndef ANIMATE_H
#define ANIMATE_H
class cAnimate{
private:
vector <HBITMAP>animation;
bool active;
int currentframe;
int frametime;
int lastupdate;
int x;
int y;
vector <int>width;
vector <int>height;
public:
cAnimate();
int getLastUpdate();
int getFrameTime();
void addFrame(HBITMAP newframe,int newwidth,int newheight);
void removeFrame(int index);
void activate();
bool isActive();
int getCurrentFrame();
void setX(int newx);
void setY(int newy);
int getX();
int getY();
int getWidth(int index);
int getHeight(int index);
void display(HWND hwnd);
}boom;
#endif


map.h

#include<vector>
#include<windows.h>
#include"player.h"
#include"animate.h"
#ifndef MAP_H
#define MAP_H
using namespace std;
class cMap{
private:
int width;
int height;
int mousex;
int mousey;
vector<cPlayer> players;
int currentplayer;
public:
int getwidth();
int getheight();
cMap();
cMap(int newwidth,int newheight);
void setMouse(int x, int y);
int getMouseX();
int getMouseY();
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);
};
#endif



i did them right didnt i?
-Cory

Share this post


Link to post
Share on other sites
My understanding of header guards says that probably wont work correctly. Header guards only protect against data definitions (right guys?) But Data declarations will still get you linker errors. So,

animate.h

void display(HWND hwnd);
}boom;
#endif




Your boom there will probably cause some problems.

instead, do this.
animate.h

void display(HWND hwnd);
};

extern class cAnimate boom;
#endif



main.cpp

#include"player.h"
using namespace std;
cAnimate boom;

Share this post


Link to post
Share on other sites
Heh, thanks ;)

I just remember that header guards and extern references were one of those things that was difficult to wrap my mind around at first. Now that I've been using them for years, its almost like second nature.

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