32,39 €
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:
Seitenzahl: 323
Veröffentlichungsjahr: 2016
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
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
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.
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.
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.
This book provides a detailed look at some of the aspects of C++ which could be used for games development.
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.
For this book you would require a Windows machine and a working copy of Visual Studio 2015 Community Edition.
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.
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:
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.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
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:
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."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
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.
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.
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:
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:
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!
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 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.
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.
In this chapter, the following recipes will be covered:
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.
In this recipe, we will find out how easy it is to install Visual Studio on your Windows machine.
To go through this recipe, you will need a machine running Windows. No other prerequisites are required.
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:
Download Visual Studio Community
A few things to note are listed here:
For the remainder of this chapter, all code examples and snippets will be provided using Visual Studio.
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.
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.
To go through this recipe, you will need a machine running Windows. No other prerequisites are required.
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.
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.
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!
