Permalink
Browse files

FIXED Using new P2DParser and format

Still not working right. But I'll fix that soon
  • Loading branch information...
1 parent 0576ac4 commit 62faab4f03c1c48fe98a60848cfb9e39e533a561 @potmo committed Apr 10, 2012
@@ -17,7 +17,6 @@ package com.potmo.p2d.atlas
public class P2DTextureAtlas
{
- private var _sizes:Vector.<Point>;
private var _offsets:Vector.<Point>;
private var _frames:Vector.<Rectangle>;
private var _frameCount:uint;
@@ -31,14 +30,17 @@ package com.potmo.p2d.atlas
private var _textureCount:int;
// the frame offsets. That is the second element will be equal to the nuber of frames of the first texture
private var _textureFrameOffsets:Vector.<uint>;
+ private var _regpoints:Vector.<Point>;
+ private var _spriteSizes:Vector.<Point>;
public function P2DTextureAtlas()
{
- _sizes = new Vector.<Point>();
_offsets = new Vector.<Point>();
_frames = new Vector.<Rectangle>();
_names = new Vector.<String>();
+ _regpoints = new Vector.<Point>();
+ _spriteSizes = new Vector.<Point>();
_textureFrameOffsets = new Vector.<uint>();
_textureBitmaps = new Vector.<BitmapData>();
_textureCount = 0;
@@ -51,17 +53,19 @@ package com.potmo.p2d.atlas
_textureFrameOffsets.push( _frames.length - 1 );
// parse the xml and get the vectors populated
- var sizes:Vector.<Point> = new Vector.<Point>();
var offsets:Vector.<Point> = new Vector.<Point>();
var frames:Vector.<Rectangle> = new Vector.<Rectangle>();
var names:Vector.<String> = new Vector.<String>();
- parser.parse( xmlDescriptor, sizes, offsets, frames, names );
+ var regpoints:Vector.<Point> = new Vector.<Point>();
+ var spriteSizes:Vector.<Point> = new Vector.<Point>();
+ parser.parse( xmlDescriptor, offsets, spriteSizes, frames, names, regpoints );
//add the populated vectors to our full list
- _sizes = _sizes.concat( sizes );
_offsets = _offsets.concat( offsets );
_frames = _frames.concat( frames );
_names = _names.concat( names );
+ _regpoints = _regpoints.concat( regpoints );
+ _spriteSizes = _regpoints.concat( spriteSizes );
_textureBitmaps.push( textureBitmap );
_textureCount++;
@@ -70,7 +74,7 @@ package com.potmo.p2d.atlas
public function handleContextCreated( context:Context3D ):void
{
- createVertices( context, "lefttop", _sizes, _offsets, _frames, _textureBitmaps, _textureFrameOffsets );
+ createVertices( context, _offsets, _frames, _textureBitmaps, _textureFrameOffsets, _regpoints );
uploadTextures( context );
@@ -147,7 +151,7 @@ package com.potmo.p2d.atlas
}
- private function createVertices( context:Context3D, registrationPoint:String, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle>, textureBitmaps:Vector.<BitmapData>, textureFrameOffsets:Vector.<uint> ):void
+ private function createVertices( context:Context3D, drawInSpriteOffset:Vector.<Point>, textureSourceFrames:Vector.<Rectangle>, textureBitmaps:Vector.<BitmapData>, textureFrameOffsets:Vector.<uint>, regPoints:Vector.<Point> ):void
{
// get the frame offset and the next frame offset
@@ -158,7 +162,7 @@ package com.potmo.p2d.atlas
var v:uint = 0;
var i:uint = 0;
- var numFrames:uint = frames.length;
+ var numFrames:uint = textureSourceFrames.length;
var vd:Vector.<Number> = new Vector.<Number>( numFrames * 24, true );
var id:Vector.<uint> = new Vector.<uint>( numFrames * 6, true );
@@ -184,108 +188,65 @@ package com.potmo.p2d.atlas
currentTextureHeight = getNextPowerOfTwo( textureBitmaps[ currentTexture ].height );
}
- var x:Number = frames[ c ].x;
- var y:Number = frames[ c ].y;
- var w:Number = frames[ c ].width;
- var h:Number = frames[ c ].height;
-
- var x0:Number = -w;
- var y0:Number = +h;
- var x1:Number = +w;
- var y1:Number = -h;
-
- if ( offsets.length > 0 )
- {
- var ox:Number = offsets[ c ].x;
- var oy:Number = offsets[ c ].y;
- x0 += ox * 2;
- y0 += oy * 2;
- x1 += ox * 2;
- y1 += oy * 2;
- }
-
- if ( registrationPoint != "center" )
- {
- var sx:Number;
- var sy:Number;
-
- if ( sizes.length > 0 )
- {
- sx = sizes[ c ].x;
- sy = sizes[ c ].y;
- }
- else
- {
- sx = frames[ 0 ].width;
- sy = frames[ 0 ].height;
- }
-
- switch ( registrationPoint )
- {
- case "lefttop":
- {
- x0 += sx;
- y0 -= sy;
- x1 += sx;
- y1 -= sy;
- }
- break;
- case "righttop":
- {
- x0 -= sx;
- y0 -= sy;
- x1 -= sx;
- y1 -= sy;
- }
- break;
- case "leftbottom":
- {
- x0 += sx;
- y0 += sy;
- x1 += sx;
- y1 += sy;
- }
- break;
- case "rightbottom":
- {
- x0 -= sx;
- y0 += sy;
- x1 -= sx;
- y1 += sy;
- }
- break;
- }
- }
-
- var u0:Number = ( x ) / currentTextureWidth;
- var v0:Number = ( y ) / currentTextureHeight;
- var u1:Number = ( x + w ) / currentTextureWidth;
- var v1:Number = ( y + h ) / currentTextureHeight;
+ // the source coordinates in the texture bitmapdata
+ var sourceX:Number = textureSourceFrames[ c ].x;
+ var sourceY:Number = textureSourceFrames[ c ].y;
+ var sourceWidth:Number = textureSourceFrames[ c ].width;
+ var sourceHeight:Number = textureSourceFrames[ c ].height;
+
+ // create the model in local coordinates. Aligned upper left
+ // first vertex in bottom left corner and then counter clockwise
+ var modelX0:Number = 0;
+ var modelY0:Number = sourceHeight;
+ var modelX1:Number = sourceWidth;
+ var modelY1:Number = 0;
+
+ // translate by texture offset in sprite frame
+ var ox:Number = drawInSpriteOffset[ c ].x;
+ var oy:Number = drawInSpriteOffset[ c ].y;
+ modelX0 += ox;
+ modelY0 += oy;
+ modelX1 += ox;
+ modelY1 += oy;
+
+ // translate by regpoing (0,0 is upper left)
+ var rx:Number = regPoints[ c ].x;
+ var ry:Number = regPoints[ c ].y;
+ modelX0 -= rx;
+ modelY0 -= ry;
+ modelX1 -= rx;
+ modelY1 -= ry;
+
+ // scale texture coordinates to UV coordinates (unit coordinates)
+ var u0:Number = ( sourceX ) / currentTextureWidth;
+ var v0:Number = ( sourceY ) / currentTextureHeight;
+ var u1:Number = ( sourceX + sourceWidth ) / currentTextureWidth;
+ var v1:Number = ( sourceY + sourceHeight ) / currentTextureHeight;
// frame sizes and uv (2 * FLOAT_2)
- vd[ v++ ] = x0;
- vd[ v++ ] = y0;
+ vd[ v++ ] = modelX0;
+ vd[ v++ ] = modelY0;
vd[ v++ ] = u0;
vd[ v++ ] = v0;
vd[ v++ ] = currentTexture == 0 ? 1 : 0; //x
vd[ v++ ] = currentTexture == 1 ? 1 : 0; //x
- vd[ v++ ] = x1;
- vd[ v++ ] = y0;
+ vd[ v++ ] = modelX1;
+ vd[ v++ ] = modelY0;
vd[ v++ ] = u1;
vd[ v++ ] = v0;
vd[ v++ ] = currentTexture == 0 ? 1 : 0; //y
vd[ v++ ] = currentTexture == 1 ? 1 : 0; //y
- vd[ v++ ] = x1;
- vd[ v++ ] = y1;
+ vd[ v++ ] = modelX1;
+ vd[ v++ ] = modelY1;
vd[ v++ ] = u1;
vd[ v++ ] = v1;
vd[ v++ ] = currentTexture == 0 ? 1 : 0; //z
vd[ v++ ] = currentTexture == 1 ? 1 : 0; //z
- vd[ v++ ] = x0;
- vd[ v++ ] = y1;
+ vd[ v++ ] = modelX0;
+ vd[ v++ ] = modelY1;
vd[ v++ ] = u0;
vd[ v++ ] = v1;
vd[ v++ ] = currentTexture == 0 ? 1 : 0; //w
@@ -301,7 +262,7 @@ package com.potmo.p2d.atlas
}
- _frameCount = frames.length;
+ _frameCount = textureSourceFrames.length;
_indexBuffer = context.createIndexBuffer( id.length );
_indexBuffer.uploadFromVector( id, 0, id.length );
@@ -319,15 +280,14 @@ package com.potmo.p2d.atlas
public function getFrameNames():Vector.<String>
{
- // clone
- return _names.concat();
+
+ return _names.concat(); // clone
}
- public function getFrameSizes():Vector.<Point>
+ public function getSpriteSizes():Vector.<Point>
{
- // clone
- return _sizes.concat();
+ return _spriteSizes.concat(); // clone
}
@@ -5,6 +5,6 @@ package com.potmo.p2d.atlas.parser
public interface AtlasParser
{
- function parse( descriptor:XML, sizes:Vector.<Point>, offsets:Vector.<Point>, frames:Vector.<Rectangle>, names:Vector.<String> ):void;
+ function parse( descriptor:XML, textureInSpriteOffsets:Vector.<Point>, spriteSizes:Vector.<Point>, textureSourceRects:Vector.<Rectangle>, names:Vector.<String>, regpointsInSprites:Vector.<Point> ):void;
}
}
@@ -1,108 +0,0 @@
-package com.potmo.p2d.atlas.parser
-{
- import flash.geom.Point;
- import flash.geom.Rectangle;
-
- public class Cocos2DParser implements AtlasParser
- {
- public function Cocos2DParser()
- {
- }
-
-
- 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
-
- var type:String;
- var data:String;
- var array:Array;
-
- var topKeys:XMLList = descriptor.dict.key;
- var topDicts:XMLList = descriptor.dict.dict;
-
- for ( var k:uint = 0; k < topKeys.length(); k++ )
- {
- switch ( topKeys[ k ].toString() )
- {
- case "frames":
- {
- var frameKeys:XMLList = topDicts[ k ].key;
- var frameDicts:XMLList = topDicts[ k ].dict;
-
- 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 ].*;
-
- for ( var m:uint = 0; m < propKeys.length(); m++ )
- {
- type = propAll[ propKeys[ m ].childIndex() + 1 ].name();
- data = propAll[ propKeys[ m ].childIndex() + 1 ];
-
- switch ( propKeys[ m ].toString() )
- {
- case "frame":
- {
- if ( type == "string" )
- {
- array = data.split( /[^0-9-]+/ );
- frames.push( new Rectangle( array[ 1 ], array[ 2 ], array[ 3 ], array[ 4 ] ) );
- }
- else
- {
- throw new Error( "Error parsing descriptor format" );
- }
- break;
- }
- case "offset":
- {
- if ( type == "string" )
- {
- array = data.split( /[^0-9-]+/ );
- offsets.push( new Point( array[ 1 ], array[ 2 ] ) );
- }
- else
- {
- throw new Error( "Error parsing descriptor format" );
- }
- break;
- }
- case "sourceSize":
- {
- if ( type == "string" )
- {
- array = data.split( /[^0-9-]+/ );
- sizes.push( new Point( array[ 1 ], array[ 2 ] ) );
- }
- else
- {
- throw new Error( "Error parsing descriptor format" );
- }
- break;
- }
- case "rotated":
- {
- if ( type != "false" )
- {
- throw new Error( "Rotated elements not supported (yet)" );
- }
- break;
- }
- }
- }
- }
- break;
- }
- }
- }
-
- if ( frames.length == 0 )
- {
- throw new Error( "Error parsing descriptor format" );
- }
- }
- }
-}
Oops, something went wrong.

0 comments on commit 62faab4

Please sign in to comment.