Wednesday, 22 February 2012

AssetManager : Overview

I know I went over this before but might as well go over the AssetManager as it's probably the only core bit of code that I'm going to use in every test.

Essentially the AssetManager will execute loads, manage the queue and dispatch an event when its complete.  It should also be the only place where a getDefinitionByName is called.  I won't post the whole file, I'm hosting them at GoogleCode for that reason.  Here is the AssetManager in the source browser though.

The only bit worth mentioning is the load function :

public function load(toLoad : Object) : void {
 if (toLoad is Asset) {
  queueAsset(Asset(toLoad));
 } else if (toLoad is Array) {
  for each (var asset : Asset in toLoad) {
   queueAsset(asset);
  }
 }
 
 if (!_loaderInProgess) loadNext();
}

You can pass it either an Asset or an Array of Assets which will then be added to the queue, if nothing is loading it will then start the load process with the loadNext() call.

The AssetManager uses to the AssetLoader to load each Asset.  It's a pretty simple class, all it does is instantiate the loader type it requires, adds / removes listeners and throws and errors if it encounters them.  To prevent it being garbage collected it passes itself to the Asset.

Both classes will accept and handle on an Asset object or multiple Asset objects.  Again not much to report here but the Asset class is more a data type.  It stores all the information required to load and access the content.  The only complicated doohickey here would be the isData() getter :

internal function get isData() : Boolean {
 var type : String = _urlRequest.url.substr(_urlRequest.url.length - 4, 4);
 
 switch (type) {
  case ".swf" : 
  case ".png" : 
  case ".jpg" : 
  case ".gif" : 
   return false;
  default :
   return true;
 }
}

This works out from the URL what type of loader to use.  Great eh?

So it's pretty easy to use, you create an instance of the AssetManager, add an event listener so you know when it's complete and pass it a load of Assets to load ::

public function AssetManagerTest(stage : Stage) {
 _stage = stage;
 _stage.addChild(new Profiler());
 
 _loadList = [ new Asset("spritesheets/explosion.xml"), 
     new Asset("spritesheets/explosion.png"),
     new Asset("spriteclips/explosion.swf")  ];
 
 _assetManager = new AssetManager();
 _assetManager.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
 _assetManager.load(_loadList);
}


To access the content you do so pretty much like you would with a loader, with the content property :

private function loadComplete(e : Event) : void {
 _updateProgressTrigger.stop();
 
 var loadedXML : XML = new XML(_loadList[0].content);
 var loadedBitmap : Bitmap = _loadList[1].content as Bitmap;
 var loadedLinkedLibrary : Class = _assetManager.getClass("Explosion");
 var loadedLinkedMovieClipInstance : MovieClip = new loadedLinkedLibrary();
 
 trace("[AssetManagerTest.loadComplete] |TEST| XML :: " + loadedXML.toXMLString());
 trace("[AssetManagerTest.loadComplete] |TEST| Bitmap :: " + loadedBitmap);
 trace("[AssetManagerTest.loadComplete] |TEST| MovieClip :: " + loadedLinkedMovieClipInstance);
}

You might wonder why I bothered with this?  It's basically to wrap the entire loading process into a nice little bundle which I can use whenever I want.  I may make this a singleton class so it can be accessed anywhere but I'd rather managed the access to it.  It also means if I need to update the way I load things I can do it one place.

Right I've uploaded a downloadable zip which has all of these classes in them and a test swf so you can see the result.  Let me know if you have any problems (if in fact anyone actually reads this!).

I'm sure loads of it will get updated as I progress along the project work flow (there isn't one yet :]).  Pretty basic stuff.  Next post I'll get into the blitting package and hopefully that will be more interesting.


Tuesday, 21 February 2012

SyntaxHighlighter

Wahey!!  Managed to figure out how to use SyntaxHighlighter.  This marvellous little do dad formats your code into a nice little code thingy :)

package {
 import flash.display.Sprite;
 import tests.BlitVSClip;
 
 /**
  * @author Charlie MacIsaac
  */
 
 [SWF(width = "800", height = "600", backgroundColor = "#000000", frameRate = "25")]
 
 public class Launcher extends Sprite {
  
  private var _memGauge : MemoryGauge;
  
  public function Launcher() : void {
   _memGauge = new MemoryGauge();
   
   runBlitVSClipTest();
  }
  
  private var _blitVSClipTest : BlitVSClip;
  private function runBlitVSClipTest() : void {
   _blitVSClipTest = new BlitVSClip(_memGauge, stage);
  }
 }
}

I used BloggerMint to figure out how to add it to the blog.  It's a really neat way of doing it!

So, finally got code showing and my project hosted, now I just need to follow through and create those downloads and link them here for the first few tests!  Should be fun :)

Friday, 17 February 2012

Fixed!

After that complete balls up I've re-written the code and tests, now finally it does indeed look like blitting is faster (as expected!).  Hurray!

I just saw that you can set up downloads on google code so I'll probably set up swf's for each test to be downloaded from there.  I'll write a proper post about it all over the next couple of days.

I *might* also set up some ANT stuff to automate some compiling tasks.  Joy!

Thursday, 16 February 2012

Blithering Idiot

Gosh, I've just realised I've got this blitting thing all wrong.  You're suppose to have a canvas bitmap which is essentially the stage, each time an object changes you need to draw the stage again!

Oh dear oh dear!  I was running some tests and realised that the method I was using was a LOT slower!

Back to the drawing board!

EDIT:  I should probably add that these two links really made me think "You plonker" : Adobe Link, Iain Lobb.

At least I'm learning something new for once :)

Wednesday, 15 February 2012

New Asset Class

Right I've drummed up a really basic class which will automatically generate a playable sprite based on the sheet and xml its passed.

I have to figure out somewhere to post the demos although it is particularly dull :)  And probably some nice way of displaying code within a post

It's all available at Google Code.

Up Next : SpriteSheets

Right so I whacked in that asset class and uploaded everything to a google code project.  Here's the link: Google Code


The Asset Manager for now does what I need it to do (I'm sure I've said that before).  Right now I'm doing a bit of research into blitting / spritesheets.


First off, there seems to be two types of sprite sheet; one which appears to be for animation and another which seems to be a characters entire collection of visible states.  The main difference between the two is the spacing, one is uniform and the other isn't.
Character Sheet
Uniform : Animation

To manage the spritesheets I'll have to use some sort of data sheet to go with it. I'm thinking the best thing to do is create an XML sheet to handle all the details for the spritesheet.  To what extent I guess I'll find out when I get to it.

Having a quick butchers on the net I've come across this website : 8bitrocket, which has a wealth of wonderful tutorials on blitting and rendering models and such.

So yeah this should be fun working out how best to approach this, I'm sure someone told me when I first started programming that you should never develop  something unless you know you're going to need it...What the hell am I going to use this for!!

Monday, 6 February 2012

I talk too much


Hmmm....  I was just thinking, perhaps it would be a lot better to have a new Class which handles all the asset details...an Asset class :)


The Asset would be created and passed to the AssetManager which would then determine what to do with it, usually creating an AssetLoader, loading it and then allowing access to the loaded object.


I'll whack it in the code next time I work on it.

Sunday, 5 February 2012

Asset Manager


Right so I made a couple of classes for the asset management tool.  I'm going to host all the code on Google Code at some point but I can't be arsed at the moment (too cold perhaps?).  Instead I'll just run through the basics.

So the bits I thought I needed were :
  • Determine whether to use the URLLoader for data (xml, text, etc) or the Loader for swf's and images.
  • Allow access to loaded data and encapsulate the getDefinitionByName function
  • Unload assets that are no longer required (Garbage Collection, yuk)
  • Queue loads
  • Report load progress, handle errors and complete events
Out of the 5 I managed to do 3, I didn't bother with the getDefinitionByName just yet because it's mainly used to grab graphical assets from Flash files which I won't be using any time soon.

I also didn't bother with the unloading of assets because again the main purpose of this is to unload load graphics swf's to free up memory. I won't really need to do that with bitmaps and handling the disposal of those is much easier.

There are only two classes; AssetManager and AssetLoader.  The AssetManager simply instantiates an AssetLoader and adds it to a loadQueue.  The AssetLoader determines what type of file is being loaded (from the extension of the URL), loads the file reporting the progress (as a trace only at the moment) and then executes a callback.  Pretty basic but hopefully a solid base to start on, adding to it when necessary.

Ok so I'll create a google code account some time later on because talking about it is rubbish...

Next on the list : SpriteSheet handling!

Thursday, 2 February 2012

To code or not to code...

A blog...to do whatever I want with....!

In making this blog...why is the Blogger spell check telling that "blog" and  "Blogger" incorrectly spelt.  Nice.

Anyway I'm hopefully going to use this to help me actually do some Flash work at home cos playing Skyrim, Minecraft, Zelda and SW:TOR is ... crap.

My first project is to build up a core library of components that I can use at home and for all the projects (many I hope) to come.  This should include some kind of loader and asset management, base classes for graphical purposes, some kind of animation handler (sprite sheet animations?) and a whole load of other stuff which I can't even think of!

The first project then : An Asset Management Tool which will load graphics, data and other bits into ApplicationDomains and allow decent management and access of them.

So I guess I should think about some sort of goal:
  • Determine whether to use the URLLoader for data (xml, text, etc) or the Loader for swf's and images.
  • Allow access to loaded data and encapsulate the getDefinitionByName function
  • Unload assets that are no longer required (Garbage Collection, yuk)
  • Queue loads
  • Report load progress, handle errors and complete events
I'm sure there are loads more but I can't think of any right now!

Right, off to play SW:TOR then....