Beginning C++ Game Programming
eBook - ePub

Beginning C++ Game Programming

Learn to program with C++ by building fun games, 2nd Edition

John Horton

Share book
  1. 746 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Beginning C++ Game Programming

Learn to program with C++ by building fun games, 2nd Edition

John Horton

Book details
Book preview
Table of contents
Citations

About This Book

Get to grips with programming techniques and game development using C++ libraries and Visual Studio 2019

Key Features

  • Learn game development and C++ with a fun, example-driven approach
  • Build clones of popular games such as Timberman, Zombie Survival Shooter, a co-op puzzle platformer, and Space Invaders
  • Discover tips to expand your finished games by thinking critically, technically, and creatively

Book Description

The second edition of Beginning C++ Game Programming is updated and improved to include the latest features of Visual Studio 2019, SFML, and modern C++ programming techniques. With this book, you'll get a fun introduction to game programming by building five fully playable games of increasing complexity. You'll learn to build clones of popular games such as Timberman, Pong, a Zombie survival shooter, a coop puzzle platformer and Space Invaders.

The book starts by covering the basics of programming. You'll study key C++ topics, such as object-oriented programming (OOP) and C++ pointers, and get acquainted with the Standard Template Library (STL). The book helps you learn about collision detection techniques and game physics by building a Pong game. As you build games, you'll also learn exciting game programming concepts such as particle effects, directional sound (spatialization), OpenGL programmable shaders, spawning objects, and much more. Finally, you'll explore game design patterns to enhance your C++ game programming skills.

By the end of the book, you'll have gained the knowledge you need to build your own games with exciting features from scratch

What you will learn

  • Set up your game development project in Visual Studio 2019 and explore C++ libraries such as SFML
  • Explore C++ OOP by building a Pong game
  • Understand core game concepts such as game animation, game physics, collision detection, scorekeeping, and game sound
  • Use classes, inheritance, and references to spawn and control thousands of enemies and shoot rapid-fire machine guns
  • Add advanced features to your game using pointers, references, and the STL
  • Scale and reuse your game code by learning modern game programming design patterns

Who this book is for

This book is perfect for you if you have no C++ programming knowledge, you need a beginner-level refresher course, or you want to learn how to build games or just use games as an engaging way to learn C++. Whether you aspire to publish a game (perhaps on Steam) or just want to impress friends with your creations, you'll find this book useful.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Beginning C++ Game Programming an online PDF/ePUB?
Yes, you can access Beginning C++ Game Programming by John Horton in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781838647650
Edition
2

Chapter 1: C++, SFML, Visual Studio, and Starting the First Game

Welcome to Beginning C++ Game Programming. I will not waste any time in getting you started on your journey to writing great games for the PC using C++ and the OpenGL powered SFML.
This is quite a hefty first chapter, but we will learn absolutely everything we need so that we have the first part of our first game up and running. Here is what we will do in this chapter:
  • Find out about the games we will build
  • Meet C++
  • Find out about Microsoft Visual C++
  • Explore SFML and its relationship with C++
  • Setting up the development environment
  • Plan and prepare for the first game project, Timber!!!
  • Write the first C++ code of this book and make a runnable game that draws a background

The games we will build

This journey will be smooth as we will learn about the fundamentals of the super-fast C++ language one step at a time, and then put this new knowledge to use by adding cool features to the five games we are going to build.
The following are our five projects for this book.

Timber!!!

The first game is an addictive, fast-paced clone of the hugely successful Timberman, which can be found at http://store.steampowered.com/app/398710/. Our game, Timber!!!, will introduce us to all the basics of C++ while we build a genuinely playable game. Here is what our version of the game will look like when we are done and we have added a few last-minute enhancements:

Pong

Pong was one of the first video games to be made, and you can find out about its history here: https://en.wikipedia.org/wiki/Pong. It is an excellent example of how the basics of game object animation and dynamic collision detection work. We will build this simple retro game to explore the concept of classes and object-oriented programming. The player will use the bat at the bottom of the screen and hit the ball back to the top of the screen:

Zombie Arena

Next, we will build a frantic, zombie survival shooter, not unlike the Steam hit Over 9,000 Zombies!, which you can find out more about at http://store.steampowered.com/app/273500/. The player will have a machine gun and must fight off ever-growing waves of zombies. All this will take place in a randomly generated, scrolling world. To achieve this, we will learn about how object-oriented programming allows us to have a large code base (lots of code) that is easy to write and maintain. Expect exciting features such as hundreds of enemies, rapid-fire weaponry, pickups, and a character that can be ā€œleveled upā€ after each wave:

Thomas was late

The fourth game will be a stylish and challenging single-player and co-op puzzle platformer. It is based on the very popular game Thomas was Alone (http://store.steampowered.com/app/220780/). Expect to learn about cool topics such as particle effects, OpenGL Shaders, and split-screen cooperative multiplayer:
Tip
If you want to play any of the games now, you can do so from the download bundle in the Runnable Games folder. Just double-click on the appropriate .exe file. Note that, in this folder, you can run either the completed games or any game in its partially completed state from any chapter.

Space Invaders ++

The final game will be a Space Invaders clone. In some ways, the game itself is not what is important about this project. The project will be used to learn about game programming patterns. As will become abundantly clear as this book progresses, our code keeps getting longer and more complicated. Each project will introduce one or more techniques for coping with this, but the complexity and length of our code will keep coming back to challenge us, despite these techniques.
The Space Invaders project (called Space Invaders ++) will show us ways in which we can radically reorganize our game code also that we can take control of and properly manage our code once and for all. This will leave you with all the knowledge you need to plan and build deep, complex, and innovative games, without ending up in a tangle of code.
The game will also introduce concepts such as screens, input handlers, and entity-component systems. It will also allow us to learn how to let the player use a gamepad instead of the keyboard and introduce the C++ concepts of smart pointers, casts, assertions, breakpoint debugging, and teach us the most important lesson from the whole book: how to build your own unique games:
Letā€™s get started by introducing C++, Visual Studio, and SFML!

Meet C++

Now that we know what games we will be building, letā€™s get started by introducing C++, Visual Studio, and SFML. One question you might have is, why use the C++ language at all? C++ is fast ā€“ very fast. What makes this true is the fact that the code that we write is directly translated into machine-executable instructions. These instructions are what make the game. The executable game is contained within a .exe file, which the player can simply double-click to run.
There are a few steps in the process of changing our code into an executable file. First, the preprocessor looks to see if any other code needs to be included within our own code and adds it. Next, all the code is compiled into object files by the compiler program. Finally, a third program, called the linker, joins all the object files into the executable file for our game.
In addition, C++ is well established at the same time as being extremely up to date. C++ is an object-oriented programming (OOP) language, which means we can write and organize our code using well-tested conventions that make our games efficient and manageable. The benefits as well as the necessity of this will reveal themselves as we progress through this book.
Most of this other code that I referred to, as you might be able to guess, is SFML, and we will find out more about SFML in just a minute. The preprocessor, compiler, and linker programs I have just mentioned are all part of the Visual Studio integrated development environment (IDE).

Microsoft Visual Studio

Visual Studio hides away the complexity of preprocessing, compiling, and linking. It wraps it all up into the press of a button. In addition to this, it provides a slick user interface for us to type our code into and manage what will become a large selection of code files and other project assets as well.
While there are advanced versions of Visual Studio that cost hundreds of dollars, we will be able to build all five of our games in the free ā€œExpress 2019 for Communityā€ version. This is the latest free version of Visual Studio.

SFML

SFML is the Simple Fast Media Library. It is not the only C++ library for games and multimedia. It is possible to make an argument to use other libraries, but SFML seems to come through for me every time. Firstly, it is written using object-oriented C++. The benefits of object-oriented C++ are numerous, and you will experience them as you pr...

Table of contents