Game Development Reference
In-Depth Information
<SFML/Window.hpp> . However, since it is derived from the Window class it can still
be used in our code without changing anything but the variable type.
The render cycle will look quite straightforward, if you have any previous game program-
ming experience. Basically it breaks down to this:
• Clear the canvas you intend to draw on
• Draw onto the canvas
• Display the canvas
This render routine happens in every frame (the loop cycle). If you are not familiar with
the rendering process, it might seem a bit odd and wasteful to throw away everything
from the last frame and render all objects in the scene again (even those which haven't
changed since the last time). However, graphics cards are well optimized to cope with this
routine, and maximize efficiency and performance wherever possible. Avoid using any
other structure, since it will only slow you down without bringing any major benefits.
Another thing to note is that the canvas which we are rendering on is double buffered.
This is very common in rendering. The way this works is quite simple—the canvas has
two sides that you can render on. Throughout the render frame, we work only on one of
the sides—the one which is not shown on the screen. After we finish rendering, we flip
the canvas and show what we've done. In the next frame, we work on the other side of the
canvas, and so on. This technique allows us to show the scene only after we've finished
rendering it. In SFML, we flip the canvas (it's also sometimes known as "swap the buf-
fers") by calling Window::display() .
Apart from that, the Window::display() method can put the to thread sleep for a cal-
culated amount of time to achieve a target framerate (frames per second). We can set the
desired framerate by calling Window::setFramerateLimit() once at the begin-
ning of the program. The function doesn't guarantee the limiting of the framerate to the
exact amount we pass it, but rather it makes a close approximation.
Window::clear() clears the canvas for redrawing. Notice that it takes a sf::Color
argument, which is an RGBA representation of a color. We can initialize it manually by
calling the constructor and passing each value individually, or we can use one of the pre-
set colors. For example Color::Red , Color::Blue , Color::Magenta , and more.
Search WWH ::

Custom Search