• Advertisement

Archived

This topic is now archived and is closed to further replies.

Stack overflows...

This topic is 5807 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

I don''t really know whether this qualifies as a "for beginners" question, but I''m so clueless on it it may as well be. Anyway: I''m working on a map editor, and trying to include a fill tool. The function I use is as follows.
  
void Fill(int atx, int aty, int filltile, int clickedtile)
{
	if (map[atx][aty][0] != clickedtile){return;}
	else{
		map[atx][aty][0] = filltile;
		for (int x = atx-1; x < atx+2; x++)
			for (int y = aty-1; y < aty+2; y++)
				if (map[x][y][0] == clickedtile)
					if (x != atx || y != aty)
						Fill(x,y,filltile,clickedtile);
	}
}
  
It is meant to fill in any tile until it reaches something that is different. That is to say, if you click on tile 4, it fills unless it hits a tile that is not tile 4. clickedtile is the number of the tile clicked on, and fill tile is the tile you''re filling the area with. atx and aty are just the location. This code works fine on smaller regions, but how can I stop it from crashing in larger regions? -Arek the Absolute

Share this post


Link to post
Share on other sites
Advertisement
for (int x = atx-1; x < atx+2; x++)
for (int y = aty-1; y < aty+2; y++)

How does it know that aty+2 exists (going outside the bounds of the array)? I''m not understanding where you''re getting -1 and +2 from...

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

Share this post


Link to post
Share on other sites
THANKS!!! =D

I can''t believe I missed that. Here''s the corrected code, and it WORKS!

  
void Fill(int atx, int aty, int filltile, int clickedtile)
{
if (map[atx][aty] != clickedtile){return;}
else if ((atx < 0) || (aty < 0) || (atx > tile_width) || (aty > tile_height)){return;}
else{
map[atx][aty] = filltile;
for (int x = atx-1; x < atx+2; x++)
for (int y = aty-1; y < aty+2; y++)
if (map[x][y] == clickedtile)
if (x != atx || y != aty)
Fill(x,y,filltile,clickedtile);
}
}


Anyway, I''ll explain how it works, as you said you didn''t understand where atx-1 and +2 came from. Thanks to EvilCrap BTW, as this is just a corrected version of his idea.

You send it the location, and it exits if it is not the same tile type you clicked on. Then it checks to see if its within the bounds of the map, and exits if not. If it''s still going, it fills the tile, and then does the two for loops. This checks all the surrounding tiles, thereby spreading out the region of the fill. aty-1 means the tile immediately above the current one, and it continues while it''s less than aty+2, which is to say it checks aty-1, aty, and aty+1. This causes the region of effect to spread out, and "fill" everything necessary. Hope that''s clear enough, but I''m saving the tutorial speech for when I''m done with the whole map editor.

-Arek the Absolute

Share this post


Link to post
Share on other sites
Well.. It mostly works. It does still bug out if the maps are large, and you try to fill the whole thing. If anyone still has suggestions, they''re more then welcome...

-Arek the Absolute

Share this post


Link to post
Share on other sites
your problem is that you are usiong a recursive function (it calls itself, and that time it calls itself, et cetera)... so each time those four parameters are put on the stack, and you are running out of stack space (on larger maps). this is the problem with recursive functions
luckily, most recursive algorithms can be re-coded to not be recursive, and thus avoid the stack overflow problem. i had a problem very similar to yours a while back (i was trying to flood-fill a tile map recursively), but unfortunately i no longer have that code. i think i had the function make a boolean array of which squares were already checked, or something along those lines... sorry if this doesn''t help.



--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites

  • Advertisement