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 - Particle System

Before – Particle System


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:

Site HW Generator©: A C# Database Management System

Back in 2008 while I was working on the MySQL database-driven PHP application demoed here, it became clear that this was not the direction to go if I ever wanted this system I’m building to be actually used in my department. There were many restrictions that forced me to move to something simpler, yet more robust and looks better. I started working on a new desktop application tool that I called “Site HW Generator” and made sure that this tool won’t remain just a prototype. I put a lot of effort into this tool including not just writing the code but also the creation of its logo, splash screens and so on.

In the following video I demonstrate the tool including a quick demo of the splash screen creation using Cinema 4D.