Loop only executed when I move the mouse/press a key

I have made a simple 2D physics engine with a mainLoop(), only the mainLoop only runs when I move my mouse or press a key.

I suppose there is a simple solution for this, but I do not know what to search for.

The function is very simple:
1
2
3
4
5
6
7
int mainLoop()
{
    while (Terminate = 0)
    {
        executePhysics();
    }
}
I don't know what may cause this but line 3 while(Terminate=0) must be while (Terminate == 0)
OS, compiler, libraries being used, more code, etc. etc.
Thank you it is Terminate == 0, but now the mainLoop freezes the form. Do you know a solution for that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    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:
1
2
3
4
5
6
7
int doEvents()
{
    if(GetMessage(&messages, NULL, 0, 0)){
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const UINT idTimer1 = 1;
UINT nTimerDelay = 5;   //


// ...

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_CREATE:
          SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
           break;
      case WM_TIMER:
         if(Terminate == 0)
         {
            executePhysics();
         }
          break;
        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;
}


hope this helps
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);
}
I will use peekmessage, thank you both.
Topic archived. No new replies allowed.