Game Development Reference
In-Depth Information
gion of the texture and just draw that part; however, in our case we want to draw the
whole thing, so to make that simpler we can pass a
nullptr
value and skip having
to manually get the size of the texture and create a rectangle that defines the entire
region.
Vectors
A vector can be simply described as an array of numbers, which in this case refer to
a co-ordinate location on the 2D screen. In this topic I will refer to vectors in the form
(X, Y)
, where
X
and
Y
represent their respective co-ordinates.
DirectXMath provides plenty of helper functions and utilities to make working with
vectors easy, and the
SimpleMath
library contained within DirectXTK makes this
even easier.
So now we can finish rendering the sprites by calling the
Draw
function we just im-
plemented for each of our sprites and then ending the batch. Return to the
Game
class'
Draw
method and add the following code after our
Clear
calls:
_spriteBatch->Begin();
_player->Draw(_spriteBatch);
_enemy->Draw(_spriteBatch);
_spriteBatch->End();
Here we're calling our new
Draw
method on each of the textures we created and
loaded. After that we call
End
on the sprite batch, which informs it that we are done
issuing commands and it can proceed with rendering.
Because we are using the default
Begin
method, we need to ensure that we specify
the order in which these textures are drawn. One useful example is if you want to
have a background in a game. If you draw the player and other objects before the
background without any form of sorting, you will end up writing over the player im-
ages, and all you will see is the background. So ensure you check your draw order
before actually drawing to the screen; and if you can't see some sprites that should
be there, make sure you aren't hiding them accidentally.