• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TheComet

Abstraction design

3 posts in this topic

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;
}
0

Share this post


Link to post
Share on other sites

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?

0

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
0

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.

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0