Game Engine Architecture

(Ben Green) #1
127

class Circle : public Shape
{
public:
void SetCenter(const Vector3& c) { m_center=c; }
Vector3 GetCenter() const { return m_center; }

void SetRadius(float r) { m_radius = r; }
float GetRadius() const { return m_radius; }

virtual void Draw()
{
// code to draw a circle
}

private:
Vector3 m_center;
float m_radius;
};

class Triangle : public Shape
{
public:
void SetVertex(int i, const Vector3& v);
Vector3 GetVertex(int i) const { return m_vtx[i]; }
virtual void Draw()
{
// code to draw a triangle
}

virtual void SetId(int id)
{
Shape::SetId(id);

Figure 3.18. pShape2 points to an instance of class Triangle.


Shape::m_id
Triangle::m_vtx[0]
Triangle::m_vtx[1]

vtable pointer pointer to SetId()
pointer to Draw ()

+0x00
+0x04
+0x08
+0x14

pShape2

Instance of Triangle Triangle’s Virtual Table

Triangle ::Draw(){
// code to draw a Triangle
}

Triangle ::SetId(int id)
{ Shape ::SetId(id);
// do additional work // specific to Triangles
}

+0x20 Triangle::m_vtx[2]

3.2. Data, Code, and Memory in C/C++

Free download pdf