Advertisement

Programming in real-time

Started by October 20, 2000 03:31 PM
2 comments, last by DJ_House 24 years, 3 months ago
Ok, maybe I''m just slow, but I can''t seem to grasp the concept of programming in real-time. It''s driving me crazy! Right now, my main loop has this code:
    
//Gets starts time of loop (in milliseconds)
DWORD startFrame = GetTickCount();

// Set end time of frame to current time + 33ms

DWORD endFrame = startFrame + 33;

// Do REALLY important game stuff. <img src="smile.gif" width=15 height=15 align=middle>


//Sync frames per second (33 frames per milisecond)
while(GetTickCount() < endFrame){}

    
So my game is running at 30 loops per second. How do I convert this to real time? Do I only *DRAW* every 30 fps and let the rest of my stuff run, like game logic and physics? Without a time/frame control loop, wouldn''t the game go SICK on an AMD 1.2GHz machine? I''m so stumped.
I think what you''re moving towards is using the full power of the machine that you''re running on. The easiest way to do this is think of your game logic in terms of actions per second rather than actions per frame. Ex: if your ball in pong currently moves at 12 pixels per frame, re-unit that to 360 pixels per second. Then at the top of your loop do a tick count, and store it away. Every pass through the loop do a delta against the last tick. So the first pass might take 30 ms and the next pass takes 28 ms and so on. Then during your logic updates instead of moving the pong ball 12 pixels per loop move it 360 * time / 1000 pixels. That way it''ll run roughly the same on a 133 MHz machine or a 1.2 GHz monster. (But it''ll be smoother on the higher end machine.)
Advertisement
Do you have a code example of this that you can email me? My address is bywf@gamestar.net

Thanks!
Here''s some code:

    DWORD dwStart=GetTickCount(); // Get inital timeDWORD dwTicks; // The time that it takes to render 1 frame// At the end of your main loop you do:dwTicks = GetTickCount() - dwStart; // Get the time it tookdwStart = GetTickCount(); // Reset variable    


Now let''s say you have a character that moves at 100 units a sec.
And let''s make another assumption that dwTicks is 100 (That means your game is running at 10 fps, but whatever )
An important part though, that dwTicks is currently in milliseconds, so let''s divide it by 1000 to get it in seconds.
dwTicks = 0.1 (Better )
Now to see what''s the speed of the character should be (In this frame):

Move_Char(dwTicks * PlayerSpeed);
That is:
Move_Char(0.1 * 100);

And the character will move 10 pixels this frame, and will move a total of 100 in one second.



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction

This topic is closed to new replies.

Advertisement