Oct 2, 2018 - SDL_RenderCopyEx


SDL_RenderCopyEx is a function in SDL2 that allows you to rotate, scale and flip sprites. But how do you use it? I was wondering the same as I wanted to add bullet trails to C-Dogs SDL. The official documentation tells you what parameters to use but not how they will look. So I just tried it out myself:


This is a simple demo project which uses SDL_RenderCopyEx to rotate, scale and flip a sprite. There’s also a center parameter which controls the center of rotation, which defaults to the center of the sprite you are rendering.


Pretty simple, although you’ll probably want some boilerplate code like I did:

	int render(
		SDL_Renderer *renderer, SDL_Surface *s, SDL_Texture *t,
		int x, int y, double angle, double sx, double sy, SDL_RendererFlip flip)
		SDL_Rect dst;
		int res;
		dst.x = x - (s->w * sx) / 2;
		dst.y = y - (s->h * sy) / 2;
		dst.w = (int)(s->w * sx);
		dst.h = (int)(s->h * sy);
		return SDL_RenderCopyEx(renderer, t, NULL, &dst, angle, NULL, flip);

Source code here: https://github.com/cxong/sdl2-rendercopyex-demo/blob/master/main.c


By using SDL_RenderCopyEx to rotate and stretch sprites, it can be used to create bullet trails, like this in C-Dogs SDL:


Aug 16, 2017 - Three Factions


Some of the most fun and interesting games feature three factions. The number 3 is a magic number, allowing for interesting asymmetries. You often see factions that are unique, provide lots of gameplay variety, and are flavourful too. But how do you mechanically balance three radically different factions? And how do you come up with three thematically unique factions?

It’s not easy. With just two factions, balance is a lot easier, as you simply buff one and nerf the other, but with three (or more) factions, anything you do could inadvertently affect the balance with the third faction.


One common method is to take a classic duality, and have two factions represent each extreme, with the third, “balanced” faction in the middle. TVTrope’s article on the subject mentions the classic duality of powerhouse/subversive. So one faction is a powerhouse, favouring raw strength, the second is subversive, favouring clever tactics and engagements, and the third balanced between the two.

Yin Yang

How does this work? The important thing is that there is no inherent advantage to either side of this duality. The powerhouse is powerful but slow and predictable; the subversive is weak but fast and flexible.

A great example of this is in Westwood’s Dune RTS games and the unique tanks of each faction. The “evil”, powerhouse Harkonnen have the Devastator, the most powerful in the game but slow and ponderous. The “insidious”, subversive Ordos have the deviator, a tank which has no attack but can temporarily take over enemy tanks. The “noble”, balanced Atreides have the sonic tank, which apart from having a unique attack, is otherwise equivalent to a heavy tank.

Devastator Deviator Sonic Tank

Another example of this (and of three factions in general) is, of course, Starcraft. The Protoss tend to have strong yet slow units, the Zerg have weaker, faster units and can expand faster, and the Terran are somewhere in between.

Duality also works thematically. In the classic good/evil dual, the middle ground is neutral - sometimes good, sometimes evil, but perhaps just driven by a morally ambiguous motive, like rational self-interest. A great example of this is the alignment system in Dungeons and Dragons, which also has another duality - law/chaos.

D&D Alignments

If you think too hard about this you’ll grow a neckbeard

There are lots of thematic duals to consider. Magic/spirituality vs. technology/science, biology/nature vs. industry, East vs. West, men vs. women… endless examples if you look for it.

The problem with the “balanced”, “neutral” faction in dualities is that they can seem bland compared to the two extreme factions. To compensate, designers usually give them something unique. The neutral faction often has special abilities or some other desirable quality.

D&D Alignments

No one picks the boring man

For example, in Golden Axe the man is clearly supposed to be the “main” character, but because he is worse than the woman at magic and worse than the dwarf at physical attacks, most players avoided playing him in favour of the other two.

C&C Generals USA Air

On the other hand, in C&C Generals, the balanced faction the USA is not as strong as China and not as subversive as GLA, but are compensated mechanically with having the most units with special abilities, and thematically by being the good guys. USA’s tanks can be augmented with special drones, and their infantry tends to have better special abilities, making them the most versatile faction.

Radical third

Making the balanced faction more special works fine, but if you take this to its extreme, you get another approach: the radical third. Instead of having a faction that’s in-between a duality, it is so different from the other two that it’s not part of the duality at all.

An interesting way to make the third faction mechanically different is to make the rules work differently for them in some way. One is to give them different objectives entirely. In the Dune novels, the Emperor and Landsraad are in a balance of power, but the discreet Spacing Guild is often so influential that they can be kingmakers. What’s unique about them is that their objective is not the accumulation of power or wealth; their raison d’etre is to maintain that balance because for them, “the spice must flow”. A bit like the British’s foreign policy on maintaining the balance of power in Europe.

Dune Guild

In Rise of Legends, the radical third faction, the Cuotl, are not only thematically unique - they’re aliens - but are logistically different, with different ways of gathering resources.

In Red Alert 2: Yuri’s Revenge, the Yuri faction has a unique mechanic: mind control, where enemy units can be converted to their own. This makes engagements very different, and mechanically the faction plays very differently to the other two. The Yuri faction was first seen as the weakest faction, but as players mastered the mind control mechanic, over time the faction has become the strongest; so strong that they are often banned at top-level tournaments!

Yuri Clones


And this illustrates one pitfall of having radically different factions; they become very hard to balance, and the balance can shift as the metagame evolves. Balance becomes an endless process, as seen by Starcraft’s two-decade legacy of patches, and constant shifts in dominant factions and strategies.

When it comes to radical third themes, a few usual suspects tend to pop up:

But there are also other creative choices. Criminal syndicates, hive minds, religious zealots, and so on.

Faction calculus

There are other ways to balance three factions. Consider rock paper and scissors, where rock beats scissors, scissors beats paper, and paper beats rock, and apply this over the whole game.


Your game probably shouldn’t be this complex

For example, let’s say there are factions A, B, and C. A’s infantry are the best, B’s mediocre, and C’s worst. But then maybe A’s vehicles are worst, B’s best, and C’s mediocre. And the situation is different yet again with aircraft, with A’s being mediocre, B’s worst, C’s best.

Thus, any non-mirror matchup contains plenty of asymmetries, but no faction has a clear advantage over the other, providing good balance and unique gameplay.

This is how Red Alert 3 is balanced; its three factions have different relative strengths in land, air and sea units, and is often considered the most balanced game of its series.


Asymmetries can also exist across space and time. Some factions could be weaker at the beginning of the game but become stronger in the late game. Some factions could be stronger on some maps or levels than others, such as factions with strong navies in water-dominated maps.

But there are downsides. All this mechanical complexity can overwhelm players, making the game feel over-designed and rules-heavy. Players often need to manage this extra faction calculus on top of the rock-paper-scissors structure of the base game, muddling the strategic aspect of the game. It also makes it harder to design things that affect the game’s rules, such as new maps, or new units. For example, a game with a heavy navy-based faction calculus means that new maps must have the “right” amount of water or risk being unbalanced - too much and the best navy faction dominates, too little and the opposite happens.

Complex mechanics can be a detriment to thematic design; it is harder to design memorable factions with unique characteristics when the mechanics are all over the place.

As is often the case with design, less is more.

Mar 6, 2017 - 3D Rendered Pixel Sprites


In the recent C-Dogs SDL release, I introduced a new walk cycle animation using pre-rendered 3D sprites; check it out (before, after):

C-Dogs SDL walk cycles

The content pipeline goes like this: Blender, bash/python render scripts, Imagemagick touch-up, montage spritesheet. The results are surprisingly good, given that no manual retouching was required; I can directly edit the blender file, and create updated spritesheets.


Initially I was leery of this whole project. Pixel art is usually done by hand and laborious enough already; creating animations just makes the amount of work explode. The original sprites only had 4 directions (left/right can’t be mirrored due to the gun-handedness), 2 poses (unarmed and holding gun), with idle (1 frame) and walk (4 frames) animations, for a total of 4 × 2 × (4 + 1) = 40 frames. The project was to show all 8 directions with an improved walk cycle of at least 8 frames, which would make the total number of frames balloon to 8 × 2 × (8 + 1) = 144 frames. That’s a lot of work to do manually!

Not only that, but there are future plans for more improvements, like “textures”, different body types, different animations, each of which multiplies the amount of sprites.

Obviously the alternative is to model and animate the whole thing in 3D. But there are big questions around this approach. Will the animations look as good? Can rendered sprites avoid that “rendered” look?


This is what pre-rendered sprites looked like in the 90’s


I haven’t really used Blender for 15 years, and I remembered it for being a very hotkey-heavy and idiosyncratic application. Amazingly, its UX hardly changed in all these years, so it was every bit as hard to use as before. Fortunately, these days we have so many free Blender projects to use as examples (e.g. from opengameart.org), and great resources like Blender Stack Exchange, it was really easy to pick things up again.

For rendering spritesheets from Blender, Clint Bellanger’s Isometric Tiles in Blender article is invaluable.

From here it’s just a matter of struggling and applying animation principles, to go from a nice base model to the proportions of the C-Dogs potbellied “Cyberdogs”:

Blender model


To render a spritesheet, we can’t just use the built-in render settings in Blender, as we’ll need to render multiple animations and multiple camera angles. Here’s where Blender’s python scripting comes in handy.

Personally I’m not really keen on learning a whole new scripting language just to get one small thing done. But Blender makes this as easy as possible by adding scripting tooltips throughout the application, so it’s super easy to know what to search for if you want to transfer something from Blender to script.

Blender tooltips


Blender can render individual images, and from here we can use Imagemagick’s montage to stitch them together into a spritesheet. But Imagemagick has more use than that.

You see, it’s very hard to avoid that “3D rendered” look from Blender. There are ways to make it look more like 2D sprites, like cel shading, and other more specific shaders, but each has a distinctive look. It can take a lot of work to set up the lighting and shading to get the right effect.

Instead, a more economical way would be to use Imagemagick’s image processing functions to tweak the output image directly. I used a simple level shift function to get the curves to match up with the old sprites.

Spritesheet levels

Final Result

Final walk cycle

The final result is surprisingly good, given how relatively simple the processing was - no special shaders used. I was a bit worried that the renders are a bit rough, but I guess when animated you don’t really notice.

If you’re currently thinking about doing a lot of sprite work but are worried about the quality and the amount of work required, definitely give this workflow a try.