Podcast
Questions and Answers
When using custom assets in place of those provided in the book's download bundle, what is crucial to maintain for compatibility?
When using custom assets in place of those provided in the book's download bundle, what is crucial to maintain for compatibility?
- The file size of the assets
- The resolution of the images
- The creation date of the assets
- The original filenames of the assets (correct)
In the initial project setup, what is the purpose of creating separate folders for 'graphics', 'sound', and 'fonts'?
In the initial project setup, what is the purpose of creating separate folders for 'graphics', 'sound', and 'fonts'?
- To enable version control
- To improve the game's performance
- To reduce the overall project size
- To organize and categorize project assets (correct)
Why is it important to visualize the assets, particularly the graphics, when adding them to the Timber!!! game project?
Why is it important to visualize the assets, particularly the graphics, when adding them to the Timber!!! game project?
- To ensure they are visually appealing
- To optimize their file size
- To understand how they will be used within the C++ code (correct)
- To verify their compatibility with different screen resolutions
What is the primary purpose of sound files in the Timber!!! project, and what file format are they?
What is the primary purpose of sound files in the Timber!!! project, and what file format are they?
Why is understanding screen resolution and internal coordinates important when developing games?
Why is understanding screen resolution and internal coordinates important when developing games?
In a coordinate system used for game development, where does the numbering of pixels typically begin?
In a coordinate system used for game development, where does the numbering of pixels typically begin?
What is a 'Sprite' in the context of 2D game development?
What is a 'Sprite' in the context of 2D game development?
What does the 'origin' of a sprite refer to?
What does the 'origin' of a sprite refer to?
What is the purpose of the //
symbols in C++ code?
What is the purpose of the //
symbols in C++ code?
What is the significance of the main()
function in a C++ program?
What is the significance of the main()
function in a C++ program?
What is the purpose of the int
keyword used before main()
in C++?
What is the purpose of the int
keyword used before main()
in C++?
What is the role of a semicolon (;
) at the end of a C++ statement?
What is the role of a semicolon (;
) at the end of a C++ statement?
What is the purpose of the return 0;
statement in the main()
function?
What is the purpose of the return 0;
statement in the main()
function?
In C++, what is the term for a section of code denoted by its indentation?
In C++, what is the term for a section of code denoted by its indentation?
What does the #include
directive do in C++?
What does the #include
directive do in C++?
What type of file is typically included using the #include
directive to access SFML functionalities?
What type of file is typically included using the #include
directive to access SFML functionalities?
When is the code from included files added to your project using the #include
directive?
When is the code from included files added to your project using the #include
directive?
What does OOP stand for in programming?
What does OOP stand for in programming?
In OOP, what is a class?
In OOP, what is a class?
In OOP, what is an object?
In OOP, what is an object?
Why is it useful to write code as classes in OOP?
Why is it useful to write code as classes in OOP?
What is a 'namespace' used for in C++?
What is a 'namespace' used for in C++?
What does the using namespace sf;
statement do?
What does the using namespace sf;
statement do?
What is the purpose of the SFML VideoMode
class?
What is the purpose of the SFML VideoMode
class?
What is the purpose of the SFML RenderWindow
class?
What is the purpose of the SFML RenderWindow
class?
Flashcards
Assets folder
Assets folder
A folder containing graphics, sound, and fonts for a project.
Sound files
Sound files
Files containing sound effects used in the game, often in .wav format, generated using BFXR.
Screen coordinates
Screen coordinates
A specific location on the screen identified by x and y coordinates.
Sprite
Sprite
Signup and view all the flashcards
Sprite origin
Sprite origin
Signup and view all the flashcards
Header file
Header file
Signup and view all the flashcards
#include directive
#include directive
Signup and view all the flashcards
OOP
OOP
Signup and view all the flashcards
Class
Class
Signup and view all the flashcards
Object
Object
Signup and view all the flashcards
Namespace
Namespace
Signup and view all the flashcards
SFML RenderWindow
SFML RenderWindow
Signup and view all the flashcards
Style::Fullscreen
Style::Fullscreen
Signup and view all the flashcards
Game loop
Game loop
Signup and view all the flashcards
While loop
While loop
Signup and view all the flashcards
Comments
Comments
Signup and view all the flashcards
Input, Update, Draw, Repeat
Input, Update, Draw, Repeat
Signup and view all the flashcards
Clearing the scene
Clearing the scene
Signup and view all the flashcards
Texture
Texture
Signup and view all the flashcards
Loading a texture
Loading a texture
Signup and view all the flashcards
Double buffering
Double buffering
Signup and view all the flashcards
Efficient GPU memory use
Efficient GPU memory use
Signup and view all the flashcards
Return value
Return value
Signup and view all the flashcards
while (window.isOpen())
while (window.isOpen())
Signup and view all the flashcards
SpriteObject.setPosition
SpriteObject.setPosition
Signup and view all the flashcards
Study Notes
- Instructions assume use of the book's download bundle assets, but personal assets can replace the provided assets if named the same.
- Project folder location is
D:\VS Projects\Timber
.
Adding Assets to the Project
- Three new folders, graphics, sound, and fonts, are created in the project folder.
- The contents of "Chapter 1/graphics" from the download bundle are copied into the
D:\VS Projects\Timber\graphics
folder. - The contents of "Chapter 1/sound" are copied into the
D:\VS Projects\Timber\sound
folder. - The Komika Poster font is downloaded from
http://www.1001freefonts.com/komika_poster.font
. - The contents of the downloaded ZIP file are extracted, and the
KOMIKAP_.ttf
file is added to theD:\VS Projects\Timber\fonts
folder.
Exploring the Assets
-
The graphics make up the Timber!!! game; this includes:
-
axe
-
background
-
bee
-
branch
-
cloud
-
log
-
player
-
rip
-
tree
-
Sound files are in
.wav
format. -
BFXR generated the sound effects.
-
chop.wav
: An axe chopping a tree -
death.wav
: A "losing" sound -
out_of_time.wav
: sound for losing by running out of time
Understanding Screen and Internal Coordinates
- Images on monitors consist of pixels.
- Gaming monitors can have a resolution of 1,920 horizontal pixels by 1,080 vertical pixels.
- Pixels are numbered from the top left of the screen, with horizontal axis 0-1,919 and vertical axis 0-1,079 in a 1,920 x 1,080 setup.
- Specific screen location identified with x and y coordinates.
- The central coordinate of a 1,920 x 1,080 screen is 960, 540.
- Game objects have their own coordinate system, with the origin at 0,0 in the top left-hand corner.
- A visual 2D game object is called a Sprite.
- Sprites have their origin
- If Sprite drawn at specific location, its origin will be located there
Getting Started with Coding the Game
- Open the Timber!!! project in Visual Studio.
- Locate the
Timber.cpp
file in the Source Files folder using Solution Explorer. - The
.cpp
file extension stands for C plus plus.
Making Code Clearer with Comments
- Code starting with
//
is a single-line comment that the compiler ignores. - Multi-line comments, or c-style comments can take up more than one line.
The Main Function
int
is a data type, representing an integer.main()
is the name of the code section, marked by curly braces{}
.- The curly braces' contents are part of
main
, called a function. - Every C++ program has a
main
function; the program's execution starts there. return 0;
returns execution to original program
Presentation and Syntax
return 0;
is indented to show it’s internal to themain
function.- A semicolon (
;
) marks the end of an instruction, called a statement. - Skipping a semicolon causes a syntax error, preventing compilation.
- Code sections indented together are called a block.
Returning Values From a Function
return 0;
moves program execution back to the code that started the function.- When
return 0;
executes, themain
function exits, ending the program. - The value after
return
is sent to the OS. - Code that starts a function "calls" the function, and the function "returns" the value.
int main()
tells the compiler that the returned value frommain
must be anint
.
Running the Game
- Run the game by clicking the Local Windows Debugger button or pressing F5.
- A black screen will appear because the program starts, executes
return 0;
, and exits.
Opening a Window Using SFML
- Add highlighted code to open a window using SFML.
- The Timber!!! game will eventually run in this window, which is 1,920 x 1,080 pixels and full screen.
#include <SFML/Graphics.hpp>
is the first new code in new code#include
adds the contents of another file, or header file before compiling.- Tells Visual Studio to include the Visual Studio
Graphics.hpp
file, giving access to features of SFML. using namespace sf;
comes after the#include
directiveusing namespace sf;
enables the omission of the sf:: prefixVideoMode vm(1920, 1080);
creates an object calledvm
from class calledVideoMode
, which set ups a resolution of1920
and1080
RenderWindow window(vm, "Timber!!!", Style::Fullscreen);
creates an object called window from the SFML-provided class calledRenderWindow
, and sets up fullscreen- The window is named Timber!!!
OOP, Classes, and Objects
- OOP stands for object-oriented programming.
- OOP structures code into a class to be reusable, maintainable, and secure.
- Objects are built from classes.
- The properties defined in the class, defines what the objects can do
Using Namespace SF
- When creating a class, it is done in a namespace.
- Its purpose is to distinguish classes from others.
- using
namespace sf;
enables omitting sf, making the code shorthand notation - Without it, there'd be over 100 instances of sf in the simple program
sf::VideoMode...
is the full way of using theVideoMode
class
SFML VideoMode and RenderWindow
- There are the following lines of code in in the main function
VideoMode vm(1920, 1080);
- This creates an object called vm from the class called VideoMode and sets up two internal values of 1920 and 1080 These values represent the resolution of the player's screen.
- There is the following next line of code
RenderWindow window(vm, "Timber!!!", Style::Fullscreen);
- A new object called window is created from the SFML-provided class called
RenderWindow
Style::FullScreen
makes window object fullscreen
Running the Game
- You will see a bigger black screen flash and then disappear
- Is the 1,920 x 1,080 fullscreen window.
- Add a code that will form the basic structure of every game in this book, known as the game loop.
The Main Game Loop
- Ways to stay in the program until the player wants to quit are needed.
- Should clearly mark out where the different parts of our code will go as the game progresses.
- Must provide a way for the player to exit when they are ready, otherwise the game will go on forever
- Add highlighted code to the existing code and then explain all of it
While Loops:
- There is a new section of code, as follows:
while (window.isOpen()) {
- This is a while loop; everything between the first curly bracket and last curly bracket executes over and over, potentially forever.
- It will continue looping until the window object is set to close
- the windows object
set to closed
is explained in Chapter 4 Loops, Arrays, Switches, Enumerations, and Functions – Implementing Game Mechanics - Once the window object is set to closed,the execution of the code will break out of the while loop and move on to the next statement
- New statement is, of course,
return 0;
,which ends the game.
C-Style Code Comments
- See what might look a bit like ASCII art just inside the while loop
Input, update, draw, repeat
- In every game, these are the phases the code will need, every game will need these phases in the code, even if the first project includes the simplest possible version of a game loop
- Get the player's input (if any).
- Update the scene based on things such as artificial intelligence, physics, or the player's input.
- Draw the current scene.
- Repeat these steps at a fast-enough rate to create a smooth, animated game world.
Detecting a Key Press
- Code is identifiable by the Handle the player's input text
- Contains this:
if (Keyboard::isKeyPressed (Keyboard::Escape))
- This code checks whether the Esc key is currently being pressed.
- The code uses the window object to close itself if it is.
- next time the while loop begins, it will see that the window object is closed and jump to the code immediately after the closing curly brace of the while loop and the game will exit.
Clearing and Drawing the Scene
- No code in the Update the scene section as of yet, so now is the time to move on to the Draw the scene section.
- the previous frame is currently getting rubbed out, using, using the following code:
window.clear();
window.display();
draws all the game objects to a hidden surface ready to be displayed.- When this display code flips from the previously displayed surface to the newly updated this is known as double buffering.
- Guarantees that the scene will be complete before it is flipped, preventing a graphical glitch known as tearing.
- object which all this drawing and clearing functionality is performed, comes from the SFML RenderWindow class.
Running the Game
- If you Run the game now, you will get a blank, full screen window that remains open until you press the Esc key.
- is a stage, we have an executing program that opens a window and loops around, waiting for the player to press the Esc key to exit. Now,
Drawing the game’s background
- Creates the first sprite, the game background
- Draw in between clearing the window and displaying/flipping it.
Preparing the Sprite Using a Texture
SFML RenderWindow
class let us create our window object- It took care of the functionality that our game’s window needs
- Will examine two more SFML classes that draw sprites to the screen
- 1 perhaps unsurprisingly = "
Sprite
" - Other class = "Texture". What is a Texture?
- Graphic stored in memory, on GPU.
What do objects from the
Sprite
class need? - Need objects from the
Texture
class to display themselves as an image Texture textureBackground;
is the first object from theSFML Texture
Class createdtextureBackground.loadFromFile("graphics/background.png");
uses thetextureBackground
object to load a graphic from the graphics folder intotextureBackground
- Path is relative to Visual Studio working directory
Sprite spriteBackground;
the Sprite objectSprite spriteBackground
from theSFML Sprite class
spriteBackground.setTexture (textureBackground);
associates the Texture object (backgroundTexture) with Sprite object (backgroundSprite)spriteBackground.setPosition(0,0);
positions the spriteBackground object in the window object at the 0,0 coordinatesbackground.png
in graphics folder is 1,920 by 1,080 > will fill entire screen
Memory
- Textures take up graphics memory, and this memory is a finite resource.Furthermore, the process of loading a graphic into the GPU’s memory is very slow
- It is useful to disassociate the actual texture (textureBackground) from any code that we will manipulate during the game loop
- Move
graphics
, we will do so using the sprite - The Texture class will sit happily on the GPU, just waiting for a Sprite to tell it where to show itself
Summary
- Textures take time to load on the GPU
- Textures can be accessed quickly once on the GPU
- Sprite objects are associated with a texture
- Manipulate position + orientation of sprite objects in the scene
- draw sprite object which displays texture associated with it
Double Buffering the Background Sprite
Need to draw sprite and it is associated texture in appropriate place in the game loop.
- Draw with
window.draw()
Handling Errors
- Errors will always occur in the project
- Find a way to word the problem / error so you can find an internet search query that solves the problem
- Configuration errors
- Probably during set up process of Visual Studio, SFML and the project itself
- Compile errors
- Most common error we will experience going forward.
- Especially semicolons on ends of lines and subtle changes in upper and lower classes/names of objects
- Link errors
- Caused by missing SFML d11 files and them not copied to project folder.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.