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

From Chilipedia
Jump to: navigation, search
(Bonus Questions)
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 object, how to add member variables to objects/<code>class</code>es, about .h (header) and .cpp files, and about the short harsh lives of variables (scope).
 
+
== 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: <br/>
+
<small>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.</small>
+
 
+
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 [[Planet Chili Forums|forum]] or the [http://www.planetchili.net Discord].
+
  
 
== Concepts Taught ==
 
== Concepts Taught ==
* Animation and double buffering
+
* 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
 
* Difference between .h and .cpp files
* Difference between a <code>class</code> and an object
+
* Adding data members to a <code>class</code>
* Adding member variables to a <code>class</code>
+
* Separating logic and drawing code
* Variable scope
+
 
+
== Video Timestamp Index ==
+
* Intro [https://youtu.be/UrK_KPYej3w 0:00]
+
* Keyboard Ghosting [https://youtu.be/UrK_KPYej3w?t=0m29s 0:29]
+
* Animation and double buffering [https://youtu.be/UrK_KPYej3w?t=1m15s 1:15]
+
* Animation sequence in the Framework [https://youtu.be/UrK_KPYej3w?t=4m54s 4:54]
+
* Variable scope (lifetime) [https://youtu.be/UrK_KPYej3w?t=7m28s 7:28]
+
* Difference between object and <code>class</code> [https://youtu.be/UrK_KPYej3w?t=9m15s 9:15]
+
* Difference between .h and .cpp source files [https://youtu.be/UrK_KPYej3w?t=10m25s 10:25]
+
* Adding member variable to a <code>class</code> [https://youtu.be/UrK_KPYej3w?t=11m15s 11:15]
+
* Variable scope name hiding [https://youtu.be/UrK_KPYej3w?t=11m42s 11:42]
+
* Member access for members of same <code>class</code> [https://youtu.be/UrK_KPYej3w?t=13m12s 13:12]
+
* Homework [https://youtu.be/UrK_KPYej3w?t=13m54s 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.
+
# 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>.
+
# 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].
+
 
+
=== Solution Errata ===
+
Read the below (click Expand) only after attempting the problem yourself.
+
  
<div class="mw-collapsed mw-collapsible" style="width:450px">
+
== Video Index ==
In the solution ([https://youtu.be/4vc8riKxPUg?t=3m32s 3:32]), Chili declares (in <code>Game</code>) a <code>bool</code> variable <code>controlIsPressed</code> to signal that the shape should be changed. First of all, the key being pressed is the SHIFT key, so this is confusing.
+
* [https://www.youtube.com/watch?v=HYiPoRk5ngY Part 1]
 +
* [https://www.youtube.com/watch?v=ry_zHxnPn8A Part 2]
 +
* [https://www.youtube.com/watch?v=zG-v7-1FN-U Part 3]
  
Secondly, a better name for this variable would have been <code>isBoxMode</code>, 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 Advice Video ==
</div>
+
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.
  
== Bonus Questions ==
+
== Homework 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.
 
This lesson's homework is to answer the following questions. When you think you have the answer, click "expand" to reveal the correct answer.
  

Revision as of 22:39, 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 about classes and object, how to add member variables to objects/classes, 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.


See also