Game Development Reference
In-Depth Information
Transformables and drawables
The
Sprite
class is derived from two classes—
Transformable
and
Drawable
. The
Drawable
class is essentially an interface, which holds a single abstract method —
Drawable::draw()
. All children have to implement this method to be able to draw
themselves onto a canvas (such as a
RenderWindow
). The
Transformable
class
holds a position, rotation, scale and origin, as well as
accessor
/
mutator
functions for
these fields. Some of them include:
Transformable::setPosition()
,
Trans-
formable::getPosition()
,
Transformable::move()
, and so on.
These functions might sound familiar. This is because we have encountered them before in
the
Shape
class. In fact, the
Shape
class inherits from
Drawable
and
Transform-
able
. This means that we can manipulate a sprite in the same way we do a Shape, and we
can draw sprites by calling
RenderWindow::draw()
. In fact, if we look closely at
RenderWindow::draw()
, we will see that it takes a
Drawable
argument rather than
a shape or sprite, which means that every class which derives from
Drawable
, can be
passed to a window to be drawn.
We can also create our own classes that inherit from
Transformable
or/and
Draw-
able
. If we want to create an optimized circle sprite for example, we can create
CircleSprite
and implement a
draw
method for it. Then, it will be as easy as passing
a
CircleSprite
object into a
draw
call to
RenderWindow
.