Thanks for the reply IanM. That explains why it only works with the escape key. But unfortunately, it raises more questions. I did some more playing about and checked the return code of LoopSDK(), and found the following:
- Pressing the escape key sets a flag but makes no difference to the return value
- Pressing Alt-F4 or the X button etc sets the flag and gets LoopSDK() to return false
- If you don't use the return value of LoopSDK() to exit, setting the flag makes it speed up
- The speed up still only works with sync rates greater than 0
#include "DarkSDK.h"
void DarkSDK()
{
dbSyncRate(40);
// dbSyncRate(0);
dbLoadImage("sprite.bmp",1);
dbSprite(1,200,200,1);
dbOffsetSprite(1,dbSpriteWidth(1)/2,dbSpriteHeight(1)/2);
bool k = true;
while (!dbReturnKey())
{
k &= LoopSDK();
dbRotateSprite(1,dbWrapValue(dbSpriteAngle(1)+1));
dbText(10,10,dbStr(dbScreenFPS()));
if (k) dbText(10,20,"True");
else dbText(10,20,"False");
dbSync();
}
}
So, from what I understand, there's a flag inside the SDK that checks for exit conditions, and you can obtain this flag by calling LoopSDK(). However, although the escape key sets this flag (if you've got it enabled), it doesn't change the return code of LoopSDK(). Other exit methods, like Alt-F4 and closing the window, set the flag too, but they return false from LoopSDK(). You can see this by pressing Alt-F4 or Escape with the code above.
Anyway, from what you say, once this flag is set dbSync() returns as quickly as possible; i.e. it doesn't wait any more. But if you've not exited the program, dbSync() continues to return quickly, and so you get this strange speed increase whilst the frame rate remains the same. And yet this speed increase only happens when the sync rate is greater than 0.
I don't understand why this high speed phenomenon occurs, nor can I see how to achieve it any other way. Setting the sync rate to 0 gives a high FPS, but it's still far slower than setting this exit flag without returning:
- dbSyncRate(0), flag not set: about 1.5 revolutions / sec (FPS is ~550)
- dbSyncRate(0), flag set: about 1.5 rev/sec
- dbSyncRate(40), flag not set: about 0.1 rev/sec
- dbSyncRate(40), flag set: about 30 rev/sec (but FPS is still ~40)
As far as I can see, dbSync() is still only maintaining a constant frame rate, but the loop is being executed many more times. This could be very useful if you had lots of processing to do but not much graphics. By using a constant frame rate, you're limited to executing the loop that many times a second; this flag means the loop is executed much more often than the screen is refreshed, so you could do all your collision detection and physics and heavy processing stuff separately from your graphics part and still maintain a high FPS.
Any thoughts? Am I even making any sense?