Something funky with my map loop?

Started by
5 comments, last by mattd 14 years, 3 months ago
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++;
		}
		
	}
Advertisement
Need more code; in particular, where verts[] and its structure is declared, and where verts[] is iterated over.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Those loops look OK.

Are you increasing the size of verts (to width * height * 4) when you increase the map dimensions too?
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);}
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.
Aha! Okay thanks. Guess I need some sleep >.<
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);}

This topic is closed to new replies.

Advertisement