C++ Game Development Cookbook - Druhin Mukherjee - E-Book

C++ Game Development Cookbook E-Book

Druhin Mukherjee

0,0
32,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

C++ is one of the preferred languages for game development as it supports a variety of coding styles that provides low-level access to the system. C++ is still used as a preferred game programming language by many as it gives game programmers control of the entire architecture, including memory patterns and usage. However, there is little information available on how to harness the advanced features of C++ to build robust games.
This book will teach you techniques to develop logic and game code using C++. The primary goal of this book is to teach you to create high-quality games using C++ game programming scripts and techniques, regardless of the library or game engine you use. It will show you how to make use of the object-oriented capabilities of C++ so you can write well-structured and powerful games of any genre. The book also explores important areas such as physics programming and audio programming, and gives you other useful tips and tricks to improve your code.
By the end of this book, you will be competent in game programming using C++, and will be able to develop your own games in C++.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 323

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

C++ Game Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why Subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Game Development Basics
Introduction
Installing an IDE on Windows
Getting ready
How to do it…
How it works…
Choosing the right source control tool
Getting ready
How to do it…
How it works…
Using call stacks for memory storage
Getting ready
How to do it...
How it works…
Using recursions cautiously
Getting ready
How to do it…
How it works…
There's more…
Using pointers to store memory addresses
Getting ready
How to do it…
How it works…
There's more…
Casting between different datatypes
Getting ready
How to do it…
How it works…
Managing memory more effectively using dynamic allocation
Getting ready
How to do it…
How it works…
There's more…
Understanding the error messages
Using bitwise operations for advanced checks and optimization
Getting ready
How to do it…
How it works…
2. Object-Oriented Approach and Design in Games
Introduction
Using classes for data encapsulation and abstraction
Getting ready
How to do it…
How it works…
Using polymorphism to reuse code
Getting ready
How to do it…
How it works…
Using copy constructors
Getting ready
How to do it…
How it works…
Use operator overloading to reuse operators
Getting ready
How to do it…
How it works…
Use function overloading to reuse functions
Getting ready
How to do it…
How it works…
Using files for input and output
Getting ready
How to do it…
How it works…
Creating your first simple game
Getting ready
How to do it…
How it works…
Templates – when to use them
Getting ready
How to do it…
How it works…
3. Data Structures in Game Development
Introduction
Using more advanced data structures
Getting ready
How to do it…
How it works…
Using linked lists to store data
Getting ready
How to do it…
How it works…
Using stacks to store data
Getting ready
How to do it…
How it works…
Using queues to store data
Getting ready
How to do it…
How it works…
Using trees to store data
Getting ready
How to do it…
How it works…
Using graphs to store data
Getting ready
How to do it…
How it works…
Using STL lists to store data
Getting ready
How to do it…
How it works…
Using STL maps to store data
Getting ready
How to do it…
How it works…
Using STL hash tables to store data
Getting ready
How to do it…
How it works…
4. Algorithms for Game Development
Introduction
Using sorting techniques to arrange items
Getting ready
How to do it…
How it works…
Using searching techniques to look for an item
Getting ready
How to do it…
How it works…
Finding the complexity of an algorithm
Getting ready
How to do it…
How it works…
Finding the endian-ness of a device
Getting ready
How to do it…
How it works…
Using dynamic programming to break down a complex problem
Getting ready
How to do it…
How it works…
Using greedy algorithms to solve problems
Getting ready
How to do it…
How it works…
Using divide and conquer algorithms to solve problems
Getting ready
How to do it…
How it works…
5. Event-Driven Programming – Making Your First 2D Game
Introduction
Starting to make a Windows game
Getting ready
How to do it…
How it works…
Using Windows classes and handles
Getting ready
How to do it…
How it works…
Creating your first window
Getting ready
How to do it…
How it works…
Adding keyboard and mouse controls with text output
Getting ready
How to do it…
How it works…
Using Windows resources with GDI
Getting ready
How to do it…
How it works…
Using dialogs and controls
Getting ready
How to do it…
How it works…
Using sprites
Getting ready
How to do it…
How it works…
Using animated sprites
Getting ready
How to do it…
How it works…
6. Design Patterns for Game Development
Introduction
Using the singleton design pattern
Getting ready
How to do it…
How it works…
Using the factory method
Getting ready
How to do it…
How it works…
Using the abstract factory method
Getting ready
How to do it…
How it works…
Using the observer pattern
Getting ready
How to do it…
How it works…
Using the flyweight pattern
Getting ready
How to do it…
How it works…
Using the strategy pattern
Getting ready
How to do it…
How it works…
Using the command design pattern
Getting ready
How to do it…
How it works…
Creating an advanced game using design patterns
Getting ready
How to do it…
How it works…
7. Organizing and Backing Up
Introduction
Versions of source control
Getting ready
How to do it...
How it works...
Installing a versioning client
Getting ready
How to do it...
How it works...
Selecting a host to save your data
Getting ready
How to do it...
How it works...
Adding source control – committing and updating your code
Getting ready
How to do it...
How it works...
Resolving conflicts
Getting ready
How to do it...
How it works...
Creating a branch
Getting ready
How to do it...
How it works...
8. AI in Game Development
Introduction
Adding artificial intelligence to a game
Getting ready
How to do it…
How it works…
Using heuristics in a game
Getting ready
How to do it…
How it works…
Using a Binary Space Partition Tree
Getting ready
How to do it…
How it works…
Creating a decision making AI
Getting ready
How to do it...
How it works…
Adding behavioral movements
Getting ready
How to do it…
How it works…
Using neural network
Getting ready
How to do it…
How it works…
Using genetic algorithms
Getting ready
How to do it…
How it works…
Step 1 (Initial Population)
Step 2 (Fitness function)
Step 3 Cross-over
Step 4 Mutate
Using other waypoint systems
Getting ready
How to do it…
How it works…
9. Physics in Game Development
Introduction
Using physics rules in your game
Getting ready
How to do it…
How it works…
Making things collide
Getting ready
How to do it…
How it works…
Installing and integrating Box2D
Getting ready
How to do it…
How it works…
Making a basic 2D game
Getting ready
How to do it…
How it works…
Making a 3D game
Getting ready
How to do it…
How it works…
Creating a particle system
Getting ready
How to do it…
How it works…
Using ragdoll in your game
Getting ready
How to do it…
How it works…
10. Multithreading in Game Development
Introduction
Concurrency in games – creating a thread
Getting ready
How to do it…
How it works…
Joining and detaching a thread
Getting ready
How to do it…
How it works…
Passing arguments to a thread
Getting ready
How to do it…
How it works…
Avoiding deadlocks
Getting ready
How to do it…
How it works…
Data race and mutex
Getting ready
How to do it…
How it works…
Writing a thread-safe class
Getting ready
How to do it…
How it works…
11. Networking in Game Development
Introduction
Understanding the different layers
Getting ready
How to do it…
How it works…
Selecting the appropriate protocol
Getting ready
How to do it…
How it works…
Serializing the packets
Getting ready
How to do it…
How it works…
Using socket programming in games
Getting ready
How to do it…
How it works…
Sending the data
Getting ready
How to do it…
How it works…
Receiving the data
Getting ready
How to do it…
How it works…
Dealing with lag
Getting ready
How to do it…
How it works…
Using synchronized simulation
Getting ready
How to do it…
How it works…
Using area of interest filtering
Getting ready
How to do it…
How it works…
Using local perception filter
Getting ready
How to do it…
How it works…
12. Audio in Game Development
Introduction
Installing FMOD
Getting ready
How to do it…
How it works…
Adding background music
Getting ready
How to do it…
How it works…
Adding sound effects
Getting ready
How to do it…
How it works…
Creating a sound effect manager
Getting ready
How to do it…
How it works…
Dealing with multiple sound file names
Getting ready
How to do it…
How it works…
13. Tips and Tricks
Introduction
Effectively commenting your code
Getting ready
How to do it…
How it works…
Using bit fields in a struct
Getting ready
How to do it…
How it works…
Writing a sound technical design document
Getting ready
How to do it…
How it works…
Using the const keyword to optimize your code
Getting ready
How to do it…
How it works…
Using bit shift operators in an enum
Getting ready
How to do it…
How it works…
Using the new lambda function of C++ 11
Getting ready
How to do it…
How it works…
Index

C++ Game Development Cookbook

C++ Game Development Cookbook

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: May 2016

Production reference: 1250516

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-272-2

www.packtpub.com

Credits

Author

Druhin Mukherjee

Reviewer

Gonzalo Peces Nicolás

Acquisition Editor

Kirk D'costa

Content Development Editor

Rashmi Suvarna

Technical Editor

Anushree Arun Tendulkar

Copy Editor

Safis Editing

Project Coordinator

Judie Jose

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Arvindkumar Gupta

Cover Work

Arvindkumar Gupta

About the Author

Druhin Mukherjee is currently the co-founder and technical Director at GodSpeed Games. He has, over the years, worked with clients such as Lightning Fish Games, Chromativity, Rockstar North, Tag Games, BBC, Dynamo Games, and Codemasters.

Druhin has been balancing making games and teaching video game programming to enthusiastic students. He spent 3 years in Auckland, New Zealand as a Senior Lecturer in the Games department at Media Design School.

As a passionate games developer, Druhin has been sharing his knowledge on the Internet as blogs and websites. His recently started website for solving game development puzzles has over thousand subscribers.

Druhin has collaborated with other writers and published many journals and papers; however, this is his first official effort to write a book.

I would like to thank my wife, Anushree, for putting up with my late night writing sessions. I also give deep thanks and gratitude to Rashmi Suvarna, without whose efforts this book quite possibly would not have happened.

I would also like to thank all of the mentors that I've had over the years. Without learning from these teachers, there is not a chance I could be doing what I do today, and it is because of them and others that I feel compelled to pass my knowledge on to those willing to learn.

About the Reviewer

Gonzalo Peces Nicolás is a Senior Game Developer based in Hong Kong. Gonzalo received his Bachelor's Degree in Computer Science in 2005 in Spain and his Master's Degree in Computer Games Development in 2011 in Scotland. Currently, he is working as Senior Software Engineer in one of the major Game Mobile publishers in Asia. He has, over the years, been involved in numerous games in some of the most prolific international game companies in Europe and Asia, developing on multiple platforms, such as PC, Mac, mobile, and console.

Furthermore, he has over a decade of professional development in several industries, which includes not only game development but also telecommunications and cryptography.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Preface

This book provides a detailed look at some of the aspects of C++ which could be used for games development.

What this book covers

Chapter 1, Game Development Basics, explains the basics of C++ programming, writing small programs to be used in games, and how to handle memory in games.

Chapter 2, Object-Oriented Approach and Design in Games, explains the use OOP concepts in games, and you will make a small prototype text-based game.

Chapter 3, Data Structures in Game Development, introduces all the simple and complex data structures in C++ and shows how to use them effectively in games.

Chapter 4, Algorithms for Game Development, explains various algorithms that can be used in games. It also covers means to measure the efficiency of an algorithm.

Chapter 5, Event-Driven Programming – Making Your First 2D Game, introduces Windows programming, creating sprites, and animation.

Chapter 6, Design Patterns for Game Development, explains how to use well-known design patterns in game development and when not to use them.

Chapter 7, Organizing and Backing Up, explains the importance of backing up data and the importance of sharing data across a team.

Chapter 8, AI in Game Development, explains how to approach writing artificial intelligence in games.

Chapter 9, Physics in Game Development, explains how to make bodies collide and how to use third-party physics libraries, such as Box2D, to make games.

Chapter 10, Multithreading in Game Development, explains how to use the thread architecture of C++11 to make games.

Chapter 11, Networking in Game Development, explains the fundamentals of writing a multiplayer game.

Chapter 12, Audio in Game Development, explains how to add sound and music effects to games, and avoiding memory leaks while playing sounds.

Chapter 13, Tips and Tricks, has some neat tips and tricks of using C++ to make games.

What you need for this book

For this book you would require a Windows machine and a working copy of Visual Studio 2015 Community Edition.

Who this book is for

This book should be primarily used by college students wanting to enter the games industry or enthusiastic school students who want to get their hands dirty early and understand the fundamentals of game programming. This book also has some very technical chapters which will be very useful for industry professionals for reference or to keep by the side while solving complex problems.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "If you have a file called main.cpp, it will generate an object code called main.o."

A block of code is set as follows:

#include <iostream> #include <conio.h> using namespace std; int countTotalBullets(int iGun1Ammo, int iGun2Ammo) { return iGun1Ammo + iGun2Ammo; }

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on Download Visual Studio Community."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/CPP-Game-Development-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Game Development Basics

In this chapter, the following recipes will be covered:

Installing an IDE on WindowsChoosing the right source control toolUsing call stacks for memory storageUsing recursions cautiouslyUsing pointers to store memory addressesCasting between various datatypesManaging memory more effectively using dynamic allocationUsing bitwise operations for advanced checks and optimization

Introduction

In this chapter, we will cover the basic concepts that you need to know to kick-start your career in game development.

The first step before a person starts coding is to install an integrated development environment (IDE). Nowadays, there are a few online IDEs that are available, but we are going to use an offline standalone IDE, Visual Studio. The next most important thing that many programmers do not start using at an early stage is revision control software.

Revision control software helps to back up the code in one central location; it has a historical overview of the changes that are made, which you can access and revert to if needed, and it also helps to resolve conflicts between files that have been worked on by different programmers at the same time.

The most useful feature of C++, in my opinion, is memory handling. It gives the developers a lot of control over how memory must be assigned depending on the current usage and needs of the program. As a result of this, we can allocate memory when there is a need and deallocate it accordingly.

If we do not de-allocate memory, we might run out of memory very soon, especially if we are using recursion. Sometimes there is a need to convert from one datatype to another to prevent loss of data, to pass the correct datatype in a function, and so on. C++ provides us a few ways by which we can do those castings.

The recipes in this chapter will primarily focus on these topics and deal with practical ways to implement them.

Installing an IDE on Windows

In this recipe, we will find out how easy it is to install Visual Studio on your Windows machine.

Getting ready

To go through this recipe, you will need a machine running Windows. No other prerequisites are required.

How to do it…

Visual Studio is a powerful IDE in which most professional software is written. It has loads of features and plugins to help us write better code:

Go to https://www.visualstudio.com.Click on Download Visual Studio Community.

Download Visual Studio Community

This should download an .exe file.After it downloads, double-click on the setup file to start the installation.Make sure you have all the updates necessary on your Windows machine.You can also download any version of Visual Studio or Visual C++ Express.If the application asks for starting environment settings, select C++ from the available options.

Note

A few things to note are listed here:

You need a Microsoft account to install it.There are other free IDEs for C++, such as NetBeans, Eclipse, and Code::Blocks.While Visual Studio works only for Windows, Code::Blocks and other such IDEs are cross-platform and can work on Mac and Linux as well.

For the remainder of this chapter, all code examples and snippets will be provided using Visual Studio.

How it works…

An IDE is a programming environment. An IDE consists of various functionalities that can vary from one IDE to another. However, the most basic functionalities that are present in all IDEs are a code editor, a compiler, a debugger, a linker, and a GUI builder.

A code editor, or a source code editor as they are otherwise known, is useful for editing code written by programmers. They provide features such as auto-correct, syntax highlighting, bracket completion and indentation, and so on. An example snapshot of the Visual Studio code editor is shown here:

A compiler is a computer program that converts your C++ code to object code. This is necessary in order to create an executable. If you have a file called main.cpp, it will generate an object code called main.o.

A linker is a computer program that converts the object code generated by the compiler to an executable or a library file:

Compiler and linker

A debugger is a computer program that helps to test and debug computer programs.

A GUI builderhelps the designer and programmer to create GUI content or widgets easily. It uses a drag and drop WYSIWYG tool editor.

Choosing the right source control tool

In this recipe, we will see how easy it is to take a backup of our code using the right version control. The advantages of having a backup to a central server is that you will never lose work, can download the code on any machine, and can also go back to any of your changes from the past. Imagine it is like a checkpoint that we have in games, and you can go back to that checkpoint if you face problems.

Getting ready

To go through this recipe, you will need a machine running Windows. No other prerequisites are required.

How to do it…

Choosing a correct version tool is very important as it will save a lot of time organizing data. There are a few versioning tools that are available, so it is very important that we should be informed about all of them so that we can use the correct one based on our needs.

First analyze the choices that are available to you. The choices primarily include Concurrent Versions System (CVS), Apache Subversion (SVN), Mercurial, and GIT.

How it works…

CVS has been around for a long time, so there is tons of documentation and help available. However, a lack of atomic operations often leads to source corruption and it is not well cut out for long-term branching operations.

SVN was made as an improvement to CVS and it does fix many of its issues relating to atomic operations and source corruption. It is free and open source. It has lots of plugins for different IDEs. However, one of the major drawbacks of this tool is that it is comparatively very slow in its operations.

GIT was made primarily for Linux but it improves operation speed a lot. It works on UNIX systems as well. It has cheap branch operations but it is not totally optimized for a single developer and its Windows support is limited compared to Linux. However, GIT is extremely popular and many prefer GIT to SVN or CVS.

Mercurial came into existence shortly after GIT. It has node-based operations but does not allow the merging of two parent branches.

So to sum up, use SVN if you want a central repository that others can push and pull. Although it has its limitations, it's easy to learn. Use Mercurial or GIT if you want a distributed model. In this case, there is a repository on every computer, and generally, one of them is regarded as the official one. Mercurial is often preferred if it is a relatively small team, and it's easier to learn than GIT.

We will look into these in more detail in a separate chapter.

Tip

Detailed steps to download the code bundle are mentioned in the Preface of this book. Please have a look.

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/C++Game-Development-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!