0
\$\begingroup\$

Possible Duplicate:
Should actors in a game be responsible for drawing themselves?

How can I refractor the following, to move my drawing functions from the h-file into a GraphicsManager class?

I have the following scenario:

//drawingFunctions.h
void drawTexturedQuad( Texture texture, Vector2 pos, Vector2 dim) {
    // bind texture...
    glBegin(...);    // draw  
    //...
    glEnd(...);
}

#include "drawingFunctions.h"
class Player { 
    void drawPlayer(){ drawTexturedQuad( texture, pos, dim) } 
};
class Enemy { 
    void drawEnemy(){ drawTexturedQuad( texture, pos, dim) } 
};
class Item { 
    void drawItem(){ drawTexturedQuad( texture, pos, dim) } 
};
// and so on for the other  components, you get the idea...

I figured I could make all these classes extend another class, say drawableGameComponent. Would that be a good idea? What are other suggestions?

\$\endgroup\$
5
  • 4
    \$\begingroup\$ You should see this question which is essentially asking the same thing (do objects draw themselves or does something else draw them?). \$\endgroup\$
    – user1430
    Mar 6, 2012 at 20:41
  • \$\begingroup\$ While this is an interesting read, I don't really see how it solves my (implementation) problem? I think I am doing what is suggested there, in the sense that I am trying to have my renderer draw the components by using my drawTexturedQuad function (instead of having the render code in the components themselves). I edited my question to make that clearer. \$\endgroup\$
    – Ben
    Mar 7, 2012 at 7:39
  • \$\begingroup\$ The idea is that your Player class, for example has a class that represents a textured quad, and then you have some kind of rendering class that collects all these objects in all of your entity types and draws them. Unless you're asking how to put drawTexturedQuad inside of a class? \$\endgroup\$
    – Tetrad
    Mar 7, 2012 at 16:54
  • \$\begingroup\$ Ah, ok, I see. So if by "has" you mean "has as a data member", wouldn't it then be a good idea to make all my entity types extend a textured quad class instead of adding the textured quad as a separate data member to every entity? For my understanding, it would help a lot if you could formulate your idea into an answer with a short code example. \$\endgroup\$
    – Ben
    Mar 8, 2012 at 1:19
  • \$\begingroup\$ Also, wouldn't your solution mean updating both the actual player object and its corresponding textured quad object every frame? That would mean doubling my updates... so that can't be a recommended approach!? \$\endgroup\$
    – Ben
    Mar 8, 2012 at 1:26

0

Browse other questions tagged .