Hello again!
In this video, we are going to add bricks to our game.
We are going to have a grid of bricks at the top of the screen.
We are going to leave a gap at each side of the grid, and at the top, so the pool can get around here,
which will make the game more interesting.
The gap at the top is equal to the height of one brick. And the gap at the left is equal to the width
of one brick, plus some offset.
And this is just really something I found by experimenting.
There is no deep calculation or scientific process behind this.
We are going to implement this, using a vector of sprite objects.
Normally games programmers avoid using the C++ Standard Library.
The reason is that a lot of operations involve memory allocation and deallocation.
So that means that you do not have a way of predicting how long an operation is going to take.
And also you can lose control of how much memory the program is using.
The other problem is that the debug implementation can be very slow compared to the release implementation
of the standard library.
Sometimes 10 or 20 times longer.
If you have a bug in a game which only appears after you have been playing for 5 minutes and you are
running it in the debugger, then it could easily take over an hour before the bug actually appears.
And that is just not acceptable.
However, in this project, we are not trying to come up with a production-quality game.
We are trying to demonstrate some C++11 features. And using a vector is better for that, than using
the built-in array.
Although we could use the library array as a kind of compromise.
I suppose.
We need to work out how to find the coordinates of each brick.
We are going to be using the origin, which is the top left hand corner of each of these bricks. For this
brick,
the "y" coordinate is going to be the height of the brick, because we are one height below the top
of the screen.
The "x" coordinate will be the offset plus the width, because that is how far we are from the left hand
edge of the screen.
When we go to the next brick on the right, that is going to be one width further away.
So the "x" coordinate will be offset plus two times the width.
And then as we go across this row, we keep adding the width to the "x" coordinate.
And similarly, as we go down the column, we add the height to the "y" coordinate.
So these have "y" equals height, these have "y" equals two times height, and then the next row has "y"
equals three times height. And so on.
We add some constants for the brick width and height, and the offset.
We also need to know the number of columns, and the number of rows in our grid.
We have a brick class which inherits from entity. Not a moving_entity, because the bricks do not move.
And this should all look very familiar.
And the source file.
We load up the brick image, but the rest of is very similar to the background, actually.
In the main coach we include the header.
Then we have our vector of brick objects.
We iterate over the columns and the rows of this grid, and then we calculate the coordinates.
So the "x" coordinate is going to be the brick offset, plus the brick width, [times] the number of bricks
and the "y" coordinates is going to be the brick height, plus the number of rose times the height of a brick.
And then we use emplace_back().
So this is a C++11 feature which will create the object, directly in this vector, without any copying.
In the game loop.
We update the bricks. Which is not going to actually do anything, but we are being consistent with the
background.
We use a range-for loop, another C++11 feature, and we make the loop variable a reference,
because in theory we could modify the brick objects.
And then we use another range-for loop to display the bricks in the window.
This time we do not need to use a reference.
We can just copy them.
Or we could perhaps use a reference to const, if you want to be more efficient.
And there you are.
So we can see that the pool can go above the bricks, and
also to the side. So we can interact with the paddle, but not with with the bricks just yet, and we are going to
do that in the next video.
But that is all for this one.
I will see you next time.
But until next time, keep coding!