1
00:00:01,002 --> 00:00:02,003
- [Narrator] Hi, and welcome

2
00:00:02,003 --> 00:00:04,000
to the last video of this section.

3
00:00:04,000 --> 00:00:09,002
Extending SFML classes,
particle systems and shaders.

4
00:00:09,002 --> 00:00:13,003
In this section we will start
with the SFML Drawable class.

5
00:00:13,003 --> 00:00:16,000
Then we would move onto
building a particle system.

6
00:00:16,000 --> 00:00:20,006
And lastly we'll see
OpenGl, shaders and GLSL.

7
00:00:20,006 --> 00:00:23,003
Now, we move onto the first
video of this section,

8
00:00:23,003 --> 00:00:27,000
that deals with the SFML Drawable class.

9
00:00:27,000 --> 00:00:29,000
In this video, we're
going to take a look at

10
00:00:29,000 --> 00:00:31,005
an alternative to
inheriting from Drawable.

11
00:00:31,005 --> 00:00:35,000
And why it is best to
inherit from Drawable.

12
00:00:35,000 --> 00:00:37,008
The Drawable class has just one function,

13
00:00:37,008 --> 00:00:39,006
it has no variables either.

14
00:00:39,006 --> 00:00:42,009
Furthermore its one and only
function is pure virtual.

15
00:00:42,009 --> 00:00:45,001
This means that if we
inherit from Drawable

16
00:00:45,001 --> 00:00:47,008
we must implement its
one and only function.

17
00:00:47,008 --> 00:00:50,008
The purpose of this is that
we can then use our class.

18
00:00:50,008 --> 00:00:54,003
Which inherits from Drawable,
as a polymorphic type.

19
00:00:54,003 --> 00:00:56,009
Put more simply, anything that SFML

20
00:00:56,009 --> 00:00:59,001
allows us to do with a Drawable object

21
00:00:59,001 --> 00:01:02,003
we will be able to do with our
class that inherits from it.

22
00:01:02,003 --> 00:01:04,003
The only requirement
is that we must provide

23
00:01:04,003 --> 00:01:08,004
a definition for the pure
virtual function, draw.

24
00:01:08,004 --> 00:01:10,004
Some classes that inherit from Drawable

25
00:01:10,004 --> 00:01:14,002
already include Sprite and
Vertex array, among others.

26
00:01:14,002 --> 00:01:16,009
Whenever we have used
Sprite or Vertex array

27
00:01:16,009 --> 00:01:18,005
we have passed them to draw function

28
00:01:18,005 --> 00:01:20,009
of the Render window class.

29
00:01:20,009 --> 00:01:22,007
The reason that we've been able to draw

30
00:01:22,007 --> 00:01:25,002
every object we've drawn in this course.

31
00:01:25,002 --> 00:01:28,001
Is that they have all
inherited from Drawable.

32
00:01:28,001 --> 00:01:31,003
We can inherit from Drawable,
with any object we like.

33
00:01:31,003 --> 00:01:34,006
As long as we implement the
pure virtual draw function.

34
00:01:34,006 --> 00:01:37,000
This is also a straightforward process.

35
00:01:37,000 --> 00:01:39,003
The header file SpaceShip.h

36
00:01:39,003 --> 00:01:41,001
of a hypothetical SpaceShip class

37
00:01:41,001 --> 00:01:44,005
that inherits from Drawable
would look like this.

38
00:01:44,005 --> 00:01:45,008
In this code we can see

39
00:01:45,008 --> 00:01:48,009
the pure virtual draw
function and a Sprite.

40
00:01:48,009 --> 00:01:51,008
Notice there is no way to
access the private Sprite

41
00:01:51,008 --> 00:01:56,004
outside of the class, not
even a get Sprite function.

42
00:01:56,004 --> 00:02:01,005
The SpaceShip.cpp file would
then look something like this.

43
00:02:01,005 --> 00:02:04,000
In this code notice the
simple implementation

44
00:02:04,000 --> 00:02:05,005
of the draw function.

45
00:02:05,005 --> 00:02:08,002
The parameters are beyond
the scope of this course.

46
00:02:08,002 --> 00:02:11,006
Just note, that the target
parameter is used to call draw,

47
00:02:11,006 --> 00:02:13,008
and passes in m_Sprite,

48
00:02:13,008 --> 00:02:16,001
as well as states the other parameter.

49
00:02:16,001 --> 00:02:19,000
Now, in the main game
loop we could now treat

50
00:02:19,000 --> 00:02:21,008
a SpaceShip instance
as if it were a Sprite.

51
00:02:21,008 --> 00:02:24,004
Or any class that inherits from Drawable.

52
00:02:24,004 --> 00:02:26,006
It is because SpaceShip is a Drawable

53
00:02:26,006 --> 00:02:29,001
that we can treat it like
a Sprite or Vertex array.

54
00:02:29,001 --> 00:02:32,001
And because we overrode the
pure virtual draw function

55
00:02:32,001 --> 00:02:34,009
everything just works as we want it to.

56
00:02:34,009 --> 00:02:36,002
Let's look at an alternative way

57
00:02:36,002 --> 00:02:40,004
of encapsulating the drawing
code into the game object.

58
00:02:40,004 --> 00:02:43,003
It is also possible to keep
all the drawing functionality

59
00:02:43,003 --> 00:02:45,008
within the class, that is
the object to be drawn,

60
00:02:45,008 --> 00:02:48,008
by implementing our own
function within our class.

61
00:02:48,008 --> 00:02:51,001
Perhaps like this.

62
00:02:51,001 --> 00:02:54,003
This code assumes that m_Sprite represents

63
00:02:54,003 --> 00:02:56,008
the visual appearance of the
current class we are drawing.

64
00:02:56,008 --> 00:03:00,001
As it has throughout this
and the previous project.

65
00:03:00,001 --> 00:03:01,009
Assuming that the instance of the class

66
00:03:01,009 --> 00:03:03,003
that contains the draw.

67
00:03:03,003 --> 00:03:06,000
This object function is called playerHero.

68
00:03:06,000 --> 00:03:08,007
And further assuming we have
an instance of Render window

69
00:03:08,007 --> 00:03:12,006
called m_window we could
then draw the object

70
00:03:12,006 --> 00:03:16,007
from the main game loop
with this line of code.

71
00:03:16,007 --> 00:03:18,008
In this solution we pass the Render window

72
00:03:18,008 --> 00:03:23,009
m_window into the draw this
object function as a parameter.

73
00:03:23,009 --> 00:03:27,003
The draw this object function
then uses the Render window

74
00:03:27,003 --> 00:03:30,008
to draw the Sprite m_Sprite.

75
00:03:30,008 --> 00:03:32,007
This solution certainly seems simpler

76
00:03:32,007 --> 00:03:34,005
than extending Drawable.

77
00:03:34,005 --> 00:03:36,005
The reason we do things the way suggested,

78
00:03:36,005 --> 00:03:39,000
extending Drawable isn't
really of any great benefit

79
00:03:39,000 --> 00:03:41,002
in its own right for this project.

80
00:03:41,002 --> 00:03:43,007
The actual reason we will
soon draw and need explosion

81
00:03:43,007 --> 00:03:45,002
using this method is because

82
00:03:45,002 --> 00:03:47,007
it is a good technique to learn.

83
00:03:47,007 --> 00:03:50,003
With each project we have
completed throughout the course

84
00:03:50,003 --> 00:03:54,005
we have learnt more about
games, C++ and SFML.

85
00:03:54,005 --> 00:03:56,005
Possibly the biggest
improvements we have made

86
00:03:56,005 --> 00:03:59,007
from one game to the next is
in the structure of our code.

87
00:03:59,007 --> 00:04:02,002
The programming patterns
that we have used.

88
00:04:02,002 --> 00:04:05,002
Think about the idea
for improving our code.

89
00:04:05,002 --> 00:04:08,000
Imagine every object
in our game is derived

90
00:04:08,000 --> 00:04:11,002
from a single, simple,
abstract base class.

91
00:04:11,002 --> 00:04:13,004
Let's call it game object.

92
00:04:13,004 --> 00:04:16,003
Game object would probably
have concrete functions

93
00:04:16,003 --> 00:04:18,003
for get position and others.

94
00:04:18,003 --> 00:04:21,000
It would likely have a pure
virtual update function,

95
00:04:21,000 --> 00:04:23,009
because every object updates differently.

96
00:04:23,009 --> 00:04:26,002
Furthermore consider that game object

97
00:04:26,002 --> 00:04:28,003
inherits from Drawable.

98
00:04:28,003 --> 00:04:31,000
Now look at this hypothetical code.

99
00:04:31,000 --> 00:04:34,003
The code is a big step up
in terms of encapsulation,

100
00:04:34,003 --> 00:04:35,009
code manageability, and elegance

101
00:04:35,009 --> 00:04:38,009
when compared to even this final project.

102
00:04:38,009 --> 00:04:40,002
You will notice there are, however,

103
00:04:40,002 --> 00:04:43,003
unanswered questions, such
as where collision detection

104
00:04:43,003 --> 00:04:44,009
fits in, for example.

105
00:04:44,009 --> 00:04:47,005
Hopefully, however, you
can see that further study

106
00:04:47,005 --> 00:04:51,008
by building lots of games will
be necessary to master C++.

107
00:04:51,008 --> 00:04:53,002
Although we will not be implementing

108
00:04:53,002 --> 00:04:55,002
an entire game in this manner.

109
00:04:55,002 --> 00:04:58,000
We will see how we can design
a class particle system

110
00:04:58,000 --> 00:05:01,005
and pass it directly to m_Window.draw

111
00:05:01,005 --> 00:05:04,006
m_myparticlesysteminstance.

112
00:05:04,006 --> 00:05:06,006
Cool, that's about it.

113
00:05:06,006 --> 00:05:11,000
In this video we've learnt
about the SFML Drawable class.

114
00:05:11,000 --> 00:05:12,001
Splendid.

115
00:05:12,001 --> 00:05:15,007
Next up we'll see building
a particle system.

