Jump to content
  • Advertisement
Sign in to follow this  
Winterbreeze

C++ Need help recovering info from class

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heya. I will first explain what I am trying to do, then right what I have. I am trying to build a map class which will have inside it a two dimensional array with IDs written on every (x,y). I have a tile class with the information about the specific tile. I have a function to run for i 0->100 { for i2 0-> } create new tile with a new id and insert its id number to the correct map[x,y]. Then I have a function that is supposed to load the map, done by finding where the player is, start from x-20,y-16 (loading 40x32 times 32x32 pixels tiles), asking the map what tile id is in there, then asking that tile ID whats its Art name to load the png file. I think the problem is in the last part, how to refer to each tile and finding out their art with the ID. Here are the specific functions and codes:
void MapInit(void) {
	int newid;
	for (int i = 0; i < 100; i++ ) {
		for (int i2 = 0; i2 < 100; i2++ ) {
			newid =LastTileID+1;
    tile newid (i,i2);
		}
	}
}

int LastTileID;

// map array

class Map {
	int coords[100][100];

public:
	void setcoords(int x, int y, int id) {
		
	coords[x][y]=id;
	}
	int showid(int x, int y) { return coords[x][y]; }	
private:

protected:

} map;

class Tile {

public:
	LPCTSTR showart() {return (TileArt);}
	Tile ();
	Tile (int, int);
	int x;
	int y;
	LPCTSTR TileArt;
	int id;
private:
protected:

} tile ;


Tile::Tile () {
	id = LastTileID + 1; 
	LastTileID +=1;
	x=NULL;
	y=NULL;
	TileArt = "GrassTile.png";
}

Tile::Tile (int coordx, int coordy) {
	id = LastTileID + 1; 
	LastTileID +=1;
	x=coordx;
	y=coordy;
	map.setcoords (coordx,coordy, id);
	TileArt = "GrassTile.png";

}

void DrawMap()
{
    RECT Part;

	float x;
	float y;
	LPCTSTR Art;
	int idx;
	int idy;
for (float i = 0; i < 40; i++ ) {
		for (float i2 = 0; i2 < 32; i2++ ){
			idx = spritex-20+i;
			idy = spritex-16+i2;
int tileid=map.showid(idx,idy);		
x = i * 32;
y = i2 * 32 +16;
Art=tileid.showart();
LoadTile(&TileName, Art);
    SetRect(&Part, 0, 0, 32, 32);
    DrawTile(TileName, Part, x, y, 255);
		}
}
	SetRect(&Part, 0, 0, 1280, 32); 
LoadGump(&GumpName, "UpperBorder1.png", 1280, 32);
DrawGump(GumpName, Part, 0, 0, 255);
    return;


}
[Edited by - Emmanuel Deloget on July 18, 2008 4:22:10 AM]

Share this post


Link to post
Share on other sites
Advertisement
First, your code is fugly. Put it between [source][/source] tags to improve formatting.

Second, why do you need an ID system? Why not simply store tiles themselves in the map ? For instance:

const int map_size = 100;
class map
{
tile tiles[map_size][map_size];
public:

typedef std::pair<int,int> coord;

tile &operator()(int i, int j) { return tiles[j]; }
const tile &operator()(int i, int j) const { return tiles[j]; }

tile &operator()(coord c) { return tiles[c.first][c.second]; }
const tile &operator()(coord c) const { return tiles[c.first][c.second]; }
};

Share this post


Link to post
Share on other sites
Because the tiles behave sorta like items and they have properties of their on.
Such as blocking, effects when you step on them, etc etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winterbreeze
Because the tiles behave sorta like items and they have properties of their on.
Such as blocking, effects when you step on them, etc etc.


None of this is an argument against storing the tiles in a map (in fact, it would be an argument for storing the tiles in the map).

Share this post


Link to post
Share on other sites
Atm my tiles each have properties such as tileart, tileid, tile functions, tiletype, coordinates, and they are set as classes so I can simply create the same tile, again, and just change its coordinates, also being able to edit it later in case of need, but without editing the rest of the tiles of its type.
If I simply put it on a structure without being able to set certain functions in them, it will limit what I need a lot.

Share this post


Link to post
Share on other sites
Quote:
Original post by Winterbreeze
Atm my tiles each have properties such as tileart, tileid, tile functions, tiletype, coordinates, and they are set as classes so I can simply create the same tile, again, and just change its coordinates, also being able to edit it later in case of need, but without editing the rest of the tiles of its type.
If I simply put it on a structure without being able to set certain functions in them, it will limit what I need a lot.


Either I'm understanding you wrong, or you have some problems with C++ classes and structures.

First, the only difference between classes and structures, in C++, is that structures default to public access and public inheritance, whereas classes default to private access and private inheritance. This is the only difference. In particular, you can turn a class into a structure and vice versa merely by overriding the default access specification with your own.

Second, there is no practical difference between storing your class instances "somewhere" and accessing them through an ID stored in a map, versus storing your class instances directly in the map. The only case where this would happen would be if you need polymorphic behaviour for your tiles, which does not seem to be the case here. On the other hand, storing class instances directly in the map improves spatial locality (and thus improves performance) and also makes your code simpler by not requiring a dereference to access your tiles.

So, I stand by my point that storing the tiles in the map is the best technical solution here.

Share this post


Link to post
Share on other sites
ToohrVyk knows what he's talking about here. Look; if you don't believe him, just try it. Right now, you're writing completely nonsensical stuff like


tile newid (i,i2);


This looks like you are trying to say "make an object of type 'tile', which is named 'newid', with these parameters passed to the constructor". But in reality, 'tile' is an instance of the class Tile, and 'newid' is already a variable of type 'int', so the statement makes no sense. Further, there is no attempt anywhere to do anything with the resulting object.

Your description of the problem already suggests fuzzy thinking:

Quote:
Then I have a function that is supposed to load the map, done by finding where the player is, start from x-20,y-16 (loading 40x32 times 32x32 pixels tiles), asking the map what tile id is in there, then asking that tile ID whats its Art name to load the png file.


Here's the thing: the tile ID, in your system, is a number. You can't ask a number anything. You ask things from objects. So you store tiles in the map, ask the object what tile is at a given location, and ask the tile what its art file name is.

You should also make sure you understand that classes - and structures - define a data type. There is an important distinction between the class Tile and the object tile. The syntax lets you instantiate the class at the same point you define it, but this is almost always not what you actually want to do.

Also, things like 'x = NULL;' are semantically wrong. NULL is supposed to be a pointer, not an integer. Of course, modern C++ programmers often just write 0 for pointers anyway. NULL is just old-fashioned :)

Please take a moment to learn, by example, a few things about modern C++:


class Tile {
Tile(const std::string& TileArt = "GrassTile.png"): TileArt(TileArt);
std::string TileArt;
public:
const std::string& art() { return TileArt; }
};

class Map {
Tile data[100][100];
public:
void set(int x, int y, const Tile& tile) { data[x][y] = tile; }
const Tile& get(int x, int y) { return data[x][y]; }
} map;

void DrawMap() {
RECT Part;
for (int i = 0; i < 40; ++i) {
for (int i2 = 0; i2 < 32; ++i2) {
// Are you sure you don't mean spritey the second time?
// And where do these other variables come from?
// Or, for that matter, the numbers?
Tile& t = map.get(spritex - 20 + i, spritex - 16 + i2);
float x = i * 32;
float y = i2 * 32 + 16;
// What does LoadTile *do*?
LoadTile(&TileName, t.art());
SetRect(&Part, 0, 0, 32, 32);
DrawTile(TileName, Part, x, y, 255);
}
}
SetRect(&Part, 0, 0, 1280, 32);
// What's a Gump?
LoadGump(&GumpName, "UpperBorder1.png", 1280, 32);
DrawGump(GumpName, Part, 0, 0, 255);
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!