From 7e64b5af19e7fb4093ebd66456e5c61beef73ec8 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 1 Nov 2014 22:43:48 +0100 Subject: [PATCH 01/11] Enable different ways of rendering frames: * Traditionally, with a single bitmapData for every frame * By drawing a rect from one single bitmap data * Using tilesheets For this render targets are introduced, which render into a "graphics" object. For now the graphics object is the "graphics" member of the AnimatedSprite. Also: Introduced flag for rendering with "ADD" blendMode. Due to some issue, rendering with blendmode "Add" works only using tilesheets on C++ targets and by setting "blendMode" of the sprite on flash target. --- spritesheet/AnimatedSprite.hx | 45 +++++++++---- spritesheet/Spritesheet.hx | 67 ++++++++++++++----- spritesheet/data/SpritesheetFrame.hx | 1 + spritesheet/render/IRenderTarget.hx | 10 +++ .../render/RenderBitmapRectToGraphics.hx | 31 +++++++++ .../render/RenderTilesheetToGraphics.hx | 32 +++++++++ .../render/RenderWholeBitmapToGraphics.hx | 30 +++++++++ 7 files changed, 188 insertions(+), 28 deletions(-) create mode 100644 spritesheet/render/IRenderTarget.hx create mode 100644 spritesheet/render/RenderBitmapRectToGraphics.hx create mode 100644 spritesheet/render/RenderTilesheetToGraphics.hx create mode 100644 spritesheet/render/RenderWholeBitmapToGraphics.hx diff --git a/spritesheet/AnimatedSprite.hx b/spritesheet/AnimatedSprite.hx index db34cc0..fc53850 100644 --- a/spritesheet/AnimatedSprite.hx +++ b/spritesheet/AnimatedSprite.hx @@ -1,18 +1,22 @@ package spritesheet; -import flash.display.Bitmap; +import spritesheet.render.IRenderTarget; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.Lib; import spritesheet.data.BehaviorData; +enum Flag { + BLEND_ADD; +} +@:access(spritesheet.Spritesheet) class AnimatedSprite extends Sprite { - public var bitmap:Bitmap; + public var renderTarget:IRenderTarget; public var currentBehavior:BehaviorData; public var currentFrameIndex:Int; public var smoothing:Bool; @@ -23,6 +27,8 @@ class AnimatedSprite extends Sprite { private var behavior:BehaviorData; private var loopTime:Int; private var timeElapsed:Int; + + private var isAFrameShown : Bool = false; //Inidicates if any frame has been drawn at all public function new (sheet:Spritesheet, smoothing:Bool = false) { @@ -33,9 +39,16 @@ class AnimatedSprite extends Sprite { this.spritesheet = sheet; behaviorQueue = new Array (); - bitmap = new Bitmap (); - addChild (bitmap); - + renderTarget = switch(sheet.imageData) { + case BITMAP_DATA(_,_): + if (sheet.useSingleBitmapData) { + new spritesheet.render.RenderBitmapRectToGraphics (this); + } else { + new spritesheet.render.RenderWholeBitmapToGraphics (this); + } + case TILESHEET(ts): + new spritesheet.render.RenderTilesheetToGraphics(this, ts); + } } @@ -156,12 +169,9 @@ class AnimatedSprite extends Sprite { var frame = spritesheet.getFrame (currentBehavior.frames [currentFrameIndex]); - - bitmap.bitmapData = frame.bitmapData; - bitmap.smoothing = smoothing; - bitmap.x = frame.offsetX - currentBehavior.originX; - bitmap.y = frame.offsetY - currentBehavior.originY; - + isAFrameShown = true; + renderTarget.drawFrame(frame, -currentBehavior.originX, -currentBehavior.originY, smoothing); + if (behaviorComplete) { if (behaviorQueue.length > 0) { @@ -193,7 +203,7 @@ class AnimatedSprite extends Sprite { loopTime = Std.int ((behavior.frames.length / behavior.frameRate) * 1000); - if (bitmap.bitmapData == null) { + if (!isAFrameShown) { update (0); @@ -203,7 +213,7 @@ class AnimatedSprite extends Sprite { } else { - bitmap.bitmapData = null; + isAFrameShown = false; currentBehavior = null; currentFrameIndex = -1; behaviorComplete = true; @@ -212,5 +222,14 @@ class AnimatedSprite extends Sprite { } + public function setFlag(flag : Flag) { + renderTarget.enableFlag(flag); + update(0); + } + + public function unsetFlag(flag : Flag) { + renderTarget.disableFlag(flag); + update(0); + } } \ No newline at end of file diff --git a/spritesheet/Spritesheet.hx b/spritesheet/Spritesheet.hx index 52b269a..be06f9f 100644 --- a/spritesheet/Spritesheet.hx +++ b/spritesheet/Spritesheet.hx @@ -2,11 +2,22 @@ package spritesheet; import flash.display.BitmapData; +import openfl.display.Tilesheet; import flash.geom.Point; import flash.geom.Rectangle; import spritesheet.data.BehaviorData; import spritesheet.data.SpritesheetFrame; +private enum ImageData { + BITMAP_DATA(sourceImage : BitmapData, sourceImageAlpha : BitmapData); + TILESHEET(sheet : openfl.display.Tilesheet); +} + +enum StorageType { + INDIVIDUAL_BITMAPS;// Store every frame in its own BITMAP_DATA + SINGLE_BITMAP; // Store all frames in onw single bitmap + TILESHEET; // Store using openfl.display.Tilesheet +} class Spritesheet { @@ -16,14 +27,29 @@ class Spritesheet { public var totalFrames:Int; private var frames:Array ; + private var imageData : ImageData; private var sourceImage:BitmapData; private var sourceImageAlpha:BitmapData; + public var useSingleBitmapData(default,null):Bool; - - public function new (image:BitmapData = null, frames:Array = null, behaviors:Map = null, imageAlpha:BitmapData = null) { - - this.sourceImage = image; - this.sourceImageAlpha = imageAlpha; + public function new (image:BitmapData = null, frames:Array = null, behaviors:Map = null, + imageAlpha:BitmapData = null, storageType : StorageType = null) { + + if (storageType == null) { + #if flash + storageType = INDIVIDUAL_BITMAPS; //Tilesheet does not work with blendmode = ADD on flash + #else + storageType = TILESHEET; //Only tilesheets work for blendmode = ADD on other targets + #end + } + + if (storageType == TILESHEET) { + this.imageData = TILESHEET(new Tilesheet(image)); + this.useSingleBitmapData = true; + } else { + this.imageData = BITMAP_DATA(image, imageAlpha); + this.useSingleBitmapData = storageType == SINGLE_BITMAP; + } if (frames == null) { @@ -46,6 +72,12 @@ class Spritesheet { this.behaviors = behaviors; } + + if (useSingleBitmapData && imageAlpha != null) { + var targetRect = new Rectangle(0,0,image.width,image.height); + var targetPoint = new Point(); + image.copyChannel (imageAlpha, targetRect, targetPoint, 2, 8); + } } @@ -80,20 +112,25 @@ class Spritesheet { var frame = frames[index]; - var bitmapData = new BitmapData (frame.width, frame.height, true); var sourceRectangle = new Rectangle (frame.x, frame.y, frame.width, frame.height); var targetPoint = new Point (); - bitmapData.copyPixels (sourceImage, sourceRectangle, targetPoint); - - if (sourceImageAlpha != null) { - - bitmapData.copyChannel (sourceImageAlpha, sourceRectangle, targetPoint, 2, 8); - + switch(imageData) { + case BITMAP_DATA(sourceImage, sourceImageAlpha): + if (useSingleBitmapData) { + frame.bitmapData = sourceImage; + } else { + var bitmapData = new BitmapData (frame.width, frame.height, true); + bitmapData.copyPixels (sourceImage, sourceRectangle, targetPoint); + + if (sourceImageAlpha != null) { + bitmapData.copyChannel (sourceImageAlpha, sourceRectangle, targetPoint, 2, 8); + } + frame.bitmapData = bitmapData; + } + case TILESHEET(sheet): + frame.tilesheetIndex = sheet.addTileRect(sourceRectangle, new Point(0,0)); } - - frame.bitmapData = bitmapData; - } diff --git a/spritesheet/data/SpritesheetFrame.hx b/spritesheet/data/SpritesheetFrame.hx index 2ef199c..6d13708 100644 --- a/spritesheet/data/SpritesheetFrame.hx +++ b/spritesheet/data/SpritesheetFrame.hx @@ -8,6 +8,7 @@ class SpritesheetFrame { public var name:String; + public var tilesheetIndex:Int;//Used when drawing this frame from a tilesheet public var bitmapData:BitmapData; public var height:Int; public var offsetX:Int; diff --git a/spritesheet/render/IRenderTarget.hx b/spritesheet/render/IRenderTarget.hx new file mode 100644 index 0000000..88757b0 --- /dev/null +++ b/spritesheet/render/IRenderTarget.hx @@ -0,0 +1,10 @@ +package spritesheet.render; + +import spritesheet.data.SpritesheetFrame; +import spritesheet.AnimatedSprite; + +interface IRenderTarget { + function drawFrame(frame : SpritesheetFrame, offsetX : Float, offsetY : Float, smoothing : Bool) : Void; + function enableFlag(flag : Flag) : Void; + function disableFlag(flag : Flag) : Void; +} \ No newline at end of file diff --git a/spritesheet/render/RenderBitmapRectToGraphics.hx b/spritesheet/render/RenderBitmapRectToGraphics.hx new file mode 100644 index 0000000..5807d5c --- /dev/null +++ b/spritesheet/render/RenderBitmapRectToGraphics.hx @@ -0,0 +1,31 @@ +package spritesheet.render; + +import spritesheet.data.SpritesheetFrame; +import flash.display.Sprite; +import flash.display.BlendMode; +import spritesheet.AnimatedSprite; + +class RenderBitmapRectToGraphics implements IRenderTarget { + private var sprite : Sprite; + public function new(o : Sprite) {this.sprite = o;} + public function drawFrame(frame : SpritesheetFrame, offsetX : Float, offsetY : Float, smoothing : Bool) { + var m = new flash.geom.Matrix(); + m.translate(offsetX + frame.offsetX - frame.x, offsetY + frame.offsetY - frame.y); + sprite.graphics.clear(); + sprite.graphics.beginBitmapFill(frame.bitmapData, m, false, smoothing); + sprite.graphics.drawRect(offsetX + frame.offsetX,offsetY + frame.offsetY,frame.width, frame.height); + sprite.graphics.endFill(); + } + public function enableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + sprite.blendMode = BlendMode.ADD; + } + } + public function disableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + sprite.blendMode = BlendMode.NORMAL; + } + } +} \ No newline at end of file diff --git a/spritesheet/render/RenderTilesheetToGraphics.hx b/spritesheet/render/RenderTilesheetToGraphics.hx new file mode 100644 index 0000000..13fbec6 --- /dev/null +++ b/spritesheet/render/RenderTilesheetToGraphics.hx @@ -0,0 +1,32 @@ +package spritesheet.render; + +import openfl.display.Tilesheet; +import spritesheet.data.SpritesheetFrame; +import flash.display.Sprite; +import spritesheet.AnimatedSprite; + + class RenderTilesheetToGraphics implements IRenderTarget { + private var sprite : Sprite; + private var tilesheet : Tilesheet; + private var flags : Int = 0; + public function new(o : Sprite, tilesheet : Tilesheet) { + this.sprite = o; + this.tilesheet = tilesheet; + } + public function drawFrame(frame : SpritesheetFrame, offsetX : Float , offsetY : Float, smoothing : Bool) { + sprite.graphics.clear(); + tilesheet.drawTiles(sprite.graphics, [offsetX + frame.offsetX,offsetY + frame.offsetY, frame.tilesheetIndex], smoothing, flags); + } + public function enableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + flags = flags | Tilesheet.TILE_BLEND_ADD; + } + } + public function disableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + flags &= 0xFFFFFFF - Tilesheet.TILE_BLEND_ADD; + } + } +} \ No newline at end of file diff --git a/spritesheet/render/RenderWholeBitmapToGraphics.hx b/spritesheet/render/RenderWholeBitmapToGraphics.hx new file mode 100644 index 0000000..90ab39e --- /dev/null +++ b/spritesheet/render/RenderWholeBitmapToGraphics.hx @@ -0,0 +1,30 @@ +package spritesheet.render; + +import flash.display.Sprite; +import flash.display.BlendMode; +import spritesheet.AnimatedSprite; + +class RenderWholeBitmapToGraphics implements IRenderTarget { + private var sprite : Sprite; + public function new(o : Sprite) {this.sprite = o;} + public function drawFrame(frame : spritesheet.data.SpritesheetFrame, offsetX : Float, offsetY : Float, smoothing : Bool) { + var m = new flash.geom.Matrix(); + m.translate(offsetX + frame.offsetX, offsetY + frame.offsetY); + sprite.graphics.clear(); + sprite.graphics.beginBitmapFill(frame.bitmapData, m, false, smoothing); + sprite.graphics.drawRect(offsetX + frame.offsetX, offsetY + frame.offsetY ,frame.width, frame.height); + sprite.graphics.endFill(); + } + public function enableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + sprite.blendMode = BlendMode.ADD; + } + } + public function disableFlag(flag : Flag) { + switch(flag) { + case BLEND_ADD: + sprite.blendMode = BlendMode.NORMAL; + } + } +} \ No newline at end of file From 606ef262e482aa2a8b7f242e70655bfaf85467ae Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 2 Dec 2014 16:33:24 +0100 Subject: [PATCH 02/11] store the original size of the frames For now only for LibGDX importer and TexturePacker importer. --- spritesheet/data/SpritesheetFrame.hx | 8 ++++++-- spritesheet/importers/LibGDXImporter.hx | 2 +- spritesheet/importers/TexturePackerImporter.hx | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spritesheet/data/SpritesheetFrame.hx b/spritesheet/data/SpritesheetFrame.hx index 2ef199c..1bdc61f 100644 --- a/spritesheet/data/SpritesheetFrame.hx +++ b/spritesheet/data/SpritesheetFrame.hx @@ -15,9 +15,11 @@ class SpritesheetFrame { public var width:Int; public var x:Int; public var y:Int; + public var origWidth:Int; + public var origHeight:Int; - public function new (x:Int = 0, y:Int = 0, width:Int = 0, height:Int = 0, offsetX:Int = 0, offsetY:Int = 0) { + public function new (x:Int = 0, y:Int = 0, width:Int = 0, height:Int = 0, offsetX:Int = 0, offsetY:Int = 0, origWidth:Int = -1, origHeight:Int = -1) { this.x = x; this.y = y; @@ -25,8 +27,10 @@ class SpritesheetFrame { this.height = height; this.offsetX = offsetX; this.offsetY = offsetY; + this.origWidth = (origWidth == -1)?width:origWidth; + this.origHeight = (origHeight == -1)?height:origHeight; } -} \ No newline at end of file +} diff --git a/spritesheet/importers/LibGDXImporter.hx b/spritesheet/importers/LibGDXImporter.hx index d29f631..0de2ea6 100644 --- a/spritesheet/importers/LibGDXImporter.hx +++ b/spritesheet/importers/LibGDXImporter.hx @@ -61,7 +61,7 @@ class LibGDXImporter { for (i in 0...frames.length) { var gdxFrame = frames[i]; - var sFrame = new SpritesheetFrame (gdxFrame.xy.x, gdxFrame.xy.y, gdxFrame.size.w, gdxFrame.size.h, gdxFrame.offset.x, gdxFrame.orig.h - gdxFrame.size.h - gdxFrame.offset.y); + var sFrame = new SpritesheetFrame (gdxFrame.xy.x, gdxFrame.xy.y, gdxFrame.size.w, gdxFrame.size.h, gdxFrame.offset.x, gdxFrame.orig.h - gdxFrame.size.h - gdxFrame.offset.y, gdxFrame.orig.w, gdxFrame.orig.h); sFrame.name = gdxFrame.filename; indexes.push (allFrames.length); diff --git a/spritesheet/importers/TexturePackerImporter.hx b/spritesheet/importers/TexturePackerImporter.hx index 9766080..99af180 100644 --- a/spritesheet/importers/TexturePackerImporter.hx +++ b/spritesheet/importers/TexturePackerImporter.hx @@ -100,7 +100,7 @@ class TexturePackerImporter { for (i in 0...frames.length) { var tpFrame:TPFrame = frames[i]; - var sFrame = new SpritesheetFrame ( tpFrame.frame.x, tpFrame.frame.y, tpFrame.frame.w, tpFrame.frame.h ); + var sFrame = new SpritesheetFrame ( tpFrame.frame.x, tpFrame.frame.y, tpFrame.frame.w, tpFrame.frame.h, tpFrame.sourceSize.w, tpFrame.sourceSize.h ); if( tpFrame.trimmed ) { From fd7a393842ebc92476fb7f6de354abb1a6957574 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 3 Dec 2014 13:21:09 +0100 Subject: [PATCH 03/11] store the original size of the frame in the AnimatedSprite --- spritesheet/AnimatedSprite.hx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spritesheet/AnimatedSprite.hx b/spritesheet/AnimatedSprite.hx index db34cc0..2b50c5e 100644 --- a/spritesheet/AnimatedSprite.hx +++ b/spritesheet/AnimatedSprite.hx @@ -17,6 +17,9 @@ class AnimatedSprite extends Sprite { public var currentFrameIndex:Int; public var smoothing:Bool; public var spritesheet:Spritesheet; + + public var currentFrameOrigWidth(default,null):Int=0; + public var currentFrameOrigHeight(default,null):Int=0; private var behaviorComplete:Bool; private var behaviorQueue:Array ; @@ -156,6 +159,9 @@ class AnimatedSprite extends Sprite { var frame = spritesheet.getFrame (currentBehavior.frames [currentFrameIndex]); + // Set frame data + currentFrameOrigWidth = frame.origWidth; + currentFrameOrigHeight = frame.origHeight; bitmap.bitmapData = frame.bitmapData; bitmap.smoothing = smoothing; @@ -213,4 +219,4 @@ class AnimatedSprite extends Sprite { } -} \ No newline at end of file +} From 9f60587e115798146dbd41e67df9881ef2f888f6 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 7 Feb 2015 19:16:34 +0100 Subject: [PATCH 04/11] Bug fix: The image, that remains, when an animation is over is now the last image of the animation. --- spritesheet/AnimatedSprite.hx | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/spritesheet/AnimatedSprite.hx b/spritesheet/AnimatedSprite.hx index 68b6e72..1ad83f6 100644 --- a/spritesheet/AnimatedSprite.hx +++ b/spritesheet/AnimatedSprite.hx @@ -134,27 +134,22 @@ class AnimatedSprite extends Sprite { } } - - + public function update (deltaTime:Int):Void { if (!behaviorComplete) { timeElapsed += deltaTime; - var ratio = timeElapsed / loopTime; - - if (ratio >= 1) { + if (timeElapsed >= loopTime) { if (currentBehavior.loop) { - - ratio -= Math.floor (ratio); - + timeElapsed -= loopTime; } else { - + behaviorComplete = true; - ratio = 1; - + timeElapsed = loopTime-1; //Stop time + } } @@ -166,10 +161,12 @@ class AnimatedSprite extends Sprite { // This is the number of ms we have been in this animation var timeInAnimation:Int = timeElapsed % loopTime; // The raw frame index is the number of frames we have had time to show - var rawFrameIndex:Int = Math.round(timeInAnimation / frameDuration); + var currentFrameIndex:Int = Std.int(timeInAnimation / frameDuration); // Make sure we loop correctly - currentFrameIndex = rawFrameIndex % frameCount; - + if (currentFrameIndex >= frameCount) { + currentFrameIndex = frameCount - 1; + } + var frame = spritesheet.getFrame (currentBehavior.frames [currentFrameIndex]); isAFrameShown = true; From c641d2a095639d1cd93689a87f144ef8800a2078 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 7 Feb 2015 19:27:00 +0100 Subject: [PATCH 05/11] allow reversing of animations --- spritesheet/data/BehaviorData.hx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spritesheet/data/BehaviorData.hx b/spritesheet/data/BehaviorData.hx index d6ce0e8..6ba6108 100644 --- a/spritesheet/data/BehaviorData.hx +++ b/spritesheet/data/BehaviorData.hx @@ -55,6 +55,11 @@ class BehaviorData { return new BehaviorData ("behavior" + (uniqueID++), frames.copy (), loop, frameRate, originX, originY); } - + + public function reverse():BehaviorData { + var nFrames = frames.copy(); + nFrames.reverse(); + return new BehaviorData (name + "Reverse", nFrames, loop, frameRate, originX, originY); + } } \ No newline at end of file From 5538f6b065c57ed13785f8bdc2ad3ca84e69f345 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 7 Feb 2015 19:27:24 +0100 Subject: [PATCH 06/11] read out color transformation factors and use in tilesheet rendering --- spritesheet/render/RenderTilesheetToGraphics.hx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spritesheet/render/RenderTilesheetToGraphics.hx b/spritesheet/render/RenderTilesheetToGraphics.hx index 13fbec6..0313a61 100644 --- a/spritesheet/render/RenderTilesheetToGraphics.hx +++ b/spritesheet/render/RenderTilesheetToGraphics.hx @@ -15,7 +15,16 @@ import spritesheet.AnimatedSprite; } public function drawFrame(frame : SpritesheetFrame, offsetX : Float , offsetY : Float, smoothing : Bool) { sprite.graphics.clear(); - tilesheet.drawTiles(sprite.graphics, [offsetX + frame.offsetX,offsetY + frame.offsetY, frame.tilesheetIndex], smoothing, flags); + var tileData : Array = [offsetX + frame.offsetX,offsetY + frame.offsetY, frame.tilesheetIndex]; // First data = offset + var usedFlags = flags; + if (sprite.transform.colorTransform != null) { + tileData.push(sprite.transform.colorTransform.redMultiplier); + tileData.push(sprite.transform.colorTransform.greenMultiplier); + tileData.push(sprite.transform.colorTransform.blueMultiplier); + tileData.push(sprite.transform.colorTransform.alphaMultiplier); + usedFlags |= Tilesheet.TILE_RGB | Tilesheet.TILE_ALPHA; + } + tilesheet.drawTiles(sprite.graphics,tileData, smoothing, usedFlags); } public function enableFlag(flag : Flag) { switch(flag) { From 3f38ebc62281e9a1c2bbac7ea54c272e0a85fefd Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 13 Feb 2015 16:21:03 +0100 Subject: [PATCH 07/11] do not overwrite currentFrameIndex with a local variable --- spritesheet/AnimatedSprite.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spritesheet/AnimatedSprite.hx b/spritesheet/AnimatedSprite.hx index 1ad83f6..cee7e0a 100644 --- a/spritesheet/AnimatedSprite.hx +++ b/spritesheet/AnimatedSprite.hx @@ -161,7 +161,7 @@ class AnimatedSprite extends Sprite { // This is the number of ms we have been in this animation var timeInAnimation:Int = timeElapsed % loopTime; // The raw frame index is the number of frames we have had time to show - var currentFrameIndex:Int = Std.int(timeInAnimation / frameDuration); + currentFrameIndex = Std.int(timeInAnimation / frameDuration); // Make sure we loop correctly if (currentFrameIndex >= frameCount) { currentFrameIndex = frameCount - 1; From 90563e99a266039190207b272d10aac7390bd3f1 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 13 Feb 2015 16:22:45 +0100 Subject: [PATCH 08/11] ignore alpha multiplier. The transparency is already given by the alpha of the sprite. --- spritesheet/render/RenderTilesheetToGraphics.hx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spritesheet/render/RenderTilesheetToGraphics.hx b/spritesheet/render/RenderTilesheetToGraphics.hx index 0313a61..7fba2c9 100644 --- a/spritesheet/render/RenderTilesheetToGraphics.hx +++ b/spritesheet/render/RenderTilesheetToGraphics.hx @@ -21,8 +21,7 @@ import spritesheet.AnimatedSprite; tileData.push(sprite.transform.colorTransform.redMultiplier); tileData.push(sprite.transform.colorTransform.greenMultiplier); tileData.push(sprite.transform.colorTransform.blueMultiplier); - tileData.push(sprite.transform.colorTransform.alphaMultiplier); - usedFlags |= Tilesheet.TILE_RGB | Tilesheet.TILE_ALPHA; + usedFlags |= Tilesheet.TILE_RGB; } tilesheet.drawTiles(sprite.graphics,tileData, smoothing, usedFlags); } From bb14bc36cf1c77d48a3cc08bc305e656d171a1ed Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 25 Jun 2015 11:24:25 +0200 Subject: [PATCH 09/11] update the frame even if a frame is already shown when showing a new behavior --- spritesheet/AnimatedSprite.hx | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spritesheet/AnimatedSprite.hx b/spritesheet/AnimatedSprite.hx index cee7e0a..5c3b862 100644 --- a/spritesheet/AnimatedSprite.hx +++ b/spritesheet/AnimatedSprite.hx @@ -54,7 +54,6 @@ class AnimatedSprite extends Sprite { } } - public function getFrameData (index:Int):Dynamic { if (currentBehavior != null && currentBehavior.frameData.length > index) { @@ -207,11 +206,7 @@ class AnimatedSprite extends Sprite { loopTime = Std.int ((behavior.frames.length / behavior.frameRate) * 1000); - if (!isAFrameShown) { - - update (0); - - } + update (0); } From 697a69c5522aed6709073fdb4901b1f36e070cd1 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 25 Jun 2015 11:24:42 +0200 Subject: [PATCH 10/11] don't recreate the arrays on every redraw --- .../render/RenderTilesheetToGraphics.hx | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/spritesheet/render/RenderTilesheetToGraphics.hx b/spritesheet/render/RenderTilesheetToGraphics.hx index 7fba2c9..966b33e 100644 --- a/spritesheet/render/RenderTilesheetToGraphics.hx +++ b/spritesheet/render/RenderTilesheetToGraphics.hx @@ -9,18 +9,33 @@ import spritesheet.AnimatedSprite; private var sprite : Sprite; private var tilesheet : Tilesheet; private var flags : Int = 0; + + // We store the tile data here because we do not want to recreate it every frame and run into garbage collector problems + // But since it is refilled every time a frame is drawn, it can be static + static private var tileDataWithColorTransform : Array = [0.0,0.0,0.0,0.0,0.0,0.0]; + static private var tileDataWithoutColorTransform : Array = [0.0,0.0,0.0]; + public function new(o : Sprite, tilesheet : Tilesheet) { this.sprite = o; this.tilesheet = tilesheet; } public function drawFrame(frame : SpritesheetFrame, offsetX : Float , offsetY : Float, smoothing : Bool) { sprite.graphics.clear(); - var tileData : Array = [offsetX + frame.offsetX,offsetY + frame.offsetY, frame.tilesheetIndex]; // First data = offset + var tileData : Array = null;// First data = offset var usedFlags = flags; - if (sprite.transform.colorTransform != null) { - tileData.push(sprite.transform.colorTransform.redMultiplier); - tileData.push(sprite.transform.colorTransform.greenMultiplier); - tileData.push(sprite.transform.colorTransform.blueMultiplier); + if (sprite.transform.colorTransform == null) { + tileData = tileDataWithoutColorTransform; + tileData[0] = offsetX + frame.offsetX; + tileData[1] = offsetY + frame.offsetY; + tileData[2] = frame.tilesheetIndex; + } else { + tileData = tileDataWithColorTransform; + tileData[0] = offsetX + frame.offsetX; + tileData[1] = offsetY + frame.offsetY; + tileData[2] = frame.tilesheetIndex; + tileData[3] = sprite.transform.colorTransform.redMultiplier; + tileData[4] = sprite.transform.colorTransform.greenMultiplier; + tileData[5] = sprite.transform.colorTransform.blueMultiplier; usedFlags |= Tilesheet.TILE_RGB; } tilesheet.drawTiles(sprite.graphics,tileData, smoothing, usedFlags); From 0b0830f5d61bf91b99f322720bb41e1a22d9f520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathan=20H=C3=BCsken?= Date: Sun, 4 Dec 2016 17:18:24 +0100 Subject: [PATCH 11/11] WIP: Starting to use Tilemap instead of Tilesheet --- spritesheet/Spritesheet.hx | 12 ++++++------ spritesheet/render/RenderTilesheetToGraphics.hx | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spritesheet/Spritesheet.hx b/spritesheet/Spritesheet.hx index be06f9f..fff2aef 100644 --- a/spritesheet/Spritesheet.hx +++ b/spritesheet/Spritesheet.hx @@ -2,7 +2,7 @@ package spritesheet; import flash.display.BitmapData; -import openfl.display.Tilesheet; +import openfl.display.Tilemap; import flash.geom.Point; import flash.geom.Rectangle; import spritesheet.data.BehaviorData; @@ -10,13 +10,13 @@ import spritesheet.data.SpritesheetFrame; private enum ImageData { BITMAP_DATA(sourceImage : BitmapData, sourceImageAlpha : BitmapData); - TILESHEET(sheet : openfl.display.Tilesheet); + TILESHEET(sheet : openfl.display.Tilemap); } enum StorageType { INDIVIDUAL_BITMAPS;// Store every frame in its own BITMAP_DATA SINGLE_BITMAP; // Store all frames in onw single bitmap - TILESHEET; // Store using openfl.display.Tilesheet + TILESHEET; // Store using openfl.display.Tilemap } class Spritesheet { @@ -37,14 +37,14 @@ class Spritesheet { if (storageType == null) { #if flash - storageType = INDIVIDUAL_BITMAPS; //Tilesheet does not work with blendmode = ADD on flash + storageType = INDIVIDUAL_BITMAPS; //Tilemap does not work with blendmode = ADD on flash #else storageType = TILESHEET; //Only tilesheets work for blendmode = ADD on other targets #end } if (storageType == TILESHEET) { - this.imageData = TILESHEET(new Tilesheet(image)); + this.imageData = TILESHEET(new Tilemap(image)); this.useSingleBitmapData = true; } else { this.imageData = BITMAP_DATA(image, imageAlpha); @@ -262,4 +262,4 @@ class Spritesheet { } -} \ No newline at end of file +} diff --git a/spritesheet/render/RenderTilesheetToGraphics.hx b/spritesheet/render/RenderTilesheetToGraphics.hx index 966b33e..2cebc31 100644 --- a/spritesheet/render/RenderTilesheetToGraphics.hx +++ b/spritesheet/render/RenderTilesheetToGraphics.hx @@ -1,13 +1,13 @@ package spritesheet.render; -import openfl.display.Tilesheet; +import openfl.display.Tilemap import spritesheet.data.SpritesheetFrame; import flash.display.Sprite; import spritesheet.AnimatedSprite; class RenderTilesheetToGraphics implements IRenderTarget { private var sprite : Sprite; - private var tilesheet : Tilesheet; + private var tilesheet : Tilemap; private var flags : Int = 0; // We store the tile data here because we do not want to recreate it every frame and run into garbage collector problems @@ -15,7 +15,7 @@ import spritesheet.AnimatedSprite; static private var tileDataWithColorTransform : Array = [0.0,0.0,0.0,0.0,0.0,0.0]; static private var tileDataWithoutColorTransform : Array = [0.0,0.0,0.0]; - public function new(o : Sprite, tilesheet : Tilesheet) { + public function new(o : Sprite, tilesheet : Tilemap) { this.sprite = o; this.tilesheet = tilesheet; } @@ -36,20 +36,20 @@ import spritesheet.AnimatedSprite; tileData[3] = sprite.transform.colorTransform.redMultiplier; tileData[4] = sprite.transform.colorTransform.greenMultiplier; tileData[5] = sprite.transform.colorTransform.blueMultiplier; - usedFlags |= Tilesheet.TILE_RGB; + usedFlags |= Tilemap.TILE_RGB; } tilesheet.drawTiles(sprite.graphics,tileData, smoothing, usedFlags); } public function enableFlag(flag : Flag) { switch(flag) { case BLEND_ADD: - flags = flags | Tilesheet.TILE_BLEND_ADD; + flags = flags | Tilemap.TILE_BLEND_ADD; } } public function disableFlag(flag : Flag) { switch(flag) { case BLEND_ADD: - flags &= 0xFFFFFFF - Tilesheet.TILE_BLEND_ADD; + flags &= 0xFFFFFFF - Tilemap.TILE_BLEND_ADD; } } -} \ No newline at end of file +}