I have been reading Bjarne Stroustrup's Programming Principles and Practice Using C++ for a while now and am nearing the end of the book. I have to tried to make a very simple game using the SDL libraries as a little exercise to practice programming and utilize techniques from the book.
I've attempted to implement a rather crude GUI system that sort of resembles the one described in the book. The problem that I am having is implementing a callback system so that when a button is constructed it takes a member function as an argument so that it can be called when the button pressed. The reason why it needs to be a member function is that the game has a state system very similar to this http://gamedevgeek.com/tutorials/managing-game-states-in-c/ since I followed the tutorial when crafting my own game state system.
The existing code looks like this:
class Button : public Widget {
private:
SDL_Surface* m_image;
SDL_Rect* m_clip;
bool m_pressed;
bool m_released;
int frame;
public:
Button(int x, int y, int w, int h, SDL_Surface* img, SDL_Rect* clip);
void handle_events(SDL_Event& event);
void show(SDL_Surface* screen, GameState* state);
bool released();
};
Button::Button(int x, int y, int w, int h, SDL_Surface* img, SDL_Rect* clip)
:Widget(x,y,w,h),
m_image(img),
m_pressed(0),
m_released(0),
m_clip(clip),
frame(0)
{
}
So my question is: How do you pass a function in the constructor and call it when the button is pressed? (Like a callback). Currently each state, for example a Menu state, will just look if a button is pressed and perform a corresponding task which is pretty bad and hardly readable.
Stroustrup's code for the GUI system in the PPP book can be found here: http://www.stroustrup.com/Programming/Graphics/ in the two GUI files.
Thanks in advance.