Our plans for haXe C++
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
- Download and install haxe
- 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
- Type "haxelib install hxcpp". This will install the hxcpp library for haXe.
- Type "haxelib install neash". Installs the neash library.
- Type "haxelib install nme". Note this may already be installed as a dependency.
- 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).
- 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"
- Add "%HXCPP%" to the PATH variable, making sure that it is seperated with a ";" and no space.
- Add "%HXCPP%\bin\Windows" to the PATH as well. This is so haxecpp.exe can be found.
- 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:
- 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.
- Add vc\bin (location of vc.exe) to the user PATH eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin"
- 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.
- If you get the error run the cl setup eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
- Run "cl" again. If you still get the error, follow next step.
- 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"
- Run "cl" again, and you should get the compiler usage message.
- Add an environment variable INCLUDE which points to the vc/include folder eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\include"
- Add an environment variable LIB which points to the vc/lib folder eg: "C:\Program Files\Microsoft Visual Studio 9.0\VC\lib"
- 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"
- 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
public static function main()
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.
haxecpp.exe -cpp cpp -main Main -cp "C:\Program Files\Motion-Twin\haxe\lib\hxcpp\0,4\std"
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:
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!
Tony - Touch My Pixel
This entry was posted on Thursday, April 23rd, 2009 at 12:41 am and is filed under haxe, hxcpp. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
April 27th, 2009 at 2:13 am
[...] 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 [...]
May 23rd, 2009 at 4:17 am
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
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'
July 5th, 2009 at 3:30 pm
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)
July 8th, 2009 at 4:30 pm
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,
class Clock extends Sprite
static public function main()
my command line
haxecpp.exe -cpp cpp -main Clock -cp "std" -lib nme -lib neash
it throw a error:
You can't access the flash package with current compilation flags...
thank you !
July 9th, 2009 at 11:35 am
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 !
July 9th, 2009 at 11:42 am
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.
July 9th, 2009 at 1:29 pm
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?
July 9th, 2009 at 1:47 pm
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
May 24th, 2010 at 5:01 pm
can i build haxe cpp for winCE? i know little about neash/nme.