Podcast
Questions and Answers
In the context of the Timber!!! game project setup, what is the primary purpose of creating separate 'graphics', 'sound', and 'fonts' folders within the project directory?
In the context of the Timber!!! game project setup, what is the primary purpose of creating separate 'graphics', 'sound', and 'fonts' folders within the project directory?
- To reduce the overall size of the project folder.
- To organize project assets for easier management and access. (correct)
- To improve the game's loading speed by segmenting asset types.
- To enable automatic asset optimization by the Visual Studio IDE.
When an image is drawn at the coordinates (960, 540) on a 1920x1080 screen, what part of the image is located at those coordinates, assuming the image's origin is at (0,0)?
When an image is drawn at the coordinates (960, 540) on a 1920x1080 screen, what part of the image is located at those coordinates, assuming the image's origin is at (0,0)?
- The top-left corner of the image. (correct)
- The part of the image that does not render, as the sprite is out of bounds.
- The center of the image.
- The bottom-right corner of the image.
When drawing a sprite on the screen, what role does the sprite's origin play?
When drawing a sprite on the screen, what role does the sprite's origin play?
- It determines the point of rotation for the sprite.
- It controls the layering order of sprites on the screen.
- It specifies the color palette used for rendering the sprite.
- It defines the point that will be located at the sprite's specified coordinates. (correct)
What is the purpose of the //
symbols in C++ code?
What is the purpose of the //
symbols in C++ code?
What does the term 'syntax error' refer to in the context of C++ programming?
What does the term 'syntax error' refer to in the context of C++ programming?
What is the purpose of the return 0;
statement in the main()
function of a C++ program?
What is the purpose of the return 0;
statement in the main()
function of a C++ program?
What does the #include
directive do in C++?
What does the #include
directive do in C++?
What does OOP stand for in the context of programming?
What does OOP stand for in the context of programming?
What is the relationship between a class and an object in object-oriented programming?
What is the relationship between a class and an object in object-oriented programming?
In SFML, what is a Sprite
?
In SFML, what is a Sprite
?
What is a 'namespace' used for in C++?
What is a 'namespace' used for in C++?
In SFML, what is the purpose of the VideoMode
class?
In SFML, what is the purpose of the VideoMode
class?
What is the role of the RenderWindow
class in SFML?
What is the role of the RenderWindow
class in SFML?
What is a 'game loop', and what is its primary function?
What is a 'game loop', and what is its primary function?
What are the fundamental steps involved in a basic game loop?
What are the fundamental steps involved in a basic game loop?
What is the purpose of the window.clear()
function in SFML?
What is the purpose of the window.clear()
function in SFML?
What does 'double buffering' refer to in the context of graphics rendering?
What does 'double buffering' refer to in the context of graphics rendering?
In SFML, what role does the Texture
class play?
In SFML, what role does the Texture
class play?
What is the relationship between a Texture
and a Sprite
in SFML?
What is the relationship between a Texture
and a Sprite
in SFML?
Why is it beneficial to disassociate the Texture from Sprite in game development?
Why is it beneficial to disassociate the Texture from Sprite in game development?
What is the role of the modulus operator (%) when generating random numbers?
What is the role of the modulus operator (%) when generating random numbers?
What is the purpose of 'seeding' the random number generator?
What is the purpose of 'seeding' the random number generator?
What value is appropriate as a seed?
What value is appropriate as a seed?
What is the significance of 'frame rate' in game development, and how does it impact the player's experience?
What is the significance of 'frame rate' in game development, and how does it impact the player's experience?
What is the SFML Clock class used for?
What is the SFML Clock class used for?
What does the term 'delta time' refer to in game development, and how is it typically used?
What does the term 'delta time' refer to in game development, and how is it typically used?
How does using delta time contribute to frame rate independence?
How does using delta time contribute to frame rate independence?
What is C++ String concatenation, and how is it commonly achieved?
What is C++ String concatenation, and how is it commonly achieved?
What advantages does the sstream
class provide in C++ when working with strings?
What advantages does the sstream
class provide in C++ when working with strings?
Which of the following describes the process of implementing a Heads-Up Display (HUD)?
Which of the following describes the process of implementing a Heads-Up Display (HUD)?
If a Text sprite needs to be repositioned on the screen, what factor needs to be considered?
If a Text sprite needs to be repositioned on the screen, what factor needs to be considered?
With all of the Timber!!! assets loaded, which of the following is the proper draw order of the sprites?
With all of the Timber!!! assets loaded, which of the following is the proper draw order of the sprites?
Flashcards
What is a Sprite?
What is a Sprite?
A visual, 2D game object made from an image file.
What is a Sprite's origin?
What is a Sprite's origin?
The (0,0) coordinates of a sprite; its location anchor.
What does #include do?
What does #include do?
Tells Visual Studio to add another file's content before compiling.
What is OOP?
What is OOP?
Signup and view all the flashcards
What is a Class?
What is a Class?
Signup and view all the flashcards
What is an Object?
What is an Object?
Signup and view all the flashcards
What is a namespace?
What is a namespace?
Signup and view all the flashcards
What does 'using namespace sf;' do?
What does 'using namespace sf;' do?
Signup and view all the flashcards
What does VideoMode do?
What does VideoMode do?
Signup and view all the flashcards
What does RenderWindow do?
What does RenderWindow do?
Signup and view all the flashcards
What is Style::Fullscreen?
What is Style::Fullscreen?
Signup and view all the flashcards
What is the game loop?
What is the game loop?
Signup and view all the flashcards
What are the four phases of the game loop?
What are the four phases of the game loop?
Signup and view all the flashcards
What is window.clear()?
What is window.clear()?
Signup and view all the flashcards
window.display()
window.display()
Signup and view all the flashcards
What is Tearing?
What is Tearing?
Signup and view all the flashcards
What is Texture Memory?
What is Texture Memory?
Signup and view all the flashcards
What is casting?
What is casting?
Signup and view all the flashcards
Why measure Time for the bee and clouds?
Why measure Time for the bee and clouds?
Signup and view all the flashcards
What does dt mean?
What does dt mean?
Signup and view all the flashcards
Variable Scope
Variable Scope
Signup and view all the flashcards
What is the sstream class?
What is the sstream class?
Signup and view all the flashcards
Time Bar
Time Bar
Signup and view all the flashcards
timeBar
timeBar
Signup and view all the flashcards
Study Notes
Adding Assets to the Project
- It's time to integrate them into the project after deciding on the assets.
- It's assumed the assets from the book's download bundle will be used
- Use same file name when replacing default sound or graphic file with your own.
- The steps for adding assets include:
- Go to the project folder: D : \VS Projects\Timber
- Create three new folders: graphics, sound, and fonts
- Copy the contents of Chapter 1/graphics into the graphics folder
- Copy the contents of Chapter 1/sound into the sound folder
- Download the Komika Poster font from http://www.1001freefonts.com/komika_poster.font
- Extract the contents of the zipped download and add the KOMIKAP_.ttf file to the fonts folder
Exploring the Assets
- Graphical assets constitute scene sections for the Timber!!! game.
Screen and Internal Coordinates
- Images viewed on monitors consist of pixels, tiny light dots
- Gaming monitors are often 1,920 pixels horizontally and 1,080 pixels vertically
- Pixels are numbered from the top left of the screen
- In a 1,920 x 1,080 example, pixels are numbered from 0 to 1,919 on the x axis and 0 to 1,079 on the y axis
- Specific screen locations are identified using x and y coordinates when drawing characters, backgrounds, bullets, and text
- A 1,920 x 1,080 screen's approximate center is at position 960, 540
- Game objects have their own coordinate systems, beginning at 0,0 in the top left-hand corner
- Location 0,0 of a character is drawn to position 960, 540 on the screen
- Visual, 2D game objects like characters or zombies are called Sprites, typically made from an image file with an origin
C++ Code and Comments
- The subsequent line observed is int main().
- int represents a data type.
- C++ supports multiple types of data; an int signifies a whole number or integer
- main () is a section of code marked by opening ({) and closing ({) curly braces, referred to as a function
- Every C++ program contains a main function where the running of the program begins.
- The opening curly brace of the function main always begins execution.
- The return keyword, alone or followed by a value, instructs the program to go back to the code that started the function and has importance.
- The running of the program jumps back to the code that initiated the function
- The operating system initiates the main function
- Return 0 indicates that the main function exits and the program ends
- Adding 0 after the return keyword also sends that value to the operating system
- The code responsible for initiating a function is said to "call" the function, while the function "returns" the value
Opening A Window Using SFML
- The code opens a 1,920-pixel by 1,080-pixel full-screen borderless SFML window
- The #include directive directs Visual Studio to add the contents of another file before compiling.
- Some code not written becomes part of the program
- "Preprocessing" refers to adding code from other files
- The file extension .hpp indicates being called a header file
- The #include statement enables preprocessor inclusion of content in the folder named SFML
Understanding Objects
- The code creates a vm object from the videoMode class setting up two internal values, 1920 and 1080
- These values represent the resolution of the player's screen.
- The code creates a window object, which is a new object named window provided from the RenderWindow
- Furthermore, are setting up some values inside the window object.
- A class is like an architectural blueprint, which allows creation of usable objects like houses
The Main Game Loop
- A method to keep the program running until quitting is desired is needed, alongside marking different code sections.
- Add the following highlighted code to the existing code and then going through it:
int main(){
// Create a video mode object
VideoMode vm(1920, 1080);
// Create and open a window for the game
RenderWindow window(vm,"Timber!!!", Style::Fullscreen);
while(window.isOpen())
{
}
}
Loops
- Everything between the opening ({) and closing (}) brackets of the while loop will continue to execute, over and over, potentially forever.
- Once the window object is set to close, the execution of the code will break out of the while loop and move on to the next statement
Input, Update, Draw and Repeat
- Although this first project uses the simplest version of a game loop, every
game needs these phases in the code.
- 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
- The following code checks whether the Esc is being pressed:
if (Keyboard::isKeyPressed (Keyboard::Escape))
{
window.close();
}
Preparing the Sprite
- An object from the Sprite class requires an object from the Texture class to display itself as an image.
- The following highlighted code is to be added:
int main(){
// Create a video mode object
VideoMode vm(1920, 1080);
//Create and open a window for the game
RenderWindow window(vm,"Timber!!!", Style::Fullscreen);
// Create a texture to hold a graphic on the GPU
Texture textureBackground;
// Load a graphic into the texture
textureBackground.loadFromFile("graphics/background.png");
// Create a sprite
Sprite spriteBackground;
// Attach the texture to the sprite
priteBackground.setTexture (textureBackground);
// Set the spriteBackground to cover the screen
spriteBackground.setPosition(0,0);
while(window.isOpen())
{
}
}
- In the highlighted code, the object called textureBackground from the SMFL Texture class is first created.
- The graphics is loaded from the folder using the textureBackground into textureBackground
Key Notes
- Textures load slow to the GPU.
- Textures are fast access once they are on the GPU.
- Texture is associated with Sprite object.
- Update the scene with position and orientation of Sprite objects
- Draw the Sprite object, which displays the Texture object in the Draw the scene section.
Game with Sprite
- Finally, the new Sprite object (spriteBackground) should be seen after using the double buffering system with window object.
Adding More Sprites
- You can add a tree, bee, and three clouds as sprites to the Timber!!! game.
Preparing, Drawing, and Moving Bee and Clouds
- Bee are created in the same way we created a background and a tree.
- For determining if the bee is active, there is a bool variable.
- There are three bool variables for determining whether each cloud is active and three float variables to hold the speed for each cloud.
- Apart the minor texture issue, the code we have just added is nothing new compared to the bee.
Random Numbers
- Two functions, alongside syntax and steps, are needed to create a random number
- Seeding the random number generator is required to make it not constantly return the same value
- Seed the random number generator with the time
// Seed the random number generator with the time
srand((int)time(0));
- The preceding code gets the time from the PC using the time function, that is, time(0)
- The call to the time function is enclosed as the value to be sent to the srand fuction
- With a conversion cast, the long code does the flowing conversion from one type to another:
- Gets the time using time
- Converts it into an int
- Sends the value to srand, which seeds the random number generator
Timing
- Consistently make game runs smoothly because C++ and SFML are exceptionally fast to draw sprites
Frame Rate Solution
- Determine and use the frame rate from controlling games
// How fast is each cloud?
float cloud1Speed
= 0;
float cloud2Speed = 0;
// Variables to control time itself
Clock clock;
while(window.isOpen())
{
- Before we can move the bee and the clouds, we need to consider timing.
Time Control
- clock type and name is delcared. with first class name and the object name the the clock type.
- Now, in the update section of the game, add the following new value:
time dt = clock.restart();
- Then, add following to bee or cloud to give consistent time:
spriteBee.setPosition(spriteBee.getPosition().x - (beeSpeed dt.asSeconds())}, spriteBee.getPosition().y)
Pausing the Code
- To pause the code:
// Start the game
if (Keyboard::isKeyPressed (Keyboard:: Return)){
pause = false;
}
Time and the HUD
- We can load, assign, and initialize SFML objects from String values.
- Implemenation is to the heads up display (HUD)
- sstream class provides useful implementation is combining Strings and other variable types
- When a different font is chosen from the existing, modification will be needed for existing code.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.