Permalink
Browse files

CHANGED the parser and added labels.

UPDATED atlas with labels and sequences. Not really happy with it
ADDED alpha shader
  • Loading branch information...
potmo committed Mar 15, 2012
1 parent ba1a0d0 commit 4b7a73c95459d804009e4deb19ffdb38e979195d
@@ -1,6 +1,8 @@
package com.potmo.p2d.atlas
{
- import com.potmo.p2d.atlas.parser.P2DAtlasParser;
+ import com.potmo.p2d.atlas.animation.P2DSpriteAtlas;
+ import com.potmo.p2d.atlas.animation.P2DSpriteAtlasSequence;
+ import com.potmo.p2d.atlas.parser.AtlasParser;
import flash.display.BitmapData;
import flash.display3D.Context3D;
@@ -21,24 +23,39 @@ package com.potmo.p2d.atlas
private var _indexBuffer:IndexBuffer3D;
private var _vertexBuffer:VertexBuffer3D;
private var _textureBitmap:BitmapData;
+ private var _names:Vector.<String>;
private var _texture:Texture;
+ private var _spriteAtlas:P2DSpriteAtlas;
+ private var _id:int;
- public function P2DTextureAtlas( xmlDescriptor:XML, textureBitmap:BitmapData, parser:P2DAtlasParser )
+
+ public function P2DTextureAtlas( id:int, xmlDescriptor:XML, textureBitmap:BitmapData, parser:AtlasParser )
{
_sizes = new Vector.<Point>();
_offsets = new Vector.<Point>();
_frames = new Vector.<Rectangle>();
- parser.parse( xmlDescriptor, _sizes, _offsets, _frames );
+ _names = new Vector.<String>();
+ _id = id;
+
+ parser.parse( xmlDescriptor, _sizes, _offsets, _frames, _names );
+
+ _spriteAtlas = new P2DSpriteAtlas( _id, _names );
+
_textureBitmap = textureBitmap;
+ }
+
+ public function getSequenceByName( name:String ):P2DSpriteAtlasSequence
+ {
+ return _spriteAtlas.getSequenceByName( name );
}
public function handleContextCreated( context:Context3D ):void
{
- createVertices( context, "lefttop", _sizes, _offsets, _frames, _textureBitmap.width, _textureBitmap.height );
+ createVertices( context, "center", _sizes, _offsets, _frames, _textureBitmap.width, _textureBitmap.height );
createTexture( context, _textureBitmap );
}
@@ -47,7 +64,7 @@ package com.potmo.p2d.atlas
{
_texture = context.createTexture( textureBitmap.width, textureBitmap.height, Context3DTextureFormat.BGRA, false );
//_texture.uploadFromBitmapData( textureBitmap, 0 );
- uploadBitmapData( _texture, textureBitmap, true );
+ uploadBitmapData( _texture, textureBitmap, false );
}
@@ -0,0 +1,94 @@
+package com.potmo.p2d.atlas.animation
+{
+
+ public class P2DSpriteAtlas
+ {
+
+ private var _sequences:Vector.<P2DSpriteAtlasSequence>;
+ private var _sequenceCount:uint;
+
+
+ public function P2DSpriteAtlas( atlas:int, names:Vector.<String> )
+ {
+
+ _sequences = new Vector.<P2DSpriteAtlasSequence>();
+ _sequenceCount = 0;
+
+ var length:int = names.length;
+
+ for ( var i:int = 0; i < length; i++ )
+ {
+ var name:String = names[ i ];
+
+ // a frame name should be formatted as
+ // spritename/00001_label.png
+
+ var spriteFrameSplit:int = name.indexOf( "/" );
+
+ if ( spriteFrameSplit == -1 )
+ {
+ throw new Error( "can not parse name: " + name + " it does not contain slash" );
+ }
+
+ var frameLabelSplit:int = name.indexOf( "_" );
+
+ if ( frameLabelSplit == -1 )
+ {
+ throw new Error( "can not parse name " + name + " it does not contain underscore" );
+ }
+
+ var frameLabelEnd:int = name.lastIndexOf( "." );
+
+ if ( frameLabelEnd == -1 )
+ {
+ frameLabelEnd = int.MAX_VALUE;
+ }
+
+ var sequenceName:String = name.substring( 0, spriteFrameSplit );
+ var sequenceFrame:int = parseInt( name.substring( spriteFrameSplit + 1, frameLabelSplit ) );
+ var label:String = name.substring( frameLabelSplit + 1, frameLabelEnd );
+
+ var sequence:P2DSpriteAtlasSequence = getSequenceByName( sequenceName );
+
+ if ( !sequence )
+ {
+ sequence = createSequence( atlas, sequenceName );
+ }
+
+ sequence.addFrame( sequenceFrame, i, label );
+
+ }
+
+ }
+
+
+ /**
+ * @returns a entry or null
+ */
+ public function getSequenceByName( name:String ):P2DSpriteAtlasSequence
+ {
+
+ for ( var i:int = 0; i < _sequenceCount; i++ )
+ {
+ var sequence:P2DSpriteAtlasSequence = _sequences[ i ];
+
+ if ( sequence.getName() == name )
+ {
+ return sequence;
+ }
+ }
+
+ return null;
+ }
+
+
+ private function createSequence( atlas:int, name:String ):P2DSpriteAtlasSequence
+ {
+ var sequence:P2DSpriteAtlasSequence = new P2DSpriteAtlasSequence( atlas, name );
+ _sequences.push( sequence );
+ _sequenceCount++;
+ return sequence;
+ }
+
+ }
+}
@@ -0,0 +1,72 @@
+package com.potmo.p2d.atlas.animation
+{
+
+ public class P2DSpriteAtlasSequence
+ {
+ private var _name:String;
+ private var _atlas:int;
+ private var _sequenceFrameCount:int;
+ private var _labels:Vector.<String>;
+ private var _atlasFrames:Vector.<uint>;
+
+
+ public function P2DSpriteAtlasSequence( atlas:int, name:String )
+ {
+ _atlas = atlas;
+ _name = name;
+ _labels = new Vector.<String>();
+ _atlasFrames = new Vector.<uint>();
+ _sequenceFrameCount = 0;
+ }
+
+
+ public function addFrame( sequenceFrame:int, atlasFrame:uint, label:String ):void
+ {
+ while ( sequenceFrame >= _sequenceFrameCount )
+ {
+ _labels.push( null );
+ _atlasFrames.push( -1 );
+ _sequenceFrameCount++;
+ }
+
+ _labels[ sequenceFrame ] = label;
+ _atlasFrames[ sequenceFrame ] = atlasFrame;
+
+ }
+
+
+ /**
+ * Returns the sequence frame of the label
+ */
+ public function getSequenceFrameOfLabel( label:String ):int
+ {
+ for ( var i:int = 0; i < _sequenceFrameCount; i++ )
+ {
+ if ( _labels[ i ] == label )
+ {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+
+
+ public function getAtlasFrameOfSequenceFrame( sequenceFrame:int ):int
+ {
+ return _atlasFrames[ sequenceFrame ];
+ }
+
+
+ public function getFrameCount():int
+ {
+ return _sequenceFrameCount;
+ }
+
+
+ public function getName():String
+ {
+ return _name;
+ }
+ }
+}
@@ -3,8 +3,8 @@ package com.potmo.p2d.atlas.parser
import flash.geom.Point;
import flash.geom.Rectangle;
- public interface P2DAtlasParser
+ public interface AtlasParser
{
- function parse( descriptor:XML, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle> ):void;
+ function parse( descriptor:XML, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle>, names:Vector.<String> ):void;
}
}
@@ -3,14 +3,14 @@ package com.potmo.p2d.atlas.parser
import flash.geom.Point;
import flash.geom.Rectangle;
- public class Cocos2DParser implements P2DAtlasParser
+ public class Cocos2DParser implements AtlasParser
{
public function Cocos2DParser()
{
}
- public function parse( descriptor:XML, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle> ):void
+ public function parse( descriptor:XML, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle>, names:Vector.<String> ):void
{
// Not very generic, only tested with TexturePacker
@@ -33,6 +33,7 @@ package com.potmo.p2d.atlas.parser
for ( var l:uint = 0; l < frameKeys.length(); l++ )
{
+ names.push( frameKeys[ l ].toString() );
var propKeys:XMLList = frameDicts[ l ].key;
var propAll:XMLList = frameDicts[ l ].*;
@@ -13,7 +13,11 @@ package com.potmo.p2d.renderer
"m33 vt0.xyz, vt0, vc0\n",
"mov op, vt0\n",
"mov v0, va1\n" ];
- private static const FRAGMENT_SHADER:Vector.<String> = new <String>[ "tex oc, v0, fs0 <2d,linear,miplinear>\n" ];
+
+ private static const FRAGMENT_SHADER:Vector.<String> = new <String>[ "tex oc, v0, fs0 <2d,linear,mipnone>\n" ];
+
+ private static const FRAGMENT_SHADER_ALPHA:Vector.<String> = new <String>[ "tex ft0, v0, fs0 <2d,linear,mipnone>\n",
+ "mul oc, ft0, fc0\n" ];
private var _program:Program3D;
@@ -38,7 +42,7 @@ package com.potmo.p2d.renderer
// Assemble fragment shader from its code
var fragmentAssembler:AGALMiniAssembler = new AGALMiniAssembler();
- fragmentAssembler.assemble( Context3DProgramType.FRAGMENT, FRAGMENT_SHADER.join( "" ) );
+ fragmentAssembler.assemble( Context3DProgramType.FRAGMENT, FRAGMENT_SHADER_ALPHA.join( "" ) );
// Upload vertex/framgment shader to our program
program.upload( vertexAssembler.agalcode, fragmentAssembler.agalcode );
Oops, something went wrong.

0 comments on commit 4b7a73c

Please sign in to comment.