Jump to content
  • Advertisement
Sign in to follow this  
ArchG

C++ Interfaces

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

Hello, I would like get better at making libraries, or...reusable code. Through the forums and some staff journals I see talks about Interfaces...but I really don't understand how they help (besides keep things organized a bit), or what they do fully. Here is what I do know (guessing a bit, so don't be hesitant to correct me) All functions should be pure virtual functions. Good practice would be to keep each one in its own header file (no .cpp file with it). The class is only derived from, never used. With that knowledge, this is what I can come up with.
class IGame
{
protected:
    virtual void Init() = 0;
    virtual void CleanUp() 0;
    virtual void MainLoop() = 0;
public:
    virtual void Run() = 0;
};

Now, that is most likely a stupid thing to use an interface for (I have no idea), but is that basically how it goes? Then my main class would derive from this one? If anyone can point me to any links or anything about things like this, that would be awesome too. Thanks, ArchG

Share this post


Link to post
Share on other sites
Advertisement
You have the basic idea of how you create interfaces right.

Imagine this, you want your game engine to load image files, one thing which could be nice would be to let the engine add support for more types.

We could have an interface for an image class:

class IImage
{
public:
virtual ~IImage(){}

virtual void Load(const std::string& pFilename) = 0;

virtual void* GetDataPointer() = 0;
virtual unsigned int GetNumberRedBits() = 0;
virtual unsigned int GetNumberBlueBits() = 0;
virtual unsigned int GetNumberGreenBits() = 0;
virtual unsigned int GetNumberAlphaBits() = 0;
};






Now imagine the classes BMPImage, TGAImage and PNGImage were derived from IImage.

You'ld now be able to do like this:

void Foo()
{
IImage* Picture = new TGAImage;
Picture->Load( "PrettyPicture.tga" );
...
}






If you wanted to use BMPs instead you only need to change "new TGAImage" to "new BMPImage" and possibly the filename.

You could abstract this even more. Something like this:

IImage* CreateImage(const std::string& pFilename)
{
IImage *Ptr = 0;
switch( GetExtension(pFilename) )
{
case "TGA":
Ptr = new TGAImage;
break;
case "BMP":
Ptr = new BMPImage;
break;
case "PNG":
Ptr = new PNGImage;
break;
default:
// Error
return 0;
}
Ptr->Load(pFilename);
return Ptr;
}






This way you dont even need to know what you are loading.

Another advantage is that all functions needing an image doesn't need to have overloads for all image types, but can just take an IImage* pointer.

In the same way if you have for example an IRenderable interface which all classes which can be rendered will be derived from. You can somewhere have a list of IRenderable pointers to render.

Share this post


Link to post
Share on other sites
For interfaces, you should only expose factory functions, and classes with pure virtual methods, in the headers. The concrete implementation class declarations go in the .cpp files.

Interfaces can be constructed using factory functions, or using class factories (which themselves are likely interfaces).

Interfaces aren't the only way to clean up your code organization, but they sure help. They also help with keeping physical dependency down.

Share this post


Link to post
Share on other sites
Some keywords to google about: Design Patterns, Liskov Substitution Principle, Single Responsibility Principle.

This will take you more into how to design your interfaces and classes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!