我如何折射以下内容,以将绘图功能从h文件移动到GraphicsManager类中?

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

//class file
#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

//gameloop file
// instantiate components objects
while (true) {
    // input, logic
    Player.drawPlayer();
    Enemy.drawEnemy();
    Item.drawItem();
    // and so on
}


(代码明显简化了,我只是在这里询问绘图)

我是不是该...


在游戏循环中将指向GraphicsManager的指针传递给drawPlayer,drawEnemy等的每次调用
具有Player,Enemy等具有指向GraphicsManger的指针作为数据成员
具有Player,Enemy等扩展了drawableGameComponent类,该类具有一个指向GraphicsManager的指针作为数据成员
还有什么吗

最佳答案

这听起来像是继承的完美用例:

class Drawable
{
public:
    void draw()
    {
         // gl stuff
    }

protected:
     Texture _texture;
     Vector2 _pos;
     Vector2 _dim;
};


class Player : Drawable
{
public:
     // should modify _texture _pos and _dim somewhere.
};

// same thing for the other objects.

关于c++ - 绘制游戏组件的重构代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9597896/

10-10 21:00