Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Examples update Genome v0.3.2

  • Loading branch information...
commit 59013b5c2bf7d60560ec8d76c21945e6aea8685b 1 parent 9b50c0a
Peter @sHTiF Stefcek authored
31 examples/Genome2DExamples.as
View
@@ -1,7 +1,8 @@
package
{
- import com.flashcore.g2d.contexts.bitmap.G2DBitmapContext;
- import com.flashcore.g2d.contexts.molehill.G2DStage3DContext;
+ import com.flashcore.g2d.context.bitmap.G2DBitmapContext;
+ import com.flashcore.g2d.context.blitting.G2DBlittingContext;
+ import com.flashcore.g2d.context.stage3d.G2DStage3DContext;
import com.flashcore.g2d.core.Genome2D;
import examples.*;
@@ -23,13 +24,14 @@ package
private var __aExamples:Vector.<Example> = new Vector.<Example>();
private var __iCurrentExample:int = 0;
- private var __aContexts:Vector.<Class> = new <Class>[G2DStage3DContext, G2DBitmapContext];
+ private var __aContexts:Vector.<Class> = new <Class>[G2DStage3DContext, G2DBitmapContext, G2DBlittingContext];
private var __iCurrentContext:int = 0;
+ private var __spUI:Sprite;
private var __tfLabel:TextField;
private var __tfContext:TextField;
-
private var __tfInfo:TextField;
+
public function set info(p_info:String):void {
__tfInfo.htmlText = p_info;
}
@@ -39,14 +41,20 @@ package
__aExamples.push(new MouseExample(this));
__aExamples.push(new HierarchyExample(this));
__aExamples.push(new CollisionExample(this));
+ __aExamples.push(new BlittingExample(this));
createUI();
+ // Setup a signal callback for initialization
Genome2D.getInstance().onInitialized.addOnce(onInitialized);
+ // Initialize genome with a selected renderer
Genome2D.getInstance().init(stage, __aContexts[__iCurrentContext]);
}
private function createUI():void {
+ __spUI = new Sprite();
+ addChild(__spUI);
+
var dtf:TextFormat = new TextFormat("Arial", 12);
dtf.align = TextFormatAlign.RIGHT;
dtf.bold = true;
@@ -58,8 +66,8 @@ package
__tfLabel.x = 10;
__tfLabel.y = 478;
__tfLabel.autoSize = TextFieldAutoSize.LEFT;
- __tfLabel.htmlText = "<font color='#000000'>["+(__iCurrentExample+1)+"/"+__aExamples.length+"] press Space for next";
- addChild(__tfLabel);
+ __tfLabel.htmlText = "<font color='#000000'>["+(__iCurrentExample+1)+"/"+__aExamples.length+"] press Space for next, press H to hide UI";
+ __spUI.addChild(__tfLabel);
dtf.bold = false;
__tfContext = new TextField();
@@ -73,7 +81,7 @@ package
var contextString:String = String(__aContexts[__iCurrentContext]);
__tfContext.htmlText = "[Press C to switch] Renderer: <b>"+contextString.substring(7, contextString.length-8)+"</b>";
- addChild(__tfContext);
+ __spUI.addChild(__tfContext);
dtf.align = TextFormatAlign.LEFT;
@@ -88,15 +96,15 @@ package
__tfInfo.background = true;
__tfInfo.backgroundColor = 0x0;
__tfInfo.wordWrap = true;
- addChild(__tfInfo);
+ __spUI.addChild(__tfInfo);
+
+ __spUI.addChild(new Stats());
}
private function onInitialized():void {
__aExamples[__iCurrentExample].init();
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
-
- stage.addChild(new Stats());
}
private function nextExample():void {
@@ -125,6 +133,9 @@ package
case 67:
nextContext();
break;
+ case 72:
+ __spUI.visible = !__spUI.visible;
+ break;
}
}
}
71 examples/examples/BasicExample.as
View
@@ -2,10 +2,9 @@ package examples
{
import com.flashcore.g2d.core.Genome2D;
import com.flashcore.g2d.g2d;
- import com.flashcore.g2d.materials.G2DMaterialLibrary;
- import com.flashcore.g2d.sprites.G2DMovieClip;
- import com.flashcore.g2d.sprites.G2DSprite;
- import com.flashcore.g2d.sprites.G2DTransform;
+ import com.flashcore.g2d.display.G2DMovieClip;
+ import com.flashcore.g2d.display.G2DSprite;
+ import com.flashcore.g2d.display.G2DTransform;
import com.flashcore.g2d.textures.G2DTexture;
import flash.display.Sprite;
@@ -17,33 +16,40 @@ package examples
private static const MinesXML:Class;
[Embed(source = "./assets/mines.png")]
private static const MinesGFX:Class;
+ [Embed(source = "./assets/ninja.xml", mimeType = "application/octet-stream")]
+ private static const NinjaXML:Class;
+ [Embed(source = "./assets/ninja.png")]
+ private static const NinjaGFX:Class;
- private var __cTexture:G2DTexture;
+ private var __cMineTexture:G2DTexture;
+ private var __cNinjaTexture:G2DTexture;
private const COUNT:int = 500;
private var __iClipCount:int;
+ private var __bMove:Boolean = true;
public function BasicExample(p_wrapper:Genome2DExamples):void {
super(p_wrapper);
}
private function updateInfo():void {
- _cWrapper.info = "<font color='#00FFFF'>BasicExample [ "+__iClipCount+" G2DMovieClips ]\n"+
+ _cWrapper.info = "<font color='#00FFFF'><b>BasicExample</b> [ "+__iClipCount+" G2DMovieClips ]\n"+
"<font color='#FFFFFF'>This is a simple stress test example rendering movieclips, each moveclip plays at its own custom framerate.\n"+
- "<font color='#FFFF00'>Press ARROW UP to increase the number of movieclips and ARROW DOWN to decrease them.";
+ "<font color='#FFFF00'>Press ARROW UP to increase the number of movieclips and ARROW DOWN to decrease them, Press P to pause movement.";
}
override public function init():void {
- __cTexture = G2DTexture.createFromBitmapAtlas((new MinesGFX()).bitmapData, XML(new MinesXML()));
-
- G2DMaterialLibrary.createMaterial("mines", __cTexture);
- G2DMaterialLibrary.createMaterial("mine", __cTexture.getSubTextureById("mine2"));
+ super.init();
+ __cMineTexture = G2DTexture.createFromBitmapAtlas((new MinesGFX()).bitmapData, XML(new MinesXML()));
+ __cNinjaTexture = G2DTexture.createFromBitmapAtlas((new NinjaGFX()).bitmapData, XML(new NinjaXML()));
+ //__cTexture = G2DTexture.createFromBitmapData((new CrateGFX()).bitmapData);
__iClipCount = 0;
addClips(COUNT);
_cGenome.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
+ _cGenome.onRender.add(onRender);
updateInfo();
}
@@ -51,33 +57,48 @@ package examples
override public function dispose():void {
super.dispose();
- __cTexture.dispose();
+ __cMineTexture.dispose();
+ __cNinjaTexture.dispose();
+
+ _cGenome.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
+ _cGenome.onRender.removeAll();
}
private function createSprite(p_x:Number, p_y:Number):G2DSprite {
- var clip:G2DSprite = new G2DSprite();
- clip.setMaterialById("mine");
+ var clip:G2DSprite = new G2DSprite(__cMineTexture.getSubTextureById("mine2"));
clip.x = p_x;
clip.y = p_y;
return clip;
}
- private function createClip(p_x:Number, p_y:Number):G2DMovieClip {
- var clip:G2DMovieClip = new G2DMovieClip();
+ private function createMine(p_x:Number, p_y:Number):G2DMovieClip {
+ var clip:G2DMovieClip = new G2DMovieClip(__cMineTexture);
clip.setFrameRate(Math.random()*10+3);
- clip.setMaterialById("mines");
- //clip.stop();
clip.setFrames(new <String>["mine2", "mine3", "mine4", "mine5", "mine6", "mine7", "mine8", "mine9"]);
clip.gotoFrame(Math.random()*8);
clip.x = p_x;
clip.y = p_y;
+ clip.rotation = Math.random()*Math.PI;
+ return clip;
+ }
+
+ private function createNinja(p_x:Number, p_y:Number):G2DMovieClip {
+ var clip:G2DMovieClip = new G2DMovieClip(__cNinjaTexture);
+ clip.setFrameRate(Math.random()*10+3);
+ clip.setFrames(new <String>["nw1", "nw2", "nw3", "nw2", "nw1", "stood", "nw4", "nw5", "nw6", "nw5", "nw4"]);
+ clip.gotoFrame(Math.random()*8);
+ clip.x = p_x;
+ clip.y = p_y;
return clip;
}
private function addClips(p_count:int):void {
__iClipCount += p_count;
for (var i:int = 0; i<p_count; ++i) {
- var clip:G2DMovieClip = createClip(Math.random()*800, Math.random()*600);
+ var clip:G2DMovieClip;
+ //if (i%2 == 0)
+ clip = createMine(Math.random()*800, Math.random()*600);
+ //else clip = createNinja(Math.random()*800, Math.random()*600);
//var clip:G2DSprite = createSprite(Math.random()*800, Math.random()*600);
_cGenome.root.addChild(clip);
}
@@ -87,6 +108,7 @@ package examples
private function removeClips(p_count:int):void {
__iClipCount -= p_count;
+ if (__iClipCount<0) __iClipCount = 0;
for (var i:int = 0; i < p_count; ++i) {
if (_cGenome.root.numChildren == 0) break;
_cGenome.root.removeChildAt(0);
@@ -95,6 +117,14 @@ package examples
updateInfo();
}
+ private function onRender():void {
+ if (!__bMove) return;
+
+ for (var i:int = 0; i<_cGenome.root.numChildren; ++i) {
+ _cGenome.root.getChildAt(i).rotation+=.1;
+ }
+ }
+
private function onKeyDown(event:KeyboardEvent):void {
trace(event.keyCode);
switch (event.keyCode) {
@@ -104,6 +134,9 @@ package examples
case 40:
removeClips(500);
break;
+ case 80:
+ __bMove = !__bMove;
+ break;
}
}
}
105 examples/examples/BlittingExample.as
View
@@ -0,0 +1,105 @@
+package examples
+{
+ import com.flashcore.g2d.context.G2DContext;
+ import com.flashcore.g2d.core.Genome2D;
+ import com.flashcore.g2d.display.G2DMovieClip;
+ import com.flashcore.g2d.display.G2DSprite;
+ import com.flashcore.g2d.display.G2DTransform;
+ import com.flashcore.g2d.g2d;
+ import com.flashcore.g2d.textures.G2DTexture;
+
+ import flash.display.Sprite;
+ import flash.events.Event;
+ import flash.events.KeyboardEvent;
+
+ public class BlittingExample extends Example
+ {
+ [Embed(source = "./assets/mines.xml", mimeType = "application/octet-stream")]
+ private static const MinesXML:Class;
+ [Embed(source = "./assets/crate.jpg")]
+ private static const CrateGFX:Class;
+
+ private var __cTexture:G2DTexture;
+
+ private const COUNT:int = 500;
+
+ private var __iClipCount:int;
+ private var __bMove:Boolean = true;
+
+ public function BlittingExample(p_wrapper:Genome2DExamples):void {
+ super(p_wrapper);
+ }
+
+ private function updateInfo():void {
+ _cWrapper.info = "<font color='#00FFFF'><b>BlittingExample</b> [ "+__iClipCount+" images ]\n"+
+ "<font color='#FFFFFF'>This is a blitting example which uses blit() function instead of G2DSprites/G2DMovieClip instances, simply blits textures into random positions.\n"+
+ "<font color='#FFFF00'>Press ARROW UP to increase the number of blitted images and ARROW DOWN to decrease them.";
+ }
+
+ override public function init():void {
+ super.init();
+ __cTexture = G2DTexture.createFromBitmapData((new CrateGFX()).bitmapData);
+
+ __iClipCount = COUNT;
+
+ _cGenome.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
+ _cGenome.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
+ // We need to disable auto render of G2D content when using blitting
+ _cGenome.autoRender = false;
+
+ updateInfo();
+ }
+
+ override public function dispose():void {
+ super.dispose();
+
+ __cTexture.dispose();
+
+ _cGenome.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
+ _cGenome.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
+ }
+
+ private function addClips(p_count:int):void {
+ __iClipCount += p_count;
+
+ updateInfo();
+ }
+
+ private function removeClips(p_count:int):void {
+ __iClipCount -= p_count;
+ if (__iClipCount<0) __iClipCount = 0;
+
+ updateInfo();
+ }
+
+ private function onEnterFrame(event:Event):void {
+ // Start genome rendering
+ _cGenome.start();
+ // Its faster to reference the genome context when blitting since this skips one function call
+ var context:G2DContext = _cGenome.getContext();
+
+ for (var i:int = 0; i<__iClipCount; ++i) {
+ // Blit the textures to the screen, the funny thing is that if we removed the Math.random() calls it would be up to 2 times faster
+ // yes random is that slow ;)
+ context.blit(Math.random()*800, Math.random()*600, __cTexture);
+ //context.blit(100, 100, __cTexture);
+ }
+ // End genome rendering
+ _cGenome.end();
+ }
+
+ private function onKeyDown(event:KeyboardEvent):void {
+ switch (event.keyCode) {
+ case 38:
+ addClips(500);
+ break;
+ case 40:
+ removeClips(500);
+ break;
+ case 80:
+ __bMove = !__bMove;
+ break;
+ }
+ }
+ }
+}
27 examples/examples/CollisionExample.as
View
@@ -1,10 +1,9 @@
package examples
{
- import com.flashcore.g2d.materials.G2DMaterialLibrary;
- import com.flashcore.g2d.sprites.G2DContainer;
- import com.flashcore.g2d.sprites.G2DMovieClip;
- import com.flashcore.g2d.sprites.G2DSprite;
- import com.flashcore.g2d.sprites.G2DTransform;
+ import com.flashcore.g2d.display.G2DContainer;
+ import com.flashcore.g2d.display.G2DMovieClip;
+ import com.flashcore.g2d.display.G2DSprite;
+ import com.flashcore.g2d.display.G2DTransform;
import com.flashcore.g2d.textures.G2DTexture;
public class CollisionExample extends Example
@@ -25,27 +24,22 @@ package examples
private var __cMineTexture:G2DTexture;
private var __cNinjaTexture:G2DTexture;
- static private const NINJA_COUNT:int = 20;
+ static private const NINJA_COUNT:int = 100;
public function CollisionExample(p_wrapper:Genome2DExamples):void {
super(p_wrapper);
}
override public function init():void {
- _cWrapper.info = "<font color='#00FFFF'>CollisionExample\n"+
+ super.init();
+ _cWrapper.info = "<font color='#00FFFF'><b>CollisionExample</b>\n"+
"<font color='#FFFFFF'>Collision example where numbers collide with ninjas ;)\n"+
"<font color='#FFFF00'>You can use mouse to DRAG ninjas around.";
__cMineTexture = G2DTexture.createFromBitmapAtlas((new MinesGFX()).bitmapData, XML(new MinesXML()));
- G2DMaterialLibrary.createMaterial("mines", __cMineTexture);
- G2DMaterialLibrary.createMaterial("mine2", __cMineTexture.getSubTextureById("mine2"));
-
__cNinjaTexture = G2DTexture.createFromBitmapAtlas((new NinjaGFX()).bitmapData, XML(new NinjaXML()));
- G2DMaterialLibrary.createMaterial("ninja", __cNinjaTexture);
- G2DMaterialLibrary.createMaterial("ninja_stood", __cNinjaTexture.getSubTextureById("stood"));
-
__cRotatingContainer = new G2DContainer();
__cRotatingContainer.x = 400;
__cRotatingContainer.y = 300;
@@ -82,9 +76,8 @@ package examples
}
private function createMine(p_x:Number, p_y:Number):G2DMovieClip {
- var clip:G2DMovieClip = new G2DMovieClip();
+ var clip:G2DMovieClip = new G2DMovieClip(__cMineTexture);
clip.setFrameRate(Math.random()*10+3);
- clip.setMaterialById("mines");
clip.setFrames(new <String>["mine2", "mine3", "mine4", "mine5", "mine6", "mine7", "mine8", "mine9"]);
clip.gotoFrame(Math.random()*8);
clip.x = p_x;
@@ -93,9 +86,8 @@ package examples
}
private function createNinja(p_x:Number, p_y:Number):G2DMovieClip {
- var clip:G2DMovieClip = new G2DMovieClip();
+ var clip:G2DMovieClip = new G2DMovieClip(__cNinjaTexture);
clip.setFrameRate(Math.random()*10+3);
- clip.setMaterialById("ninja");
clip.setFrames(new <String>["nw1", "nw2", "nw3", "nw2", "nw1", "stood", "nw4", "nw5", "nw6", "nw5", "nw4"]);
clip.gotoFrame(Math.random()*8);
clip.x = p_x;
@@ -109,6 +101,7 @@ package examples
}
private function onClipMouseUp(p_target:G2DTransform, p_dispatcher:G2DTransform, p_x:Number, p_y:Number):void {
+ trace("onMouseUp");
p_dispatcher.stopDrag();
}
4 examples/examples/Example.as
View
@@ -1,7 +1,6 @@
package examples
{
import com.flashcore.g2d.core.Genome2D;
- import com.flashcore.g2d.materials.G2DMaterialLibrary;
public class Example
{
@@ -14,12 +13,11 @@ package examples
}
public function init():void {
-
+ _cGenome.autoRender = true;
}
public function dispose():void {
_cGenome.root.dispose();
- G2DMaterialLibrary.clear();
}
}
}
16 examples/examples/HierarchyExample.as
View
@@ -1,8 +1,7 @@
package examples
{
- import com.flashcore.g2d.materials.G2DMaterialLibrary;
- import com.flashcore.g2d.sprites.G2DContainer;
- import com.flashcore.g2d.sprites.G2DMovieClip;
+ import com.flashcore.g2d.display.G2DContainer;
+ import com.flashcore.g2d.display.G2DMovieClip;
import com.flashcore.g2d.textures.G2DTexture;
public class HierarchyExample extends Example
@@ -16,21 +15,19 @@ package examples
private var __cTexture:G2DTexture;
- static public const MAX_MILL_SPEED:Number = .05;
+ static public const MAX_MILL_SPEED:Number = .005;
public function HierarchyExample(p_wrapper:Genome2DExamples):void {
super(p_wrapper);
}
override public function init():void {
- _cWrapper.info = "<font color='#00FFFF'>HierarchyExample\n"+
+ super.init();
+ _cWrapper.info = "<font color='#00FFFF'><b>HierarchyExample</b>\n"+
"<font color='#FFFFFF'>Simple hierarchy setup to showcase an example hierachical scene setup in G2D";
__cTexture = G2DTexture.createFromBitmapAtlas((new MinesGFX()).bitmapData, XML(new MinesXML()));
- G2DMaterialLibrary.createMaterial("mines", __cTexture);
- G2DMaterialLibrary.createMaterial("mine", __cTexture.getSubTextureById("mine2"));
-
__aMills = new Vector.<G2DContainer>();
var mill:G2DContainer
@@ -56,9 +53,8 @@ package examples
container.x = p_x;
container.y = p_y;
for (var i:int = 0; i < p_size; ++i) {
- var clip:G2DMovieClip = new G2DMovieClip();
+ var clip:G2DMovieClip = new G2DMovieClip(__cTexture);
clip.setFrameRate(Math.random()*10+3);
- clip.setMaterialById("mines");
clip.setFrames(new <String>["mine2", "mine3", "mine4", "mine5", "mine6", "mine7", "mine8", "mine9"]);
clip.gotoFrame(Math.random()*8);
clip.x = -(p_size-1)*24/2 + i*24;
20 examples/examples/MouseExample.as
View
@@ -1,9 +1,8 @@
package examples
{
- import com.flashcore.g2d.materials.G2DMaterialLibrary;
- import com.flashcore.g2d.sprites.G2DMovieClip;
- import com.flashcore.g2d.sprites.G2DSprite;
- import com.flashcore.g2d.sprites.G2DTransform;
+ import com.flashcore.g2d.display.G2DMovieClip;
+ import com.flashcore.g2d.display.G2DSprite;
+ import com.flashcore.g2d.display.G2DTransform;
import com.flashcore.g2d.textures.G2DTexture;
import flash.display.Sprite;
@@ -24,14 +23,13 @@ package examples
}
override public function init():void {
- _cWrapper.info = "<font color='#00FFFF'>MouseExample\n"+
+ super.init();
+ _cWrapper.info = "<font color='#00FFFF'><b>MouseExample</b>\n"+
"<font color='#FFFFFF'>In this example it showcases the mouse interactivity and its modes, green ninjas have pixel perfect mouse precision enabled where red ninjas don't and capture mouse events only using their geometry.\n"+
"<font color='#FFFF00'>You can switch mouse mode of a particular ninja by CLICKing on him.";
__cTexture = G2DTexture.createFromBitmapAtlas((new NinjaGFX()).bitmapData, XML(new NinjaXML()));
- G2DMaterialLibrary.createMaterial("ninja", __cTexture);
-
for (var i:int = 0; i<COUNT; ++i) {
var clip:G2DMovieClip = createClip(Math.random()*800, Math.random()*600);
_cGenome.root.addChild(clip);
@@ -45,9 +43,8 @@ package examples
}
private function createClip(p_x:Number, p_y:Number):G2DMovieClip {
- var clip:G2DMovieClip = new G2DMovieClip();
+ var clip:G2DMovieClip = new G2DMovieClip(__cTexture);
clip.setFrameRate(15);
- clip.setMaterialById("ninja");
clip.setFrames(new <String>["nw1", "nw2", "nw3", "nw2", "nw1", "stood", "nw4", "nw5", "nw6", "nw5", "nw4"]);
clip.gotoFrame(Math.random()*8);
//clip.stop();
@@ -64,6 +61,7 @@ package examples
clip.onMouseOver.add(onClipMouseOver);
clip.onMouseOut.add(onClipMouseOut);
+ clip.onMouseUp.add(onMouseUp);
clip.onMouseClick.add(onClipMouseClick);
return clip;
}
@@ -89,5 +87,9 @@ package examples
p_dispatcher.blue = 0;
}
}
+
+ private function onMouseUp(p_target:G2DTransform, p_dispatcher:G2DTransform, p_x:Number, p_y:Number):void {
+ trace("onMouseUp");
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.