Good way to handle frames per second?

This is a discussion on Good way to handle frames per second? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing an application that must show frames at a specified speed... I'm doing it like this: Code: Uint32 ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    55

    Good way to handle frames per second?

    Hi, I'm writing an application that must show frames at a specified speed... I'm doing it like this:
    Code:
    	Uint32 offset = SDL_GetTicks();
    
    	for (unsigned int i=0; i<videoFileManager.getFramesNumber(); i++){
    		showNextFrame();
    		while ((SDL_GetTicks() - offset) < (i*1000/videoFileManager.getFPS()) ){} //Waits until it's time to show the next frame
    	}
    Ok. That works great... However, when I try to close my application before all the frames have been shown, it hangs (I get that Windows error message with the "Don't send" button). How could I manage to achieve managing the frames per second, being also able to manually close the application?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If you're a decent windows programmer then I'll assume that you know what this all means:
    Code:
    MSG Msg;
    Uint32 offset = SDL_GetTicks();
    
    for (unsigned int i=0; i<videoFileManager.getFramesNumber(); i++){
         if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE)
         {
              if (Msg.message == WM_QUIT)
                   break;
              TranslateMessage(&Msg);
              DisplatchMessage(&Msg);
         }
         showNextFrame();
         while ((SDL_GetTicks() - offset) < (i*1000/videoFileManager.getFPS()) ){} //Waits until it's time to show the next frame
    }
    Checking for messages, displaying a frame, then syncing you FPS should all be part of any game loop you ever write. Nuff said.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    Great, that works. However, I'd really love not having to depend on windows for this... Any ideas of how to do it with SDL?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get handle by name
    By System_159 in forum Windows Programming
    Replies: 7
    Last Post: 03-16-2009, 08:45 AM
  2. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  3. Invalid Handle Error
    By smarta_982002 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2008, 10:48 AM
  4. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  5. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21