Difference between revisions of "Beginner C++ Game Programming Tutorial 4"

From Chilipedia
Jump to: navigation, search
(Homework)
(Download Links)
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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 how to add member variables to objects/<code>class</code>es, we learn about .h (header) and .cpp files, we learn about the short harsh lives of variables (scope), and I make a joke in very bad taste. All par for the course!
+
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/<code>class</code>es, about .h (header) and .cpp (source) files, and about the short harsh lives of local variables (scope).
  
 
== Concepts Taught ==
 
== Concepts Taught ==
* Animation and double buffering
+
* Animation and the game loop
* Difference between .h and .cpp files
+
* Difference between a <code>class</code> and an object
+
* Adding member variables to a <code>class</code>
+
 
* Variable scope
 
* 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 <code>class</code>
 +
* Separating logic and drawing code
  
 
== Video Timestamp Index ==
 
== Video Timestamp Index ==
* Intro [https://youtu.be/UrK_KPYej3w 0:00]
+
* [https://www.youtube.com/watch?v=HYiPoRk5ngY Part 1]
* Keyboard Ghosting [https://youtu.be/UrK_KPYej3w?t=0m29s 0:29]
+
** Intro [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=0m0s 0:00]
* Animation and double buffering [https://youtu.be/UrK_KPYej3w?t=1m15s 1:15]
+
** Keyboard ghosting: Keypress combination limits [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=1m47s 1:47]
* Animation sequence in the Framework [https://youtu.be/UrK_KPYej3w?t=4m54s 4:54]
+
** The problem of reticle movement from Tutorial 3 [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=2m21s 2:21]
* Variable scope (lifetime) [https://youtu.be/UrK_KPYej3w?t=7m28s 7:28]
+
** The game loop [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=4m29s 4:29]
* Difference between object and <code>class</code> [https://youtu.be/UrK_KPYej3w?t=9m15s 9:15]
+
** Stepping through the game loop using the debugger [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=7m21s 7:21]
* Difference between .h and .cpp source files [https://youtu.be/UrK_KPYej3w?t=10m25s 10:25]
+
** Scope of a variable: A problem of persisting reticle position across loop cycles [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=12m10s 12:10]
* Adding member variable to a <code>class</code> [https://youtu.be/UrK_KPYej3w?t=11m15s 11:15]
+
** Nested scopes and variables of the same name [https://www.youtube.com/watch?v=HYiPoRk5ngY&t=15m12s 15:12]
* Variable scope name hiding [https://youtu.be/UrK_KPYej3w?t=11m42s 11:42]
+
* [https://www.youtube.com/watch?v=ry_zHxnPn8A Part 2]
* Member access for members of same <code>class</code> [https://youtu.be/UrK_KPYej3w?t=13m12s 13:12]
+
** Intro [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=0m0s 0:00]
* Homework [https://youtu.be/UrK_KPYej3w?t=13m54s 13:54]
+
** Objects [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=0m24s 0:24]
 +
** Classes [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=2m05s 2:05]
 +
** How classes define objects: data members and member functions [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=5m19s 5:19]
 +
** Header files (.h) and source files (.cpp) [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=8m59s 8:59]
 +
** Persisting reticle position across loop cycles: The solution [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=11m44s 11:44]
 +
** Quick note: Drawing pixels outside the window bounds [https://www.youtube.com/watch?v=ry_zHxnPn8A&t=16m25s 16:25]
 +
* [https://www.youtube.com/watch?v=zG-v7-1FN-U Part 3]
 +
** Intro [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=0m0s 0:00]
 +
** Old homework tasks [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=1m11s 1:11]
 +
** Separate drawing code from game logic [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=2m13s 2:13]
 +
** Control reticle velocity instead of movement [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=9m31s 9:31]
 +
** Prevent reticle from ballin' outta control [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=12m18s 12:18]
 +
** Summing up Tutorial 4 [https://www.youtube.com/watch?v=zG-v7-1FN-U&t=17m5s 17:05]
 +
 
 +
== 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?<br />
 +
<div class="mw-collapsed mw-collapsible" style="width:450px">
 +
A: The curly braces <code>{}</code>.
 +
</div><br />
 +
2. What are the two types of members that define a class?<br />
 +
<div class="mw-collapsed mw-collapsible" style="width:450px">
 +
A: Data members and member functions.
 +
</div><br />
 +
3. What is the relationship between classes and objects.<br />
 +
<div class="mw-collapsed mw-collapsible" style="width:450px">
 +
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.
 +
</div><br />
 +
4. How do we achieve the movement of the reticle.<br />
 +
<div class="mw-collapsed mw-collapsible" style="width:450px">
 +
A: We achieve movement of the reticle by making the variables <code>x</code> and <code>y</code> member variables of <code>class Game</code>. 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 <code>ComposeFrame()</code> function, i.e. after each frame).
 +
</div><br />
 +
5. How are the members <code>gfx</code> and <code>wnd</code> in <code>class Game</code> different?<br />
 +
<div class="mw-collapsed mw-collapsible" style="width:450px">
 +
A: <code>Game::gfx</code> is an object which is embedded directly into objects of the <code>Game</code> class, whereas <code>Game::wnd</code> is a reference to a <code>MainWindow</code> object that exists outside of and is independent of the <code>Game</code> object.
 +
</div><br />
 +
 
 +
== 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 <code>gb</code> 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. ;)
  
== Homework ==
+
== Notes ==
This lesson has three homework problems. Feel free to attempt any or all of them.
+
* 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 <code>Release</code> and not <code>Debug</code>.
# Separate the game logic code from the drawing code. The game logic code should go in the <code>Game::UpdateModel()</code> member function, and the drawing code should remain in <code>Game::ComposeFrame()</code>.
+
* 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.
# Change the behavior of the reticle so that it moves at a velocity that is controlled with the arrow keys. This means that the arrow keys don't control direction of movement, but that they control the acceleration.
+
# Make it so that the acceleration of the reticle is more controllable. How this is accomplished is left up to you. Full marks for using only concepts introduced so far ;)
+
  
The solutions are given in [https://youtu.be/4vc8riKxPUg this video].
+
== Download Links ==
 +
* [https://planetchili.net/downloads/apworks/4/Beginner%20Tutorial%204.2%20Code.zip Tutorial 4.2 Code]
 +
* [https://planetchili.net/downloads/apworks/4/Beginner%20Tutorial%204.3%20Code.zip Tutorial 4.3 Code]
  
 
== See also ==
 
== See also ==
 
* [[Beginner C++ Game Programming Tutorial 5|Next in series (Tutorial 5)]]
 
* [[Beginner C++ Game Programming Tutorial 5|Next in series (Tutorial 5)]]
 
* [[Beginner C++ Game Programming Series]]
 
* [[Beginner C++ Game Programming Series]]

Latest revision as of 12:53, 12 October 2019

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/classes, about .h (header) and .cpp (source) files, and about the short harsh lives of local 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 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
    • Intro 0:00
    • Objects 0:24
    • Classes 2:05
    • How classes define objects: data members and member functions 5:19
    • Header files (.h) and source files (.cpp) 8:59
    • Persisting reticle position across loop cycles: The solution 11:44
    • Quick note: Drawing pixels outside the window bounds 16:25
  • Part 3
    • Intro 0:00
    • Old homework tasks 1:11
    • Separate drawing code from game logic 2:13
    • Control reticle velocity instead of movement 9:31
    • Prevent reticle from ballin' outta control 12:18
    • Summing up Tutorial 4 17:05

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.


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 not Debug.
  • 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

See also