My Mutex Implementation

There are already lots of well-known and popular libraries out there that provide mutices. But I wanted to try implementing it myself for the sake of just making it. Hey, it’s summer session at DigiPen and I don’t have to work on any school game projects. This means I can do whatever programming exercise I want during this time :)

My implementation relies on “Windows.h”, which already provides mutex objects. But hey, it’s summer session! Besides, I actually find my interface design more intuitive and easier to use, so I might end up using it in my next game project.

Below is the interface for the Mutex class. It has a default constructor, a copy constructor and a destructor. It also has an assignment operator. Finally, it defines the very two operations a mutex object has: lock and unlock.

Continue reading

Posted in C/C++ | 1 Comment

Signal System using Delegates

I introduced how to build your own C++ delegates in the previous post. Now I’m going to show you the first practical (and probably the most common) usage of delegates: signal/event systems.

To clarify the terminology, the mapping between the system I’m going to show here and the terms in the Observer Pattern is as follows: a signal object is a subject object, and a delegate that listens to a signal object is an obeserver object that observes a subject object. The basic idea here is that the listeners that are interested in a specific signal object get invoked when the signal object dispatches. This is a very useful pattern in engine design, which can be used to eliminate busy waiting conditions, because the observers are informed passively as opposed to actively querying the subject for new information (this can be wasting CPU cycles if there is no actual data update).

Continue reading

Posted in C/C++ | Leave a comment

Easy C++ Delegates

As I mentioned in my previous post, I was working on delegates for C++.

The first article I read was recommended by one of my programming TAs. The author’s main concern was portability and performance, so he special-cased extensively throughout his code, in order to make sure the code works perfectly fine across multiple compilers while still yielding optimal performance. This resulted in very verbose and convoluted code. I was half way through comprehending all his code and gave up. The article itself, however, is certainly worth reading; it covers in details the difference between function pointers and member pointers and points out why they are not easy (also not too hard) to deal with correctly.

Continue reading

Posted in C/C++, Design Patterns, Programming | 1 Comment

Function Pointers vs Member Function Pointers

My second semester at DigiPen has almost come to an end. For the project of this semester, we are required to write our code in strict C, no fancy C++ stuff like generics and polymorphism. In order to get around this limitation, our team used a technique described in this post to mimic object-orientation in C.

I’m planning on starting building our team’s new game engine early in this summer to prepare for the upcoming fall semester. This time, of course, we’re allowed to write code in C++.

The first thing I’m planning on building is the event system, which is basically a framework with event dispatchers and event listener functions. But things turned out to be more complicated than I thought. In ActionScript, Java, and C#, the only functions we can define are strictly member functions, functions that are defined in a class body. Even the “main” function is simply the constructor of a document class or a public static void function inside a class. So we don’t have to worry about whether an event listener function is a member function or not.

In C++, however, not everything is object-oriented. A function can be an ordinary function defined in global scope or a member function defined in a class (or struct) body. The invocation to both types of functions are different, so we cannot treat them as equal: we cannot assign them to the same type of function pointer. But I want to treat them equally, so that I can register ordinary functions and member functions of the same signature to the same event dispatcher. This leads me to the research of function delegates.

For now, I’m just going to discuss the fundamental difference between ordinary functions and member functions. I’m also going to point out the difference between function pointers versus member function pointers.

Continue reading

Posted in C/C++ | 3 Comments

Software Design Style

This is an essay for one of my classes at DigiPen. We have to read the first few items of C++ Coding Standards and write a 500-word essay on “one entity, one cohesive responsibility”, information hiding, and abstraction. I thought I might as well just share it on my blog :)

Continue reading

Posted in Programming | 3 Comments

Object-Oriented Programming Synonyms

This post is written for my classmates at DigiPen.

Most of the class have started learning object-oriented programming really for only two weeks, and the professor uses quite a few synonyms that sound really different but are actually the same things. I thought it is very confusing to many new-comers to OOP, so I felt like writing a post to clarify things up. I’ll just group synonyms of the exact same meaning together:

Continue reading

Posted in Uncategorized | 7 Comments

Object-Oriented Structures in C

This is my second semester at DigiPen. We’re not allowed to build our game in C++ for this semester; instead, we can only use C. That means no function overloading, no polymorphism, no generics, and no operator overloading. However, this does not stop me from trying to mimic object-oriented programming with C. My instructor for the programming class last semester, Elie Abi Chahine, mentioned a book called “Object-Oriented Programming with ANSI C” by?Axel-Tobias Schreiner (published in 1993). And that was where I started looking for inspirations.

The book was written almost 20 years ago, and a lot of the sample code is obsolete. After all, now that we have C++ at hands, why would anyone want to mimic object-oriented programming with C? But we have to use C, so I read the book. Half-way through the book, the code started to get ugly. Tons of code that I could not decipher was dedicated to fancy features such as run-time type check and virtual function tables. I decided to stop, because I thought I had learned enough concepts and the second half of the book would be an overkill for our project.

I spent a week trying to work out a feasible design to mimic object-oriented programming with C, and finally I came up with an approach that is simple enough, yet sufficient for our project.

Continue reading

Posted in C/C++, Gamedev | 4 Comments

Astrobunny!

Here it is! Our game project for the first semester at DigiPen. We used a game editor provided by the school called ProjectFUN. I coded in C++.

Download Astrobunny

I basically only used ProjectFUN as a render engine, since I pretty much ported the Easing Equations from ActionScript 3.0, built a tweening engine, created a command framework based on one of my articles, and put together a baby version of Stardust, called StarLite. ProjectFUN provides a built-in physics engine, but I found it’s collision detection system for circles quite buggy, so I implemented one my own.

I built the template levels in the editor, so that we could create new levels just by drag-and-drop. All the planet-orbit linking, orb position correction, initial velocity based on object orientations are all taken care of by my code automatically.

What I’m very proud of our game, is the Credits level. We actually made the Credits screen into a fully playable level, where all the information are shown on planets and speech bubbles.

And here’s a dedicated planet to one of my classmates, Justin. He is such an annoying play-tester (i.e. he play-tested a lot, so I couldn’t use my laptop) that I thought he deserved his own planet :)

So this is Astrobunny. I really enjoyed making it, and I hope you enjoy Astrobunny’s journey through space. Be sure to check out the awesome Credits level, too!

Posted in Gamedev | 7 Comments

Astrobunny ProjectFUN Prototype

Astrobunny is my term project at DigiPen for this semester. We’re building the game with the game engine provided by the school, DigiPen ProjectFUN.

It’s a 2D top-down space-themed puzzle game, where the player controls a bunny in a spaceship that can jump from planet orbit to orbit.

Continue reading

Posted in Gamedev | Leave a comment

Rusher 2 – Renderer2D

Go to Rusher 2 project homepage
Rusher 2 tutorial list
View Renderer2D example
Example source

There is a built-in 2D camera-layer-based renderer system in Rusher 2, which is based on the 2D renderer in Rusher and the rendering system in DigiPen ProjectFUN. You have other options than this, however. As you have already seen, you can implement your own 2D renderer like the DisplayObjectUpdater system in previous examples.

Cameras & Layers

The Renderer2D system models the scene as a camera, a HUD (heads-up display), and layers. The HUD is a sprite you can add display objects to that will be displayed on top of the entire scene and will not be distorted whatsoever. This is useful for displaying HUD elements such as character health, timer, and scores. Layers, on the other hand, will be displaced, rotated, scaled according to their relative position to the camera.

The Renderer2D system uses right-handed coordinate system, meaning the positive z axis is pointing into the screen. The camera’s z position would be its negative focal length. Any layer having a z value smaller than the negative focal length would not be visible, since it is “behind” the camera. The position of every layer is updated every frame according to its relative position to the camera, which you may make use of to create a parallax effect. In addition, setting the Layer.usePerspectiveScale property of a layer to true makes it scale according to the perspective projection equation:

//(x, y, z) is the point to be projected
(projectedX, projectedY) = (x, y) * (focalLength / (z - cameraZ));

Continue reading

Posted in Rusher | 1 Comment