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

Casting between different datatypes


Casting is a conversion process of changing some data into a different type of data. We can convert between built-in types or our own datatypes. Some of the conversions are done automatically by the compiler, and the programmer does not have to intervene. Such conversions are called implicit conversions. Other conversions, which have to be directly specified by the programmer, are called explicit conversion. Sometimes we may get warnings about loss of data. We should pay heed to these warnings and think about how this might adversely affect our code. Casting is commonly used when the interface expects a particular type, but we want to feed it data of a different type. With C, we can cast anything to everything. However, C++ provides us with finer controls.

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 we can easily cast or convert between various datatypes. Usually, a programmer uses C-style casting even in C++, but this is not recommended. C++ provides us with its own style of casting for different situations which we should use:

  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()
    {
        int iNumber = 5;
        int iOurNumber;
        float fNumber;
    
        //No casting. C++ implicitly converts the result into an int and saves 
        //into a float
        fNumber = iNumber/2;
        cout << "Number is " << fNumber<<endl;
    
        //C-style casting. Not recommended as this is not type safe
        fNumber = (float)iNumber / 2;
        cout << "Number is " << fNumber<<endl;
    
        //C++ style casting. This has valid constructors to make the casting a safe one
        iOurNumber = static_cast<int>(fNumber);
        cout << "Number is " << iOurNumber << endl;
    
        _getch();
        return 0;
    }

How it works…

There are four types of casting operators in C++, depending on what we are casting: static_cast, const_cast, reinterpret_cast, and dynamic_cast. Now, we are going to look at static_cast. We will look at the remaining three casting technique after we discuss dynamic memory and classes. Converting from a smaller datatype to a larger type is called promotion and is guaranteed to have no data loss. However, conversion from a larger datatype to a smaller one is called demotion and may lead to data loss. Compilers will generally give you a warning when this happens, and you should pay heed to this.

Let us look at the previous example. We have initialized an integer with the value 5. Next, we have initialized a floating point variable and stored the result of 5 divided by 2, which is 2.5. However, when we display the variable fNumber, we see that the displayed value is 2. The reason is the C++ compiler implicitly casts the result of 5/2 and stores it as an integer. So it is evaluating something similar to int (5/2) which is int (2.5), evaluating to 2. So to achieve our desired result, we have two options. The first method is a C-style explicit cast, which is not recommended at all because it does not have a type safe check. The format for the C-style cast is (resultant_data_type) (expression), which in this case is something like float (5/2). We are explicitly telling the compiler to store the result of the expression as a floating point number. The second method, and a more C++ style way of doing the cast, is by using the static_cast operation. This has suitable constructors to dictate that the conversion is type safe. The format for a static_cast operation is static_cast<resultant_data_type> (expression). The compiler checks if the casting conversion is safe and then executes the type casting operation.