Jump to content

  • Log In with Google      Sign In   
  • Create Account

What's the best way to do this?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Plethora   Members   -  Reputation: 679

Like
0Likes
Like

Posted 08 May 2013 - 01:03 PM

I posted a topic a little while ago in which the responses made me learn about and focus on the Dependency Inversion principle.  I've set about using interfaces rather than straight inheritance and I definitely see the benefit to a lot of the changes I've been making to my codebase, there are several things that already work much better than they did before.

 

So that said, there's one bit of my code that I'm having a bit of trouble refactoring.  I'll try to explain it as concisely as possible, but there's a bit of complexity here:

 

Original Implementation:

I had a parent class called Drawable and several types inherited from it including the relevant ones for this discussion called Tile and Enemy.  I then had a class called Battle_Map and another called Graphic_Arranger.  This all worked together like so:

class Drawable
{
private:
    point position; //point is just a simple struct the has two ints, x and y.

public:
    Drawable();
    point getPos();
};

class Tile : public Drawable
{
    //Does stuff, not relevant here
};

class Enemy : public Drawable
{
    //Does stuff, not relavent here
{;

class Battle_Map
{
private:
    std::vector<Enemy> enemies;
    std::vector<Tile> tiles;

public:
    std::vector<Drawable> getDrawables();
};

//Implementation of getDrawables()
std::vector<Drawable> Battle_Map::getDrawables()
{
    std::vector<Drawable> toDraw;
    toDraw.insert(toDraw.end(), enemies.begin(), enemies.end());
    toDraw.insert(toDraw.end(), tiles.begin(), tiles.end());
    return toDraw;
}


 

Graphic_Arranger had access to Battle_Map, and would then call getDrawables().  It then did stuff like sort the objects and arrange them such that the graphics engine could draw everything properly.

 

Now, this implementation worked, but it had drawbacks.  For one thing it was making copies of everything, and I would be passing vectors full of stuff around at every iteration.  For another, as my program grew, using a straight parent class like Drawable was bound to get frustrating especially when multiple inheritance (which I would have needed) came into play.

 

SOOOOOOO, for my fancy new implementation, I created the interface class iDrawable.  It has two virtual methods called getPos() and getKeyID().  Tile and Enemy now derive from it, and they have their own versions of those methods.  Ok, so far so good, no problems.

 

Well, now I can't have a std::vector<iDrawable> as its an abstract class.  I'll need to use pointers to get the same effect.  I'm fairly inexperienced with the use of c++ smart pointers, so I thought this would be a good place to learn, but I'm having issues.  Rather than get into the specifics I think it would be more productive to ask the following questions:

 

1)  What should the return type be for Battle_Map::getDrawables()?  I first thought that I would have to use a shared_ptr seeing as I am in some sense moving ownership around, but maybe that's not true?  

 

2)  What if I wanted to return a pointer to a vector full of pointers... that seems overly complicated, but is it bad?  Would I want to do it?

 

3)  If I have a std::vector<std::shared_ptr<iDrawable>> object, and I copy it... would it contain addresses to the objects as I would want it to?  Or would it contain useless gibberish pointing to uninitialized memory?

 

I'm sure there is a simple solution here, but I've reached a point where the more I think about it, the more I confuse myself.  I'm gonna put up this post and step away for a bit, lol.

 


I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

 

http://infinityelephant.wordpress.com


Sponsor:

#2 HappyCoder   Members   -  Reputation: 2886

Like
1Likes
Like

Posted 08 May 2013 - 02:36 PM

Don't make enemies inherit from a drawable object, rather you should have the enemy use a drawable object.

Make the enemy a subclass of an entity object, that other entity objects can then inherit from.

Use smart pointers in your vectors, the standard library has shared pointer you can use (shared_ptr). This way objects don't have to be copied everywhere.
class Entity
{
    std::shared_ptr<Drawable> drawable;
public:
    Entity(const std::shared_ptr<Drawable>& drawable);
};

class Enemy : Entity
{
public:
};

Now that you have separated your drawable class from your entity class you can change how an enemy is drawn simply by passing it a different drawable. Whenever the enemy moves or changes in any other way, it updates its corresponding drawable object. This may seem like extra work but in the end it is a much better design and will let your code be much more flexible and your project can continue to grow without coming apart at the seams.

Edited by HappyCoder, 08 May 2013 - 02:37 PM.


#3 Plethora   Members   -  Reputation: 679

Like
0Likes
Like

Posted 08 May 2013 - 04:19 PM

First off, I want to +1 your answer, but its pretty damned cool that your rep is currently 666, so I'll do it later.  smile.png  (EDIT:  You tipped over to 667, rep points awarded!) 

 

 

Whenever the enemy moves or changes in any other way, it updates its corresponding drawable object.

 

But to the point at hand... I'm resisting moving to a component-entity approach.  Believe me when I say I understand the theory behind it and its likely that whenever I start my next project (or revamp this one with greater ambitions in mind) I will use it, but for now that's off the table.  Besides, in my current implementation, your quoted statement is already true.  iDrawable is directly linked with the Enemy object (or Tile, or Player Character, or Minion, etc.).  It has access to a few small bits of data (namely, position, keyNumber, and a bool isAnimated).  Neither iDrawable, nor my now deprecated Drawable class did anything graphical directly, they were merely interfaces that would grant my Graphics class the information needed to draw the appropriate sprites in the appropriate places.  Again, your approach is very possibly better, but its a completely different paradigm.  Unless I'm wrong, moving in that direction would likely mean starting from scratch. 

 

On the other hand, std::shared_ptr is definitely the way to go.  I'm curious though, most everything I read says to use std::unique_ptr whenever possible, but it seems to me that unique_ptr is severely limited in its usefullness.  I know that shared_ptr has a bit of overhead with it, but is there more than just that reason why I should try to use unique_ptr when possible?


Edited by Plethora, 08 May 2013 - 07:29 PM.

I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

 

http://infinityelephant.wordpress.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS