Hello everyone!
I am attempting to implement the deWitters game loop (The one where the game rate is consistent but the FPS is independent), but I have no luck!
For one reason or a other I have a tiny stutter in my image as it moves across the screen even if vsync is enable.
If you guys could take a look at it and tell me what Im doing wrong that would be great.
The code below is in C++ but still is openGL based!
Thanks!
//Set up the ticks and such
double ticksPerSec = 25.0;
double skipTicks = 1000.0 / ticksPerSec;
double maxFrameSkip = 10;
//SDL_GetTicks returns the number of ticks since the init of the game window in milliseconds
//Set the clock and the loop count
Uint32 gameClock = SDL_GetTicks();
int loops = 0;
while(gameRunning)
{
//Poll for events such as window close
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
gameRunning = false;
}
//reset the loopcount
loops = 0;
while(SDL_GetTicks() > gameClock && loops < maxFrameSkip)
{
player.prevX = player.x;
player.prevY = player.y;
player.x += 1;
gameClock += skipTicks;
loops++;
}
double alpha = (double)(SDL_GetTicks() + skipTicks - gameClock) / skipTicks;
player.viewX = player.x + alpha * (player.x - player.prevX);
player.viewY = player.y;
drawGame();
SDL_GL_SwapBuffers();
}
The draw function
//Open GL code to render the things that need to be drawn
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Bind the texture to the quad to draw
glBindTexture(GL_TEXTURE_2D, playerImage);
//Begin the simple render
//All of the player values (player.x, player.y, player.viewX, etc) are in double
//player.width / player.height is the size in pixels of the image (in this case 32 x 32 image)
glPushMatrix();
glBegin(GL_QUADS);
//Upper left
glTexCoord2d(0, 0);
glVertex2d(player.viewX, player.viewY);
//Upper right
glTexCoord2d(1, 0);
glVertex2d((player.viewX + player.width), player.viewY);
//Bottom right
glTexCoord2d(1, 1);
glVertex2d((player.viewX + player.width), (player.viewY + player.height));
//Bottom left
glTexCoord2d(0, 1);
glVertex2d(player.viewX, (player.viewY + player.height));
glEnd();
glPopMatrix();
EDIT : Posted draw function code