Sign in to follow this  
CirdanValen

Something funky with my map loop?

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this