Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

hammerstein_02

C++ and Function Pointers

This topic is 5684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I spent a long time last night trying to figure this out. Maybe my design is flawed, but I would like to know if what I am trying to do is at least possible. I have an input class, and I want to be able to load in keymaps etc without having to change the program, so I have decided on this method. Create an input manager class that creates my input object and then calls the update method to gather the keypresses. Fine so far, now I want to pass the Inputmanager class my player object and assign the playerobjects function pointer, at the moment defined as: void(cInputManager::*pfnKeypress)(TKEYDATA); I have to declare my cInputManager class in my cPlayer class, which I am not overjoyed about but can live with. All this is so when I call my players move object, it will in turn call the function pointer, update the keypresses and know which of its keys have been pressed. My logic makes sense to me... Now I am having a problem, I can''t call the function pointer. If I give my cPlayer class a pointer to the cInputManager class I can do this (inputmanager->*pfnKeypress)(keys); but woops, I think I am doing something wrong there.. cos it crashes if I call my players move method(which calls the keypress function). Now again, my design was thought up at about 12:30am last night, so I was a little tired.. I haven''t really had much experience of function pointers, but I knew I couldn''t make my inputmanager functions statics (as they need to call the inputmanagers update). If you can see what I am trying to do, and can suggest a better design then I have no problems with that, if you can help me work out what I am doing wrong, I would be most grateful. Either way, thank-you for any help you can provide. =*= If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore =*=

Share this post


Link to post
Share on other sites
Advertisement
Aren''t things a little backward here? Shouldn''t the player move method be called in response to input, basically implying that the move method is called by the keypress method (directly or indirectly)? So shouldn''t player objects be "registered" with the input manager which "dispatches" messages to the players upon receiving specific input?


  
// rough, top-of-my-head code


class InputManager
{
private:
struct move_method
{
Player * p;
(Player::*move)(void * param0, void * param1 );
void operator ( void * param0, void * param1 )
{
p->*move( param0, param1 );
}
};
 
typedef std::map< unsigned long, move_method > method_map;
typedef std::map< unsigned long, move_method >::iterator method_map_iterator;
 
method_map MMap;
public:
move_method AddMethod( unsigned long keycode, move_method new_method )
{
method_map_iterator iter = MMap.find( keycode );
MMap[ keycode ] = new_method; // you can also use insert here


if( iter != MMap.end() )
return iter->second;
 
return 0;
}
};

Now you''d use AddMethod to connect a particular keycode (however you choose to define keycodes) to a particular Player instance and method.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!