Controlling Game Loop

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
So I'm back Smile. And I'm trying to create a game loop in SDL. I'm trying to used a fixed step to control my loop along with vsync, but it doesn't seem to be working. Anyone have any ideas for me? Thanks.

Code:
void Game::gameLoop()
{
    SDL_Event event;
    int lastUpdateTickCount = SDL_GetTicks();
    int lastTickCount = SDL_GetTicks();
    int tickCount = SDL_GetTicks();
    
    while (!quit)
    {
        while (tickCount <= lastTickCount + (1000 / parameters.desiredFPS))
        {
            while (SDL_PollEvent(&event))
            {
                switch (event.type)
                {
                    case SDL_QUIT:
                        quit = true;
                    case SDL_KEYDOWN:
                    case SDL_KEYUP:
                        handleKeyboard(event.key);
                        break;
                    case SDL_MOUSEMOTION:
                        handleMouseMotion(event.motion);
                        break;
                    case SDL_MOUSEBUTTONDOWN:
                    case SDL_MOUSEBUTTONUP:
                        handleMouseButton(event.button);
                        break;
                    case SDL_USEREVENT:
                        HandleUserEvent(event.user);
                        break;
                }
            }
            
            int elapsedTicks = tickCount - lastUpdateTickCount;
            float elapsedTime = (float)elapsedTicks / 1000.0f;
            Update(elapsedTime);
            
            lastUpdateTickCount = tickCount;
            tickCount = SDL_GetTicks();
        }
        
        beginDraw();
        Draw();
        endDraw();
    
        lastTickCount = tickCount;
    }
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Thanks. That seemed to do just the trick.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone game loop agreendev 2 4,386 Jul 27, 2010 11:32 AM
Last Post: AnotherJake
  Trouble with the canonical game loop TomorrowPlusX 27 11,377 Aug 12, 2009 04:22 AM
Last Post: TomorrowPlusX
  Proper game update loop in Cocoa? LoneIgadzra 13 6,818 May 30, 2008 05:49 AM
Last Post: ThemsAllTook
  Good Game Loop with Physics Units blobbo 6 4,913 Oct 14, 2005 02:59 AM
Last Post: unknown
  Game Loop in Cocoa robmcq 9 6,431 Sep 27, 2005 10:49 AM
Last Post: akb825