Jump to content
  • Advertisement
Sign in to follow this  
RPR

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

This topic is 2537 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)
{
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[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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!