Advertisement Jump to content
Sign in to follow this  

OpenGL Question About Scrolling A Tilemap In C++/SDL/OpenGL

This topic is 2690 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

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)
if (offsetX >= 320.f) offsetX = 320.f;

GLfloat widthTilesetImage =;
GLfloat heightTilesetImage =;

int widthMap = 40 + offsetX/32;
int heigthMap = 23;


glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );

glBindTexture( GL_TEXTURE_2D, );

for (int i = 0; i < heigthMap; i++)
int startX = offsetX/32;

for (int j = 0; j < 40; j++)
int frameIndex = layerMap[startX];

if (frameIndex == 0)

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



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.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!