Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning C++ Game Programming

You're reading from   Beginning C++ Game Programming Learn C++ from scratch by building fun games

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781835081747
Length 648 pages
Edition 3rd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Welcome to Beginning C++ Game Programming Third Edition! 2. Variables, Operators, and Decisions: Animating Sprites FREE CHAPTER 3. C++ Strings, SFML Time: Player Input and HUD 4. Loops, Arrays, Switch, Enumerations, and Functions: Implementing Game Mechanics 5. Collisions, Sound, and End Conditions: Making the Game Playable 6. Object-Oriented Programming – Starting the Pong Game 7. AABB Collision Detection and Physics – Finishing the Pong Game 8. SFML Views – Starting the Zombie Shooter Game 9. C++ References, Sprite Sheets, and Vertex Arrays 10. Pointers, the Standard Template Library, and Texture Management 11. Coding the TextureHolder Class and Building a Horde of Zombies 12. Collision Detection, Pickups, and Bullets 13. Layering Views and Implementing the HUD 14. Sound Effects, File I/O, and Finishing the Game 15. Run! 16. Sound, Game Logic, Inter-Object Communication, and the Player 17. Graphics, Cameras, Action 18. Coding the Platforms, Player Animations, and Controls 19. Building the Menu and Making It Rain 20. Fireballs and Spatialization 21. Parallax Backgrounds and Shaders 22. Other Books You May Enjoy
23. Index

Understanding screen and internal coordinates

Before we move on to the actual C++ coding, let’s talk a little about coordinates. All the images that we see on our monitors are made from pixels. Pixels are tiny dots of light that combine to make the images we see on the screen.

There are many different resolutions of a monitor but, as an example, consider that a typical monitor might have 1,920 pixels horizontally and 1,080 pixels vertically.

The pixels are numbered, starting from the top left of the screen. As you can see from the following diagram, our 1,920 x 1,080 example is numbered from 0 through to 1,919 on the horizontal (x) axis and 0 through 1,079 on the vertical (y) axis:

A square with a square in it

Description automatically generated

Figure 1.21: Screen and internal coordinates

A specific and exact screen location can therefore be identified by an x and y coordinate. We create our games by drawing the game objects such as the background, characters, bullets, and text to specific locations on the screen.

These locations are identified by the coordinates of the pixels. Take a look at the following hypothetical example of how we might draw at the approximately central coordinates of the screen. In the case of a 1,920 x 1080 screen, this would be at the 960, 540 position:

A cartoon character with a grid of lines

Description automatically generated with medium confidence

Figure 1.22: Drawing central coordinates

In addition to the screen coordinates, our game objects will each have their own similar coordinate system as well. Like the screen coordinate system, their internal or local coordinates start at 0,0 in the top-left corner.

In the previous image, we can see that 0,0 of the character is drawn at 960, 540 of the screen. A visual 2D game object, such as a character or perhaps a zombie, is called a Sprite. A sprite is typically made from an image file. All sprites have what is known as an origin.

If we draw a sprite to a specific location on the screen, it is the origin that will be located at this specific location. The 0,0 coordinates of the sprite are its origin. The following image demonstrates this:

A pixelated person with a beard and blue hat

Description automatically generated

Figure 1.23: Illustration of a sprite with its origin

Therefore, in the image showing the character drawn to the screen, although we drew the image at the central position (960, 540), it appears off to the right and down.

This is important to know as it will help us understand the coordinates we use to draw all the graphics.

Note that, in the real world, gamers have a huge variety of screen resolutions, and our games will need to work with as many of them as possible. In the third project, we will see how we can make our games dynamically adapt to almost any resolution. In this first project, we will need to assume that the screen resolution is 1,920 x 1,080 or higher.

Now, we can write our first piece of C++ code and see it in action.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime