WEBVTT

00:00.090 --> 00:00.620
Hello again!

00:00.990 --> 00:06.660
In this video, we're going to look at making it a basic window in SFML. So let's start off by looking

00:06.660 --> 00:08.910
at the things we need to know about SFML.

00:09.630 --> 00:17.430
These classes are defined in the SFML slash "graphics" dot "hpp" header. They are inside the "sf" namespace.

00:18.570 --> 00:20.580
The "Window" is a base class.

00:21.030 --> 00:24.390
This represents a window on the screen which does nothing, really.

00:24.390 --> 00:25.620
So that is not very interesting.

00:26.490 --> 00:32.580
We are going to use the "RenderWindow", which is a subclass of "Window". And this is a window which supports

00:32.600 --> 00:33.090
rendering.

00:33.600 --> 00:37.620
So you can use this for doing two-dimensional drawing, inside a window on the screen.

00:38.730 --> 00:44.550
There is also the "Event", which is a union, not a class, and this will contain details about user interaction

00:44.550 --> 00:44.970
events.

00:45.690 --> 00:50.910
So if the user clicks on the window, or if they press a key, while the window has focus.

00:53.110 --> 00:55.090
There are three important member functions

00:55.090 --> 00:58.180
of the RenderWindow. clear() will, obviously, clear the window.

00:58.660 --> 01:00.820
draw() will do drawings in the window.

01:00.820 --> 01:05.440
But this will only affect a memory buffer which is maintained by the window.

01:05.980 --> 01:08.440
It will not change anything that the user sees on the screen.

01:09.370 --> 01:14.440
To do that, we call display(). And that will make the contents of the memory buffer appear on the

01:14.440 --> 01:14.920
display.

01:15.490 --> 01:22.870
So, when we write a game or a simulation, we have a loop in which we clear the window, update the frame,

01:22.870 --> 01:25.630
and then we display it, and make it visible to the user.

01:26.020 --> 01:28.630
So these are the member functions that we use for doing that.

01:31.740 --> 01:32.310
In the code,

01:32.310 --> 01:38.670
I have a "constants.h" file which has the parameters for the game. And for this program, it is just the height

01:38.670 --> 01:39.930
and width of the window.

01:42.110 --> 01:48.200
In the main program, we include the SFML graphics header. And also our constants header, of course.

01:49.550 --> 01:52.400
We start off by creating an object of this RenderWindow.

01:52.970 --> 01:59.600
The constructor takes a vector which contains the width and the height of the window. And you can also

01:59.600 --> 02:03.410
provide a C++ string, which will be the title of the window.

02:05.510 --> 02:11.420
Usually it is a good idea to limit the frame rate. So this controls how often the window will update

02:11.420 --> 02:12.230
the display.

02:13.100 --> 02:19.580
If you have this too high, then the game is going to use a lot of CPU power, just updating the window.

02:20.840 --> 02:23.780
If you have this too low, then the game will appear to be "jerky".

02:24.470 --> 02:31.430
So 60 is the normal value for games. And then we have our loop where we update the window.

02:32.090 --> 02:39.020
So while the window is open. We start off by clearing the window. And this argument will set the window

02:39.020 --> 02:40.370
to have a black background.

02:41.780 --> 02:44.180
And then we check for user interaction events.

02:45.080 --> 02:47.960
We call the pollEvent() member the function of the window.

02:48.350 --> 02:51.230
So the window will stop and wait for some user event.

02:51.980 --> 02:56.150
If it times out, this will return false and carry on with the rest of the programme.

02:56.600 --> 02:58.820
If there is an event, then we check the type.

02:59.540 --> 03:04.670
And because this event is a union, we always need to check the type before we do anything with it.

03:06.720 --> 03:12.020
"Event::Closed" means the user has done something which will close the window. So on Windows,

03:12.030 --> 03:16.820
this means clicking the 'X' in the corner, or doing all "Alt-F4". On a mMc,

03:16.830 --> 03:19.410
that would mean clicking the red button in the top left.

03:19.770 --> 03:21.640
Or, I think it is "Command F4".

03:23.220 --> 03:27.210
And in that case, we close the game window, which will terminate this loop.

03:29.880 --> 03:31.920
And we can also check for particular keys.

03:32.370 --> 03:35.670
So this is a static member function of the Keyboard class.

03:36.420 --> 03:38.330
And the argument will be the key to check for.

03:38.850 --> 03:44.280
So if the user presses the escape key, then we close the window again. And terminate the loop.

03:46.050 --> 03:51.480
In the actual game, we are going to do some calculations here, which will compute the next frame, but

03:51.480 --> 03:52.590
we are not doing anything here.

03:53.100 --> 03:57.560
And then finally we display the updated graphics. The next frame in the game.

04:00.410 --> 04:08.810
So I can click on the 'X', and that sends a "Close" event, and the game terminates. Or I can press the escape

04:08.810 --> 04:11.660
key, and that will also cause the game to terminate.

04:12.140 --> 04:13.490
Okay, so that is it for this video.

04:13.820 --> 04:14.600
I will see you next time.

04:14.810 --> 04:16.690
Until then, keep coding!
