Hello again!
In this video, we're going to look at making it a basic window in SFML. So let's start off by looking
at the things we need to know about SFML.
These classes are defined in the SFML slash "graphics" dot "hpp" header. They are inside the "sf" namespace.
The "Window" is a base class.
This represents a window on the screen which does nothing, really.
So that is not very interesting.
We are going to use the "RenderWindow", which is a subclass of "Window". And this is a window which supports
rendering.
So you can use this for doing two-dimensional drawing, inside a window on the screen.
There is also the "Event", which is a union, not a class, and this will contain details about user interaction
events.
So if the user clicks on the window, or if they press a key, while the window has focus.
There are three important member functions
of the RenderWindow. clear() will, obviously, clear the window.
draw() will do drawings in the window.
But this will only affect a memory buffer which is maintained by the window.
It will not change anything that the user sees on the screen.
To do that, we call display(). And that will make the contents of the memory buffer appear on the
display.
So, when we write a game or a simulation, we have a loop in which we clear the window, update the frame,
and then we display it, and make it visible to the user.
So these are the member functions that we use for doing that.
In the code,
I have a "constants.h" file which has the parameters for the game. And for this program, it is just the height
and width of the window.
In the main program, we include the SFML graphics header. And also our constants header, of course.
We start off by creating an object of this RenderWindow.
The constructor takes a vector which contains the width and the height of the window. And you can also
provide a C++ string, which will be the title of the window.
Usually it is a good idea to limit the frame rate. So this controls how often the window will update
the display.
If you have this too high, then the game is going to use a lot of CPU power, just updating the window.
If you have this too low, then the game will appear to be "jerky".
So 60 is the normal value for games. And then we have our loop where we update the window.
So while the window is open. We start off by clearing the window. And this argument will set the window
to have a black background.
And then we check for user interaction events.
We call the pollEvent() member the function of the window.
So the window will stop and wait for some user event.
If it times out, this will return false and carry on with the rest of the programme.
If there is an event, then we check the type.
And because this event is a union, we always need to check the type before we do anything with it.
"Event::Closed" means the user has done something which will close the window. So on Windows,
this means clicking the 'X' in the corner, or doing all "Alt-F4". On a mMc,
that would mean clicking the red button in the top left.
Or, I think it is "Command F4".
And in that case, we close the game window, which will terminate this loop.
And we can also check for particular keys.
So this is a static member function of the Keyboard class.
And the argument will be the key to check for.
So if the user presses the escape key, then we close the window again. And terminate the loop.
In the actual game, we are going to do some calculations here, which will compute the next frame, but
we are not doing anything here.
And then finally we display the updated graphics. The next frame in the game.
So I can click on the 'X', and that sends a "Close" event, and the game terminates. Or I can press the escape
key, and that will also cause the game to terminate.
Okay, so that is it for this video.
I will see you next time.
Until then, keep coding!