Event Dispatching, void pointer and alternatives

Posted by PeeS on Stack Overflow See other posts from Stack Overflow or by PeeS
Published on 2012-10-31T16:52:03Z Indexed on 2012/10/31 17:00 UTC
Read the original article Hit count: 166

Filed under:
|
|

i have my event dispatching / handling functionality working fine, but there is one issue that i need to resolve. Long story short, here are the details.

// The event structure
struct tEventMessage 
{
    // Type of the event
    int Type;

    // (void*) Allows those to be casted into per-Type objects
    void *pArgument1;   
    void *pArgument2;
};

I am sending events from different modules in my engine by using the above structure, which requires a pointer to an argument.

All messages are queued, and then dispatched on the next ::Tick().

It works fine, untill i try to send something that doesn't exist in next ::Tick, for example:

When a mouse click is being handled, it calculates the click coordinates in world space. This is being sent with a pointer to a vector representing that position, but after my program quits that method, this pointer gets invalid obviously, cause local CVector3 is destructed:

      CVector2 vScreenSpacePosition = vAt;
    CVector3 v3DPositionA = CVector3(0,0,0);
    CVector3 v3DPositionB = CVector3(0,0,0);

    // Screen space to World space calculation for depth   zNear
    v3DPositionA   =  CMath::UnProject(vScreenSpacePosition,
                                            m_vScreenSize,
                                            m_Level.GetCurrentCamera()->getViewMatrix(),
                                            m_Level.GetCurrentCamera()->getProjectionMatrix(),
                                            -1.0 );


    // Screen space to World space calculation for depth    zFar
    v3DPositionB   =  CMath::UnProject(vScreenSpacePosition,
                                            m_vScreenSize,
                                            m_Level.GetCurrentCamera()->getViewMatrix(),
                                            m_Level.GetCurrentCamera()->getProjectionMatrix(),
                                            1.0);



       // Send zFar position and ScreenSpace position to the handlers
// Obviously both vectors won't be valid after this method quits.. 
        CEventDispatcher::Get()->SendEvent(CIEventHandler::EVENT_SYSTEM_FINGER_DOWN,
                                           static_cast<void*>(&v3DPositionB),
                                           static_cast<void*>(&vScreenSpacePosition));

What i want to ask is, if there is any chance i could make my tEventMessage more 'template', so i can handle sending objects like in the above situation + use what is already implemented?

Can't figure it out at the moment.. What else can be done here to allow me to pass some locally available data ? Please can somebody shed a bit of light on this please?

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers