Book Image

C++ Game Development Cookbook

By : Druhin Mukherjee
Book Image

C++ Game Development Cookbook

By: Druhin Mukherjee

Overview of this book

<p>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.</p> <p>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.</p> <p>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++.</p>
Table of Contents (20 chapters)
C++ Game Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using pointers to store memory addresses


In the previous two recipes, we have seen how not having sufficient memory can be a problem to us. However, until now, we have had no control over how much memory is assigned and what is assigned to each memory address. Using pointers, we can address this issue. In my opinion, pointers are the single most important topic in C++. If your concept of C++ has to be clear, and if you are to become a good developer in C++, you must be good with pointers. Pointers can seem very daunting at first, but once you get the hang of it, pointers are pretty easy to use.

Getting ready

For this recipe, you will need a Windows machine with a working copy of Visual Studio.

How to do it…

In this recipe, we will see how easy it is to work with pointers. Once you are comfortable using pointers, we can manipulate memory and store references in memory quite easily:

  1. Open Visual Studio.

  2. Create a new C++ project.

  3. Select Win32 Console Application.

  4. Add a source file called main.cpp or anything that you want to name the source file.

  5. Add the following lines of code:

    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
        float fCurrentHealth = 10.0f;
    
        cout << "Address where the float value is stored: " << &fCurrentHealth << endl;
        cout << "Value at that address: " << *(&fCurrentHealth) << endl;
    
        float* pfLocalCurrentHealth = &fCurrentHealth;
        cout << "Value at Local pointer variable: "<<pfLocalCurrentHealth << endl;
        cout << "Address of the Local pointer variable: "<<&pfLocalCurrentHealth << endl;
        cout << "Value at the address of the Local pointer variable: "<<*pfLocalCurrentHealth << endl;
    
        _getch();
        return 0;
    }

How it works…

One of the most powerful tools of a C++ programmer is to manipulate computer memory directly. A pointer is a variable that holds a memory address. Each variable and object used in a C++ program is stored in a specific place in memory. Each memory location has a unique address. Memory addresses will vary depending on the operating system used. The amount of bytes taken up depends on the variable type: float = 4 bytes, short = 2 bytes:

Pointers and memory storage

Each location in the memory is 1 byte. The pointer pfLocalCurrentHealth holds the address of the memory location that has stored fCurrentHealth. Hence, when we display the contents of the pointer, we get the same address as that of the address containing the fCurrentHealth variable. We use the & operator to get the address of the pfLocalCurrentHealth variable. When we reference the pointer using the * operator, we get the value stored at the address. Since the stored address is same as the address storing fCurrentHealth, we get the value 10.

There's more…

Let us consider the following declarations:

  • const float* pfNumber1

  • float* const pfNumber2

  • const float* const pfNumber3

All of these declarations are valid. But what do they mean? The first declaration states that pfNumber1 is a pointer to a constant float. The second declaration states that pfNumber2 is a constant pointer to a float. The third declaration states that pfNumber3 is a constant pointer to a constant integer. The key differences between references and these three types of const pointers are listed here:

  • const pointers can be NULL

  • A reference does not have its own address, whereas a pointer does

    The address of a reference is the actual object's address

  • A pointer has its own address and it holds as its value the address of the value it points to