Game Development Reference
In-Depth Information
Shape drawing
Now that we are familiar with the render routine, let's render some shapes on the screen.
We will start with the basic shapes and explore the alternatives later on. When we want to
draw a shape, we have to create the object first. Here is the initialization code for two
shapes. Place it just before the game loop.
A few new classes make an appearance in this example—
CircleShape
,
Rect-
angleShape
, and
Vector2f
.
You can probably guess what the
Vector2f
class is for—it is a 2D vector which holds
two
floats
. There are also classes such as
Vector2i
(for integers),
Vector2u
(for
unsigned
integers
),
Vector3i
(for 3D vectors which hold integers), and
Vector3f
(3D vectors which hold floats). We can even create our own 2D and 3D vectors, which
hold custom types, by using the template classes
sf::Vector2<class>
and
sf::Vector3<class>
.
CircleShape
,
RectangleShape
, and
ConvexShape
derive from the abstract class
Shape
, which is defined by a set number of vertices (points). The
CircleShape
is just
a regular polygon with a set number of vertices. We can specify how detailed the circle
should be with the second argument in the constructor, which is optional, with a default
value of 30. On the other hand,
RectangleShape
always has four vertices. The con-
structors of both shapes take their dimensions—the radius of the circle and the width and
height of the rectangle.
ConvexShape
is a shape for which we have to specify the vertices explicitly. There isn't
a restriction on the number of vertices, but they have to form a convex shape, otherwise the
shape will not be drawn correctly.