Thread: tile map bounds checking

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001

    tile map bounds checking

    If any one can help me with the bounds checking for my tilemap I would greatly appreciate it.

    My tilemap engine works perfectly untill it tries to scroll to the very horizontal or vertical limit. If I scroll by the size of the tiles used then it gets to the xMaxPos (or yMaxPos) position without a hitch. I am therefore assuming that these two values are being calculated correctly.

    The problem occurs when I try to scroll to the boundary by a unit less than the tile size or try to scroll away from the boundary when it's at the max position. The progam crashes very ungracefully.

    I searched for threads on similar problems and an answer that comes up occasonally is to just add an extra row and column of tiles. I can't figure this out because surely as I approach the max coordinates or leave the max cordinates it disects the last visible tile, which can be displayed with no problems...

    void TileMapLayer::draw(BITMAP *dest) {
       int xSrc, ySrc;
       int xDest, yDest;
       int topTile = xPos / xTileSize;
       int leftTile = yPos / yTileSize;
       int topTileOffset = xPos % xTileSize;
       int leftTileOffset = yPos % yTileSize;
       int bottomTile  = (int)((xPos + xScreenSize) / (float)xTileSize + 0.5) + ((topTileOffset == 0) ? 0 : 1);
       int rightTile = (int)((yPos + yScreenSize) / (float)yTileSize + 0.5) + ((leftTileOffset == 0) ? 0 : 1);
       for(xSrc = topTile; xSrc < bottomTile; xSrc++ ) {
          for(ySrc = leftTile; ySrc < rightTile; ySrc++ ) {
             xDest = (xSrc - topTile) * xTileSize - topTileOffset;
             yDest = (ySrc - leftTile) * yTileSize - leftTileOffset;
             blit(cell[xSrc][ySrc].image, dest, 0, 0, xDest, yDest, xTileSize, yTileSize);
    bool TileMapLayer::scrollUp(int yOffset) {
       yPos -= yOffset;
       if (yPos < 0) {
          yPos = 0;
          return false;
       return true;
    bool TileMapLayer::scrollDown(int yOffset) {
       yPos += yOffset;
       if (yPos > yMaxPos - yTileSize) {
          yPos = yMaxPos - yTileSize;
          return false;
       return true;
    bool TileMapLayer::scrollLeft(int xOffset) {
       xPos -= xOffset;
       if (xPos < 0) {
          xPos = 0;
          return false;
       return true;	
    bool TileMapLayer::scrollRight(int xOffset) {
       xPos += xOffset;
       if (xPos > xMaxPos - xTileSize) {
          xPos = xMaxPos - xTileSize;
          return false;
       return true;	

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Have you tried walking this through a debugger and monitoring your values and pointers?

    this is a classic range check problem. Because you're not aligned, your exceeding a boundary you're just not checking for because you've assumed it's taken care of.

    1) Flow chart your program and test values
    2) Walk it with a debugger and see specifically where it breaks.

    This is how you solve this problem and learn by it.

    Someone else won't always be around to help you.

    Good luck.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Need testers for editor
    By VirtualAce in forum Game Programming
    Replies: 43
    Last Post: 07-10-2006, 08:00 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Tile map loading/saving
    By sand_man in forum Game Programming
    Replies: 16
    Last Post: 04-23-2005, 09:38 PM