Skip to content

Commit

Permalink
Merge pull request #46 from tong/add-macro-armpack-example
Browse files Browse the repository at this point in the history
Add armpack macro usage example
  • Loading branch information
luboslenco committed Feb 17, 2022
2 parents c3bbf6c + 69c5644 commit 17bc551
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions macro_armpack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Use build macro to:
- Create trait constructor
- Create trait init function
- Parse exported `Scene.arm` file and auto create references to objects in scene
13 changes: 13 additions & 0 deletions macro_armpack/Sources/arm/Game.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package arm;

import iron.math.Vec4;

@:build(arm.Macros.build())
class Game extends iron.Trait {

function update() {
Cube.transform.rotate( Vec4.zAxis(), 0.01 );
Icosphere.transform.rotate( Vec4.yAxis(), 0.01 );
Torus.transform.rotate( Vec4.xAxis(), 0.01 );
}
}
85 changes: 85 additions & 0 deletions macro_armpack/Sources/arm/Macros.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package arm;

#if macro

import haxe.macro.Context;
import haxe.macro.Expr;
import iron.data.SceneFormat;
import iron.system.ArmPack;
import sys.FileSystem;
import sys.io.File;

using StringTools;
using haxe.io.Path;

class Macros {

static function build() {
var fields = Context.getBuildFields();
var pos = Context.currentPos();
var target = if( Context.defined('kha_krom') ) 'krom';
else if( Context.defined('kha_html5') ) 'html5';
else Context.fatalError('Only krom and html5 are supported targets for this project', pos );
var dir = Sys.getCwd() + target;
var files = FileSystem.readDirectory(dir);
var sceneData : TSceneFormat = null;
var meshes = new Array<String>();
for( file in files ) {
if( file == 'Scene.arm' ) {
var p = '$dir/$file';
sceneData = ArmPack.decode( File.getBytes(p) );
trace(sceneData);
trace(sceneData.name);
for( obj in sceneData.objects ) {
if( obj.properties != null )
for( p in obj.properties ) trace(p.name +' = '+ p.value);
if( obj.type == 'mesh_object' )
meshes.push(obj.name);
}
break;
}
}

var constructorExprs = new Array<Expr>();
constructorExprs.push(macro super() );
constructorExprs.push(macro notifyOnInit(init) );
fields.push({
access: [APublic,AInline],
name: 'new',
pos: pos,
kind: FFun({
args: [],
ret: macro: Void,
expr: { expr: EBlock( constructorExprs ), pos: pos }
}),
});

var initExprs = new Array<Expr>();
initExprs.push( macro trace("Init") );
for( mesh in meshes ) {
fields.push({
name: mesh,
access: [APublic],
kind: FVar(macro: iron.object.MeshObject, macro null),
pos: pos
});
initExprs.push( macro this.$mesh = iron.Scene.active.getMesh($v{mesh}) );
}
initExprs.push( macro notifyOnUpdate(update) );

fields.push({
name: 'init',
access: [],
kind: FFun({
args: [],
ret: macro : Void,
expr: { expr: EBlock( initExprs ), pos: Context.currentPos() }
}),
pos: pos
});

return fields;
}
}

#end
Binary file added macro_armpack/macro_armpack.blend
Binary file not shown.

0 comments on commit 17bc551

Please sign in to comment.