problem with scrolling tile based game...

Started by
2 comments, last by Ilankt 20 years, 8 months ago
this is how i draw the map:

 for (int y=yoffset/TileSize;y<((yoffset+ScreenHeight)/TileSize)+1;y++)
   {
      for(int x=xoffset/TileSize;x<((xoffset+ScreenWidth)/TileSize)+1;x++)
      {
            Tiles[x][y].PosX=x;
            Tiles[x][y].PosY=y;
            DrawTile(Tiles[x][y].srcX,Tiles[x][y].srcY,x*TileSize-xoffset,y*TileSize-yoffset,TileSize,g_pTileTexture,D3DCOLOR_RGBA(255,255,255,255),Tiles[x][y].Rotation);
      }
   }
what is important in all this code, it''s the -xoffset and -yoffset. now, for scrolling, i''m increasind/reducing the offset. now, i have a problem deretminating, when i reached the end of the map... for scrolling to the right i do this: if (xoffset<(MapWidth*TileSize)-ScreenWidth) xoffset++; but this is not working (the game crash on me...) and i dont know why... Thanks in advance.
How appropriate, you fight like a cow!
Advertisement
Ilankt - If you wish your game world to scroll beyond its edge by wrapping around to the beginning, one thing I notice is -- I see no means to do so in your code, which would definately cause it to crash (due to out of bounds array access).

There may be more involved, but for starters I suggest adding a macro or inline function to do something like the following:

inline int WrapX (int xPos)
{
return (xPos % xSize);
}

...where xPos is the x index of your Tiles[x][y] array and xSize is the size of the x component of the tile array. Repeat for yPos and ySize.

Now you can address Tiles by Tiles[WrapX(x)][WrapY(y)] instead of Tiles[x][y] and not worry about accessing out of array bounds.

Good luck.
THANKS!!!
this helps alot!
even that my problem is solved (by you ), I really wanted to know what is the problem with the code...
but, never mind now, at least i dont have any problems now (yet).
How appropriate, you fight like a cow!
Ilankt -- Great...

To get it working for negative offsets, try modifying the macro/inline function (Boldface indicates the change):

inline int WrapX (int xPos)
{
return ((xSize + xPos) % xSize);
}

Alternatively, if you want to keep xoffset and yoffset from going negative in the first place, try applying this logic to them instead of the x and y indeces. Change this:

if (xoffset<(MapWidth*TileSize)-ScreenWidth)
xoffset++;

To this:

if (xoffset<(MapWidth*TileSize)-ScreenWidth)
xoffset = ((xoffset_max + xoffset + 1) % xoffset_max);

where xoffset_max is the largest xoffset value + 1.

You actually only need to add the max when scrolling left or up (to prevent negative values), btw, but the mod (%) is always needed.

Peace.

EDIT: Fixed a couple of typos.

[edited by - BrianMJC on August 3, 2003 5:24:06 PM]

This topic is closed to new replies.

Advertisement