1

I have two classes, both inherited from the same base class. It's for a game loop and depending on which "state" you're in, it should send a pointer to the active object in the CALLBACK method - but it doesn't. It only sends the second object's pointer.

If I set ObjectA first through PostMessage() and ObjectB secondly ObjectB doesn't work, it doesn't get a pointer to that object.

And if I set the opposite, ObjectB works while ObjectA doesn't.

This is the general layout of the loop: (what am I missing?)

Game loop:

PostMessage( hwnd, WM_USER, 0, (LPARAM)&ObjectA );
PostMessage( hwnd, WM_USER, 0, (LPARAM)&ObjectB );

while(running)
{
    while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
    {
        /* ... */
    }

    if (STATE == StateA)
    Update(&ObjectA);

    if (STATE == StateB)
    Update(&ObjectB);
}

In Callback method:

static ObjectA* objA;
static ObjectB* objB;

if (objA || objB)
{
    // Do stuff to objA or objB
}
else
    switch(msg)
    {
        case WM_USER:

        if (STATE == StateA)
            objA = (ObjectA*) lparam;

        if (STATE == StateB)
            objB = (ObjectB*) lparam;
        break;

        // default and WM_CLOSE
    }
}
Deukalion
  • 2,516
  • 9
  • 32
  • 50
  • An example, it responds to WM_KEYDOWN on objectA, but if I change state to B it doesn't respond to WM_KEYDOWN because ObjectB never gets a pointer. – Deukalion Aug 13 '12 at 19:25
  • What exactly happens when you try to get the second message? – E_net4 Aug 13 '12 at 19:33
  • Access violotion, with this example. Just tried a different one, using the baseclass as the object to store the pointer in - so there's only and always one object pointer. Gets an error when doing that too. Why can't I have two objects sending events to the callback? if that's the right term. – Deukalion Aug 13 '12 at 19:53
  • First object is the Menu, the second object is the Game. They both use the same base class each sharing the same Screen object and such, just depending on state they should only be active in the right state. – Deukalion Aug 13 '12 at 19:57
  • First object (Menu) responds to keyboard, when entering second state (Game) it doesn't respond to the keyboard events. Changing the order (game first, menu last) it crashes with Access Violation. – Deukalion Aug 13 '12 at 20:00

1 Answers1

0
while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
{
    /* ... */
}

Upon review, your code most likely does the following:

  1. Iterates through all messages, discarding them one-by-one
  2. After all the messages have been "processed", calls Update with the parameters of last message, which are (I assume) read in the "/* ... */" block.

Can you try this variation?

while(running)
{
    while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
    {
        /* ... */

        if (STATE == StateA)
        Update(&ObjectA);

        if (STATE == StateB)
        Update(&ObjectB); // Note the "Update" callbacks moved to inner loop
    }
}
DarkWanderer
  • 8,739
  • 1
  • 25
  • 56