• Advertisement
Sign in to follow this  

Something funky with my map loop?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Those loops look OK.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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);

}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement