C++ Game Development By Example
eBook - ePub

C++ Game Development By Example

Learn to build games and graphics with SFML, OpenGL, and Vulkan using C++ programming

Siddharth Shekar

  1. 420 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

C++ Game Development By Example

Learn to build games and graphics with SFML, OpenGL, and Vulkan using C++ programming

Siddharth Shekar

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Explore modern game programming and rendering techniques to build games using C++ programming language and its popular libraries

Key Features

  • Learn how you can build basic 2D and complex 3D games with C++
  • Understand shadows, texturing, lighting, and rendering in 3D game development using OpenGL
  • Uncover modern graphics programming techniques and GPU compute methods using the Vulkan API

Book Description

Although numerous languages are currently being used to develop games, C++ remains the standard for fabricating expert libraries and tool chains for game development. This book introduces you to the world of game development with C++.

C++ Game Development By Example starts by touching upon the basic concepts of math, programming, and computer graphics and creating a simple side-scrolling action 2D game. You'll build a solid foundation by studying basic game concepts such as creating game loops, rendering 2D game scenes using SFML, 2D sprite creation and animation, and collision detection. The book will help you advance to creating a 3D physics puzzle game using modern OpenGL and the Bullet physics engine. You'll understand the graphics pipeline, which entails creating 3D objects using vertex and index buffers and rendering them to the scene using vertex and fragment shaders. Finally, you'll create a basic project using the Vulkan library that'll help you get to grips with creating swap chains, image views, render passes, and frame buffers for building high-performance graphics in your games.

By the end of this book, you'll be ready with 3 compelling projects created with SFML, the Vulkan API, and OpenGL, and you'll be able take your game and graphics programming skills to the next level.

What you will learn

  • Understand shaders and how to write a basic vertex and fragment shader
  • Build a Visual Studio project and add SFML to it
  • Discover how to create sprite animations and a game character class
  • Add sound effects and background music to your game
  • Grasp how to integrate Vulkan into Visual Studio
  • Create shaders and convert them to the SPIR-V binary format

Who this book is for

If you're a developer keen to learn game development with C++ or get up to date with game development, this book is for you. Some knowledge of C++ programming is assumed.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que C++ Game Development By Example est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  C++ Game Development By Example par Siddharth Shekar en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Ciencia de la computaciĂłn et ProgramaciĂłn en C++. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2019
ISBN
9781789537345

Section 1: Basic Concepts

This section covers some basic concepts of C++ game development. We need to have a good understanding of math, programming, and computer graphics to get ready for the later sections in the book.
The following chapters are in this section:
Chapter 1, C++ Concepts
Chapter 2, Mathematics and Graphics Concepts

C++ Concepts

In this chapter, we will explore the basics of writing a C++ program. Here, we will cover just enough to wrap our heads around the capabilities of the C++ programming language. This will be required to understand the code used in this book.
To run the examples, use Visual Studio 2017. You can download the community version for free at https://visualstudio.microsoft.com/vs/:
The topics covered in this chapter are as follows:
  • Program basics
  • Variables
  • Operators
  • Statements
  • Iteration
  • Functions
  • Arrays and pointers
  • Struct and Enum
  • Classes and inheritance

Program basics

C++ is a programming language, but what exactly is a program? A program is a set of instructions executed in sequence to give a desired output.
Let's look at our first program:
#include <iostream> // Program prints out "Hello, World" to screen int main() { std::cout<< "Hello, World."<<std::endl; return 0; } 
We can look at this code line by line.
The hash (#) include is used when we want to include anything that is using valid C++ syntax. In this case, we are including a standard C++ library in our program. The file we want to include is then specified inside the <> angle brackets. Here, we are including a file called iostream.h. This file handles the input and output of data to the console/screen.
On the second line, the // double slash marks the initiation of a code comment. Comments in code are not executed by the program. They are mainly to tell the person looking at the code what the code is currently doing. It is good practice to comment your code so that when you look at code you wrote a year ago, you will know what the code does.
Basically, main() is a function. We will cover functions shortly, but a main function is the first function that is executed in a program, also called the entry point. A function is used to perform a certain task. Here, the printing of Hello, World is tasked to the main function. The contents that need to be executed must be enclosed in the curly brackets of the function. The int preceding the main() keyword suggests that the function will return an integer. This is why we have returned 0 at the end of the main function, suggesting that the program can be executed and can terminate without errors.
When we want to print out something to the console/screen, we use the std::cout (console out) C++ command to send something to the screen. Whatever we want to send out should start and end with the output operator, <<. Furthermore, <<std::endl is another C++ command, which specifies the end of a era line and that nothing else should be printed on the line afterward. We have to use the prefix before std:: to tell C++ that we are using the standard namespace with the std namespace. But why are namespaces necessary? We need namespaces because anyone can declare a variable name with std. How would the compiler differentiate between the two types of std? For this, we have namespaces to differentiate between the two.
Note that the two lines of code we have written in the main function have a semicolon (;) at the end of each line. The semicolon tells the compiler that this is the end of the instructions for that line of code so that the program can stop reading when it gets to the semicolon and go to the next line of instruction. Consequently, it is important to add a semicolon at the end of each line of instruction as it is mandatory.
The two lines of code we wrote before can be written in one line as follows:
std::cout<< "Hello, World."<<std::endl;return 0; 
Even though it is written in a single line, for the compiler, there are two instructions with both instructions ending with a semicolon.
The first instruction is to print out Hello, World to the console, and the second instruction is to terminate the program without any errors.
It is a very common mistake to forget semicolons, and it happens to beginners as well as experienced programmers every now and then. So it's good to keep this in mind, for when you encounter your first set of compiler errors.
Let's run this code in Visual Studio using the following steps:
  1. Open up Visual Studio and create a new project by going to File | New | Project.
  2. On the left-hand side, select Visual C++ and then Other. For the Project Type, select Empty Project. Give this project a Name. Visual Studio automatically names the first project MyFirstProject. You can name it whatever you like.
  3. Select the Location that you want the project to be saved in:
  1. Once the project is created, in Solution Explorer, right-click and select Add | New Item:
  1. Create a new .cpp file, called the Source file:
  1. Copy the code at the start of the section into the Source.cpp file.
  2. Now press the F5 key on the keyboard or press the Local Window Debugger button at the top of the window to run the application.
  3. A popup of the console should appear upon running the program. To make the console stay so that we can see what is happening, add the following highlighted lines to the code:
#include <iostream> 
#include <conio.h>
// Program prints out "Hello, World" to screen int main() { std::cout << "Hello, World." << std::endl;
_getch();
return 0; }
What _getch() does is it stalls the program and waits for a character input to the console without printing the character to the console. So, the program will wait for some input and then close the console.
To see what is printed to the console, we just add it for convenience. To use this function, we need to include the conio.h header.
  1. When you run the project again, you will see the following output:
Now that we know how to run a basic program, let's look at the different data types that are included in C++.

Variables

A variable is used to store a value. Whatever value you store in a variable is stored in the memory location associated with that memory location. You assign a value to a variable with the following syntax.
We can first declare a variable type by specifying a type and then the variable name:
Type variable;
Here, type is the variable typ...

Table des matiĂšres

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Section 1: Basic Concepts
  8. C++ Concepts
  9. Mathematics and Graphics Concepts
  10. Section 2: SFML 2D Game Development
  11. Setting Up Your Game
  12. Creating Your Game
  13. Finalizing Your Game
  14. Section 3: Modern OpenGL 3D Game Development
  15. Getting Started with OpenGL
  16. Building on the Game Objects
  17. Enhancing Your Game with Collision, Loops, and Lighting
  18. Section 4: Rendering 3D Objects with Vulkan
  19. Getting Started with Vulkan
  20. Preparing the Clear Screen
  21. Creating Object Resources
  22. Drawing Vulkan Objects
  23. Other Books You May Enjoy