# Something funky with my map loop?

This topic is 3213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So something funky is happening with my tile map loop. When I increase the number of tiles wide the map is, the height shrinks, and I can't figure out why. Shouldn't it work just like the loop describes? For each row, generate the row of tiles across. For example, if I set the number of y tiles to 24 and x tiles to 20, the map does not grow by that many tiles...it almost stays the same size except for one new row.

int index = 0;
float srcX = 16.0 / 256.0;
float srcY = 0;
float srcW = 16.0 / 256.0;
float srcH = 16.0 / 256.0;
for(int y = 0; y < 14; y++) {
for(int x = 0; x < 18; x++) {
verts[index].x = x * 32;
verts[index].y = y * 32;
verts[index].tx = srcX;
verts[index].ty = srcY;

index++;

verts[index].x = (x * 32) + 32;
verts[index].y = y * 32;
verts[index].tx = srcX + srcW;
verts[index].ty = srcY;

index++;

verts[index].x = (x * 32) + 32;
verts[index].y = (y * 32) + 32;
verts[index].tx = srcX + srcW;
verts[index].ty = srcY + srcH;

index++;

verts[index].x = x * 32;
verts[index].y = (y * 32) + 32;
verts[index].tx = srcX;
verts[index].ty = srcY + srcH;

index++;
}

}


##### Share on other sites
Need more code; in particular, where verts[] and its structure is declared, and where verts[] is iterated over.

##### Share on other sites
Those loops look OK.

Are you increasing the size of verts (to width * height * 4) when you increase the map dimensions too?

##### Share on other sites
yea I set the array to be 2000, should be enough for a 20x20 map.

struct Vertex {	float x, y;	float tx, ty;};void Graphics::drawMapLayer(Texture *texture) {	Vertex *verts = new Vertex[2000];	int index = 0;	float srcX = 16.0 / 256.0;	float srcY = 0;	float srcW = 16.0 / 256.0;	float srcH = 16.0 / 256.0;	for(int y = 0; y < 20; y++) {		for(int x = 0; x < 20; x++) {			verts[index].x = x * 32;			verts[index].y = y * 32;			verts[index].tx = srcX;			verts[index].ty = srcY;						index++;						verts[index].x = (x * 32) + 32;			verts[index].y = y * 32;			verts[index].tx = srcX + srcW;			verts[index].ty = srcY;						index++;						verts[index].x = (x * 32) + 32;			verts[index].y = (y * 32) + 32;			verts[index].tx = srcX + srcW;			verts[index].ty = srcY + srcH;						index++;						verts[index].x = x * 32;			verts[index].y = (y * 32) + 32;			verts[index].tx = srcX;			verts[index].ty = srcY + srcH;						index++;		}			}		glLoadIdentity();		glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vbo);	glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Vertex) * 2000, &verts[0].x, GL_STATIC_DRAW_ARB);	delete verts;		glColor4f(1.0, 1.0, 1.0, 1.0);	glBindTexture(GL_TEXTURE_2D, texture->getTexture());		glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vbo);		glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_TEXTURE_COORD_ARRAY);		glVertexPointer(2, GL_FLOAT, sizeof(Vertex), 0);	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(8));		glDrawArrays(GL_QUADS, 0, 1140);		glDisableClientState(GL_TEXTURE_COORD_ARRAY);	glDisableClientState(GL_VERTEX_ARRAY);		glBindBufferARB(GL_ARRAY_BUFFER,0);}

##### Share on other sites
You need to pass the increased amount of vertices to glDrawArrays. You are currently using a constant value of 1140, it should be width * height * 4.

##### Share on other sites
Aha! Okay thanks. Guess I need some sleep >.<

##### Share on other sites
Just to clarify, unhardcoding things like the tile and map dimensions would be a good idea.

const int mapWidth = 20, mapHeight = 20;const int tileWidth = 32, tileHeight = 32;struct Vertex {	float x, y;	float tx, ty;};void Graphics::drawMapLayer(Texture *texture) {	const int numVertices = mapWidth * mapHeight * 4;	Vertex *verts = new Vertex[numVertices];	int index = 0;	float srcX = 16.0 / 256.0;	float srcY = 0;	float srcW = 16.0 / 256.0;	float srcH = 16.0 / 256.0;	for(int y = 0; y < mapHeight; y++) {		for(int x = 0; x < mapWidth; x++) {			verts[index].x = x       * tileWidth;			verts[index].y = y       * tileHeight;			verts[index].tx = srcX;			verts[index].ty = srcY;						index++;						verts[index].x = (x + 1) * tileWidth;			verts[index].y = y       * tileHeight;			verts[index].tx = srcX + srcW;			verts[index].ty = srcY;						index++;						verts[index].x = (x + 1) * tileWidth;			verts[index].y = (y + 1) * tileHeight;			verts[index].tx = srcX + srcW;			verts[index].ty = srcY + srcH;						index++;						verts[index].x = x       * tileWidth;			verts[index].y = (y + 1) * tileHeight;			verts[index].tx = srcX;			verts[index].ty = srcY + srcH;						index++;		}			}		glLoadIdentity();		glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vbo);	glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Vertex) * numVertices, &verts[0].x, GL_STATIC_DRAW_ARB);	delete verts;		glColor4f(1.0, 1.0, 1.0, 1.0);	glBindTexture(GL_TEXTURE_2D, texture->getTexture());		glBindBufferARB(GL_ARRAY_BUFFER_ARB, _vbo);		glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_TEXTURE_COORD_ARRAY);		glVertexPointer(2, GL_FLOAT, sizeof(Vertex), 0);	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(8));		glDrawArrays(GL_QUADS, 0, numVertices);		glDisableClientState(GL_TEXTURE_COORD_ARRAY);	glDisableClientState(GL_VERTEX_ARRAY);		glBindBufferARB(GL_ARRAY_BUFFER,0);}

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632907
• Total Posts
3009164
• ### Who's Online (See full list)

There are no registered users currently online

×