Last active
October 10, 2015 17:07
-
-
Save jgranick/3723186 to your computer and use it in GitHub Desktop.
How to add inline C++ code (NME recipe)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.