Tony Polinelli
Tarwin Stroh-Spijer

contact [at]
touchmypixel.com

6/25 Easey Street
Collingwood, 3066
Vic, Australia

+61 3 8060 5321

Our plans for haXe C++

hxcpp?

We've been recently looking at expanding our skills of games dev, to finally break out of Flash! (The whole platform that is.) Looking at Unity3D (seems to be a great platform) but also interested in getting a little closer to the root of most great games... C++.

Coming from a web background, and developing in Flash for about 10 years, I don't want to abandon all of my existing knowledge, and quite frankly, after learning C++ for a few days, it looks kinda clunky - or maybe just scary ;P

Luckily, Hugh Sanderson has been developing a C++ target for haXe (the open source Flash compiler) which after a little testing seems to look like it will take a LOT of the pain out of the transition.  For those who don't know, haXe is a multipurpose compiler, it can compile code, similar to ECMAScript, to many targets. Some of these being:  SWF bytecode (which out performs the Flex and Flash compilers),  JS, PHP, and more. This new C++ target will allow people to build a game which can compile to a SWF & C++.  Why would would a C++ and SWF output be good you ask? If PC, Mac and Linux arn't enough, iPhone can natively run c++ for instance.  If we want to make a game which will need more performance than Flash can offer or just want to target C++, at the very least it will allow us to develop games in the same style, and code base, as our existing efforts.

Many of the technicalities are still being worked on as the project isn't fully released as yet, but there are some great 2D tests, and some varying in progress OpenGL 3D. I was amazed that Hugh has even included a SWF reader in neash which will allow import of SWF assets and animations to your C++ games - wow!

Getting stated with hxcpp (haXe C++)  with Visual Studio C++ Express

I've been through some trials working out installation (as I've never compiled C++ before),  so i thought it would be good to share my findings for other noobs ;P

So without further adieu here's what you do.

haXe installation and setup for hxcpp

  1. Download and install haxe
  2. Access the the windows command prompt (console): Start->Run->"cmd"
    note you will have to close and re-open the command prompt after making any changes to environment variables before they will take effect in the command promt
  3. Type "haxelib install hxcpp". This will install the hxcpp library for haXe.
  4. Type "haxelib install neash". Installs the neash library.
  5. Type "haxelib install nme". Note this may already be installed as a dependency.
  6. Open your System Properties dialogue (Start->Control Panel->System), go to the Advanced tab, and then click Environment Variables button. Here we'll be adding some environment paths. You can add them either in the User variables (the top list, which will only work for the current user) or System variables (the bottom list, which work for all users).
  7. Set an environment variable called "HXCPP" by clicking New under the list and entering Variable name "HXCPP" and Variable value to hxcpp install directory ie:  "C:\Program Files\Motion-Twin\haxe\lib\hxcpp\0,4"
  8. Add "%HXCPP%" to the PATH variable, making sure that it is seperated with a ";" and no space.
  9. Add "%HXCPP%\bin\Windows"  to the PATH as well. This is so haxecpp.exe can be found.
  10. Type "haxecpp" in the command-prompt to check if it finds the compiler

Microsoft Visual Studio C++ Express intallation and setup

This is how to setup MSVSC++ for command-line compiling:

  1. Download and install Microsoft Visual Studio C++ Express from http://www.microsoft.com/express/vc/
    Once these are installed, we need to set up some Environment Variables to help it work. As far as we can tell it's meant to do thi stuff itself, but never seems to.
  2. Add vc\bin (location of vc.exe) to the user PATH  eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin"
  3. Type "cl" into the command-promt. This will see if your C++ compiler is working correctly. It will probably throw an error, saying that it cant find some DLLs.
    1. If you get the error run the cl setup eg:  "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
    2. Run "cl" again. If you still get the error, follow next step.
    3. Add Visual Studio 9.0\Common7\IDE to the user PATH (similar to before)  eg: "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE"
    4. Run "cl" again, and you should get the compiler usage message.
  4. Add an environment variable INCLUDE which points to the vc/include folder eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\include"
  5. Add an environment variable LIB which points to the vc/lib folder eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\lib"
  6. Add another path to the LIB variable to points to the folder containing kernel32.lib eg:  "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib"
  7. For the hxcpp 1.0 release you also need another step
    Add another path th the INCLUDE which points to the SDK Includes - eg.   C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include

Now you should be ready to compile some C++!

You may find you have to restart your computer for the Environment Variables (PATH etc) to take effect globally in your system. No idea, but restarting Windows seems to fix it.

Hello World Test

make a main class

Main.hx:

Actionscript:
  1. class Main
  2. {
  3. public static function main()
  4. {
  5. trace("hello world");
  6. }
  7. }

I tried to get hxml files working on my system (assign 'open with...' to the haxecpp.exe, but it kept reverting to haxe.exe) so you can use a simple batch file as follows. Note the "-cp" command tells the compiler where to find the standard libraries for the new hxcpp.

make_cpp.bat:

CODE:
  1. haxecpp.exe -cpp cpp -main Main -cp "C:\Program Files\Motion-Twin\haxe\lib\hxcpp\0,4\std"
  2. cd cpp
  3. nmake -nologo
  4. Main.exe
  5. pause

This should compile the haxe, compile the cpp, run Main.exe and pause to show your trace.

Compiling the neash and nme examples

Remember to check out the neash and nme examples (found in the haxe/lib/xxx/samples  folders)  to see how to get started.

Just make a make_cpp.bat which compiles the haxe, as the .hxml would have, then compiles as per the previous example

Compiling with FlashDevelop

In flash devlop you can use the Build tab (project->properties->build) to set off the haxe & c++ compilers this will allow you traces in the results tab too. Use the make_cpp.bat from before:

in the build put:

make_cpp.bat
cpp/Main.exe

This will put all trace commands in the results panel. If you start to do things like include files, or loops - basically most more advanced things, the results might start to not show up - I'm not too sure how to keep them coming up in here - like how neko examples have a window for traces, and a window for the app - Hugh - any ideas?

Remember that you might need to restart FlashDevelop for all PATH and Environment vars to take effect

Also, Check out GameHaxe for more about hxcpp

Now that i've got this all working, i just need to find some time to make something cool!

Cheers,

Tony - Touch My Pixel

15 comments


  1. [...] Tony at touchmypixel.com has posted some very useful information to help you get started with hxcpp. Be sure to go over there and check it out. hxcpp, neash, nme Haxe, hxcpp [...]


  2. Nice article! I can't wait for hxcpp to evolve even more as well as Neash/NME.

    On the flip side, I wish Adobe would just make Flash Player faster with better OpenGL support so we wouldn't have to do all this craziness.


  3. True- a faster flash player would definately be great. HXCPP will hopefully allow us to use other existing c++ games libraries too.. not just use neash/nme - altho they are great!

  4. hosey

    I am getting a
    >nmake -nologo main.exe
    cl -nologo -I"C:\Program Files (x86)\Motion-Twin\haxe\lib\hxcpp,4\/include" -Iinclude /EHsc -c -Zi -O2 src/__main__.cpp -Foobj/__main__.obj
    __main__.cpp
    C:\Documents and Settings\adminjoe\Desktop\haxegui\cpp\src\__main__.cpp : fatal
    error C1083: Cannot open compiler generated file: 'obj/__main__.obj': No such file or directory
    NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\cl.EXE"' : return code '0x1'
    Stop.

    Any ideas?


  5. Have you made sure that you have setup all of your environment variables correctly? This was the thing that stopped me for a long time.
    Could could post it on the haxe mailinglist as some more experienced c++ peeps might help.

  6. selmo

    Thanks for the helpful post!

    FYI, I had trouble with getting the generated c++ code to compile, because I have multiple versions of visual studio installed and the configured PATH and environment variables were set for the wrong version. Yet I didn't want to change all my environment variables, lest it break one of my other projects. The solution was to add a line to the make_cpp.bat file, just before nmake:

    call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"

    (or whatever your VS2008 install path is to vcvarsall.bat)

  7. robo

    hi,I got a problem like this.

    when I follow your way it can work!,
    but I change My code copy from gamehaxe site clock.hx,
    such like

    import neash.display.Sprite;
    import neash.display.Shape;
    import neash.geom.Matrix;
    class Clock extends Sprite
    {
    . .............
    static public function main()
    {
    neash.Lib.Init("Clock",640,480);
    neash.Lib.SetBackgroundColour(0xffffff);

    new Clock(320,240,200);

    neash.Lib.Run();
    }
    }

    my command line

    haxecpp.exe -cpp cpp -main Clock -cp "std" -lib nme -lib neash
    pause

    it throw a error:

    You can't access the flash package with current compilation flags...

    why? anyidea?
    thank you !


  8. yes - you need to remap the 'flash' package to 'neash' as that is how c++ handles all of the flash api - so you need the compile flag --remap flash:neash

  9. robo

    I have another qustion.
    when I first build with compile flag --remap flash:neash it take me lots time to make a lots of cpp files ,but I want to ask ,if I build it again.can I don't make these cpp files again?thank you !


  10. I am not sure if the compiler only rebuilds altered cpp files (i think it might just refresh them all)... it may tho. I guess you can check the date changed. It has always been fast enough that i havent checked- you would have to ask Hugh at gamehaxe.com.
    The makefile that it generates only tells cl.exe to recompile the modified files however - creating new .obj files - which makes the c++ compilation a LOT faster - so you should notice that it all compiles very fast.

  11. robo

    yes ! I noticce it compiles very fast When I build cpp again.
    I want to ask one more quetion .
    when I open exe file,I don't want to open another cmd window,How can I do it ?
    how can I change exe ico?


  12. I'm not really sure - i'm pretty much at the same point as you, just been playing around with hxcpp generator, seeing what it can do. I dont really have much of a background in c++ so probably asking hugh would be the best shot (altho he has been very quiet for the last few weeks, maybe on holidays) or people on the haxe list might know


  13. It's possible to use MinGW instead of MSVC?


  14. you should post this question on Hugh's blog at gamehaxe.com - i'm not the best c++ advice ;P

  15. wat

    can i build haxe cpp for winCE? i know little about neash/nme.
    thinks!

 

Leave a Reply

Our Friends:

Powered by haXe / poko cms | Copyright 2008-09 TouchMyPixel