Results 1 to 3 of 3

Thread: Game cycle time

  1. #1
    Lesser Knight
    Join Date
    Sep 2010
    Posts
    41

    Default Game cycle time

    Hello, I wonder that examples consist with code like:
    Code:
    sleep(10);
    I have a few questions about this:
    1) How to get real elapsed time?
    Code:
    mElapsed = CL_System::get_time() - mStartTime;
    returns some crazy data
    When I output the mElapsed, the number is like: 0.000000 and it makes bigger each render frame like: 250.0000, 600.0000. It comes to the 2000.0000 and then became a zero again.
    2) How to organize the cycle without sleep?
    3) Can you explain the argument of flip function from CL_Window. When the FPS is like monitor update rate. What value is the best?
    How not to "freeze" the fps but make game working in the same speed in different PCs?

  2. #2

    Default

    Disclaimer: I am very new to clanlib, but I think I can offer some advice, in a general context.

    1) Are you resetting mStartTime at the start of every cycle? (Based on what you said I doubt this is the case)

    2) I believe you may still want a sleep call, but you will want it hidden inside a function that calculates how long it should go to sleep. So if you want to cap the upper frame rate at 60 fps, you can calculate how long it just took you to do the current cycle. And if you want 60 fps you can then calculate how long you need to sleep for before running another cycle, and assuming that the next cycle takes approximately the same amount of time. (Aim a tad low on this calculation, it's better then high). And if your aiming to pump out as many frames as possible and don't care about hammering the system then you don't need to sleep at all Although a stron cautionary about that, you will have inconsistent results with all logic, and gameplay, and depending upon what else is happening on your computer this will result in very inconsistent results. Also with a straight sleep of just a value and without a time based events as I describe below you will still have inconsistent results. Especially across multiple computers.

    3) I can't offer any sound input here, and I'd have to go look it up and experiment.

    On the topic of not freezing and running at the same speeds, it is usually better to run all your logic and graphical stuff based on a computed time since the last time it ran. This will allow you to skip steps in your animation if they are no longer accurate, and process effects at the correct speed.

  3. #3
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Wickedshell is right.

    I will try to explain in my own works, in a hope that someone will add it to ClanLib's Wiki :)

    Refresh Rate

    The refresh rate (sometimes known as vertical sync) is the number of times in a second that display hardware draws the data.

    Historicially in Americia the refresh rate for TV's was 60hz. In Europe it was 50hz. This made is easy for game to judge timing.

    However, PC monitors and modern televisions have a varied refresh rate. Usually between 60hz and 80hz, but refresh rates of 120hz are now available.

    CL_Display::flip()

    You only want to draw to the screen after you have finished drawing, else a partial drawn image is shown (known as screen tearing)

    The display hardware contains 2 screens in memory. One that is being written to and one that is being displayed. For reference, this is known by OpenGL as the GL_DRAW_FRAMEBUFFER and CL_READ_FRAMEBUFFER.

    To swap these screens you need to call flip()

    This is known as "double buffering" or "page switching"

    It is a waste to draw, 2 screens during 1 display refresh. Ideally you should wait after the first screen swap until the next display refresh.

    Fortunately, OpenGL (and DirectX) helps you do this. (Many years ago, you had to wait for the vertical blanking interrupt instead)

    There is an integer parameter used in flip()
    • -1 (Default). The screen is drawn using the display card default setting, this may be "do not wait for the next display refresh" or "wait for the next display refresh".
    • 0 .The screen is drawn without waiting for the next display refresh
    • 1 . The screen is drawn after the next display refresh
    • n . The screen is drawn after the nth display refresh


    You must not assume that the application will wait for the next display refresh even if you asked for it. Some display drivers contain an option for the user to override the application setting. With NVidia it is the "Vertical Sync" option.

    If you want to test your application speed, use flip(0). For normal use, use flip(1)

    Do not use flip() to control the game cycle time

    CL_DisplayWindowDescription

    This contains: set_refresh_rate()

    It is not used by the OpenGL target. It used to in the past, but it had to be removed because it caused a number of problems.

    It is used by the SWRender target, to make it easier for the programmer to switch between ClanLib display targets. It calls CL_System::sleep() to give the application the appearance that it is being synchronized to the display refresh. If it is not defined, it defaults to 60hz.

    To recap, it is only used on clanSWRender when used with flip(1)

    Also this class contains: set_swap_interval()

    This controls the initial flip() swap interval setting, changing the default state of flip()

    I.E. It is only used when you call flip(-1) or flip().

    This function is useful for controlling the display refresh when using clanGUI

    CL_System::sleep()

    This sleeps for a number of milliseconds, reducing cpu usage of your application.

    Internally, this function causes a thread to relinquish the remainder of its time slice.
    The system clock "ticks" at a constant rate. If the time is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If time is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on

    If you specify 0 milliseconds, the thread will relinquish the remainder of its time slice, thus it will not return immediately.

    Note, on the windows platform, you can adjust the accuracy of sleep() using timeBeginPeriod - http://msdn.microsoft.com/en-us/libr...24(VS.85).aspx

    ClanLib currently does not do this.

    CL_KeepAlive::process

    This processes the operating system messages (keyboard, mouse etc)

    The parameter to this function is the time out (in milliseconds)

    For games, you always want to keep this at 0.

    If you set it to -1 (Wait unless message is found), your game main loop will only operate when the mouse is moved (an interesting but useless effect).

    Main Loop Timing

    The ideal code will be:

    Code:
    	unsigned int last_time = CL_System::get_time();
    
    	while (!quit)
    	{
    		unsigned int current_time = CL_System::get_time();
    		int time_difference = current_time - last_time;
    		if (time_difference > 1000)
    			time_difference = 1000;		// Limit the time difference, if the application was paused (eg, moving the window on WIN32)
    		float time_delta_ms = static_cast<float> (time_difference);
    		last_time = current_time;
    
    		*** GAME CODE HERE 
    		gc.clear(CL_Colorf(0.0f,0.0f,0.2f));
    
    		sprite_position += speed * time_delta_ms;
    		*** END CODE ENDS
    
    		window.flip(1);
    
    		CL_KeepAlive::process(0);
    
    		current_time = CL_System::get_time();
    		const int main_loop_rate = 10;	// 10 ms (100 hz)
    		int time_to_sleep_for = main_loop_rate - (current_time - last_time);
    		if (time_to_sleep_for > 0)
    			CL_System::sleep(time_to_sleep_for);
    
    	}

Similar Threads

  1. dinky demensions - the end of time.
    By feathers in forum Dink Smallwood HD
    Replies: 1
    Last Post: 05-27-2007, 05:22 PM
  2. Long time
    By in forum RTsoft Tavern
    Replies: 0
    Last Post: 11-21-2005, 06:10 AM
  3. How much time for licence
    By in forum RTsoft Tavern
    Replies: 1
    Last Post: 06-03-2004, 11:08 PM
  4. Server slows down over time
    By File Sponge in forum Funeral Quest
    Replies: 4
    Last Post: 09-04-2003, 01:18 PM
  5. Server Time
    By in forum Funeral Quest
    Replies: 2
    Last Post: 04-02-2003, 12:07 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •