Jump to content
  • Advertisement
Sign in to follow this  
TheComet

Abstraction design

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

The basic setup is as follows.

My game should be able to handle a variety of different tile map configurations, such as your typical euclidean 2D map, but also hexagonal maps, octagonal maps, trigonometrical maps, and if required some higher dimensions, too.

 

The catch: The "Level" class can't know of any of these configurations - it needs to work with abstraction in order to make a more general interface for working with all of these map configurations.

 

The problem.

I'm having trouble coming up with a clean enough design to implement this. Could someone criticize my code and give me some pointers on what could be done differently, how you would do it, etc.?

 

I've written a small example demonstrating my current design. There are comments inserted where I would like to get some feedback. I deliberately removed a whole bunch of stuff to simply demonstrate what this is about.

 

Specifically:

  • Is the overall design acceptable? Could it be done better?
  • Line 40-48: Is downcasting a base class like that clean? I have a guarantee that the derived class is in fact the class being targeted by the cast, but does this make it any more acceptable? Is there a cleaner way to do so?
  • Line 64 and 80: This is how I'd instantiate a Level object. Is it OK to instantiate the appropriate map implementation in the constructor like that and have Level's destructor clean up? Is there a better way of doing this?

Here's the code.

#include <iostream>

// abstract class defining a position
class PositionBase
{
public:
    PositionBase() {}
    virtual ~PositionBase() {}
};

// abstract class describing a map
class TileMapBase
{
public:
    TileMapBase() {}
    virtual ~TileMapBase() {}
    virtual void setTile( const PositionBase& pos, const char& tile ) = 0;
    virtual const char& getTile( const PositionBase& pos ) const = 0;
};

// Euclidean coordinate implementation
class EuclideanPosition : public PositionBase
{
public:
    EuclideanPosition() {}
    EuclideanPosition( int x, int y ) : x(x), y(y) {}
    ~EuclideanPosition() {}
    int x;
    int y;
};

// Euclidean map impelentation
class EuclideanTileMap : public TileMapBase
{
public:
    EuclideanTileMap() {}
    ~EuclideanTileMap() {}

    // is downcasting "pos" like this clean?
    void setTile( const PositionBase& pos, const char& tile )
    {
        m_Map[ ((EuclideanPosition*)&pos)->x ][ ((EuclideanPosition*)&pos)->y ] = tile;
    }

    const char& getTile( const PositionBase& pos ) const
    {
        return m_Map[ ((EuclideanPosition*)&pos)->x ][ ((EuclideanPosition*)&pos)->y ];
    }

private:
    char m_Map[20][20]; // for simplicity's sake
                        // I'm using a graph class for this
};

// contains various information about the level
// the catch: this class cannot know of what tile
// system is being implemented
class Level
{
public:

    // constructor, requires a new map object to be passed in
    // is this clean?
    Level( TileMapBase* map ) : m_Map( map ) {}

    // destructor, cleans up the map
    // again, clean or not?
    ~Level() { delete m_Map; }

    TileMapBase* getTileMap( void )
    {
        return m_Map;
    }
private:
    TileMapBase* m_Map;
};

int main()
{
    Level myLevel( new EuclideanTileMap() );
    myLevel.getTileMap()->setTile( EuclideanPosition(4,4), 'a' );
    std::cout << "tile at position 4,4: " << myLevel.getTileMap()->getTile( EuclideanPosition(4,4) ) << std::endl;
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

At least for the DEBUG configuration I suggest to implement a way to check for the concrete type. You'll spend endless time on troubleshooting if you just assume a correct type without proving it. A "guarantee" cannot be given as long as a project is work in progress and has not passed a suitable amount of test runs. You already have a virtual destructor in the PositionBase; if you also use RTTI, then a dynamic_cast may be a possibility. Another way would be to spend a const (enum) field in PositionBase that is set once by class constructor. Then check for the type before using it.

 

Usually the instance that allocates memory should be responsible also for its de-allocation. However, this sometimes isn't possible and/or wanted for some reason. I personally work with handing over lifetime responsibility in some cases, but make this absolutely clear in the documentation. Using copies, wrappers, smart pointers, reference counting, ... may be ways around this.

 

BTW: Using "const char& tile" for passing a single char is strange. Why not use a simple "char tile" instead?

Share this post


Link to post
Share on other sites

Hi,

 

Thanks for your reply.

 

 

 

Another way would be to spend a const (enum) field in PositionBase that is set once by class constructor. Then check for the type before using it.

 

This idea crossed my mind as well, however I ended up solving it using templates. smile.png

 

 

 

BTW: Using "const char& tile" for passing a single char is strange. Why not use a simple "char tile" instead?

 

Yes you are right.

 

On that note though, doesn't a simple "char" copy the passed value where "const char&" would not (implying "const char&" could be more efficient)?

Edited by TheComet

Share this post


Link to post
Share on other sites


On that note though, doesn't a simple "char" copy the passed value where "const char&" would not (implying "const char&" could be more efficient)?
With a primitive type, a const-ref means that the compiler can choose to either pass by value (which makes a copy), or pass by pointer. In the case of a primitive type, it's probably faster to just make a copy than it is to write the value to RAM and then pass this memory address as the argument instead so the function can then download that value from RAM itself.

 

For objects that are expensive to copy (e.g. std::vector<char>) then passing by const-ref is preferable. For primitive types though (e.g. int, char, float), then passing by value is preferable.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!