Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Game loop and time measurement.

Game loop and time measurement.

Scheduled Pinned Locked Moved C / C++ / MFC
comgame-devquestionlounge
3 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15212768
    wrote on last edited by
    #1

    There is the function `Sys_Milliseconds`:

    int curtime;
    int Sys_Milliseconds (void)
    {
    static int base;
    static qboolean initialized = false;

    	if (!initialized)
    	{	// let base retain 16 bits of effectively random data
    		base = timeGetTime() & 0xffff0000;
    		initialized = true;
    	}
    	curtime = timeGetTime() - base;
    
    	return curtime;
    }
    

    Used in time calculation in the Quake 2 main game loop implementation:

    //...
    oldtime = Sys_Milliseconds();
    //...
    while (1) {
    //...

    		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
    			if (!GetMessage(&msg, NULL, 0, 0))
    				Com\_Quit();
    			sys\_msg\_time = msg.time;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    
    		do {
    			newtime = Sys\_Milliseconds();    			
    			time = newtime - oldtime;
    		} while (time < 1);
    //...
    }
    

    I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:

    //...
    oldtime = timeGetTime();
    //...
    while (1) {
    //...

    		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
    			if (!GetMessage(&msg, NULL, 0, 0))
    				Com\_Quit();
    			sys\_msg\_time = msg.time;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    
    		do {
    			newtime = timeGetTime();    			
    			time = newtime - oldtime;
    		} while (time < 1);
    //...
    }
    
    Mircea NeacsuM C 2 Replies Last reply
    0
    • M Member_15212768

      There is the function `Sys_Milliseconds`:

      int curtime;
      int Sys_Milliseconds (void)
      {
      static int base;
      static qboolean initialized = false;

      	if (!initialized)
      	{	// let base retain 16 bits of effectively random data
      		base = timeGetTime() & 0xffff0000;
      		initialized = true;
      	}
      	curtime = timeGetTime() - base;
      
      	return curtime;
      }
      

      Used in time calculation in the Quake 2 main game loop implementation:

      //...
      oldtime = Sys_Milliseconds();
      //...
      while (1) {
      //...

      		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
      			if (!GetMessage(&msg, NULL, 0, 0))
      				Com\_Quit();
      			sys\_msg\_time = msg.time;
      			TranslateMessage(&msg);
      			DispatchMessage(&msg);
      		}
      
      		do {
      			newtime = Sys\_Milliseconds();    			
      			time = newtime - oldtime;
      		} while (time < 1);
      //...
      }
      

      I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:

      //...
      oldtime = timeGetTime();
      //...
      while (1) {
      //...

      		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
      			if (!GetMessage(&msg, NULL, 0, 0))
      				Com\_Quit();
      			sys\_msg\_time = msg.time;
      			TranslateMessage(&msg);
      			DispatchMessage(&msg);
      		}
      
      		do {
      			newtime = timeGetTime();    			
      			time = newtime - oldtime;
      		} while (time < 1);
      //...
      }
      
      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Member 15212768 wrote:

      What's the point in `base = timeGetTime() & 0xffff0000` line?

      To avoid being too close to rollover point. From "timeGetTime function | Microsoft Docs"[^]:

      Quote:

      The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations.

      Mircea

      1 Reply Last reply
      0
      • M Member_15212768

        There is the function `Sys_Milliseconds`:

        int curtime;
        int Sys_Milliseconds (void)
        {
        static int base;
        static qboolean initialized = false;

        	if (!initialized)
        	{	// let base retain 16 bits of effectively random data
        		base = timeGetTime() & 0xffff0000;
        		initialized = true;
        	}
        	curtime = timeGetTime() - base;
        
        	return curtime;
        }
        

        Used in time calculation in the Quake 2 main game loop implementation:

        //...
        oldtime = Sys_Milliseconds();
        //...
        while (1) {
        //...

        		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
        			if (!GetMessage(&msg, NULL, 0, 0))
        				Com\_Quit();
        			sys\_msg\_time = msg.time;
        			TranslateMessage(&msg);
        			DispatchMessage(&msg);
        		}
        
        		do {
        			newtime = Sys\_Milliseconds();    			
        			time = newtime - oldtime;
        		} while (time < 1);
        //...
        }
        

        I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:

        //...
        oldtime = timeGetTime();
        //...
        while (1) {
        //...

        		while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) {
        			if (!GetMessage(&msg, NULL, 0, 0))
        				Com\_Quit();
        			sys\_msg\_time = msg.time;
        			TranslateMessage(&msg);
        			DispatchMessage(&msg);
        		}
        
        		do {
        			newtime = timeGetTime();    			
        			time = newtime - oldtime;
        		} while (time < 1);
        //...
        }
        
        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        Thanks to parallel processing these days you can feed in your game loop a constant value as time variable. (there is no fluctuation between frames as far as time is concerned). You should care about Frame Rate Independent movement only if you`re developing a game meant to hit the store shelf.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups