Windows Programming

Discussion in 'Technical Discussion' started by Alriightyman, Oct 18, 2012.

  1. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Using win32 API, why does deltaTime always end up as zero?
    Code (Text):
    1.  
    2.     int startTime = timeGetTime();
    3.  
    4.     // start message loop
    5.     while(msg.message != WM_QUIT)
    6.     {
    7.         if(::PeekMessage(&msg,0,0,0,PM_REMOVE))
    8.         {
    9.             ::TranslateMessage(&msg);
    10.             ::DispatchMessage(&msg);
    11.         }
    12.         else
    13.         {
    14.             int t = timeGetTime();
    15.             int deltaTime = (t - startTime);
    16.  
    17.             // For each frame update and render our game
    18.             app.Update(deltaTime);
    19.             app.Render();
    20.  
    21.             startTime = t;
    22.         }
    23.     }
    24.  
    25.  
     
  2. Billy

    Billy

    RIP Oderus Urungus Member
    1,981
    66
    28
    Colorado, USA
    Indie games
    I'm not sure if it'll fix your problem but try declaring your variables t and deltaTime to NULL before the loop.
     
  3. Chilly Willy

    Chilly Willy

    Tech Member
    746
    0
    0
    Doom 32X
    Remember that the granularity of timeGetTime() can be greater than 5 milliseconds. That might be an issue depending on how fast your PC is.
     
  4. Glitch

    Glitch

    Tech Member
    163
    3
    18
    Also, you'll probably need to change your message loop to:

    Code (Text):
    1.  
    2. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
    3. {
    4.     translate..
    5.     dispatch...
    6. }
    7.  
    8. //update game logic
    9. ...
    10.  
    Otherwise you might find that you're not processing windows events fast enough and your event queue will just keep growing.
     
  5. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    I have a very fast laptop so that may be the issue. My question now is, how do I fix it?

    That was something I didn't know. Thanks.
     
  6. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    354
    3
    18
    Largo, FL
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Well, I seemed to have fixed it by moving into the timing variables into the app.Update()

    I am not sure as to why it works though and would love to understand that.

    Here is my main loop now:
    Code (Text):
    1.  
    2. int startTime = timeGetTime();
    3.  
    4.     // start message loop
    5.     while(msg.message != WM_QUIT)
    6.     {
    7.        
    8.  
    9.         while(::PeekMessage(&msg,0,0,0,PM_REMOVE))
    10.         {
    11.             ::TranslateMessage(&msg);
    12.             ::DispatchMessage(&msg);
    13.         }
    14.  
    15.  
    16.         // For each frame update and render our game
    17.         app.Update(startTime);
    18.         app.Render();
    19.     }
    20.  
    These lines:
    Code (Text):
    1.  
    2.  
    3.         int t = timeGetTime();  
    4.         int deltaTime = (t - startTime);
    take place in the app.Update function now.