I been working with scrolling this week but I recently encounter a problem that I don't understand why it's happening.
In a 2D scroller I want to scroll a tilemap pixel per pixel which gives a smooth scrolling in the game, but I encounter a weird problem, when the movement of the map corresponds with the exact size of the tile, for example, the map it's scrolling 32 pixels to the right, the map seems to jump to the next tile and not the one that corresponds correctly with the screen.
This is the code for drawing the map:
bool Tilemap::drawTilemap(GLfloat sizeTile, int indexTileset)
{
offsetX++;
if (offsetX >= 320.f) offsetX = 320.f;
GLfloat widthTilesetImage = tilesetList.at(indexTileset).getWidthImage();
GLfloat heightTilesetImage = tilesetList.at(indexTileset).getHeightImage();
int widthMap = 40 + offsetX/32;
int heigthMap = 23;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glBindTexture( GL_TEXTURE_2D, tilesetList.at(indexTileset).getTexture() );
for (int i = 0; i < heigthMap; i++)
{
int startX = offsetX/32;
for (int j = 0; j < 40; j++)
{
int frameIndex = layerMap[i][startX];
if (frameIndex == 0)
{
continue;
}
frameIndex -= 1;
const GLfloat tileX = 0.0f + ( (sizeTile)*j);
const GLfloat tileY = 0.0f + (sizeTile*i);
const GLfloat verts[] = {
tileX, tileY,
tileX + sizeTile, tileY,
tileX + sizeTile, tileY + sizeTile,
tileX, tileY + sizeTile
};
const GLfloat textureWidth = (sizeTile - offsetX/widthTilesetImage) / (GLfloat)widthTilesetImage;
const GLfloat textureHeight = sizeTile / (GLfloat)heightTilesetImage;
const int numFramePerRow = (GLfloat)widthTilesetImage / sizeTile;
const GLfloat textureX = ((frameIndex % numFramePerRow) * textureWidth)+offsetX/widthTilesetImage;
const GLfloat textureY = (frameIndex / numFramePerRow ) * textureHeight;
const GLfloat texVerts[] = {
textureX, textureY,
textureX + textureWidth, textureY,
textureX + textureWidth, textureY + textureHeight,
textureX, textureY + textureHeight
};
glVertexPointer(2, GL_FLOAT, 0, verts);
glTexCoordPointer(2, GL_FLOAT, 0, texVerts);
glDrawArrays(GL_QUADS, 0, 4);
startX++;
}
}
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
return true;
}
The variable offsetX it's the one that helps me move one tile at a time the map. The variable startX indicates when it's no longer drawing one tile, so in this one start in the next column of the matrix of tiles.
So, what could be wrong in my code?
Thanks for the help.