Jump to content
  • Advertisement

Archived

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

Caesar

Designing a game

This topic is 6241 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

I''m going (in fact I''ve already started twice) to make a simple american football simulation. I''m making it under DOS (DJGPP, Allegro) to eliminate the problems I have using DirectX. My first appoarch was to code build it up continously from the ground, so I made the graphics, then the ball physics and then the problems came when I tried to make input lib. So now I''m planning to do some OO model and to make the comunication between the objects message driven. Is this finally the right appoarch? Any suggestions or hints? Thanks a lot.

Share this post


Link to post
Share on other sites
Advertisement
It depends on what you''re trying to accomplish.

OO usually leans towards an event-driven model over a message queue; but message queues can sometimes provide better overall performance.


What was the problem with the input lib?

Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post


Link to post
Share on other sites
The problem was, that I didn''t know how to make it cooperate with the other parts of the program (the first version was non OO and has never been planned to go so far as it had). In the past when I made some game, it''s main loop seemed like if(key=LEFT) {...} but then I begun with making engines for each of these things and till now the only solution I found was to use messages. If I want to tell the graphics that it''s time to redraw, I send a message, If I want my game object to know a direction key has been pressed I send it a message (In fact all those "engines" or objects are derived from a parent one and have a virtual SendMessage function to recieve the messages so I can use polymorphism (I send the messages to the loop and the loop sends it via this function to all the objects in the objects-list I have). But I think there may be a better appoarch as well. Can you explain me the difference between event-driven and message driven, I imagine it as quite the same thing, but maybe I''ve misunderstood somthing. Thanks

Share this post


Link to post
Share on other sites
In an event driven model you make something happen right when the event occurs, with a message driven model you toss a message in a queue for processing later.


I've been thinking about different ways to handle input... I haven't come up with a good solution yet.

I want rebindable keys/buttons/motion and a reusable input module. Callbacks seems the way to do it, and the observer pattern is the OO equivalent of callbacks.

    
class IInputObserver
{
public:
virtual void KeyHit(CInput*, int iKey)=0;
virtual void AnalogMotion(CInput*, int iAxis, float fPosition)=0;
};

class CInput : public IObservable<IInputObserver>
{
public:
HRESULT BindKey(IInputObserver*, int iKey);
HRESULT BindAxis(IInputObserver*, iAxis);
HRESULT Poll(IInputObserver*);
};


class CGameEngine : private IInputObserver
{
private:
CInput* m_pInput;
virtual void KeyHit(CInput*, int iKey);
virtual void AnalogMotion(CInput*, int iAxis, float fPosition);

};


You bind they keys or analog axis you care about, and when you call Poll() the Input engine only notifies you of the key/axis you care about. The GameEngine handles the translation from key/axis change to character action, selection, etc... The Engine itself is an input observerable so other objects can attach to the gameengine instead of directly to the input engine to received filtered input events.

That's about all I got as of now.

Edited by - Magmai Kai Holmlor on July 20, 2001 11:31:15 PM

Share this post


Link to post
Share on other sites
I''m sorry but I don''t really know what does mean, I think it has something to do with templates, but I''ve used OO just for polymorphism... Can you tell me what''s the meaning? Each of these has a "static" copy of IInputObserver or what? Sorry for my inablility

Share this post


Link to post
Share on other sites
I wish the source tags didn't eat white space... it'd be much easier to read...

The CInput class maintains a list of input observers, and whenever something happens, it calls all the observers to let them know what happened.

CGameEngine inherits from IInputObserver, and when it's initialized it'll need to call pInput->AddObserver(this); in order to receive input events.

Then whenever a key is hit, the CInput class would call ->KeyHit(...) on all it's observers.




I use a simple template to implement the skeleton code of observer patterns

    
template <typename TObserver>
class CObservable
{
public:
HRESULT AddObserver(TObserver* pObserver)
{
m_listObserver.insert(m_listObserver.end(), pObserver);
return(S_OK);
}
HRESULT RemoveObserver(TObserver* pObserver)
{
m_listObserver.remove(pObserver);
return(S_OK);
}

protected:
typedef std::list<TObserver*> tyListObserver;
tyListObserver m_listObserver;
};


It's a template so that it'll work with any type of observer; all it does it maintain the list of observers, and as soon as I figure out how, it'll let you call a method on each of them easily as well.

Basicly the input engine allows another object to watch for keyhits or analog motion. In order to do this, the other object must inherit from IInputObserver. In the example IGameEngine is an input observer, so the engine will watch for all the input the game needs.

Magmai Kai Holmlor
- Not For Rent

Edited by - Magmai Kai Holmlor on July 20, 2001 11:44:34 PM

Share this post


Link to post
Share on other sites
To do event input in allegro I think you
need to use this, from Shawn Hargreaves, allegro.txt

extern int (*keyboard_callback)(int key);
If set, this function is called by the keyboard handler in response to every keypress. It is passed a copy of the value that is about to be added into the input buffer, and can either return this value unchanged, return zero to cause the key to be ignored, or return a modified value to change what readkey() will later return. This routine executes in an interrupt context, so it must be in locked memory.

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!