?
=?ISO-8859-2?Q?Rafa=B3?= Maj
Say we have a class Model (a 3d model) and class Texture (representing an
Texture).
Model have a texture, Model can be Drawed. Texture have method that returns
data needed for drawing.
class Texture {
public:
// openGl data here
glInt GetInt();
};
class Model {
public:
Texture *tex;
void Draw() {
glInt x = tex->GetInt();
// ...
}
};
And all is well.
But now - say I want to make this design more flexible, to have
opengl-texture (and way of drawing for the monster)
and directX-texture (and directX way of drawing of the monster).
So I will make Texture a base class, and move OpenGL stuff into TextureOGL
etc.
class Texture { public: };
class TextureOGL : public cTexture
{ public: /* opengl data */ glInt GetInt(); };
// more Texture*
THE PROBLEM: but what then happens to the monster?
1) how to add methods for drawing in different ways (I want that to be
choosed in runtime)
2) how to access the texture
My possible solutions
Solution-1 - only child have texture
class Texture { public: };
class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
class cMonster { virtual void Draw()=0; };
class cMonsterOGL : public cMonster { public:
cTextureOGL *tex;
virtual void Draw() { tex->GetInt(); /*...*/ }
};
Almost ideal but that doesnt modell that EVERY monster always have SOME
texture (and that information migt be needee)
Solution-2
class Texture { public: virtual string Name(); };
class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
class cMonster { public: cTexture *tex; virtual void Draw()=0;
void GeneralUseOfTexture() { MsgBox(tex->Name()); }
};
class cMonsterOGL : public cMonster { public:
virtual void Draw() {
dynamic_cast<cTextureOGL>(tex)->GetInt(); /*...*/
// else/catch - wrong dynamic cast - print error "Wrong texture!"
}
};
That models all informations and allow general use of texture ->Name();
The problem is - it uses a dynamic_cast with is consider not so good, also I
wonder about speed implications.. upcast is fast and O(1) or not really?
Other solutions?
Perhaps using templates... but writting entire Monster<T> implementation
inside header .h is not too good since its complicated class.
Also it would be not so easy to have a std::vector of monsters etc.
Texture).
Model have a texture, Model can be Drawed. Texture have method that returns
data needed for drawing.
class Texture {
public:
// openGl data here
glInt GetInt();
};
class Model {
public:
Texture *tex;
void Draw() {
glInt x = tex->GetInt();
// ...
}
};
And all is well.
But now - say I want to make this design more flexible, to have
opengl-texture (and way of drawing for the monster)
and directX-texture (and directX way of drawing of the monster).
So I will make Texture a base class, and move OpenGL stuff into TextureOGL
etc.
class Texture { public: };
class TextureOGL : public cTexture
{ public: /* opengl data */ glInt GetInt(); };
// more Texture*
THE PROBLEM: but what then happens to the monster?
1) how to add methods for drawing in different ways (I want that to be
choosed in runtime)
2) how to access the texture
My possible solutions
Solution-1 - only child have texture
class Texture { public: };
class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
class cMonster { virtual void Draw()=0; };
class cMonsterOGL : public cMonster { public:
cTextureOGL *tex;
virtual void Draw() { tex->GetInt(); /*...*/ }
};
Almost ideal but that doesnt modell that EVERY monster always have SOME
texture (and that information migt be needee)
Solution-2
class Texture { public: virtual string Name(); };
class TextureOGL : public cTexture { public: /*...*/ glInt GetInt(); };
class cMonster { public: cTexture *tex; virtual void Draw()=0;
void GeneralUseOfTexture() { MsgBox(tex->Name()); }
};
class cMonsterOGL : public cMonster { public:
virtual void Draw() {
dynamic_cast<cTextureOGL>(tex)->GetInt(); /*...*/
// else/catch - wrong dynamic cast - print error "Wrong texture!"
}
};
That models all informations and allow general use of texture ->Name();
The problem is - it uses a dynamic_cast with is consider not so good, also I
wonder about speed implications.. upcast is fast and O(1) or not really?
Other solutions?
Perhaps using templates... but writting entire Monster<T> implementation
inside header .h is not too good since its complicated class.
Also it would be not so easy to have a std::vector of monsters etc.