HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Could this depth system for a game be improved?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisdepthimprovedsystemcouldgamefor

Problem

I am still new to C++ and don't have a great insight on my coding yet, so I would be very grateful to anyone and everyone that gives advice.

Also, this code is meant to: keep all of my objects in an ordered fashion based on depth. I have a couple functions that allow for easy management and I made object friends with the depth manager because I only want the depthManager to have control over each objects idDepth and depth which are two different things.

The reason I need a depth system is because I need to have objects execute there code in an orderly fashion, and I also need to have control of what objects are drawn first to last.

This class has been tested and works as expected.

  • Stay up to date with this project on GitHub.com



Abstract object class:

class object{
    // Placement Data
        unsigned int depth, idDepth, idObject, idMain;

    // Friends
        friend class depthManager;
        friend class objectManager;
protected:
    unsigned int getDepthId(){ return idDepth; }
public:
    virtual void update() = 0;
    virtual void draw() = 0;
    unsigned int getDepth() { return this->depth; }
};


note: I have each object handle its own update and draw events for easier design. I'm mainly asking on approval for the depth system.

depthManager class

class depthManager{
private:
    std::map* > objectMap;

    void changeListPlacement( unsigned int depth, unsigned int position, int change);
public:
    void objectAdd( unsigned int depth, object* obj);
    void objectRemove( object* obj );
    void objectMove( unsigned int depth, object* obj );
};


depthManagers Functions:

```
void depthManager::objectAdd(unsigned int depth, object *obj)
{
obj->depth = depth;
// Check if depth key existant
if ( objectMap.find( depth ) != objectMap.end() )
{
std::vector* &refVec = objectMap[ depth ];
refVec->push_back( obj );
obj->idDepth = (unsigned)(int)refVec->size() - 1

Solution

Some remarks to your current map-based implementation:

-
The idiomatic way of accessing an item in a map and adding it if not present usually goes like this:

if not map.contains(key)
    map[key] = new value

value = map[key]
... modify value


So objectAdd could be shortened:

obj->depth = depth;
// make sure we have an object vector for the given depth
if ( objectMap.find( depth ) == objectMap.end() )
{
    objectMap[ depth ] = new std::vector;
}

std::vector* &refVec = objectMap[ depth ];
refVec->push_back( obj );
obj->idDepth = (unsigned)(int)refVec->size() - 1;


-
In objectRemove it is apparently illegal to pass an object with a non-existent depth. Printing an error message to stdout is not the best way to handle an error like that. You should throw an appropriate exception (fail early is a valuable debugging tool) or allow it by ignoring invalid depths.

-
It is not imminently clear what objectMove exactly does based on its name and the names of its parameters. It looks like it moves an object to a different depth. So a better name and signature might be:

void depthManager::changeDepth(unsigned int newDepth, object *obj)


-
In class object the methods getIdDepth() and getDepth do not modify the object state so you should consider making them const (i.e. unsigned int getDepth() const { return this->depth; }).

-
I'm not 100% convinced of the idDepth property. It basically just reflects the current position of the object in the depth-list and you are writing a fair amount of boiler plate code to keep it that way. This increases the complexity of the class somewhat. I'd revisit the concept and check if I can't get by without it.

Code Snippets

if not map.contains(key)
    map[key] = new value

value = map[key]
... modify value
obj->depth = depth;
// make sure we have an object vector for the given depth
if ( objectMap.find( depth ) == objectMap.end() )
{
    objectMap[ depth ] = new std::vector< object* >;
}

std::vector< object* >* &refVec = objectMap[ depth ];
refVec->push_back( obj );
obj->idDepth = (unsigned)(int)refVec->size() - 1;
void depthManager::changeDepth(unsigned int newDepth, object *obj)

Context

StackExchange Code Review Q#37402, answer score: 2

Revisions (0)

No revisions yet.