Desktop applications with Haxe and Waxe – part 1: Compiling the samples

Posted on May 19, 2012

9


Haxe (haxe.org), created by Nicolas Canasse,  is amazing. I’ve never before had this feeling of “linearity” between the original programming ideas and the running results. Being able to use the same codebase both on server applications (targeting neko or php) and on the browser client (js or flash) is a huge timesaver. Compiling the same graphic-intensive code to c++, flash and html5 – using the fantastic NME toolset (haxenme.org) is just amazing!

There’s just one area that Haxe didn’t cover for me, and that is native desktop applications. I do a lot of data manipulation, for example on sqlite based files, and using command line tools for that works, but it’s not that… graphic. And my collegues are not that happy with command line utilities.

Due to the great work of Joshua Granick and Hugh Sanderson and the combination of NME and Waxe, a wxWidgets (wxwidgets.org) based toolset for native desktop gui, things are beginning to change. Joshua blogged some time ago about this possibility and here I want to share my experiences on how to get going.

So, here’s what we are going to do:

  1. Compile the Waxe samples
  2. Strip away everything but the basics to create a simple Waxe Hello world example.
  3. Reuse some waxe code to add some missing functionality in one of the widgets.
  4. Start delving into the waxe cpp source code to add some more function, and recompiling the result into a new version of waxe.ndll (the waxe library needed by our app to run)

Let me make clear here that I have almost no c++ experience at all. What I’m presenting here is a result of trial-and-error, not real knowledge… But as the outcome is what I’ve been looking for in Haxe for quite a long time, I can’t resist presenting it to anyone interested!

Also note that I’m working on Windows machines – I can’t say anything about if the same is working on Mac or Linux.

Compiling the following Waxe samples of course requires an installation of Haxe – versions 2.08 or 2.09 should work. NME and Waxe are of course also needed, and they can both be downloaded via haxelib.

Haxe 2.09 note!

Haxe 2.09 handles the neko 31 bit limitations a bit diffently than 2.08. For the following to work in 2.09 you might need to change the wx.Window, lines 51-54 from…

   public static inline var HSCROLL = 1<<30;
   #if !neko
   public static inline var VSCROLL = 1<<31;
   #end

…to

   #if !neko    
   public static inline var HSCROLL = 1<<30;       
   public static inline var VSCROLL = 1<<31;    
   #end

Compiling the Waxe 1.0.1 samples to Neko

If you didn’t know, the neko virtual machine (http://nekovm.org/) is a part of the Haxe installation, and makes it possible to run neko-compiled haxe solutions on win, mac and linux.

Navigate to your Haxe installation directory and look for the lib/waxe/samples folder. (On my XP installation it’s C:\Program Files\Motion-Twin\haxe\lib\waxe\1,0,1\samples, on my Win7 it’s C:\Motion-Twin\haxe\lib\waxe\1,0,1\samples). We start with the 01-Simple example, so open a command prompt window in that directory.

Now, run the following command:

> haxelib run nme test Simple.nmml neko

Tada! NME takes care of the process by first generating some classes (for example ApplicationMain.hx) and then compiling it into a neko vm executable: bin/neko/windows/bin/Simple01.exe. It also copies the needed dlls and the waxe.ndll to that destination. Finally it should run the Simple01.exe for you:

Nothing fancy at all, but a good start!

The second example could be compiled the same way. Navigate to {waxe-path}/samples/02-Dialog, and run the following command:

> haxelib run nme test NmmlEdit.nmml neko

The following application should be created and displayed:

A bit more complex this time. Please note that the “Haxe Libs” tab should display the haxelib libraries that you have installed on your system, and that the “Class Paths” tab should open a browse dialog if the path list is doubleclicked.

Great! Up and running with native desktop gui apps with Haxe and NME/Waxe!

Compiling the Waxe 1.0.1 samples to c++/Windows

Unfortunately, what’s described above doesn’t work right now when trying to compile using cpp to a native Windows app (not dependant on the need of neko vm to run). Trying the following…

> haxelib run nme test Simple.nmml windows

…should be the way to go, but right now ends with the following error:

applicationdomain.cpp(89) : error C4716: 'ApplicationMain_obj::getAsset' : must return a value

UPDATE:

Thanks to Andreas Mokros (https://groups.google.com/d/msg/haxelang/K9HssibfNZE/aY0ZOLUeN_0J), this can now be sorted out! The problem is that the NME template for ApplicationMain.getAsset() method doesn’t return anything at all if the target is not set to neko.

This can be solved by changing the [\]Motion-Twin\haxe\lib\nme\[3,3,1]\tools\command-line\haxe\ApplicationMain.hx getAsset() method in the following way:

public static function getAsset(inName:String):Dynamic
{
#if nme
::foreach assets::
if (inName == "::id::")
{
::if (type=="image")::
return nme.Assets.getBitmapData ("::id::");
::elseif (type=="sound")::
return nme.Assets.getSound ("::id::");
::elseif (type=="music")::
return nme.Assets.getSound ("::id::");
::elseif (type== "font")::
return nme.Assets.getFont ("::id::");
::else::
return nme.Assets.getBytes ("::id::");
::end::
}
::end::
return null;

#elseif cpp            // <---- Add this line...
return null;    // <---- ...and this one!
#end
}

Now, it should be possible to compile the 01-Simple sample with the following command:

> haxelib run nme test Simple.nmml windows

The 02-Dialog NmmlEdit.hx is refering to neko.io.Process, wich obviously doesn’t work on a cpp target. The following lines have to be added around line 11:

#if neko // <-- Add this line
import neko.io.Process;
#elseif cpp // <-- and this one
import cpp.io.Process; // <-- and this one
#end // <-- and this one!

Now, it should be possible to compile with

> haxelib run nme test NmmlEdit.nmml windows

Thank you, Andreas!

Advertisements
Posted in: Haxe, Waxe