Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Last active April 19, 2017 02:46
Show Gist options
  • Save ruby0x1/38aa6d96377f80a9b3b2e9152fc03fcd to your computer and use it in GitHub Desktop.
Save ruby0x1/38aa6d96377f80a9b3b2e9152fc03fcd to your computer and use it in GitHub Desktop.
class Main extends luxe.Game {
override function config(config:luxe.GameConfig) {
config.preload = PreloadAssets.parcel;
return config;
}
...
}
//An example of a luxe early alpha preload parcel macro.
//The macro loads a preload json file, and applies it
//to a static member that the config function can use.
//You could also use filesystem reads to populate this as an exercise.
#if !macro
@:build(PreloadAssetList.build())
class PreloadAssets {
//this class gets populated by the build macro!
//it looks like this, a variable named parcel:
//var parcel: { json:[...], text:[...], }
//and it is exactly the contents in the json
}
#end
private typedef PreloadParcel = {
? bytes: Array<Dynamic>,
? texts: Array<Dynamic>,
? jsons: Array<Dynamic>,
? textures: Array<Dynamic>,
? fonts: Array<Dynamic>,
? shaders: Array<Dynamic>,
? sounds: Array<Dynamic>
}
class PreloadAssetList {
macro public static function build():Array<haxe.macro.Expr.Field> {
#if macro
var path = haxe.macro.Context.resolvePath('preload.json');
var contents = sys.io.File.getContent(path);
//instead of parsing the json file, you could use sys.Filesystem
var parcel:PreloadParcel = haxe.Json.parse(contents);
//The fields array is the list of class fields/members in our PreloadAssets class.
var fields:Array<haxe.macro.Expr.Field> = haxe.macro.Context.getBuildFields();
//we add one, and fill it with the contents of our parcel json
fields.push({
name: "parcel",
doc: "The parcel, as defined inside of preload.json",
access: [APublic, AStatic],
kind: FVar(macro :PreloadParcel, macro $v{parcel}),
pos: haxe.macro.Context.currentPos(),
});
return fields;
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment