Beginner C++ Game Programming Tutorial 4
In this tutorial we explore the idea of animation in computer graphics and we make the reticle move freely around the screen. Along the way we learn about classes and object, how to add member variables to objects/class
es, about .h (header) and .cpp files, and about the short harsh lives of variables (scope).
Concepts Taught
- Animation and the game loop
- Variable scope
- The concept of objects
- Classes and their relation to objects
- Data members (member variables) and member functions
- Difference between .h and .cpp files
- Adding data members to a
class
- Separating logic and drawing code
Video Index
Bonus Advice Video
The bonus video mentioned at the end of Part 3 with advice for beginners hasn't been made yet. Wait first eh! It will be made in due time, and when it is you will get a notification if you put good old Chili in your notification list for YouTube. I'll put the link here as well when the video gets made and uploaded. Until then, don't take any wooden nickels or whatever.
Homework Questions
This lesson's homework is to answer the following questions. When you think you have the answer, click "expand" to reveal the correct answer.
1. What symbols are a common indicator of the scope of a local variable?
A: The curly braces {}
.
2. What are the two types of members that define a class?
A: Data members and member functions.
3. What is the relationship between classes and objects.
A: Classes are like a template or a blueprint that specifies the shape of an object. Objects are data structures in memory that are created, shaped, and which behave based on how they are specified in their class. We say that an object is an instance of a class.
4. How do we achieve the movement of the reticle.
A: We achieve movement of the reticle by making the variables x
and y
member variables of class Game
. In doing so, we no longer lose the values of these variables from frame to frame (when they are local variables, they are destroyed after exiting the ComposeFrame()
function, i.e. after each frame).
5. How are the members gfx
and wnd
in class Game different?
A: Game::gfx
is an object which is embedded directly into objects of the Game
class, whereas Game::wnd
is a reference to a MainWindow
object that exists outside of and is independent of the Game
object.