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

From Chilipedia
Jump to: navigation, search
(Bonus Questions)
(Bonus Questions)
Line 56: Line 56:
 
1. What symbols are a common indicator of the scope of a local variable?<br />
 
1. What symbols are a common indicator of the scope of a local variable?<br />
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
The curly braces <code>{}</code>.
+
A: The curly braces <code>{}</code>.
 
</div><br />
 
</div><br />
 
2. What are the two types of members that define a class?<br />
 
2. What are the two types of members that define a class?<br />
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
Data members and member functions.
+
A: Data members and member functions.
 
</div><br />
 
</div><br />
 
3. What is the relationship between classes and objects.<br />
 
3. What is the relationship between classes and objects.<br />
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
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.
+
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 />
 
</div><br />
 
4. How do we achieve the movement of the reticle.<br />
 
4. How do we achieve the movement of the reticle.<br />
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
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).
+
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 />
 
</div><br />
 
5. How are the members <code>gfx</code> and <code>wnd</code> in class Game different?<br />
 
5. How are the members <code>gfx</code> and <code>wnd</code> in class Game different?<br />
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
 
<div class="mw-collapsed mw-collapsible" style="width:450px">
<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.
+
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.
 
</div><br />
 
</div><br />
  

Revision as of 19:36, 11 June 2017

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/classes, 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!

Notes!

This video has a high dropout rate. First of all, at around 12:22 ~ 12:30 I delete two lines (lines 45 & 46 in Game.cpp), but I don't show it in the video. Make sure you delete them at around that time. If you're having trouble with the concepts of this video, or getting the code or the homework done, don't just give the fuck up! Visit us on the forum or the Discord and get some help! Or at least leave a comment on the YouTube video.

From a YouTube comment:
I also had a hard time understanding how setting the variables in game.h causes the reticle to have different behavior compared to setting them in the local scope. I think its because when the local scope is looped game.h allows the program to remember where the reticle was in the previous iteration, but leaving it in game.cpp forces it to be reset after each loop. Maybe that could be made clearer somehow, because it took me a while to get what was going on.

After watching the video 3 times I fully understood it though.

When the x and y variables are declared in the ComposeFrame() function, they are getting created and destroyed every frame, and thus there is no continuity of the values between frames (they are being reset every time). When they are declared in Game.h, in the class declaration of Game, they become part of the game object and thus they remain alive across function calls and retain their values from one call of ComposeFrame() to the next.

Again, if this is too complicated you should ask for help on the forum or the Discord.

Concepts Taught

  • Animation and double buffering
  • Difference between .h and .cpp files
  • Difference between a class and an object
  • Adding member variables to a class
  • Variable scope

Video Timestamp Index

  • Intro 0:00
  • Keyboard Ghosting 0:29
  • Animation and double buffering 1:15
  • Animation sequence in the Framework 4:54
  • Variable scope (lifetime) 7:28
  • Difference between object and class 9:15
  • Difference between .h and .cpp source files 10:25
  • Adding member variable to a class 11:15
  • Variable scope name hiding 11:42
  • Member access for members of same class 13:12
  • Homework 13:54

Homework

This lesson has three homework problems. Feel free to attempt any or all of them. If you don't have any idea about how to do any of them, don't worry about it! Just check out the solution video below and Chili will hook you up.

  1. Separate the game logic code from the drawing code. The game logic code should go in the Game::UpdateModel() member function, and the drawing code should remain in Game::ComposeFrame().
  2. 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.
  3. 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 this video.

Solution Errata

Read the below (click Expand) only after attempting the problem yourself.

In the solution (3:32), Chili declares (in Game) a bool variable controlIsPressed to signal that the shape should be changed. First of all, the key being pressed is the SHIFT key, so this is confusing.

Secondly, a better name for this variable would have been isBoxMode, since the drawing code doesn't really care what key was actually pressed, all it wants to know is what shape it should be drawing. This doesn't affect the behavior of the program in any way, but it makes it more logically coherent.

Bonus Questions

This section is a work in progress. Ignore. 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.


See also