Page 1 of 1
Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 4:14 pm
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:
- I scroll over a specific amount, I will choose 65 (pixels).
- I draw some tiles.
- 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
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 4:23 pm
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.
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 5:11 pm
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;
}
}
}
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 5:36 pm
by X Abstract X
Can we see the code where you actually lay new tiles?
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 5:44 pm
by ParticleGames
Sure, I just added it into the above post.
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 6:16 pm
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
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 7:02 pm
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.
Re: Help on the most ridiculous map editor bug ever?
Posted: Thu Jun 16, 2011 9:57 pm
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
, it should work now.
Re: Help on the most ridiculous map editor bug ever?
Posted: Mon Jun 20, 2011 8:48 pm
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
, 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