Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question about tiling quads

Started by Debonaire Hero Aug 22, 2000 at 9:00 PM 0 replies 450+ views
Original Post
Debonaire Hero
Debonaire Hero
I asked around on Nehe''s board but couldn''t get an answer that worked. My problem is this: I have so far a 20 * 10 map of 256X256 pixel tiles, although when drawing to screen it only shows 4 * 3 tiles at 1024*768 resolution. In the drawing loop, I use a double-nested loop to first move everything over by how much the player has moved from his original spot, then draw all 200 tiles. looks like this: glTranslate(x,y,z); // moves over the textures by // how much the player has moved for (row = 0; row < 10; row++) { for (col = 0; col < 20; col++) { glBindTexture(GL_TEXTURE_2D, texture[maparray[row][col]) glBegin(GL_QUADS); //draw the tile .... glEnd(GL_QUADS); glTranslate(256.0,0.0,0.0); // move over for the next tile } glTranslate(-5120.0,256.0,0.0);// move down to next row } while this works quite well in a map so small, eventually this will no longer be feasible as the framerate gets shot to hell while rendering thousands of tiles. I''m trying to figure out how to make a small loop that changes the textures as the player moves around. So far, what I''ve got is this: another double nested loop, this time 4 then 5 (not 3 by 4, because some times you''ve got half of a texture on one side and half on another, then you would have 5 textures, although the ends of the screen would only show part of the textures), then have the program pick the texture by looking through the maparray ( a matrix which stores which texture number the tile should use) and looking at the row then adding the player''s y coordinate divided by 256 and the same for the column with the x coordinate instead. I should subtract 2 from each to get the textures right since the player is in the center of the screen and otherwise, it would use textures from the center and never use textures to the left and top of the player, I think. Then I used the absolute value of the x and y distance the player has traveled and subtracted it to zero every time it got up to 256 so they would never travel beyond the textures on the screen. I then translated the distance they had travelled so the textures would move over an appropriate distance, then translated the row * 256, and the column * 256 for the x and y coords respectively. Then I drew the tile, and loaded identity. BUT IT DOESNT WORK!! All I get it a bunch of tiles with the wrong textures drawn, in a seemingly random order, and the textures usually move in the opposite direction the player is moving and bleh. Well, sorry for this rather lengthy post, but I really need help!
Poontardis
Poontardis
I've got just the answer you're looking for:

my code displays 3*3 tiles at any one time out of a total of any number of tiles (limit is max value if GLuint).

How it works is.... um what's the best way of explaining this?

OK.

Make each tile 1x1 OpenGL units in size (it's easier that way). You can always use glScale() if you don't want square tiles (I do).

For your player data, you need to store their position (in tile coords) (e.g. (2.56, 9.79)), and the tile they are currently in (e.g. (2, 9)).

so your rendering loop would look similar to:

        
glLoadIdentity();

glTranslatef(-PlayerData.CurrentPos.x, -PlayerData.CurrentPos.y, 0.0f);

int Tilex1, Tilex2, Tiley1, Tiley2;

for (int row = -1; i < 2; i++)
for (int col = -1; j < 2; j++);
{
Tilex1 = PlayerData.CurrentTile.x + col;
Tilex2 = Tilex1 + 1;
Tiley1 = PlayerData.CurrentTile.y + row;
Tiley2 = Tiley1 + 1;

glBindTexture(GL_TEXTURE_2D, texture[row][col]);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(Tilex1, Tiley1);
glTexCoord2i(1, 0); glVertex2i(Tilex2, Tiley1);
glTexCoord2i(1, 1); glVertex2i(Tilex2, Tiley2);
glTexCoord2i(0, 1); glVertex2i(Tilex1, Tiley2);
glEnd();
}

You'll have to do some range checking to make sure that you don't try and draw a tile that's outside of your world scope, but that's simple enough.

Of course, the above example is for drawing a 3*3 selection of tiles around your current position, you'll just have to change it so that it draws 5*5 or whatever you need to use.

I hope this helps - if you need further explanation of example, or whatever, just ask and I'll see what I can do.

Laters.

Edited by - poontardis on August 23, 2000 5:50:31 AM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.