Search code examples
c++tile

C++ 2D Array Map for Tiling Output Inverted


I have created a 2D array that represents a map for my tiles to be placed:

int sMap[12][20] = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
  {1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1},
  {1, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 1, 0, 1, 1, 2, 2, 2, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

Once my tiles have been loaded in, I use this function() to place the tiles:

for (int y = 0; y < 12; y++){ //for (int y = 11; y >= 0; y--){
    for (int x = 0; x < 20; x++){ //for (int x = 19; x >= 0; x--){
        if    (sMap[y][x] == 1)
            glBindTexture( GL_TEXTURE_2D, brick1);
        else if (sMap[y][x] == 2)
            glBindTexture( GL_TEXTURE_2D, brick2);
        else
            glBindTexture( GL_TEXTURE_2D, wall );

        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x),     float(y), 0.0f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x),     float(y + 1), 0.0f);
        glEnd();
    }
}

For some reason, my Map is inverted... (So the bottom line of digits in the map above is the top line of tiles on screen and the second line up on the map is the second line of tiles down on screen).

Its my understanding, you iterate through selecting each digit and assign the correct tile. I've tried changing the x/y's but that rotates the map on screen so its 12 tiles on the x and 20 tiles on the y.

I want my tiles on screen to represent the map above. Any ideas how to fix this? Thanks.


Solution

  • Problem is in the fact that OpenGL actually has the coordinate system that point (0, 0) is in the lower-left part of the viewport. So if you want to represent that matrix in the code the same way as it should draw, you need to calculate y coordinate for drawing as <height of viewport> - (calculated y). How to get that height - it's up to you, probably you defined it earlier in the code. For instance, float(y + 1 + offsety) will become float(HEIGHT - (y + 1 + offsety)).

    EDIT

    Oh, I just realized - you have that loop, so, you can put

    for (int y = 11; y >= 0; y--){

    instead existing start of outer loop and that's it. But, anyway, it's good to know that it is NOT accidentally inverted as you thought - it's just up to coordinate system.