Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Issues with tilemap system  (Read 2597 times)

0 Members and 1 Guest are viewing this topic.

Noba

  • Newbie
  • *
  • Posts: 4
    • View Profile
Issues with tilemap system
« on: January 29, 2022, 05:50:21 pm »
Hi, so, I'm kinda new to all of this but do have *sort* of a grasp on C++. I know that my issue is potentially a bit general, but unfortunately I am unsure on where else to ask.

I am currently working on my own tilemap system that uses a string that forms a room of arbitrary size (currently set to 16x12 tiles). If I edit the "position" and "texcoords" values manually I can successfully use the vertex array to display a tile in one place with a single sprite. More accurately, this is simply the entire vertex array set to a single tile and overlapping each other, but my point still stands.

When I modify this to use the "iterator", which should form the entire grid of tiles, no tiles are displayed at all. Could anyone help me with this? I'm at a loss on things to try.

Thanks.
Quote
void RoomController::LoadRoom()
{
   //First parse the string from the specific room
   //For now, use the given room
   string room = rooms[0];
   istringstream roomParser(room);
   string tile;

   //Create a blank vertex array
   VertexArray tiles(Quads, 4 * roomXSize * roomYSize);

   while(getline(roomParser, tile, ','))
   {

      //Create an iterator that acts as a base for all calculations following
      int iterator = roomXSize * yIterator + xIterator;

      //Get the current tile
      int currentTile = stoi(tile);

      //Get the position in the tilesheet
      int tileXPos = tilesetCoordinates[currentTile].x;
      int tileYPos = tilesetCoordinates[currentTile].y;

      cout << "Tile:" << iterator << "\n";
      cout << "The X position of the tile sprite is:" << tileXPos << "\n";
      cout << "The Y position of the tile sprite is:" << tileYPos << "\n";

      //Set their positions
      int x1 = 8 * xIterator;
      int y1 = 8 * yIterator;
      int x2 = (8 * xIterator) + 8;
      int y2 = (8 * yIterator) + 8;
      tiles[0 + iterator].position = Vector2f(x1, y1);
      tiles[1 + iterator].position = Vector2f(x2, y1);
      tiles[2 + iterator].position = Vector2f(x2, y2);
      tiles[3 + iterator].position = Vector2f(x1, y2);

      //Set their texture
      x1 = tileXPos;
      y1 = tileYPos;
      x2 = tileXPos + 8;
      y2 = tileYPos + 8;
      tiles[0 + iterator].texCoords = Vector2f(x1, y1);
      tiles[1 + iterator].texCoords = Vector2f(x2, y1);
      tiles[2 + iterator].texCoords = Vector2f(x2, y2);
      tiles[3 + iterator].texCoords = Vector2f(x1, y2);
      
      if (xIterator < 16)
      {
         xIterator += 1;
      }
      else
      {
         xIterator = 0;
         yIterator += 1;
      }

   }

   //sets it to the map
   currentRoom = tiles;

}

Apologies for the quote, I am unsure of how to use code blocks on this forum.
« Last Edit: January 29, 2022, 09:57:18 pm by Noba »

LucasM

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Issues with tilemap system
« Reply #1 on: January 29, 2022, 07:34:34 pm »
Are you initializing xIterator, yIterator to zero?

Noba

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with tilemap system
« Reply #2 on: January 29, 2022, 07:44:08 pm »
Are you initializing xIterator, yIterator to zero?

They're initialised in the header file, yes.

Noba

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with tilemap system
« Reply #3 on: January 30, 2022, 02:29:48 pm »


The above string is normally set to a single line, I did this for visibility, and to show that the LUA is in fact fine.
Little bit of an update on this - It almost works! It didn't occur to me that the quads within the vertex array were actually smaller vertex arrays, so I changed up the position setting code and texture setting code to account for that. However, I now have this. Does anyone know what my problem could be? The map is definitely still 16x12, wondering if the problem is with my string generation code in LUA.

Edit: Definitely not. It's something to do with how I'm setting the quads.

Quote
      //Get a pointer to the current tile's quad
      Vertex* quad = &currentRoom[(xIterator + yIterator * roomXSize) * 4];

      //Set the positions of each of the 4 vertices
      int x1 = 8 * xIterator;
      int y1 = 8 * yIterator;
      int x2 = 8 * (1 + xIterator);
      int y2 = 8 * (1 + yIterator);
      quad[0].position = Vector2f(x1, y1);
      quad[1].position = Vector2f(x2, y1);
      quad[2].position = Vector2f(x2, y2);
      quad[3].position = Vector2f(x1, y2);

      //Set the texture coordinates of each of the 4 vertices
      x1 = tileXPos;
      y1 = tileYPos;
      x2 = tileXPos + 8;
      y2 = tileYPos + 8;
      quad[0].texCoords = Vector2f(x1, y1);
      quad[1].texCoords = Vector2f(x2, y1);
      quad[2].texCoords = Vector2f(x2, y2);
      quad[3].texCoords = Vector2f(x1, y2);
« Last Edit: January 30, 2022, 02:51:22 pm by Noba »

LucasM

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Issues with tilemap system
« Reply #4 on: January 30, 2022, 07:57:54 pm »
I still have no idea.  What I usually find helps is to step through the code with a debugger … you know what the value of the sprite positions and texture positions should be so see if the values line up in the debugger.  Just google using the debugger in your development environment of choice.  That helps me.  You can even see if the flow path of your program is skipping a loop or if statement or not.
Also I find that I write some code… and miraculously it works.  So then it is the fun times of trying to figure out how it works and rewriting the entire thing again.  Not a fast process.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Issues with tilemap system
« Reply #5 on: January 30, 2022, 09:04:08 pm »
In the first code, it looks like the line:
int iterator = roomXSize * yIterator + xIterator;
should be
int iterator = (roomXSize * yIterator + xIterator) * 4;
since iterator is used later as an index of the first vertex of each quad in the vertex array.

Noba

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issues with tilemap system
« Reply #6 on: January 31, 2022, 11:45:17 am »
Hey guys, thanks for the responses. There were a few issues like this, but you're not going to believe what the main culprit was...

I had to change a 16 to a 15 where it resets xIterator and increments yIterator.  ::)

 

anything