Loading Maps ... with a pointer ?

Started by
3 comments, last by McGrane 19 years, 4 months ago
Hi all, Im trying to make a DrawMap function that can take in all my maps and Draw which ones i want, like ...

void DrawMap(int* Map)
{
	
	for( int y = 0; y < MAPHEIGHT; y++)
	{
	for( int x = 0; x < MAPWIDTH;  x++)
	{

	switch(&Map[y][x])
	{
	case 0:
	DrawQuad(/*Whatever Pos's And Textures*/);
	break;
	}

	}
	}

}

Im not sure on how to get it to work, or evan if this is the best way to do it? (c++, opengl, dev-cpp, windows) Thanks for anyhelp ;)
Advertisement
This wouldn't be very optimal if you had a very large map. I would suggest inserting the quads into a quad-tree structure, or sectorizing the map in some way. That way you can have very fast visibility and collision culling without having to check against every quad in your map.
But the map shouldnt get too big, frogot to mention,
Im making it that once you go off the first map the 2nd one is displayed ... thats why i wanted a map loader. It goes screen by screen.
Im not too sure on what a quadtree is so im googling it now,
Thanks for the reply ;)
For a simple tile-based system you don't need a quadtree (it's for worlds with quads laid out in an arbitrary way).

This is an OK way to do things, though I'd recommend two changes:

Firstly, you're only every going to be using one map at a time, right? In which case you're probably better off having a global pointer called something like g_pCurrentMap, which the DrawMap function always uses, instead of asking people to pass in a pointer to the map each time. It's simpler if you can just change the map pointer in one place and have everything else just use the new map automatically without having to throw the pointer around.

Secondly, I'm not convinced that indexing works, because Map is an int* instead of an int*[]. If you're going to be throwing pointers around like this, I'd recommend sticking to a 1D array using index math - so Map[y][x] becomes Map[ (y * MAPWIDTH) + x ]. That way there's never any question about how it works and is laid out in memory.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Thanks for the help ;)
I see what you mean and will try that out when i get home.
This post was ment to help me find a good way of doing this,
so i wont be throughing around pointers anymore ;)
Thanks again :P

This topic is closed to new replies.

Advertisement