Haxe logo I have switched to a new project at work and it surprised me with the use of a programming language called Haxe. I have just begun, so I will not be able to explain to you all its intricacies, but I am probably going to write some more blog posts about it as I tread along.

What is interesting about Haxe is that it was not designed as just a language, but as a cross platform toolkit, meaning that when you compile the code you've created, it generates code in other languages and platforms, be it C++, C#, Java, Javascript, Flash, PHP, Lua, Java, Python, etc on Windows, iOS, Linux, Android and so on. It's already version 3, so you probably did hear of it, it was just me that was ignorant. Anyway, let's explore a little bit what Haxe can do.

Installing


The starting guide from their web site is telling us to follow some steps, but the gist of it is this:
  1. Download and install an IDE - we'll use FlashDevelop for this intro, for no other reason than this is what I use at work (and it's free)
  2. Once it starts, it will start AppMan, which lets you choose what to install
  3. Select Haxe+Neko
  4. Select Standalone debug Flash Player
  5. Select OpenFL Installer Script
  6. Click Install 3 Items



Read the starting guide for more details.

Writing Code


In FlashDevelop, go to Project → New Project and select OpenFL Project. Let's call it - how else? - HaxeHelloWorld. Note that right under the menu, in the toolbar, you have two dropdowns, one for Debug/Release and another for the target. Let's choose Debug and neko and run it. It should show you an application with a black background, which is the result of running the generated .exe file (on Windows) "HaxeHelloWorld\bin\windows\neko\debug\bin"\HaxeHelloWorld.exe".

Let's write something. The code should look like this, to which you add the part written in italics:
package;

import openfl.display.Sprite;
import openfl.Lib;
/**
* ...
* @author Siderite
*/
class Main extends Sprite
{

public function new()
{
super();

var stage = flash.Lib.current.stage;
var text = new flash.text.TextField();
text.textColor = 0xFFFFFF;
text.text = "Hello world!";
stage.addChild(text);


}
}

Run it and it should show a "Hello world!" message, white on black. Now let's play with the target. Switch it to Flash, html5, neko, windows and run it.



They all show more or less the same white text on a black background. Let's see what it generates:
  • In HaxeHelloWorld\bin\flash\debug\bin\ there is now a file called HaxeHelloWorld.swf.
  • In HaxeHelloWorld\bin\html5\debug\bin\ there is now a web site containing index.html, HaxeHelloWorld.js, HaxeHelloWorld.js.map,favicon.png,lib\howler.min.js and lib\pako.min.js. It's a huge thing for a hello world and it is clearly a machine generated code. What is interesting, though, is that it uses a canvas to draw the string
  • In HaxeHelloWorld\bin\windows\neko\debug\bin\ there are several files, HaxeHelloWorld.exe and lime.ndll being the relevant ones. In fact, lime.ndll is not relevant at all, since you can delete it and the program still works, but if you remove Neko from your system, it will crash with an error saying neko.dll is missing, so it's not a real Windows executable.
  • Now it gets interesting: in D:\_Projects\HaxeHelloWorld\bin\windows\cpp\debug\bin\ you have another HaxeHelloWorld.exe file, but this time it works directly. And if you check D:\_Projects\HaxeHelloWorld\bin\windows\cpp\debug\obj\ you will see generated C++: .cpp and .h files

How about C#? Unfortunately, it seems that the only page explaining how to do this is on the "old.haxe.org" domain, here: Targeting the C# Platform. It didn't work with this code, instead I made it work with the simpler hello world code in the article. Needless to say, the C# code is just as readable as the Javascript above, but it worked!

What I think of it


As far as I will be working with the language, I will be posting stuff I learn. For example, it is obvious FlashDevelop borrowed a lot from Visual Studio, and Haxe a lot from C#, however the familiarity with those might confuse you when Haxe does weird stuff like not having break instructions in switch blocks or not having the protected or internal access modifiers, yet having inheriting classes able to access private members of their base class.

What now?


Well, at the very least, you can try this free to play and open source programming toolkit to build applications that are truly cross platform. Not everything will be easy, but Haxe seems to have built a solid code base, with documentation that is well done and a large user base. It is not the new C# (that's D#, obviously), but it might be interesting to be familiar with it.

Comments

Justin L Mills

OpenFL has had some problems with latest Haxe but I think they have fixed it now might be worth clean install of Haxe and OpenFL, sometimes NME is more reliable ( but does not do web but pretty similar just less documents but you can normally remap all your code easily/automatically it will replace openfl package for the nme package). I think Kha or Luxe is more reliable but I don't often use OpenFL these days so I can't be sure, Kha and Luxe suffer on not having full svg and swf support, but they normally just work and since my weekend time is limited I have less patience when stuff does not setup first time and I know flash really really well so less interest in exploring flash type api's. But they all work pretty good once you get them setup, what is great is you can if you code smart move code between toolkits, try to use "composite" and "flat structures" with openfl, and if your smart it will not be too hard to keep a lot of logic and even some visual structures the same between it and another toolkit.

Justin L Mills

Siderite

Wow! A really detailed reply. Thank you for that! I don't know how much I will be able to explore all of this (when I installed Haxe and OpenFL at work my project stopped working, I had to uninstall everything - I think they use Haxe 2?) but I certainly appreciate the comment.

Siderite

Justin L Mills

There is some c# haxe over on https://github.com/cambiata Flash Develop now has specific version for Haxe - HaxeDevelop - for cross platform you might be interested that Visual Studio Code has quite a bit of Haxe support and works quite well. https://marketplace.visualstudio.com/items?itemName=vshaxe.haxe-extension-pack I believe Private is like Protected in other languages. A theory of Haxe is to trust the developer, so it's always possible to access a field in Haxe, it maybe private but if you need access then you can have access at your own responsibility. https://haxe.org/manual/lf-access-control.html Haxe may have some roots in Flash as well as languages like Ocaml, the first generation of Cross target graphics toolkits were certainly more flash api focused, like OpenFL and NME, but there are now cross target toolkits for Haxe that offer different approaches the first being Flambe. Luxe seems to have good tooling for WebGL/OpenGL, Heaps is also pretty interesting with Hashlink, and Kha give you really low level simple power over a range of GPU tech and has wonderful Blender support via Armory, and hxElectron like a node browser allows all your code to end up as js allowing you access to the world of npm. And there is unity and unreal externs support. Different graphics toolkits offer different advantages worth checking them all out, long term Kha seems to have the edge but other toolkits may offer swf, svg, flump or other advantages so you really need to explore and see what suits you. On your last comment maybe you did not realize HashLink short name is probably H# ;) !

Justin L Mills

Adrian Dumitrescu

Flash Debug Player is only needed if you target Flash Player or want to test in Flash Player and not Neko. It helps with debugging in Flash Develop. Neko also works nice for debugging in Flash Develop. OpenFL is only needed if you want to keep a very similar rendering workflow for Flash Programmers. Brings all the stage API and more to the table. There are other solutions for rendering that work with lime. Also, you don't necessarily need rendering if you're doing server work for instance.

Adrian Dumitrescu

Post a comment