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 bitwise operations for advanced checks and optimization


In most cases, a programmer will not need to worry too much about bits unless there is a need to write some compression algorithms, and when we are making a game, we never know when a situation such as that arises. In order to encode and decode files compressed in this manner, you need to actually extract data at the bit level. Finally, you can use bit operations to speed up your program or perform neat tricks. However, this is not always recommended.

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 use bitwise operations to perform operations by manipulating memory. Bitwise operations are also a great way to optimize code by directly interacting with memory:

  1. Open Visual Studio.

  2. Create a new C++ project.

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

  4. Add the following lines of code:

    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    void Multi_By_Power_2(int iNumber, int iPower);
    void BitwiseAnd(int iNumber, int iNumber2);
    void BitwiseOr(int iNumber, int iNumber2);
    void Complement(int iNumber4);
    void BitwiseXOR(int iNumber,int iNumber2);
    
    int main()
    {
      int iNumber = 4, iNumber2 = 3;
      int iPower = 2;
      unsigned int iNumber4 = 8;
    
      Multi_By_Power_2(iNumber, iPower);
      BitwiseAnd(iNumber,iNumber2);
      BitwiseOr(iNumber, iNumber2);
      BitwiseXOR(iNumber,iNumber2);
      Complement(iNumber4);
    
      _getch();
      return 0;
    }
    
    void Multi_By_Power_2(int iNumber, int iPower)
    {
      cout << "Result is :" << (iNumber << iPower)<<endl;
    }
    void BitwiseAnd(int iNumber, int iNumber2)
    {
      cout << "Result is :" << (iNumber & iNumber2) << endl;
    }
    void BitwiseOr(int iNumber, int iNumber2)
    {
      cout << "Result is :" << (iNumber | iNumber2) << endl;
    }
    void Complement(int iNumber4)
    {
      cout << "Result is :" << ~iNumber4 << endl;
    }
    void BitwiseXOR(int iNumber,int iNumber2)
    {
      cout << "Result is :" << (iNumber^iNumber2) << endl;
    }

How it works…

The left shift operator is the equivalent of moving all the bits of a number a specified number of places to the left. In our example, the numbers we are sending to the function Multi_By_Power_2 is 4 and 3. The binary representation of 4 is 100, so if we shift the most significant bit, which is 1, three places to the left, we get 10000, which is the binary of 16. Hence, left shift is equivalent to integer division by 2^shift_arg, that is, 4*2^3, which is again 16. Similarly, the right shift operation is equivalent to integer division by 2^shift_arg.

Now let us consider we want to pack data so that the data is compressed. Consider the following example:

int totalammo,type,rounds;

We are storing the total bullets in a gun; the type of gun, but it can only be a rifle or pistol; and the total bullets per round it can fire. Currently we are using three integer values to store the data. However, we can compress all the preceding data into one single integer and hence compress the data:

int packaged_data;
packaged_data = (totalammo << 8) | (type << 7) | rounds;

If we assume the following notations:

  • TotalAmmon: A

  • Type: T

  • Rounds: R

The final representation in the data would be something like this:

AAAAAAATRRRRRRR