Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: communicati...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 9514 of 9831
Post > Topic >>

Re: communication between cl*****

by Lance Diduck <lancediduck@[EMAIL PROTECTED] > Apr 18, 2008 at 05:18 PM

On Apr 17, 1:42 pm, carvalho.mig...@[EMAIL PROTECTED]
 wrote:
> hi there
>....
> i want to avoid public variables and static variables/methods.
> basically i want a dynamic model to communicate between all the
> cl*****.
> thanks in advance =)
What you want here is to implement the MVC pattern
http://en.wikipedia.org/wiki/Model-view-controller.
The Controller is the mouse and keyboard event handler. The View
****tion are object that implement cg::Draw. The Model ****tion is stuff
that implements cg::Entity. Normally this is separated into two
different cl***** so you could have more than one View per Model (i.e.
more than one perspective on a ****p) but in your case putting them
both in the same class is fine.
When Views and Models are created, they register themselves with the
Controller, specifying which events they are interested in. (Using the
Vistor pattern may also help with this part
http://en.wikipedia.org/wiki/Visitor_pattern).
The Model and Views also shared pointers to each other.
So you may have:
struct Event{
//info about the event
};

struct IListener{
virtual void onEvent(Event const&)=0;
};

Then
class ****p : public cg::Entity, public cg::IDraw, public IListener

and whatever class processes keyboard clicks and such would have a
container that maintains a bunch of poitners to IListeners
enum EventType{};//different sorts of events
struct IEventProcessor{
virtual void add(IListener* , EventType)=0;
virtual void remove(IListener* , EventType)=0;
};
So on each event type that contaner is scanned and onEvent is called.

I have no idea on how to register of keypresses and such in opengl,
you will have to figure out that part. Also, in real life I use
shared_ptrs instead of raw pointers, plus I would not use pointers as
the ObjectId as I did here (it would looke more like

struct IObject{
virtual ObjectId get_id()const=0;
};
struct IListener:virtual IObject{
virtual void onEvent(Event const&)=0;
};
struct IEventProcessor:virtual IObject{
virtual void add(shared_ptr<IListener>const& , EventType)=0;
//do nothing on duplicate Id
virtual void remove(ObjectId , EventType)=0;
};


but setting all this up is way overkill for a student Asteroids game

Good Luck
Lance


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 4 Posts in Topic:
communication between classes
carvalho.miguel@[EMAIL PR  2008-04-17 11:42:38 
Re: communication between classes
Christopher <cpisz@[EM  2008-04-18 06:13:19 
Re: communication between classes
Tony Delroy <tony_in_d  2008-04-18 17:15:17 
Re: communication between classes
Lance Diduck <lancedid  2008-04-18 17:18:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:56:46 CDT 2008.