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 objects, how to add member variables to objects/class
es, about .h (header) and .cpp (source) files, and about the short harsh lives of local variables (scope).
Contents
[hide]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 Timestamp Index
- Part 1
- Intro 0:00
- Keyboard ghosting: Keypress combination limits 1:47
- The problem of reticle movement from Tutorial 3 2:21
- The game loop 4:29
- Stepping through the game loop using the debugger 7:21
- Scope of a variable: A problem of persisting reticle position across loop cycles 12:10
- Nested scopes and variables of the same name 15:12
- Part 2
- Part 3
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?
2. What are the two types of members that define a class?
3. What is the relationship between classes and objects.
4. How do we achieve the movement of the reticle.
5. How are the members gfx
and wnd
in class Game
different?
Errata
- In Part 1 at 6:00 there is a mistake in the diagram where Chili shows the concept of animation from one frame to the next. The "frame 2": "draw 2nd object" (blue triangle) should have the blue triangle drawn to the right as it is shown in "draw 3rd object".
- In Part 3 when we make the
gb
variable a member variable, it will maintain its value from frame to frame. So if we don't reset it every frame, then the first time we press CTRL and set it to 255, it will forever after be stuck at 255, which is probably not desired behavior. ;)
Notes
- If you get a crash when the reticle goes off the screen when Chili can make his reticle wrap from right to left, make sure your build configuration is set to
Release
and notDebug
. - This is version 2 of this tutorial. Version 1 was released back in August 2016, but it caused a lot of people to lose their shit and give up on the awesomeness that is C++, so Chili remade it. It's now over twice as long, and jam packed full of juicy knowledge, with a liberal smattering of memes as well.
Download Links
- Tutorial 4.2 Code (WORK IN PROGRESS)
- Tutorial 4.3 Code (WORK IN PROGRESS)