Haxe: C-style enum macros

I've recently stumbled upon this little macro by Justo Delgado. It takes an "enum abstract" and sets the values of it's constant-fields incrementally. This brings up an interesting point.

See, in C, you can leave out the values and have them assigned automatically (0, 1, 2, ...), set the values manually, or mix the two, having the values continue "upwards" from the last specified value. So, for example, if I have this,

enum class Op {
	Add = 0x90, Sub,
	Mul = 0xA0, Div, Mod,
};

Op::Div will be equal to 0xA1, which is convenient, and allows grouping enum constants together (in this case storing operator priority in the upper 4 bits).

Haxe doesn't have that "out of box", unfortunately - either you make an actual enum and have the values assigned automatically, or make an @:enum abstract and assign the values manually.

But, of course, that can be fixed with a macro.

Continue reading

Haxe: Shorthand expression matching

This is a small post about a small, useful macro for Haxe.

As you may (or may not) know, Haxe has fancy enums (which are actually algebraic data types). That's nice. And you can do all sorts of fancy value matching on them, which is also nice.

What is less nice, however, is that the only conventional way to extract enum arguments is to "match" it, and thus in some situations you can only do code like this,

var r = switch (expr) {
    case one(k): k;
    default: -1;
};

adding a couple lines of code in place of what feels like it could have been a single operator.

So I wrote a tiny macro which adds just that:

var r = ematch(expr, some(k), k, -1);
Continue reading

Top-down bouncing loot effects

GIF
In action

A little while ago, I was asked about what would be a good approach to creating an effect for a top-down game where coins would fly out of a smashed object. Not recalling any tutorials on the matter, I've made an example of this exact thing, and this is a post detailing everything related to such an effect.

Continue reading

C#: Implicit conversion from null to struct (and more)

MyStruct some = null; // some == MyStruct(...)

This is a small post about a rather specific construct in C# syntax, permitting to assign "null" to struct-typed variables (which are non-nullable) and treat this specific case as you please.

If you did not come here looking for this particular thing, you may or may not be in a dire need of the post's subject, but explanation may be of interest.

Continue reading

Introducing: hxpico8

Have you heard of PICO-8? It's a "fantasy console" with little built-in sprite/code/level/sound/music editors and a carefully crafted spec. And (slightly changed) Lua scripting. And a web player export (example). A rather interesting option if you like working with restrictions and/or tiny pixelart.

Long story short, I've made a little Haxe compiler target that generates compact Lua code that runs on PICO-8. This post covers reasoning, some technical details, and tricks used to accomplish this.

Continue reading

Introducing: BitFontReader

BitFontReader screenshot

Let's talk about the process of making pixel fonts for a bit.

At first, it may seem like pixel/bitmap making fonts should be an easy process. After all, there were lots of pixel fonts back in the day, right? Although those were primarily raster FON files, while vast majority of modern programs require you to supply fonts in either TrueType (TTF) or OpenType (OTF) formats, both of which generally imply vector-based representations of glyphs (symbols).

Obviously, drawing a pixel font in a vector editor is as bad of an idea as it sounds.
Fortunately, there's a bunch (more like a couple) of programs specifically made for creating pixel fonts. However, these naturally imply that you will have to [re-]draw your font in the program itself, regardless of whether the program is to your tastes or not.

At this point one may be wondering, why isn't it possible to just draw the font in the pixelart editor of your choice and have it automagically converted into an actual font.
After thinking about this for some time (while trying to find "right-looking" fonts), my patience kind of ran out, and I've decided to make such a thing.

So I've made a program that does just that - you give it an image of your font, note the letters/symbols written in it, and get a code that you can import into BitFontMaker2 to make it into an actual font. It's small, clean, and runs right in the browser. Sounds good to be true? See for yourself:

Launch BitFontReader

(note: if the program does not load, change HTTPS in the URL to HTTP)

Hosting HTML5 games on Google Drive

Hosting HTML5 games on Google Drive for free

Being able to quickly upload a HTML5 game to the web is important.

It's not just a more comfortable format for sharing, but sometimes a requirement, since games created with GameMaker: Studio, Construct 2, Haxe+OpenFL and many other tools may not necessarily fully function when launched locally due to browsers laying restrictions over local file access (meaning that sending a ZIP with game files may not quite work).

While it used to be possible to host HTML5 games on Dropbox for free (or, rather, it still is possible, but only if you have enabled the public folder before the late 2013, else it'll cost you some), you can still host HTML5 games freely on Google Drive.

And this article explains the process of hosting your games on Google Drive in detail.

Continue reading

BitFive: Now with multitouch

This is only a small thing out of everything I need to make/finish/release in next months, but...
So there is now multi-touch support in openfl-bitfive, which is an alternative OpenFL HTML5 backend with focus on blitting and compatibility across platforms.
Usage is identical to Flash/AIR - you add an according event listener to your desired DisplayObject, and handle the TouchEvent objects it catches:

stage.addEventListener(TouchEvent.TOUCH_BEGIN, function(e) {
	trace("Touched at (" + e.stageX + ", " + e.stageY + ")");
});

And, same as it goes for mouse events, the rest is handled behind the scenes - events are routed automatically, and you don't need to worry about browsers and devices with partial or glitchy support.
You can even have your mouse events converted into touch events for ease of testing if you want (see bitfive_mouseTouches flag).

Addition is already live at both repository and haxelib, there's a demo available (probably want to view it from mobile device), and this post contains some extra details on what it took.
Also, if you needed this for some time, you can pretty much thank Ozdy (which is also the largest supporter of backend) for suggesting the feature.

Continue reading

Haxe: Simplistic .properties parser

If you have ever been using Java, you may already be familiar with .properties file format. This is a simplistic configuration file format, somewhat similar to INI:

# Comment
key = Value

For ones not liking that, there's also alternative syntax:

! Comment
key: Value

Where leading/trailing spaces are optional and are trimmed in parsing process.

Since I've seen people ask about this kind of thing, and also have seen people invent ridiculous (and non-functional) regular expressions to parse this file format, I thought that I may as well make an implementation that works "just right".

Continue reading