zaterdag 19 december 2015

Stencyl / Haxe : Loading images in background



For: Stencyl
Version: 3

Works with:
  1. Android
  2. iOS
  3. Flash
  4. Windows
  5. Mac OSX
  6. (Linux)


Note:
  1. HTTP URL only (!)
  2. Base64 coding required … JPG/PNG is not translated well with setPixel ….


* Download the github mloader haxe package (massiveinteractive)
* Download the signal github package from massiveinteractive
* put the mloader (Src) into the extension directory
* put the msignal (src) into the extension directory
* Code to load something:

<pre>
import mloader.Loader;
import mloader.ImageLoader;
import mloader.Loader.LoaderEvent;

class MLoader{
public static var loader:ImageLoader;
private static var func:Dynamic;
public static function load(url:String, theFunc:Dynamic){
trace("Loading URL :"+url);
loader=new ImageLoader(url);
loader.loaded.add(loaded);
loader.load();
func=theFunc(); // Call Back
// theFunc(); // <- do call back
//
// Load image as a text ...
var m1=new mloader.StringLoader(url);
m1.loaded.add(loadedstring);
m1.load();
}

public static function loaded(event:LoaderEvent<Dynamic>){
trace("mloader.Loader.LoaderEvent: "+event);
}


public static function loadedstring(event:LoaderEvent<Dynamic>){
if(event.type == LoaderEventType.Complete){
// Make an image from the Text !!
if(func!=null)func();
} // complete?
}

}
</pre>
<pre>

/**
* ...
*
* Version: 2014_04_13
*
*
*/

import com.stencyl.Engine;

import mloader.Loader;
import mloader.ImageLoader;
import mloader.Loader.LoaderEvent;

import com.stencyl.behavior.Script;

// queue
import mloader.LoaderQueue;

class MLoader
{

private static var script:Script=null;

public static var loader:ImageLoader;
public static var func:Dynamic;
public static var abcd;
   public static function load(eng:Engine, url:String, theFunc:Dynamic){
trace("Load url: "+url);
  script = new com.stencyl.behavior.Script(eng);
func=theFunc;
/*
// not used
var queue=new LoaderQueue();
queue.maxLoading=2;
queue.ignoreFailures=false;
queue.loaded.addOnce(queueComplete).forType(Complete);
queue.add(new ImageLoader(url));
queue.add(new ImageLoader(url));
queue.load();
*/
/*
// Not used <- does not work with neko > ios/android?!
loader=new ImageLoader(url);
loader.loaded.add(loaded);
loader.load();
*/

// We use the text mechanism
// Load image as a text ...
var m1=new mloader.StringLoader(url);
m1.loaded.add(loadedstring);
m1.load();
  }

// not used
public static function queueComplete(event:LoaderEvent<Dynamic>){
script.setGameAttribute("Debug", script.getGameAttribute("Debug")+event.type);
}

//
// String is loaded
// When complete -> create image from string
//
public static function loadedstring(event:LoaderEvent<Dynamic>){
   
//script.setGameAttribute("Debug", script.getGameAttribute("Debug")+event.type);
if(event.type == LoaderEventType.Complete){
script.setGameAttribute("Debug", script.getGameAttribute("Debug")+" TeXT ");

// wrapper
// if(func!=null)func();

// Make an image from the Text !!
// works with small files, but why does it fail with big files?
script.setGameAttribute("Debug", script.getGameAttribute("Debug")+" = "+(event.target.content).substring(0,20));
script.attachImageToHUD(new flash.display.Bitmap(script.imageFromText(event.target.content)), 20,20);

} // complete?
}
// image loading function that fails in neko?!
public static function loaded(event:LoaderEvent<Dynamic>){

script.setGameAttribute("Debug", script.getGameAttribute("Debug")+event.type);
if(event.type == LoaderEventType.Complete){
script.setGameAttribute("Debug", script.getGameAttribute("Debug")+"YESSSS");
script.setGameAttribute("Debug", script.getGameAttribute("Debug")+event.target.content);

// Debug : test the creation of a bitmap and put it on the screen
var source:nme.display.BitmapData = new nme.display.BitmapData(300,300);

var bi:Int=0;
var bj:Int=0;
var brc:Int=0;
var bgc:Int=0;
var bbc:Int=0;
for(bi in 1 ... 1020){
for(bj in 1 ... 1020){
var pixel=bi;
brc=pixel;
bgc=pixel+brc;
bbc=pixel+bgc;
source.setPixel(bi,bj,(brc<<16|bgc<<8|bbc));
}
}
var bitmap=new flash.display.Bitmap(event.target.content);
script.attachImageToHUD(bitmap, 0,0);

if(func!=null)func();
}// if/switch
} // loaded

} // CLASSloader
//
// --------- END OF loader
//


</pre>


Geen opmerkingen:

Een reactie posten