Skip to content
Browse files

First commit : structure

  • Loading branch information...
1 parent d9b9053 commit 438e4bbc17c80b2beef53653caaaa514cc97cc33 @po8rewq committed May 5, 2012
View
0 src/com/revolugame/hxSpriter/BitmapRenderer.hx
No changes.
View
85 src/com/revolugame/hxSpriter/BlittingRenderer.hx
@@ -0,0 +1,85 @@
+package com.revolugame.hxSpriter;
+
+import flash.display.BitmapData;
+import flash.geom.ColorTransform;
+import flash.geom.Matrix;
+import flash.geom.Point;
+
+/**
+ * ...
+ * @author adrien
+ */
+class BlittingRenderer
+{
+ public var buffer : BitmapData;
+ var _matrix : Matrix;
+ var _colorTransform : ColorTransform;
+ var _point : Point;
+
+ public function new ()
+ {
+ _matrix = new Matrix();
+ _colorTransform = new ColorTransform();
+ _point = new Point();
+ }
+
+ public function updateFrame(frame: DataFrame, smooth: Bool):Void
+ {
+ var sprites : List<DataFrameSprite> = frame.sprites;
+
+ var offsetX : Float = frame.x;
+ var offsetY : Float = frame.y;
+
+ buffer = new BitmapData( Std.int(frame.width - offsetX), Std.int(frame.height - offsetY), true, 0);
+
+ var x : Float = 0;
+ var y : Float = 0;
+ var angle : Float = 0;
+ var scaleX : Float = 1;
+ var scaleY : Float = 1;
+ var color : Int = 0;
+ var img : BitmapData;
+
+ var sprite : DataFrameSprite;
+ for(sprite in sprites)
+ {
+ img = nme.Assets.getBitmapData(sprite.image);
+
+ x = sprite.x - offsetX;
+ y = sprite.y - offsetY;
+
+ color = sprite.color; // TODO NME
+ _colorTransform.redMultiplier = (color >> 16 & 0xFF) / 255;
+ _colorTransform.blueMultiplier = (color >> 8 & 0xFF) / 255;
+ _colorTransform.greenMultiplier = (color & 0xFF) / 255;
+ _colorTransform.alphaMultiplier = sprite.opacity;
+
+ angle = -sprite.angle;
+
+ scaleX = sprite.width / img.width;
+ scaleY = sprite.height / img.height;
+
+ if (sprite.xflip) scaleX = -scaleX;
+ if (sprite.yflip) scaleY = -scaleY;
+
+ if (sprite.color != 0xFFFFFF || _colorTransform.alphaMultiplier != 1 || angle != 0 || scaleX != 1 || scaleY != 1)
+ {
+ _matrix.b = _matrix.c = 0;
+ _matrix.a = scaleX;
+ _matrix.d = scaleY;
+ if (angle != 0) _matrix.rotate(angle * 0.017453292519943295);
+ _matrix.tx = x;
+ _matrix.ty = y;
+ buffer.draw(img, _matrix, _colorTransform, null, null, smooth);
+ }
+ else
+ {
+ _point.x = x;
+ _point.y = y;
+
+ buffer.copyPixels(img, img.rect, _point, null, null, true);
+ }
+ }
+ }
+
+}
View
57 src/com/revolugame/hxSpriter/DataAnimation.hx
@@ -0,0 +1,57 @@
+package com.revolugame.hxSpriter;
+
+import haxe.xml.Fast;
+
+/**
+ * ...
+ * @author adrien
+ */
+class DataAnimation
+{
+ public var name(default, null) : String;
+ var _frames : Array<DataAnimationFrame>;
+
+ public var frame : DataAnimationFrame;
+ var _frameIndex : Int;
+ var _elapsed : Float;
+
+ var _onChangeFrame : Void->Void;
+
+ public function new (pSource: Fast, pOnChangeFrame: Void->Void)
+ {
+ name = pSource.node.name.innerData;
+ _elapsed = 0;
+ _frameIndex = 0;
+
+ _frames = new Array();
+
+ var node : Fast;
+ for(node in pSource.nodes.frame)
+ _frames.push( new DataAnimationFrame(node) );
+
+ _onChangeFrame = pOnChangeFrame;
+ }
+
+ public function update(pElapsed : Float):Void
+ {
+ _elapsed += pElapsed;
+
+ if(_elapsed > frame.duration)
+ {
+ _elapsed -= frame.duration;
+ ++_frameIndex;
+ if (_frameIndex >= _frames.length) _frameIndex = 0;
+ frame = _frames[_frameIndex];
+
+ _onChangeFrame();
+ }
+ }
+
+ public function reset(pNewFrame:Int):Void
+ {
+ _frameIndex = pNewFrame;
+ _elapsed = 0;
+ frame = _frames[_frameIndex];
+ }
+
+}
View
26 src/com/revolugame/hxSpriter/DataAnimationFrame.hx
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * Copyright (c) 2012 by Adrien Fischer.
+ * This content is released under the MIT License.
+ * For questions mail me at adrien[at]revolugame[dot]com
+ ******************************************************************************/
+
+package com.revolugame.hxSpriter;
+
+import haxe.xml.Fast;
+
+/**
+ * ...
+ * @author adrien
+ */
+class DataAnimationFrame implements haxe.Public
+{
+ var name : String;
+ var duration : Float;
+
+ public function new (source: Fast)
+ {
+ name = source.node.name.innerData;
+ duration = Std.parseFloat(source.node.duration.innerData) / 100;
+ }
+
+}
View
111 src/com/revolugame/hxSpriter/DataFrame.hx
@@ -0,0 +1,111 @@
+package com.revolugame.hxSpriter;
+
+import haxe.xml.Fast;
+
+/**
+ * ...
+ * @author adrien
+ */
+class DataFrame implements haxe.Public
+{
+ private static var _frameSizeCache:Hash<DataRectangle> = new Hash();
+
+ var name : String;
+ var sprites : List<DataFrameSprite>;
+
+ var width : Int;
+ var height : Int;
+ var x : Int;
+ var y : Int;
+
+ public function new (pSource: Fast)
+ {
+ name = pSource.node.name.innerData;
+
+ width = 0;
+ height = 0;
+
+ var rect : DataRectangle;
+ var cached : Bool = false;
+ if(_frameSizeCache.exists(name) )
+ {
+ rect = _frameSizeCache.get(name);
+ x = rect.x;
+ y = rect.y;
+ width = rect.width;
+ height = rect.height;
+
+ cached = true;
+ }
+
+ sprites = new List();
+ var sprite : Fast;
+ if(cached)
+ {
+ for(sprite in pSource.nodes.sprite)
+ sprites.add(new DataFrameSprite(sprite));
+ }
+ else
+ {
+ rect = new DataRectangle();
+ var s : DataFrameSprite;
+ for(sprite in pSource.nodes.sprite)
+ {
+ s = new DataFrameSprite(sprite);
+ determineBoundingBox(s, rect);
+
+ if( Math.isNaN(x) || x > rect.x)
+ x = rect.x;
+
+ if( Math.isNaN(x) || y > rect.y)
+ y = rect.y;
+
+ if (width < rect.width) width = rect.width;
+ if (height < rect.height) height = rect.height;
+
+ sprites.add(s);
+ }
+ _frameSizeCache.set(name, new DataRectangle(x, y, width, height) );
+ rect = null;
+ }
+ }
+
+ private function determineBoundingBox(sprite: DataFrameSprite, rect: DataRectangle):Void
+ {
+ var theta:Float = sprite.angle;
+ var ct:Float = Math.cos(theta * 0.017453292519943295);
+ var st:Float = Math.sin(theta * 0.017453292519943295);
+
+ var x1:Float = sprite.x;
+ var y1:Float = sprite.y;
+ var x2:Float = sprite.x + sprite.width;
+ var y2:Float = sprite.y + sprite.height;
+
+ var x1prim:Float = x1 * ct - y1 * st;
+ var y1prim:Float = x1 * st + y1 * ct;
+
+ var x12prim:Float = x1 * ct - y2 * st;
+ var y12prim:Float = x1 * st + y2 * ct;
+
+ var x2prim:Float = x2 * ct - y2 * st;
+ var y2prim:Float = x2 * st + y2 * ct;
+
+ var x21prim:Float = x2 * ct - y1 * st;
+ var y21prim:Float = x2 * st + y1 * ct;
+
+ var rx1:Float = Math.min( Math.min(x1prim, x2prim), Math.min(x12prim, x21prim) );
+ var ry1:Float = Math.min( Math.min(y1prim, y2prim), Math.min(y12prim, y21prim) );
+
+ var rx2:Float = Math.max( Math.max(x1prim, x2prim), Math.max(x12prim, x21prim) );
+ var ry2:Float = Math.max( Math.max(y1prim, y2prim), Math.max(y12prim, y21prim) );
+
+ rect.x = Math.floor(rx1);
+ rect.y = Math.floor(ry1);
+ rect.width = Math.ceil(rx2 - rx1);
+ rect.height = Math.ceil(ry2 - ry1);
+
+ if (sprite.xflip) rect.x -= rect.width;
+ if (sprite.yflip) rect.y -= rect.height;
+ }
+
+}
View
51 src/com/revolugame/hxSpriter/DataFrameSprite.hx
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2012 by Adrien Fischer.
+ * This content is released under the MIT License.
+ * For questions mail me at adrien[at]revolugame[dot]com
+ ******************************************************************************/
+
+package com.revolugame.hxSpriter;
+
+import haxe.xml.Fast;
+
+/**
+ * ...
+ * @author Adrien Fischer
+ */
+class DataFrameSprite implements haxe.Public
+{
+ static inline var SPRITES_DIR : String = 'sprites/';
+
+ var image : String;
+ var color : Int;
+ var opacity : Float;
+ var angle : Float;
+ var xflip : Bool;
+ var yflip : Bool;
+ var width : Float;
+ var height : Float;
+ var x : Float;
+ var y : Float;
+
+ public function new (source: Fast)
+ {
+ image = source.node.image.innerData;
+ image = SPRITES_DIR + StringTools.replace(image, '\\', '/');
+
+ color = Std.parseInt( source.node.color.innerData );
+
+ opacity = Std.parseFloat( source.node.opacity.innerData ) * 0.01;
+
+ angle = Std.parseFloat( source.node.angle.innerData );
+ if(angle == 360) angle = 0;
+
+ xflip = source.node.xflip.innerData == "1";
+ yflip = source.node.yflip.innerData == "1";
+
+ width = Std.parseInt( source.node.width.innerData );
+ height = Std.parseInt( source.node.height.innerData );
+ x = Std.parseInt( source.node.x.innerData );
+ y = Std.parseInt( source.node.y.innerData );
+ }
+
+}
View
28 src/com/revolugame/hxSpriter/DataRectangle.hx
@@ -0,0 +1,28 @@
+//::///////////////////////////////////////////////////////////////////
+//::// Copyright (c) 2012 by Adrien Fischer.
+//::// This content is released under the MIT License.
+//::// For questions mail me at adrien[at]revolugame[dot]com
+//::///////////////////////////////////////////////////////////////////
+
+package com.revolugame.hxSpriter;
+
+/**
+ * ...
+ * @author Adrien Fischer
+ */
+class DataRectangle implements haxe.Public
+{
+ var x : Int;
+ var y : Int;
+ var width : Int;
+ var height : Int;
+
+ public function new (?pX: Int = 0, ?pY: Int = 0, ?pWidth: Int = 0, ?pHeight:Int = 0)
+ {
+ x = pX;
+ y = pY;
+ width = pWidth;
+ height = pHeight;
+ }
+
+}
View
79 src/com/revolugame/hxSpriter/DataSpriterCharacter.hx
@@ -0,0 +1,79 @@
+package com.revolugame.hxSpriter;
+
+import haxe.xml.Fast;
+import flash.utils.ByteArray;
+
+/**
+ * ...
+ * @author adrien
+ */
+class DataSpriterCharacter
+{
+ var _name : String;
+
+ var _animations : Hash<DataAnimation>;
+ var _frames : Hash<DataFrame>;
+
+ public var frame(getFrame, null): DataFrame;
+ var _animation : DataAnimation;
+
+ public var onChangeFrame:Void->Void;
+
+ public function new (pData: String, pOnChangeFrame: Void->Void)
+ {
+ var source : Fast = new Fast( Xml.parse( nme.Assets.getText(pData) ) );
+ var node : Fast = null;
+
+ source = source.node.spriterdata;
+
+ _animations = new Hash();
+ for (node in source.node.char.nodes.anim)
+ _animations.set(node.node.name.innerData, new DataAnimation(node, pOnChangeFrame) );
+
+ _frames = new Hash();
+ for( node in source.nodes.frame)
+ _frames.set( node.node.name.innerData, new DataFrame(node) );
+
+ // animation name
+ _name = source.node.char.node.name.innerData;
+
+ onChangeFrame = pOnChangeFrame;
+ }
+
+ public function play(pName:String, ?pReset:Bool = false, ?pFrame:Int = 0):Void
+ {
+ if (_animation != null && _animation.name == pName)
+ {
+ if (pReset) _animation.reset(pFrame);
+ return;
+ }
+
+ _animation = _animations.get(pName);
+ _animation.reset(pFrame);
+
+ onChangeFrame();
+ }
+
+ public function update(pElapsed:Float):Void
+ {
+ if (_animation != null)
+ _animation.update(pElapsed);
+ }
+
+ private function onAnimChangeFrame():Void
+ {
+ onChangeFrame();
+ }
+
+ /**
+ * Return the dataframe of the current animation
+ */
+ private function getFrame():DataFrame
+ {
+ if (_animation != null)
+ return _frames.get(_animation.frame.name);
+
+ return null;
+ }
+
+}

0 comments on commit 438e4bb

Please sign in to comment.
Something went wrong with that request. Please try again.