Communicating through objects

Started by
13 comments, last by Mybowlcut 16 years, 2 months ago
Quote:Original post by phantom
Depends on how you are doing your input, however for the sake of arguement lets say you are using a mouse, the steps would be;

- Detect mouse event (I recommend 'mouse up' as that's how GUIs like Windows work)
- Find out which component is under the mouse when the even happens
- Call the component's "selected" event function

This 'selected' function will then do the work of "somehow" moving the game to the selected screen (game, instructions, etc).
This button class derives from Base_XImage and this particular function would be the "event" function that you mentioned:
void XButton::Update(const SDL_Event& event_){	const SDL_Rect mouse = {event_.motion.x, event_.motion.y};	if(Is_Mouse_Over(mouse))	{		switch(event_.type)		{		case SDL_MOUSEMOTION:			if(current_state == DOWN)				break;			Set_Current_State(OVER);			break;		case SDL_MOUSEBUTTONDOWN:			Set_Current_State(DOWN);			break;		case SDL_MOUSEBUTTONUP:			Set_Current_State(OVER);			break;		}	}	else		Set_Current_State(OFF);}
I've got that.. but I'm unsure of how/why I'd start a game from inside a Button's Update function. :(

Advertisement
Well, you wouldn't have to, this comes back to what was said earlier in the thread about observers.

The process goes something like;

- App tells button it has been pressed
- Button then tells everyone on its observer list that it has been pressed
- Observers go off and act on that happening

The observers could do anything from load a script to kick the game off, to create a new 'game' object and kick of it's 'run' function or whatever it is required to do to act on the button being pressed.
Ok, I'm working on that now. But I've got an error whilst testing:
void Screen::Notify(Observable* observable, void* oe){	Base_XImage::OBSERVABLE_EVENT* event_ = (Base_XImage::OBSERVABLE_EVENT*)oe;	switch(*event_)	{	case Base_XImage::LMB_CLICK:		std::cout << "Left mouse click!" << std::endl;		break;	case Base_XImage::RMB_CLICK:		std::cout << "Right mouse click!" << std::endl;		break;	case Base_XImage::MOUSE_OVER:		std::cout << "Mouse over!" << std::endl;		break;	}}


Quote:First-chance exception at 0x0041612c in SDL_Testing.exe: 0xC0000005: Access violation reading location 0x00000002.


I'm calling it like this:
Notify_Observers(this, (void*)MOUSE_OVER);
Is it because it's a constant/literal? If so, how would I get around it?

Change this:

Notify_Observers(this, (void*)MOUSE_OVER);


to this:

int e = (int)MOUSE_OVER;Notify_Observers(this, (void*)&e);
Ok. Just out of curiosity, why? Could I replace int e with the enum type?
OBSERVABLE_EVENT e = MOUSE_OVER;Notify_Observers(this, (void*)&e);


Edit: Woohoo! It's working now! Thanks, Gage!

This topic is closed to new replies.

Advertisement