Skip to content

Instantly share code, notes, and snippets.

@jgranick
Last active October 10, 2015 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/3723186 to your computer and use it in GitHub Desktop.
Save jgranick/3723186 to your computer and use it in GitHub Desktop.
How to add inline C++ code (NME recipe)
import nme.display.Sprite;
#if cpp
@:headerCode ("#include <time.h>")
#end
class TestNME extends Sprite {
public function new () {
super ();
var offset = getTimezoneOffset ();
trace (offset);
}
#if cpp
@:functionCode ("
time_t currtime;
struct tm * timeinfo;
time(&currtime);
timeinfo = gmtime(&currtime);
time_t utc = mktime(timeinfo);
timeinfo = localtime(&currtime);
time_t local = mktime(timeinfo);
int offsetFromUTC = difftime(utc, local) / 3600;
// Adjust for DST
if (timeinfo->tm_isdst)
{
offsetFromUTC -= 1;
}
return offsetFromUTC;
")
#end
private function getTimezoneOffset () {
#if !cpp
return untyped Date.now ().getTimezoneOffset () / 60;
#else
return 0;
#end
}
}
@jgranick
Copy link
Author

Haxe does not currently provide a way to get the current timezone offset, but the Date objects for Javascript and Flash both already have the method, it is just not exposed.

This is an example of how you can use the @:headerCode and @:functionCode metadata tags in order to inject pure C++ code into a function. At compile time, the contents of the getTimezoneOffset() method is filled with the C++ function code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment