Chapter 2. Drawing 2D Sprites
Some newer independent games make use of only audio or other feedback systems to convey the game state to he player; however, most games still use the tried and true method of showing the game to the user through a graphical representation. To really get started, we need to be able to put something onto the screen for the player. In this chapter we will do just that. We'll look at how to load the images from files on disk, and then display them on the screen, in 2D through a 3D API.
One of the best things you can do to make your life easier when writing code is rely on the efforts of others before you. Many thousands of people have written 2D rendering algorithms before this book was even written, and their learnings and efforts should not be wasted by scrapping all of that information and starting from scratch. Microsoft provides an open source library that tries to collect this information and give you a head start in developing your game. DirectXTK is a small library designed to do all of these common tasks for you. We'll be using this to simplify the task of loading textures from disk, rendering them onto the screen, and working with fonts to display some text on the screen.
Through the course of this chapter we will add the ability to draw images and text. We'll start by learning how to load in textures and draw them to the screen using DirectXTK. After that, we will look at text rendering and how to add that functionality to the game for future use. By the end of the chapter, you should have some images and text on screen, ready for gameplay.
In this chapter you will:
- Download and build DirectXTK
- Learn about sprites and textures
- Understand co-ordinate spaces and projections
- Learn about texture formats
- Discover how to load textures
- Draw some sprites to the screen
- Learn about text rendering
- Render some text to the screen