Help on the most ridiculous map editor bug ever?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Help on the most ridiculous map editor bug ever?

Post by ParticleGames »

I am attempting (key word there is "attempting") to make a map editor for other games I make in the future. Along the way, I have encountered the most odd bug I have ever seen.

So my program has the ability to scroll left and right with the arrow keys. When I launch my program, I can click and drag to draw the tiles, and everything is quite smooth. But after I scroll the map even a tiny bit, everything gets screwed up. It appears as if when I click to draw the tiles, everything is drawn correctly. Although when I draw, other tiles start to disappear! The tiles disappear in a pattern. The pattern it goes in is like this:
  1. I scroll over a specific amount, I will choose 65 (pixels).
  2. I draw some tiles.
  3. The tiles are correctly draw, although the tile in the tile location exactly 65 pixels left of where I draw disappears.
Sorry if this is kind of confusing, post if you don't understand. I really need some help on this :(
Check out my iPhone app here: http://twilighthop.com
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help on the most ridiculous map editor bug ever?

Post by dandymcgee »

Can we see your tile updating code? I'm guessing your updating the draw coords but overwriting the tile at the old coords.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: Help on the most ridiculous map editor bug ever?

Post by ParticleGames »

Yeah sure. It is kinda messy but here it is:

updating graphics:

Code: Select all

void Sys::update(ImageMan* imageMan, Input input)
{
	SDL_Rect scr;
	scr.x = 0;
	scr.y = 0;
	scr.w = 840;
	scr.h = 680;

	//graphics is everything below
	SDL_FillRect(screen,&scr,SDL_MapRGB(screen->format,0,0,0));

	for(int w = 0; w < mapW; w++)
	{
		for(int h = 0; h < mapH; h++)
		{
			if(map[w][h].declared == false)
			{

			}else if (map[w][h].get_x() <= camera.x + camera.w && map[w][h].get_x() + TILEW >= camera.x && map[w][h].get_y() + TILEH >= 100 && map[w][h].get_y() <= camera.y + camera.h + (TILEH * 3))
			{
				apply_surface(map[w][h].get_x() - camera.x,map[w][h].get_y() - camera.y,map[w][h].get_image(),screen,NULL);
			}
		}
	}

	SDL_FillRect(screen,&banner,SDL_MapRGB(screen->format,255,0,255));
	apply_surface(xBaseT,yBaseT,currentTile->get_image(),screen,NULL);
}
Tile placement:

Code: Select all

void Sys::put_tile(ImageMan* imageMan)
{
	int x=0, y=0;
	x = getCloseX(); //rounds to the lowest multiple of 32.
	y = getCloseY();
	//rounds to lowest multiple of 32 excluding banner

	if(y >= banner.h)
	{
		float tileW, tileH;
		tileW = ((float)x-(float)camera.x) / 32;
		tileH = ((float)y-(float)camera.y-100) / 32;
		tileH = floor(tileH);
		if((int)tileW >= 0 && (int)tileW <= mapW-1 && (int)tileH >= 0 && (int)tileH <= mapH-1)
		{
			map[(int)tileW][(int)tileH].give_image(currentTile->get_image());
			map[(int)tileW][(int)tileH].set_x(x);
			map[(int)tileW][(int)tileH].set_y(y);
			map[(int)tileW][(int)tileH].declared = true;
		}
	}
}
Last edited by ParticleGames on Thu Jun 16, 2011 5:44 pm, edited 2 times in total.
Check out my iPhone app here: http://twilighthop.com
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Help on the most ridiculous map editor bug ever?

Post by X Abstract X »

Can we see the code where you actually lay new tiles?
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: Help on the most ridiculous map editor bug ever?

Post by ParticleGames »

Sure, I just added it into the above post.
Check out my iPhone app here: http://twilighthop.com
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Help on the most ridiculous map editor bug ever?

Post by X Abstract X »

When you are indexing into map[][], it looks like you're using the tiles screen position instead of world position. Also, you don't need to be storing positions in your tiles if your tiles are in a grid. You should be doing something like this when you lay a tile (assuming origin is in the top left and the cameras position is considered the top left of the camera's rectangle):

Code: Select all

int tileX = mouseClick.x + camera.x;
int tileY = mouseClick.y + camera.y;

map[tileX / TILE_SIZE][tileY / TILE_SIZE].give_image(currentTile->get_image());
And when you draw tiles:

Code: Select all

int startW = camera.x / TILE_SIZE;
int startH = camera.y / TILE_SIZE;

for (int w = startW; w < startW + screen.w / TILE_SIZE; ++w)
    for (int h = startH; h < startH + screen.h / TILE_SIZE; ++h)
        apply_surface(w * TILE_SIZE, h * TILE_SIZE, map[w][h].get_image(), NULL);
I've left out some of your stuff but this should get you on the right track as long as I'm not trippin' :P
Last edited by X Abstract X on Thu Jun 16, 2011 10:10 pm, edited 2 times in total.
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: Help on the most ridiculous map editor bug ever?

Post by ParticleGames »

Code: Select all

int Sys::getCloseX()
{
	int x = 0, y = 0;
	SDL_GetMouseState(&x,&y);
	x += camera.x;
	double temp = x / 32;
	temp = floor(temp);
	return (int)(temp * 32);
}

int Sys::getCloseY()
{
	int x = 0, y = 0;
	SDL_GetMouseState(&x,&y);
	y += camera.y;
	double temp = y;
	temp = temp / 32;
	temp = floor(temp);
	return (int)(temp * 32)+4; //+4 compensates for banner
}
These get the coordinates in relation to the whole world. It also rounds down to the nearest 32. (For the gridding)

Your code doesn't really seem to work. Why is it camera.x for the width, but camera.h for the height? And I don't see the problem with my for loop starting at 0 and ending at the finish of my array. One more thing, is TILE_SIZE the map size or the tile width/height? When I tried your method nearly none of the tiles applied, and when they did show up, they were not in the correct spot.
Check out my iPhone app here: http://twilighthop.com
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Help on the most ridiculous map editor bug ever?

Post by X Abstract X »

Yep sorry, should have been camera.y. There is no problem starting the for loop at 0 when rendering, it's just inefficient and easily made more efficient. I changed it because I had to change the rendering loop anyway in the code I posted. In my sample TILE_SIZE is the width/height of a single tile. There was infact a bug in the code I posted, I edited it above :oops: , it should work now.
User avatar
ParticleGames
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 53
Joined: Sat Feb 19, 2011 1:20 pm
Current Project: A map editor for a platformer I plan to develop
Favorite Gaming Platforms: Wii, Gamecube
Programming Language of Choice: C++

Re: Help on the most ridiculous map editor bug ever?

Post by ParticleGames »

X Abstract X wrote:Yep sorry, should have been camera.y. There is no problem starting the for loop at 0 when rendering, it's just inefficient and easily made more efficient. I changed it because I had to change the rendering loop anyway in the code I posted. In my sample TILE_SIZE is the width/height of a single tile. There was infact a bug in the code I posted, I edited it above :oops: , it should work now.
I had to slightly alter your code still, although I am in such an ecstatic state at the moment I can't quite remember if it was from bugs or just for personal needs. If you haven't noticed, I fixed this pathetic bug. Needless to say, the problem I had was I was compensating for the camera offsets in two different spots, therefore resulting in tiles displace by the offset. Thank you for your help though, it made my code far more neat and easier to read :)
Check out my iPhone app here: http://twilighthop.com
Post Reply