Jump to content
  • Advertisement

Archived

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

Ademan555

Polymorphism and game programming

This topic is 5317 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, the way i had always thought id implement my game engine was to have a vector of NON_STATIC* which is a pointer to my base class for entities... it was further abstracted to a working PLAYER class :-D (yay) and now i sat down and thought about the theory, would i just new (im in c++) whatever class i need, and insert it into the vector? er copy the piointer into it? and from there i could just iterate through the vector and use my virtual function render() from all of my NON_STATIC classes? and would they use the correct render function? not the one specified for teh NON_STATIC class? but the PLAYER class? if anyone kinda gets what im saying and can help me with my logic, or tell me im right... thatd be great, thanx -Dan

Share this post


Link to post
Share on other sites
Advertisement
You sound like you know java if so work it like you would an interface. Create an header that just has defualt virtual funtions in it, with no implementation this will be a base class. Now extend that class with a more specific class
public class ext : base
now you can have a ptr that points to base objects. This prt will know the correct fucntion to call.
It is so powerful.
hope that helped

Share this post


Link to post
Share on other sites
actually, im doing it in c++... lol... hope the same implementation will work, and yeah thats what i meant, only much less wordy, and now i just gotta hope its possible in c++ (it not being completely OO) no reason i can see why it shouldnt, also, i just kinda assumed this was how it would work, how do other people store their entities?

Share this post


Link to post
Share on other sites
Yeah for the game I currently working on I set it up like this.
I have a class called engine which is has the virtual functions:
preframe()
postframe()
handleinput()
render()
etc
For every state that my game has, menu,play,options etc. I create a dirived engine that has specific implementation of render()etc. I have a stack that holds engine objects and I just push the current state onto the stack so my game loop is so simple if the stack is not empty process the top most engine.
my process funtion just calls the functions in the engine class with the pointer to the node at the top of the stack. It works great, and I use c++, I was just saying if you are use to java use your understanding of interfaces.
In lot of ways java has made me a much better programmer.

Share this post


Link to post
Share on other sites
Yup, quite possible in C++. I do it all the time, when programming games or anything else. Just remember that if you don''t declare methods virtual in your base class, the overriding methods in your derived classes will not be reached when called from a pointer to its base class.

Share this post


Link to post
Share on other sites
Yeah, i always write my methods, no virtual function shall have no definition! anyways, from your reaction to my method apeos, it seemed like there was a different/better way to store game entities... how are other people storing theirs?

Share this post


Link to post
Share on other sites
Yes, you can put the pointer to the base class into a vector of that class, as long as you derive from it. Note that memcpy() of the pointer won''t work, becuase they compiler will likely make a small adjustment to the actual value of the pointer, to point exactly to the "Baseclass" part of your entire object.


class IRenderable {
public:
virtual void getRenderingInfo( timestamp_t when, Geometry * outGeometry, State * outState ) = 0;
};

class IUISink {
public:
virtual void onKey( timestamp_t when, int key, int flags ) = 0;
virtual void onMouse( timestamp_t when, float x, float y, int flags ) = 0;
};

class IInterfaceSource {
public:
bool checkForInterface( char const * interfaceName, void ** outPtr ) = 0;
};

std::vector< IRenderable > gAllRenderables;

class CPlayer : public IRenderable, public IUISink {
public:
CPlayer( char const * name );
void getRenderingInfo( timestamp_t when, Geometry * outGeometry, State * outState );
void onKey( timestamp_t when, int key, int flags );
void onMouse( timestamp_t when, float x, float y, int flags );
void checkForInterface( char const * interfaceName, void ** outPtr );
...
};


IInterfaceSource * NewPlayer( char const * name )
{
CPlayer * p = new CPlayer( name );
gAllRenderables.push_back( static_cast< IRenderable * >( p ) );
return static_cast< IInterfaceSource * >( p );
}


Note that I suggest STRONGLY that you define a bunch of narrow interfaces that provide specific access (IRenderable, ICollidable, ISoundSource, IRefCounted, ...) and then make your concrete classes implement all the interfaces they want. But never reference the concrete classes outside of the class itself; only expose interfaces.

Note that this is very similar to the way COM is structured. It helps keep components separate, and works very well.

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!