C++ Code Optimization – Optimizing a Particle System

In the fall quarter of 2013 at DePaul University, I took the GAM491 – Optimized C++ class. In this class we learned very advanced techniques to optimize any C++ code. As a final project, we were provided with an old and slow particle system. Each student was required to individually submit an optimized version of it applying as many techniques of what we have studied as possible. The rules were:

  • Keep the number of particles at 20000 for the benchmarks.
  • Keep the particles lifetime at 11 seconds.
  • Do not modify any OpenGL calls, just the modification of the  C++ code, the algorithms and the data structures used were allowed.
  • Do not change any of the particle system behavior.

The results of the optimizations that I made were impressive. They are shown below (an improvement by a factor of about 160 times):

Before

Before - Particle System

Before – Particle System

After

After - Particle System

After – Particle System

Car Traffic Simulation Application

In the Fall quarter of 2013 at DePaul University, I wrote the Car Traffic Simulation Application as the final project for the class SE450 – Object-Oriented Software Development. The purpose of this project was to apply the studied object-oriented design principles and patterns throughout the quarter. The professor provided the class with start-up code from which each student can build his solution. I chose to discard that code completely and design and build my own solution from scratch. The reason why I did this was because I believe while still being a student, every opportunity possible to learn something and write as much code as possible must be taken.

I started by designing and implementing a framework on top of Java with which I can build any simulation application. Then I used that framework to build the car simulation. I also wrote an extensive design document with UML diagrams, fully documented all the code in a Java-doc style documentation, and extensively wrote unit tests for the framework that I built.

Nine design patterns were used in the implementation of the application. They helped making the code very organized, minimal, and data driven. Those design patterns were:

  • The command design pattern.
  • The singleton design pattern.
  • The strategy design pattern.
  • The state design pattern.
  • The static factory design pattern.
  • The template method design pattern.
  • The null object design pattern.
  • The visitor design pattern.
  • The observer design pattern.

The below video demonstrates the application.

Building An Android Game Using Web Technologies

There are so many things one can do with web technologies nowadays, since almost all devices have web browsers. I recently read the Supercharged JavaScript Graphics book, which demonstrated a lot of ways you can use JavaScript to write some cool games that run on the browser. After I finished reading it, I wanted to get my hands dirty by writing my own version of one of the example games presented in the book. I called the game “MangaPuzzle”, which is a tile-based puzzle game that uses images of drawings of Japanese animated manga. Those images are not mine, I just found them online and used them for the sake of my personal education and practice.

The purpose of this personal exercise for me was to improve my knowledge of JavaScript and some JavaScript libraries. The UI of the game was easily designed using jQuery Mobile. The tricky part was to write the game code in JavaScript implementing an Object-Oriented design, since JavaScript is not a conventional class-based OO language. Much of the OO features in JavaScript are simulated using closures and the fact that functions are objects. My code is completely different than the one presented in the book and that was my mission, to see if I’ll be able to write it differently my own way.

Web Technologies used were:

  • HTML
  • CSS
  • JavaScript
  • JavaScript Libraries (jQuery, and jQuery Mobile)

I then used the PhoneGap framework to convert the game into an Android app. In the following video I will be reviewing the game.

Converting an Existing XNA Game into a Networked Multiplayer One

Video demo is at the end.

In the GAM490 – Multiplayer Networking for Games class that I took at DePaul University, we studied a lot of interesting networking topics and then tried to apply them on a game-development related project. In order to prepare for that final networked game project, we studied and implemented best ways to serialize objects, taking into consideration the data sizes and their alignment requirements, so that objects can be efficiently stored and transferred over the network. Each student implemented his own TCP and UDP servers and clients using Windows Sockets, in order to understand the differences between the two protocols. We also studied how to write good data-driven programs employing input and output queues in order to make the processing of sent and received networked packets over the network simple and easy.

For the final project, we were supplied by and XNA game written by a previous student that each student was required to individually convert it into a networked multiplayer game. The requirements of the final game were:

  • Refactor the game code to make it completely data-driven using input and output queues to process the different game events.
  • Adding the ability to create a multiplayer network session either as an XNA system link, or an Xbox Live session. Each player must be able to either create a network session or join an existing one.
  • Adding a Game Lobby screen, in which players can view other players joined in the current session and from there they can start playing the game.
  • The game must be multiplayer playable on two separate machines connected by the network, and the game play must be exactly as that of the original locally-playable game.
  • The game must include network quality simulation configurations, simulating different network latencies, and percent packet loss.
  • The game must implement prediction and smoothing algorithms to account for the network latency and packet loss.
  • The game must be developed as incremental submission to Perforce with detailed description of each submission.
  • Detailed design documents must be submitted.

I had to choose between implementing the network topology of the game as a peer-to-peer topology or a client-server topology. I went for peer-to-peer as I thought it would be easier to implement since the game is a two-player one only, but in fact it was more challenging to implement as I had to deal with a lot of synchronization issues between the two machines which occur due to the network latency and the packet losses. To deal with these issues, good smoothing and prediction algorithms were very important.

Periodic update packets were added to the game to account for any losses. The number of these packets per one frame is adjustable by the game host, and they only contain the ships’ positions and rotations. Smoothing is done by linearly interpolating between the current ships position and rotation values and the new ones received from the newest periodic update. This makes the transition from an old value to a new value appear to be very smooth.

For the prediction, my algorithm is very simple. It starts by calculating the latency; i.e. the time it took the newly received periodic update packet to be delivered. Use this latency value to try to predict where the ship should have been now had this packet arrived on time.

Testing and debugging this game was a challenge, since I had to use two machines and constantly synchronize any code changes between the two. Perforce was very important for this project.

The final game is reviewed in the below video.

Building The Animation System of My Game Engine

For the second milestone of the Game Engine Programming II class that I took in the spring quarter 2013 at DePaul University, I added animation capability to both my game engine and my FBX converter. It was one of the most difficult tasks that I did for this class. It took a lot of iterations to write a good easy-to-use animation system. My intention was to make the system completely data-driven, having the FBX converter extracting all the needed animation data from the FBX file (an FBX file can contain multiple models, with each model having multiple animations in a so-called animation stacks) and placing these data as a table-indexed binary chunks into a .dpu file for the model data, and into a .bdpu file for the skeleton data. The extracted skeleton data are:

  • The skeleton bones hierarchy.
  • The skeleton’s animation stacks.
  • The keyframes associated with each bone in each animation stack.

I then made the game engine capable of loading these data, and using them to draw the bones and animate them.

The Math Engine Library that I wrote for the first part of this class had to be changed to support quaternion math operations and to fully integrate them with the existing vector and matrix operations. Linear & Spherical Linear Interpolation (LERP and SLERP) operations have been added as well. These operations are crucial to any animation engine.

The Animation Engine Features

  • Completely data-driven (loads all needed data from a flexible expandable binary file).
  • Ability to draw bones based on the data read from the binary file. Bones colors can be changed by the user of the system.
  • If the FBX model has multiple animations in multiple animation stacks, they will all be available for that model’s bones and any of them can be selected to be the current active animation in the engine.
  • Ability to play back the animations forward, backward, faster, and slower.

Next thing I will be working on is “Skinning”. The above features are demoed in the below video.

Building an FBX File to My Game Engine Binary File Converter

During the second class of Game Engine Programming, which I’m currently taking this quarter, we are learning how to write offline tools to help us feed data (models and animations) into our own game engines. The choice was to create a command-line tool that converts an FBX file to the binary format expected by my own game engine ( I call it the .dpu file ). It’s a command line tool so that it can be easily used in a script to handle multiple conversion. We also chose to work with FBX files since they are widely supported by most 3D graphics creation suites. Its SDK is very well documented, and the documentations contain a lot of examples.

For the first milestone of this class, each student was required to individually write his own converter, and modify his own game engine as needed. I modified my game engine so that it’s more dynamic when loading the .dpu files. It deals now with indexed binary chunks rather than a fixed set of data in the file. It can also deal with models that are built from multiple meshes placed in a certain hierarchy. The game engine can now load these hierarchies dynamically into the scene graph and transform them correctly according to the extracted matrices from the FBX file.

Each student was required to record a demo video of his converter and Model Viewer (modified game engine), and write a design document demonstrating his own game engine binary format and why he made his own design decisions.

Here is the video:

Easy4PHPUnit: Easy 4 Steps PHP Unit Testing Framework

I’m fascinated a lot by the power and confidence that unit testing gives to the software developer. We use unit testing in all of my classes assignments, and I can’t imagine my life without them. I’m not a web developer, but I like to play around with different programming languages to make sure that any new concepts I learn or any new skills I acquire are transferable to any area regardless of the programming language that I use. That’s why I started writing this very basic unit testing framework in PHP just as personal educational exercise. Now I would like to share it with anyone who might find it useful and wants either to use it or learn something from it.

Note: Demo video is available at the end of this post if you prefer to watch the quick tutorial.
The GitHub repo link is available at the end of the post as well.

Framework Architecture

Easy4PHPUnit UML Diagram

Easy4PHPUnit UML Diagram

The simplicity of this framework is reflected in the simplicity of its architecture. The above UML is not meant to reflect every bit of code  in the framework, but it gives the big picture. The heart of this framework is the singleton class TestContainer, which stores a number of tests (instances of concrete derived classes from the abstract class TestCase). Those tests will eventually be run when the runAll() method is invoked, and any test failures will be added to a list of Failure instances that TestContainer manages, so that it can report those failures at the end of the test.

The abstract TestCase class has a constructor that automatically adds itself to the list of tests managed by TestContainer. So all that needs to be done is to write a concrete class that extends TestCase and implements its abstract method run(). This concrete class will represent a test case to be run, its run() method is where the unit test code should be added. The unit test code should use TestCase‘s protected methods check_equal() and check_float_equal(). Those methods will check for the equality of an expected and an actual value, and will automatically register a failure if any.

Obviously those two unit testing methods (check_equal() and check_float_equal()) are not enough, and hence TestCase needs to be extended with many more methods that do more sorts of tests, and that’s where the framework needs improvement.

If there are any failures, they will be conveniently reported with their file name(s) and line numbers as shown below:

Easy4PHPUnit Tests Results

Easy4PHPUnit Tests Results

The 4 Easy Steps

1 – Create a PHP File To Hold Your Tests

You can have multiple files as well, with each having a bunch of related test cases. In the beginning of each file require the TestIncludes.php file.

require_once 'TestIncludes.php';

2 – Create One or More Concrete Classes That Extend TestCase And Implement Its run() Method

Each concrete class represents a test case. The actual unit test code is inside your implementation of the run() method. run() must use the unit testing methods of TestCase (check_equal() and check_float_equal()) or any other similar methods you might have extended TestCase with.

class FloatingPointTest1 extends TestCase
{
     public function run()
     {
         $float1 = 0.003;
         $float2 = 0.00302;
         $this->check_float_equal($float1, $float2);
     }
}

3 – For Each Concrete TestCase Class You Created, Instantiate It

Instantiate the concrete classes you just created, so that the constructor of the abstract base class TestCase will add that test case to the list on the TestContainer.

$t1 = new FloatingPointTest1();

4 – Run The Tests

Go to the run.php file and require any PHP files that you created previously to hold your concrete test case classes and their instances. run.php automatically calls the runAll() method of the TestContainer class. Just execute this run.php file and the tests will be run and their results will be reported.

require_once 'TestIncludes.php';
require_once 'MyTestCase.php';
TestContainer::getInstance()->runAll();


The code is well documented and is easy to follow. It is available at the following GitHub repository.

Easy4PHPUnit on GitHub

I also recorded a quick demo video to demonstrate the framework for those who like videos. Here you go:

Thank you everyone, and I hope you liked it.