Posts Tagged ‘C++’

C++ WebSocket server – Source-Code released

Hey folks,

I open sourced my C++ WebSocket Server implementation. It is a pretty basic C++11 server running on POSIX/MacOSX systems. It supports the most recent WebSocket implementation and works fine with Chrome/Safari and Firefox. Windows is not supported on purpose.

It was an awesome learning process to write it and I learned so much about HTML etc, that I can suggest for anyone who is interested in low level network programming to do the same.

The server supports MYSQL, JSON and runs on multiple threads. So quite cool 🙂

The read loop is not yet finished tho.. If you for example stream a big video file, the current thread will be blocked until it is received. That is a no-go and i am working on fixing it!

The runloop is based on select (shame on me i know) and uses the same principle NodeJS uses for example. One steady loop with event callbacks.

Check it out:—Websocket-Server

OpenSource OpenGL 2D RPG Engine (iPhone)

April 10, 2012 8 comments

Hey guys,

if you are following this blog long enough than you surely know that I was writing a 2D RPG Engine about 9 months ago. Well, i stopped the project at the end of last year due to more interesting stuff but had a lot of questions in the last time if I couldn’t make it available for the community.

So here we go.. Download the Engine Source code here:

READ THIS: – The code is by NO MEANS perfect. It was the first time that I approached a big project like this, so stuff is sometimes a bit messy. Especially the implementation of the script engine is nothing i am proud of but ok 🙂 I think for learning purposes it is pretty good. The engine can render up to 3000×3000 tiles on 60 fps on a ipod 4th gen.. That is quite powerful 🙂

Okidok! Have fun..

P.S. The source code for the Level Editor written in Cocoa and C++ will be online next week! Stay tuned 🙂

Cocos2dx Extensions: CCValue

March 25, 2012 1 comment

Hey guys,

i added another small extensions to my github. It is a template class which can be used like the NSValue class from the Cocoa library so basically speaking it allows you to store structs like CCRect/CCPoint in CCArray and CCDictionary which can be incredible useful sometimes.

Clone it here:

Some example code:

CCRect t1;

t1.size = CCSizeMake(100, 200);

t1.origin = CCPointMake(10, 33);

CCValue<CCRect> *value = CCValue<CCRect>::valueWithValue(t1);

CCArray *array = CCArray::arrayWithCapacity(2);


CCRect t2;

t2.size = CCSizeMake(100, 200);

t2.origin = CCPointMake(10, 33);

CCValue<CCRect> *v2 = CCValue<CCRect>::valueWithValue(t2);

CCValue<CCRect> *v3 = static_cast<CCValue<CCRect> *>(array->objectAtIndex(0));

CCValue<CCRect> *v4 = CCValue<CCRect>::valueWithValue(v3);

CCRect rectv4 = v4->getValue();

rectv4.size = CCSizeMake(10,10);

if (v3->isEqualToValue(v2))




if (v3->isEqualToValue(v4))


    CCLog("still equal");


Cocos2dx Extension – Vertically and Horizontally flipped Tiles

March 18, 2012 2 comments

Hey guys,

for a new game i am currently working on, I decided to use the cocos2dx library in combination with the tiled level editor. After writing several 2d engines from scratch I decided that it is just too much work to get it really really right (especially with a this cool features, that modern games must have) and that I actually wanted to start making a game. The library its just awesome and was currently released as version 1.0.

One thing I found out yesterday that there was no support for flipped tiles when exporting a map from the Tiled Map Editor. I think that this was a big shame and digger into the source codes of both, Cocos2dx and Tiled to find out the right way to to implement it. You can get the final result here:

It was actually pretty easy. Tiled Editor stores all maps in TXML format which is actually a dialect of the XML format. When saved uncompressed, you can see that maps are stored like this:

<layer name=”Tile Layer 1″ width=”5″ height=”5″>
<tile gid=”1″/>
<tile gid=”2147483649″/>
<tile gid=”0″/>
<tile gid=”2147483650″/>
<tile gid=”2″/>
<tile gid=”1073741825″/>
<tile gid=”3221225473″/>
<tile gid=”0″/>

In this example, the tile vids with 1 and 2 are the original tiles 1 and 2 unflipped. This high integer values are the tile values for flipped tiles.

I found out, that the guy who implemented flipping in the Tiled Editor uses two bit flags to check wether a tile is flipped or not.

Horizontally Flipped = 0x80000000

Vertically Flipped = 0x400000000

So in order to determine if a tile is flipped we just have to check against these two flags and than clear the gid. Pretty easy 🙂

To use it, download the two source files and replace them with those in you current cocos2dx library. It will work out of the box (supposed that you don’t have made any changes on your own) 🙂

The only affected cocos2dx method is CCTXMLLayer::appendTileForGid( … )

It is a very basic implementation until now and supports the flipping of tiles at the parsing stage. If you want to change or add tiles at runtime you are on your own 🙂 But I believe most of us won’t do it.

Have fun!


Hint 1: Using flipped tiles reduces the size of your tile set dramatically and allows for more proper level design.

Hint 2: Press ‘x’ or ‘y’ to flip on the respective axles.

Pictures of my new game:

How to write a generic delegate class with C++ and Templates. (Tutorial)

Delegate Tutorial C++

Good morning, it is a cold but beautiful Saturday right now in Amsterdam and I decided to write a new tutorial for my blog. This time not about Objective C but about the world’s sexiest things after microchips: C++, templates and function pointers to methods.
During the last week I was improving code in my game engine and while implementing a messaging system for my objects I stumbled over the problem that function pointers are actually great but don’t work fine with class methods.
C++11 has this great template std::function which I adore and that can handle class methods with std::bind as well. But in my situation it was not suitable and so I had to think of my own solution.

The problem:
The problem I had to solve was to implement an EventHandler system which can take any arbitrary function or class method. In ActionScript, the coder can call the method stage.addEventListener (, …) to add listeners to certain objects which will than receive events, like in this case mouse clicks. I adored the idea behind it and wanted to implement something like this as well.

REResponder -> Callback to Class Methods:
Our function will have to overloads. One will take a lambda function and the other one will take a class method which gets called when it receives an event. The lambda overload will take a std::function argument while the class method overload will take our own class. In this tutorial I will only write about the Responder method as the lambda method is actually pretty easy to implement.

So let’s see what we have:

typedef void* id;			// thanks to apple for that typedef

template <class T>
class RESelector: {
     RESelector(T* _target, void (T::*_func)(id, RE_MSG_CODE), id _msgObj,  RE_MSG_CATEGORY _category){
         this->target = _target;
         this->category = _category;
         this->msgObj = _msgObj;
         this->actionFunc = _func;

    void fire (RE_MSG_CODE code){
         (this->target->*actionFunc)(this->msgObj, code);

    Inline RE_MSG_CATEGORY getCategory() const {
         return this->category;

    T* target;
    id msgObj;
    RE_MSG_CATEGORY category;
    void (T::*actionFunc)(id, RE_MSG_CODE);

typedef std::vector<id> SelectorCache;
typedef std::vector<RE_MSG_CODE> Messages;

class REMessageHandler {
	Messages *msgCache;
	SelectorCache *selectorCache;
	template<class T>
	void addEventListener(REResponder<T> *responder) {

        Inline void postEvent(RE_MSG_CODE code){
	void evaluateMessages();

First of all, don’t worry about RE_MSG_CODE and RE_MSG_CATEGORY. It is just my way of decoding messages but doesn’t actually influence how the code works. RE_MSG_CODE is actually the code itself like KEY_A or MOUSE_LEFT while RE_MSG_CATEGORY is the category the object has to listen to like RE_MSG_CATEGORY_TOUCH or RE_MSG_CATEGORY_RUNLOOP.

So what is happening here? We have a template class called RESelector which consists of not much but 2 methods and a constructor.
The field variables are a pointer to a target which is the template type. An enum category which is the category the object will listen to, a pointer to a member method from a class of T and an id (or void*) to an object which gets send with the message.
Than we have a std::vector that takes id’s and finally the class REMessageHandler. This class is by no means complete but it will do the job for this tutorial.
We have 2 pointers to vectors in this class. One takes MSG_CODES while the other one is id vector. You will ask, why do we store raw pointers in a vector that should store objects from RESelector? Well, we only need the addresses stored  and because what we will store is actually a template class, we are not able to do: std::vector . We would have to define a std::vector FOR EACH template and this would suck.
The two corresponding messages are straightforward. Both push back stuff on the right vector.
Let’s move on to the most interesting method of this class.
Receiving Messages

void REMessageHandler::evaluateMessages() {
    for (auto it = this->selectorCache->begin(); it != this->selectorCache->end(); ++it){
	RESelector<__tPlaceholder> *selector = (RESelector<__tPlaceholder>*)*it;
	If (selector->getCategory() & RE_MSG_CATEGORY_INPUT) {
            for (auto sec_it = this->msgCache->begin(); sec_it != this->msgCache->end(); ++sec_it) {
	        if (*set_it & RE_MSG_CATEGORY_INPUT) {
		     RE_MSG_CODE code = (RE_MSG_CODE) ((*sec_it) & (~RE_MSG_CATEGORY_INPUT));
		     selector->fire (code);

Wow.. Looks kinda weird right? I try to explain. We first iterate through the selectorCache which contains all the objects which are registered to listen for events.
As the selectorCache stores only addresses, we need to cast the object to the right type. RESelector is a template class so can’t create a pointer without a type. Of course we want it as generic as possible, so making the method itself a template message is absolutely no option. I came up with the idea to define a struct __tPlaceholder which is just empty. The typedef looks like this

typedef struct __tPlaceholder {} __tPlaceholder

and it’s only purpose is to act as a placeholder. So we create a pointer to an RESelector of type placeholder and assign the value of the iterator to it. Nice!
The next step to check is to what category the selector is listening to. In this example I only implemented RE_MSG_CATEGORY Input so we check if it does and if yes we iterate through all the messages. For each message we check if it is a message for the input category and if yes we fire.
For completeness, this is how a message gets encoded and decoded:

RE_MSG_CODE encoded=(RE_MSG_CODE)(category|code);

With this bunch of code we can now easily make stuff like this

class Foo {
	void mouseCallback(id obj, RE_MSG_CODE msg) {

	     dbgPrint(“I received msg %x from %p”, msg, obj); 
                 case RE_MSG_MOUSE_INPUT_KEY_UP: {
                     // do
                 default: break;

void Runloop::Init(){
	Foo *foo = new Foo();
        RESelector<Foo> selector = new RESelector<Foo>(foo, &Foo::mouseCallback, this, RE_MSG_CATEGORY_INPUT);

bool Runloop::Run() {
        return this->goOn();

And every time somewhere gets a input message posted the mouseCallback function in Foo gets called.

Cool? Isn’t it? There are still areas to improve of course but I will leave this to you to figure out what the best approach for your problem is.

Well. That was a huge tutorial and I hope I did an ok job. If you like it I would appreciate a small comment or 


October 21, 2011 4 comments

Hey Guys,

attached a BRAND NEW GAMEPLAY VIDEO from my Game. It shows random encounter and the Turn Based Battle Engine. Until now I’ve not implemented a lot of features but time will tell.

I AM STILL LOOKING FOR PARTNERS!! The team is growing and there is already a Composer on Board, but I need still Level Designers, AI Coders, Item Scripters and A STORYWRITER!!!!

Don’t hesitate to contact me!! We can make great things together!



The BattleEngine


finally I started with the Battle Engine. I was discussing a lot with myself if I really should go for a Turn Based Battle System .. Nearly all successful RPGs out there are Action Real Time Combat with a lot of Kaboom and Kabang and so I thought i should maybe do the same.

But fuck it… My first plan was to bring the Old Feeling back, and so I will stick with it. Attached a cool Screenshot from a Combat Scene.  🙂

Tomorrow I will bring some Logic in ..