You should not rely on rendering speed for your game logic. Instead, track the time spent from the last logical step in the game to the current. Then, if the time spent exceeds a certain amount, you complete the game step (in rare cases, when the computer is so slow that two steps should happen, you might want to develop an intelligent solution to ensure that the game does not lag).
Thus, the logic of the game is separated from the visualization logic, and you do not need to worry about changing the speed of the game depending on whether vertical synchronization is turned on or off, or if the computer is running slower or faster than yours.
Some pseudo codes:
// now() would be whatever function you use to get the current time (in // microseconds or milliseconds). int lastStep = now(); // This would be your main loop. while (true) { int curTime = now(); // Calculate the time spent since last step. int timeSinceLast = curTime - lastStep; // Skip logic if no game step is to occur. if (timeSinceLast < TIME_PER_STEP) continue; // We can't assume that the loop always hits the exact moment when the step // should occur. Most likely, it has spent slightly more time, and here we // correct that so that the game doesn't shift out of sync. // NOTE: You may want to make sure that + is the correct operator here. // I tend to get it wrong when writing from the top of my head :) lastStep = curTime + timeSinceLast % TIME_PER_STEP; // Move your game forward one step. }
source share