mainLoop();
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
I tryed adding doEvents() to the loop, but then I am back to the same problem where it only moves with I move my mouse:
GetMessage sleeps until an messages reaches a queue, so if you don't have any messages, then nothing happens.
Null's suggestion of using a Timer works because timers create messages every so often (in his example, once every 5 milliseconds).
Another approach is to not use GetMessage, but use PeekMessage instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while(Terminate == 0)
{
// process all messages on the queue
while(PeekMessage(&messages,NULL,0,0,PM_REMOVE)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
executePhysics();
// note that because PeekMessage doesn't sleep, your program will run as fast as possible and suck up
// all available CPU time. Therefore you probably will want to sleep:
Sleep(1);
}