├── README ├── src ├── sl2d │ ├── slITicker.as │ ├── slLayer.as │ ├── slButton.as │ ├── slRemoteTexture.as │ ├── slSimpleTextureLoader.as │ ├── slTicker.as │ ├── slAnimController.as │ ├── slButtonGroup.as │ ├── slDisplayNode.as │ ├── slTextField.as │ ├── slTextureFactory.as │ ├── slWilParser.as │ ├── slWorld.as │ ├── slBoundsGroup.as │ ├── slCamera.as │ ├── slTextTexture.as │ ├── slTexture.as │ ├── slAGALHelper.as │ └── slBounds.as └── Text2Texture.as └── swfobject ├── template.html └── swfobject.js /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/sl2d/slITicker.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | public interface slITicker 4 | { 5 | function update():void; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/sl2d/slLayer.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | public class slLayer extends slDisplayNode { 3 | public function slDisplayContainer() : void { 4 | } 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/sl2d/slButton.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | public class slButton extends slBoundsGroup 4 | { 5 | public function slButton() : void { 6 | } 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/sl2d/slRemoteTexture.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.display.BitmapData; 3 | 4 | public class slRemoteTexture extends slTexture { 5 | 6 | protected var url : String; 7 | protected var parse : uint = 0; 8 | 9 | protected var parser : slWilParser; 10 | protected var factory : slTextureFactory; 11 | 12 | public function slRemoteTexture(factory : slTextureFactory, url : String) : void { 13 | this.url = url; 14 | this.factory = factory; 15 | 16 | parser = new slWilParser(this); 17 | parser.parse(url); 18 | 19 | status = PREPARING; 20 | } 21 | 22 | public function setTextureFromBitmap(bd : BitmapData, w : uint, h : uint) : void { 23 | texture = factory.createTexture(bd, w, h); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /swfobject/template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @project_name@ 5 | 6 | 7 | 10 | 11 | 12 |
13 |

Alternative content

14 |

Get Adobe Flash player

15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /src/sl2d/slSimpleTextureLoader.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.display.Loader; 3 | import flash.display.LoaderInfo; 4 | 5 | public class slSimpleTextureLoader { 6 | protected var world : slWorld; 7 | 8 | public function slSimpleTextureLoader(world : slWorld) : void { 9 | this.world = world; 10 | } 11 | 12 | protected var textureName : String; 13 | 14 | public function loadTexture(textureName : String, textureURL : String, coordURL : String) : void { 15 | var loader0 : Loader = new Loader(); 16 | var loader1 : URLLoader = new URLLoader(); 17 | 18 | this.textureName = textureName; 19 | 20 | loader0.contentLoaderInfo.addEventListener(Event.COMPLETE, onTextureLoaded); 21 | loader1.addEventListener(Event.COMPLETE, onCoordLoaded); 22 | 23 | loader0.load(new URLRequest(textureURL)); 24 | loader1.load(new URLRequest(textureURL)); 25 | } 26 | 27 | public function onTextureLoaded(evt : Event) : void { 28 | var bm : Bitmap = Bitmap(evt.target.content); 29 | var t : slTexture = world.textureFactory.createTextureFromBitmap(bm, textureName, true); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/sl2d/slTicker.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.display.Stage; 3 | import flash.events.Event; 4 | 5 | public class slTicker { 6 | private var stage : Stage; 7 | 8 | public function slTicker(stage : Stage) : void { 9 | this.stage = stage; 10 | } 11 | 12 | public function start() : void { 13 | this.stage.addEventListener(Event.ENTER_FRAME, onUpdate); 14 | } 15 | 16 | public function stop() : void { 17 | this.stage.removeEventListener(Event.ENTER_FRAME, onUpdate); 18 | } 19 | 20 | public function onUpdate(evt : Event) : void { 21 | var len : int = listeners.length; 22 | 23 | for(var i : int = 0; i < len; i++) { 24 | listeners[i].update(); 25 | } 26 | } 27 | 28 | protected var listeners : Vector. = new Vector.(); 29 | 30 | public function addListener(l : slITicker) : void { 31 | var i : int = listeners.indexOf(l); 32 | if(i < 0)listeners.push(l); 33 | } 34 | 35 | public function removeListener(l : slITicker) : void { 36 | var i : int = listeners.indexOf(l); 37 | if(i > 0) listeners.splice(i, 1); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/sl2d/slAnimController.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | public class slAnimController { 3 | public var frameStart : uint = 0; 4 | public var frameEnd : uint = 0; 5 | public var frameRate : uint = 4; 6 | 7 | public var currentFrame : uint = 0; 8 | public var loop : Boolean; 9 | 10 | public function slAnimController(frameStart : int, frameEnd : int, frameRate : int = 4, loop : Boolean = true) : void { 11 | this.frameStart = frameStart; 12 | this.frameEnd = frameEnd; 13 | this.frameRate = frameRate; 14 | this.loop = loop; 15 | 16 | this.currentFrame = frameStart; 17 | this.currentRes = currentFrame; 18 | } 19 | 20 | public function nextFrame() : int { 21 | currentFrame ++; 22 | 23 | if(currentFrame > frameEnd) { 24 | currentFrame = loop ? frameStart : frameEnd; 25 | } 26 | 27 | currentRes = currentFrame; 28 | return currentRes; 29 | } 30 | 31 | public function prevFrame() : int { 32 | currentFrame --; 33 | if(currentFrame < frameStart) { 34 | currentFrame = loop ? frameEnd : frameStart; 35 | } 36 | 37 | currentRes = currentFrame; 38 | return currentRes; 39 | } 40 | 41 | private var currentCount : uint = 0; 42 | private var currentRes : int; 43 | 44 | public function update() : int { 45 | currentCount++; 46 | 47 | if(currentCount % frameRate == 0){ 48 | return nextFrame(); 49 | } 50 | 51 | return currentRes; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/sl2d/slButtonGroup.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | public class slButtonGroup extends slDisplayNode implements slITicker { 4 | public function slButtonGroup() : void { 5 | } 6 | 7 | protected var buttonList : Vector. = new Vector.(); 8 | protected var _numButtons : uint = 0; 9 | 10 | public function addButton(b : slButton) : slButton { 11 | if(buttonList.indexOf(b) >= 0) return b; 12 | return addButtonAt(b, _numButtons); 13 | } 14 | 15 | public function addButtonAt(b : slButton, index : int) : slButton { 16 | if(b && index <= _numButtons){ 17 | buttonList.splice(index, 0, b); 18 | //b.parent = this; 19 | }else{ 20 | throw new Error("Error: slButton Object can't be added."); 21 | } 22 | 23 | _numButtons = buttonList.length; 24 | 25 | return b; 26 | } 27 | 28 | public function removeButton(b : slButton, recycle : Boolean = true) : slButton { 29 | return removeButtonAt(getButtonIndex(b)); 30 | } 31 | 32 | public function removeButtonAt(index : int) : slButton { 33 | var b : slButton; 34 | if(index >= 0 && index < _numButtons){ 35 | b = buttonList.splice(index, 1)[0] as slButton; 36 | //b.parent = null; 37 | }else{ 38 | throw new Error("Error: slButton Object can't be removed."); 39 | } 40 | 41 | _numButtons = buttonList.length; 42 | 43 | return b; 44 | } 45 | 46 | public function getButtonIndex(b : slButton) : int { 47 | return buttonList.indexOf(b); 48 | } 49 | 50 | public function setButtonIndex(b : slButton, index : int) : void { 51 | var i : int = getButtonIndex(b); 52 | if(i == index) return; 53 | 54 | if( i > -1){ 55 | buttonList.splice(i, 1); 56 | buttonList.splice(index, 0, b); 57 | } 58 | } 59 | 60 | public function update() : void { 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/sl2d/slDisplayNode.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.geom.Matrix3D; 3 | import flash.geom.Rectangle; 4 | 5 | public class slDisplayNode { 6 | internal static var renderIndex : uint = 0; 7 | 8 | protected var children : Vector. = new Vector.(); 9 | 10 | protected var children_not_validated : Vector. = new Vector.(); 11 | 12 | protected var parent : slDisplayNode; 13 | 14 | protected var _numChildren : int = 0; 15 | 16 | public function get numChildren() : int { 17 | return _numChildren; 18 | } 19 | 20 | public function slDisplayNode() : void { 21 | } 22 | 23 | //NODE FUNCTIONS 24 | public function addChild(child : slDisplayNode) : slDisplayNode { 25 | return addChildAt(child, _numChildren); 26 | } 27 | 28 | public function addChildAt(child : slDisplayNode, index : int) : slDisplayNode { 29 | if(child && index <= _numChildren){ 30 | children.splice(index, 0, child); 31 | child.parent = this; 32 | }else{ 33 | throw new Error("Error: slDisplayNode Object can't be added."); 34 | } 35 | 36 | _numChildren = children.length; 37 | 38 | return child; 39 | } 40 | 41 | public function removeChild(child : slDisplayNode) : slDisplayNode { 42 | return removeChildAt(getChildIndex(child)); 43 | } 44 | 45 | public function removeChildAt(index : int) : slDisplayNode { 46 | var child : slDisplayNode; 47 | if(index >= 0 && index < _numChildren){ 48 | child = children.splice(index, 1)[0] as slDisplayNode; 49 | child.parent = this; 50 | }else{ 51 | throw new Error("Error: slDisplayNode Object can't be removed."); 52 | } 53 | 54 | _numChildren = children.length; 55 | 56 | return child; 57 | } 58 | 59 | public function getChildIndex(child : slDisplayNode) : int { 60 | return children.indexOf(child); 61 | } 62 | 63 | public function setChildIndex(child : slDisplayNode, index : int) : void { 64 | var i : int = getChildIndex(child); 65 | if(i == index) return; 66 | 67 | if( i > -1){ 68 | children.splice(i, 1); 69 | children.splice(index, 0, child); 70 | } 71 | } 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/sl2d/slTextField.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | import flash.geom.Rectangle; 4 | 5 | public class slTextField extends slBoundsGroup 6 | { 7 | public static var textures : Vector. = Vector.([new slTextTexture(), new slTextTexture()]); 8 | 9 | public static function createTextField(fontSize : uint = 12, lineWidth : uint = 100) : slTextField { 10 | return new slTextField(fontSize, lineWidth); 11 | } 12 | 13 | public function slTextField(fontSize : uint, lineWidth : uint) : void { 14 | _fontSize = fontSize; 15 | _lineWidth = lineWidth; 16 | } 17 | 18 | protected var _text : String; 19 | 20 | public function set text(value : String) : void { 21 | _text = value; 22 | createLines(value); 23 | } 24 | 25 | public function get text() : String { 26 | return _text; 27 | } 28 | 29 | protected var _fontSize : uint = 12; 30 | protected var charScale : Number = 12/18; 31 | 32 | public function set fontSize(value:uint) : void { 33 | _fontSize = value; 34 | charScale = _fontSize/slTextTexture.DEFAULT_FONT_SIZE; 35 | } 36 | 37 | public function get fontSize() : uint { 38 | return _fontSize; 39 | } 40 | 41 | 42 | protected var _lineWidth : uint = 100; 43 | public function set lineWidth(value:uint) : void { 44 | _lineWidth = value; 45 | } 46 | 47 | public function get lineWidth() : uint { 48 | return _lineWidth; 49 | } 50 | 51 | 52 | protected var last_w : uint = 0; 53 | protected var last_h : uint = 0; 54 | 55 | protected function createLines(str : String) : void { 56 | var len : uint = str.length; 57 | var uvIndex : Vector. = new Vector.(len, true); 58 | var textTexture : slTextTexture = textures[0]; 59 | 60 | for(var i : int = 0; i < len; i++){ 61 | var char : String = str.charAt(i); 62 | uvIndex[i] = textTexture.findCharacater(char); 63 | } 64 | 65 | var texture : slTexture = textTexture.getTexture(); 66 | 67 | for(i = 0; i < len; i++) { 68 | var bounds : slBounds = texture.createBounds(); 69 | bounds.setUV(uvIndex[i]); 70 | 71 | if((last_w + bounds.width) > _lineWidth){ 72 | last_h += Math.ceil(bounds.height * charScale); 73 | last_w = 0; 74 | } 75 | 76 | bounds.x = last_w; 77 | bounds.y = last_h; 78 | bounds.color = 0xFF9900; 79 | 80 | bounds.width = Math.ceil(bounds.width * charScale); 81 | bounds.height = Math.ceil(bounds.height * charScale); 82 | addBounds(bounds); 83 | 84 | last_w += bounds.width; 85 | } 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/sl2d/slTextureFactory.as: -------------------------------------------------------------------------------- 1 | /* 2 | * SL2D - The next generation flash 2d game engine. 3 | * ..................... 4 | * 5 | * Author: Aspirin - Sleep2Death 6 | * Copyright (c) SNDA 2011 7 | * 8 | * 9 | */ 10 | package sl2d { 11 | import flash.display.Bitmap; 12 | import flash.display.BitmapData; 13 | import flash.display3D.*; 14 | import flash.display3D.textures.*; 15 | 16 | public class slTextureFactory { 17 | private var context : Context3D; 18 | 19 | public function slTextureFactory(context : Context3D){ 20 | this.context = context; 21 | } 22 | 23 | private var textureNames : Vector. = new Vector.(); 24 | private var textures : Vector. = new Vector.(); 25 | 26 | public function getIndexByName(texture_name : String) : int { 27 | return textureNames.indexOf(texture_name); 28 | } 29 | 30 | public function getTextureByName(texture_name : String) : slTexture { 31 | return textures[getIndexByName(texture_name)]; 32 | } 33 | 34 | public function createTextureFromBitmap(bm : Bitmap, texture_name : String, dispose : Boolean = true, forceNew : Boolean = false) : slTexture { 35 | var index : int = getIndexByName(texture_name); 36 | 37 | if(index >= 0 && !forceNew){ 38 | return textures[index]; 39 | }else if(index < 0 || forceNew){ 40 | var w : int = Math.pow(2, Math.ceil(Math.LOG2E * Math.log(bm.width))); 41 | var h : int = Math.pow(2, Math.ceil(Math.LOG2E * Math.log(bm.height))); 42 | 43 | var texture : Texture = createTexture(bm.bitmapData, w, h); 44 | 45 | var res : slTexture = new slTexture(texture, w, h); 46 | res.status = slTexture.INITIALIZED; 47 | 48 | textureNames.push(texture_name); 49 | textures.push(res); 50 | 51 | if(dispose) bm.bitmapData.dispose(); 52 | 53 | return res; 54 | } 55 | 56 | return null; 57 | } 58 | 59 | public function createTextureFromWil(wil_path : String, texture_name : String) : slRemoteTexture { 60 | var index : int = getIndexByName(texture_name); 61 | 62 | if(index < 0){ 63 | var res : slRemoteTexture = new slRemoteTexture(this, wil_path); 64 | textureNames.push(texture_name); 65 | textures.push(res); 66 | 67 | return res; 68 | } 69 | 70 | return textures[index] as slRemoteTexture; 71 | } 72 | 73 | public function createTexture(bd : BitmapData, w : uint , h : uint) : Texture { 74 | var texture : Texture = context.createTexture(w, h, Context3DTextureFormat.BGRA, true); 75 | texture.uploadFromBitmapData(bd); 76 | return texture; 77 | } 78 | 79 | public function getTextures() : Vector. { 80 | return textures; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/sl2d/slWilParser.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | import flash.display.*; 4 | import flash.events.*; 5 | import flash.geom.*; 6 | import flash.net.*; 7 | import flash.utils.*; 8 | 9 | public class slWilParser 10 | { 11 | protected var handler : slRemoteTexture; 12 | 13 | public function slWilParser(handler : slRemoteTexture) 14 | { 15 | this.handler = handler; 16 | } 17 | 18 | // Public Methods 19 | public function parse( wilPath : String ) : void{ 20 | var loader:URLLoader = new URLLoader; 21 | loader.dataFormat = URLLoaderDataFormat.BINARY; 22 | loader.addEventListener(Event.COMPLETE, fileLoadedHandler); 23 | loader.load(new URLRequest(wilPath)); 24 | } 25 | 26 | // Private Methods 27 | private function readIndex(uv:ByteArray):uint{ 28 | var id1 : uint = uv.readByte(); 29 | var id2 : uint = uv.readByte(); 30 | var id3 : uint = uv.readByte(); 31 | var id4 : uint = uv.readByte(); 32 | 33 | var res : String = (b2s(id1) + b2s(id2) + b2s(id3) + b2s(id4)); 34 | return Number(res); 35 | } 36 | 37 | private function b2s(id : uint) : String { 38 | var str : String = id.toString(); 39 | if(id < 10) str = "0" + str; 40 | return str; 41 | } 42 | 43 | // Event Handlers 44 | private function fileLoadedHandler(evt:Event):void{ 45 | var packet:ByteArray = evt.target.data as ByteArray; 46 | packet.endian = "littleEndian"; 47 | 48 | var width : uint = packet.readUnsignedInt(); 49 | var height : uint = packet.readUnsignedInt(); 50 | var frame : uint = packet.readUnsignedInt(); 51 | var offset_len : uint = frame; 52 | 53 | var fixed_w : int = Math.pow(2, Math.ceil(Math.LOG2E * Math.log(width))); 54 | var fixed_h : int = Math.pow(2, Math.ceil(Math.LOG2E * Math.log(height))); 55 | 56 | //trace("width: " + width); 57 | //trace("height: " + height); 58 | //trace("fixed: " + fixed_w + "x" + fixed_h); 59 | //trace("frame: " + frame); 60 | 61 | for(var i : uint = 0; i < offset_len; i++){ 62 | //var index:int = readIndex(uv); 63 | 64 | var l : Number = packet.readUnsignedInt()/fixed_w; 65 | var t : Number = packet.readUnsignedInt()/fixed_h; 66 | var w : Number = packet.readUnsignedInt(); 67 | var h : Number = packet.readUnsignedInt(); 68 | var x : int = packet.readUnsignedInt(); 69 | var y : int = packet.readUnsignedInt(); 70 | 71 | //trace(l + " " + t + " " + w + " " + h + " " + x + " " + y + " "); 72 | 73 | handler.setTextureCoord(i, Vector.([l, t, l + (w/fixed_w), t + (h/fixed_h), x, y, w, h])); 74 | } 75 | 76 | 77 | var image : ByteArray = new ByteArray(); 78 | packet.readBytes(image); 79 | 80 | var bd : BitmapData = new BitmapData(width, height, true, 0); 81 | bd.setPixels(new Rectangle(0,0,width,height), image); 82 | 83 | handler.setTextureFromBitmap(bd, fixed_w, fixed_h); 84 | handler.frameCount = frame; 85 | handler.status = slTexture.INITIALIZED; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/sl2d/slWorld.as: -------------------------------------------------------------------------------- 1 | /* 2 | * SL2D - The next generation flash 2d game engine. 3 | * ..................... 4 | * 5 | * Author: Aspirin - Sleep2Death 6 | * Copyright (c) SNDA 2011 7 | * 8 | * 9 | */ 10 | package sl2d { 11 | import flash.display.*; 12 | import flash.display3D.*; 13 | import flash.display3D.textures.*; 14 | import flash.geom.*; 15 | import flash.events.Event; 16 | 17 | public class slWorld implements slITicker{ 18 | protected var stage : Stage; 19 | protected var viewPort : Rectangle; 20 | 21 | protected var stage3D : Stage3D; 22 | protected var context : Context3D; 23 | 24 | public var readyCallBack : Function; 25 | 26 | public function slWorld(stage : Stage, index : uint = 0, viewPort : Rectangle = null, readyCallBack : Function = null) : void { 27 | if(!viewPort) { 28 | this.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); 29 | }else{ 30 | this.viewPort = viewPort; 31 | } 32 | 33 | 34 | this.readyCallBack = readyCallBack; 35 | this.stage = stage; 36 | 37 | stage3D = stage.stage3Ds[ index ]; 38 | trace(this.viewPort); 39 | 40 | if(stage3D.context3D == null){ 41 | stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContext3DCreate); 42 | stage3D.requestContext3D("auto"); 43 | }else{ 44 | initialize(); 45 | } 46 | } 47 | 48 | 49 | protected function onContext3DCreate(evt : Event) : void { 50 | var stage3D : Stage3D = evt.target as Stage3D; 51 | context = stage3D.context3D; 52 | 53 | stage3D.x = this.viewPort.x; 54 | stage3D.y = this.viewPort.y; 55 | 56 | initialize(); 57 | 58 | if(readyCallBack != null) readyCallBack.call(this); 59 | } 60 | 61 | public static var antiAlias : uint = 4; 62 | 63 | public var helper : slAGALHelper; 64 | public var textureFactory : slTextureFactory; 65 | public var camera : slCamera; 66 | 67 | protected function initialize() : void { 68 | context.enableErrorChecking = true; 69 | context.configureBackBuffer( viewPort.width, viewPort.height, antiAlias, true); 70 | 71 | context.setCulling(Context3DTriangleFace.NONE); 72 | context.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ZERO); 73 | context.setDepthTest(true, Context3DCompareMode.GREATER_EQUAL); 74 | 75 | helper = new slAGALHelper(context); 76 | 77 | textureFactory = new slTextureFactory(context); 78 | 79 | camera = new slCamera(viewPort.width, viewPort.height); 80 | helper.setCamera(camera); 81 | } 82 | 83 | protected var bRed : Number = 0; 84 | protected var bGreen : Number = 0; 85 | protected var bBlue : Number = 0; 86 | 87 | public function set backgroundColor(value : uint) : void { 88 | bRed = ((value & 0xFF0000) >> 16)/256; 89 | bGreen = ((value & 0x00FF00) >> 8)/256; 90 | bBlue = ((value & 0x0000FF))/256; 91 | } 92 | 93 | public function update() : void { 94 | context.clear(bRed, bGreen, bBlue,0,0); 95 | helper.draw(textureFactory.getTextures()); 96 | context.present(); 97 | } 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /src/sl2d/slBoundsGroup.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | public class slBoundsGroup extends slDisplayNode implements slITicker { 3 | public function slBoundsGroup() : void { 4 | } 5 | 6 | protected var boundsList : Vector. = new Vector.(); 7 | protected var _numBounds : uint = 0; 8 | 9 | public function containsBound(b : slBounds) : Boolean { 10 | return boundsList.indexOf(b) >= 0; 11 | } 12 | 13 | public function addBounds(b : slBounds) : slBounds { 14 | if(boundsList.indexOf(b) >= 0) return b; 15 | return addBoundsAt(b, _numBounds); 16 | } 17 | 18 | public function addBoundsAt(b : slBounds, index : int) : slBounds { 19 | if(b && index <= _numBounds){ 20 | boundsList.splice(index, 0, b); 21 | b.parent = this; 22 | }else{ 23 | throw new Error("Error: slBounds Object can't be added."); 24 | } 25 | 26 | _numBounds = boundsList.length; 27 | 28 | return b; 29 | } 30 | 31 | public function removeBounds(b : slBounds, recycle : Boolean = true) : slBounds { 32 | return removeBoundsAt(getBoundsIndex(b)); 33 | } 34 | 35 | public function removeBoundsAt(index : int, recycle : Boolean = true) : slBounds { 36 | var b : slBounds; 37 | if(index >= 0 && index < _numBounds){ 38 | b = boundsList.splice(index, 1)[0] as slBounds; 39 | b.parent = null; 40 | if(recycle) b.texture.recycle(b); 41 | }else{ 42 | throw new Error("Error: slBounds Object can't be removed."); 43 | } 44 | 45 | _numBounds = boundsList.length; 46 | 47 | return b; 48 | } 49 | 50 | public function getBoundsIndex(b : slBounds) : int { 51 | return boundsList.indexOf(b); 52 | } 53 | 54 | public function setBoundsIndex(b : slBounds, index : int) : void { 55 | var i : int = getBoundsIndex(b); 56 | if(i == index) return; 57 | 58 | if( i > -1){ 59 | boundsList.splice(i, 1); 60 | boundsList.splice(index, 0, b); 61 | } 62 | } 63 | 64 | private var _x : Number = 0; 65 | 66 | public function set x(value : Number) : void { 67 | _x = value; 68 | } 69 | 70 | public function get x() : Number { 71 | return _x; 72 | } 73 | 74 | private var _y : Number = 0; 75 | 76 | public function set y(value : Number) : void { 77 | _y = value; 78 | } 79 | 80 | public function get y() : Number { 81 | return _y; 82 | } 83 | 84 | public function setPosition(x : Number, y : Number) : void { 85 | _x = x; 86 | _y = y; 87 | } 88 | 89 | protected var global_x : Number = 0; 90 | protected var global_y : Number = 0; 91 | 92 | public function setGlobalOffset(x : Number, y : Number) : void { 93 | global_x = x + _x; 94 | global_y = y + _y; 95 | } 96 | 97 | 98 | protected var oX : Number = 0; 99 | protected var oY : Number = 0; 100 | 101 | protected var invalidate : Boolean = true; 102 | 103 | public function update() : void { 104 | var len : int = _numChildren; 105 | 106 | for(var i : int = 0; i < len; i++) { 107 | var child : slBoundsGroup = children[i] as slBoundsGroup; 108 | child.setGlobalOffset(global_x, global_y); 109 | child.update(); 110 | } 111 | 112 | len = _numBounds; 113 | for(i = 0; i < len; i++) { 114 | var bounds : slBounds= boundsList[i] as slBounds; 115 | bounds.setOffset(global_x, global_y); 116 | bounds.update(); 117 | } 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/sl2d/slCamera.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.geom.Matrix3D; 3 | import flash.geom.Vector3D; 4 | 5 | public class slCamera { 6 | 7 | protected var renderMatrix:Matrix3D = new Matrix3D(); 8 | protected var projectionMatrix:Matrix3D = new Matrix3D(); 9 | protected var viewMatrix:Matrix3D = new Matrix3D(); 10 | 11 | protected var _sceneWidth:Number; 12 | protected var _sceneHeight:Number; 13 | 14 | public var invalidated:Boolean = true; 15 | 16 | public function slCamera(w:Number, h:Number) { 17 | resizeCameraStage(w, h); 18 | } 19 | 20 | public function resizeCameraStage(w:Number, h:Number):void { 21 | _sceneWidth = w; 22 | _sceneHeight = h; 23 | invalidated = true; 24 | projectionMatrix = makeOrtographicMatrix(0, w, 0, h); 25 | } 26 | 27 | public function makeOrtographicMatrix(left:Number, right:Number, top:Number, bottom:Number, zNear:Number = -1, zFar:Number = 1):Matrix3D { 28 | 29 | return new Matrix3D(Vector.([ 30 | 2 / (right - left), 0, 0, 0, 31 | 0, 2 / (top - bottom), 0, 0, 32 | 0, 0, 1 / (zFar - zNear), 0, 33 | 0, 0, 0, 1 34 | ])); 35 | } 36 | 37 | public function getViewProjectionMatrix():Matrix3D { 38 | if(invalidated) { 39 | invalidated = false; 40 | 41 | viewMatrix.identity(); 42 | viewMatrix.appendScale(2/_sceneWidth * zoom,-2/_sceneHeight * zoom,1/3000); 43 | viewMatrix.appendTranslation(-1 - (x/_sceneWidth) * 2, 1 + (y/_sceneHeight) * 2,0); 44 | 45 | viewMatrix.appendRotation(_rotation, Vector3D.Z_AXIS); 46 | 47 | } 48 | 49 | return viewMatrix; 50 | } 51 | 52 | public function reset():void { 53 | x = y = rotation = 0; 54 | zoom = 1; 55 | } 56 | 57 | private var _x:Number = 0.0; 58 | 59 | public function get x():Number { 60 | return _x; 61 | } 62 | 63 | public function set x(value:Number):void { 64 | invalidated = true; 65 | _x = value; 66 | } 67 | 68 | private var _y:Number = 0.0; 69 | 70 | public function get y():Number { 71 | return _y; 72 | } 73 | 74 | public function set y(value:Number):void { 75 | invalidated = true; 76 | _y = value; 77 | } 78 | 79 | private var _zoom:Number = 1.0; 80 | 81 | public function get zoom():Number { 82 | return _zoom; 83 | } 84 | 85 | public function set zoom(value:Number):void { 86 | invalidated = true; 87 | _zoom = value; 88 | } 89 | 90 | private var _rotation:Number = 0.0; 91 | 92 | public function get rotation():Number { 93 | return _rotation; 94 | } 95 | 96 | public function set rotation(value:Number):void { 97 | invalidated = true; 98 | _rotation = value; 99 | } 100 | 101 | public function get sceneWidth():Number { 102 | return _sceneWidth; 103 | } 104 | 105 | public function get sceneHeight():Number { 106 | return _sceneHeight; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/sl2d/slTextTexture.as: -------------------------------------------------------------------------------- 1 | package sl2d 2 | { 3 | import flash.display.BitmapData; 4 | import flash.display.Bitmap; 5 | import flash.geom.Matrix; 6 | import flash.geom.Rectangle; 7 | import flash.utils.Dictionary; 8 | import flash.text.TextField; 9 | import flash.text.TextFormat; 10 | import flash.text.engine.*; 11 | 12 | public class slTextTexture 13 | { 14 | public static var world : slWorld; 15 | public static const DEFAULT_TEXTURE_WIDTH : uint = 512; 16 | public static const DEFAULT_TEXTURE_HEIGHT : uint = 512; 17 | 18 | public static const DEFAULT_TEXT_WIDTH : uint = 20; 19 | public static const DEFAULT_TEXT_HEIGHT : uint = 25; 20 | public static const DEFAULT_FONT_COLOR : uint = 0xFFFFFFFF; 21 | public static const DEFAULT_FONT_SIZE : uint = 20; 22 | 23 | public static const DEFAULT_LINE_HEIGHT : uint = 22; 24 | public static const DEFAULT_OFFSET_Y : uint = 20; 25 | 26 | public var dict : Dictionary = new Dictionary(); 27 | public var bd : BitmapData; 28 | public var bm : Bitmap; 29 | 30 | private var bdInvalidated : Boolean = false; 31 | private var texture : slTexture; 32 | 33 | public var char_w : uint = DEFAULT_TEXT_WIDTH; 34 | public var char_h : uint = DEFAULT_TEXT_HEIGHT; 35 | 36 | public var font_size : uint = DEFAULT_FONT_SIZE; 37 | 38 | public var w : uint = DEFAULT_TEXTURE_WIDTH; 39 | public var h : uint = DEFAULT_TEXTURE_HEIGHT; 40 | 41 | public var col : uint = 0; 42 | public var row : uint = 0; 43 | 44 | public var offset_y : int = -14; 45 | 46 | public var textClip : Rectangle; 47 | public var textMatrix : Matrix = new Matrix(); 48 | 49 | public var used : Vector. = new Vector.(); 50 | 51 | public function slTextTexture(w : uint = DEFAULT_TEXTURE_WIDTH, h : uint = DEFAULT_TEXTURE_HEIGHT) : void { 52 | this.w = w; 53 | this.h = h; 54 | 55 | init(); 56 | } 57 | 58 | protected var tb : TextBlock = new TextBlock(); 59 | protected var tl : TextLine; 60 | protected var te : TextElement; 61 | protected var ef : ElementFormat; 62 | 63 | protected var last_w : uint = 0; 64 | protected var last_h : uint = 0; 65 | protected var max_W : uint = DEFAULT_TEXTURE_WIDTH * DEFAULT_TEXTURE_HEIGHT; 66 | 67 | protected var mtx : Matrix = new Matrix(); 68 | 69 | public var isFull : Boolean = false; 70 | 71 | public function init() : void { 72 | bd = new BitmapData(w, h, true, 0x00000000); 73 | bm = new Bitmap(bd); 74 | 75 | var fontDescriptionNormal : FontDescription = new FontDescription("Arial", FontWeight.NORMAL , FontPosture.NORMAL); 76 | var ef:ElementFormat = new ElementFormat(fontDescriptionNormal); 77 | ef.fontSize = DEFAULT_FONT_SIZE; 78 | ef.color = DEFAULT_FONT_COLOR; 79 | 80 | te = new TextElement(null, ef); 81 | } 82 | 83 | public function hasCharacter(char : String) : int { 84 | if(dict[char]){ 85 | return dict[char] 86 | } 87 | return -1; 88 | } 89 | 90 | 91 | public function findCharacater(char : String, canBeAdded : Boolean = true) : int { 92 | if(char.length > 1) throw new Error("Can not find find more than ONE Character."); 93 | 94 | var index : int = hasCharacter(char); 95 | 96 | if(index > -1){ 97 | return index; 98 | }else if(canBeAdded){ 99 | bdInvalidated = true; 100 | te.text = char; 101 | tb.content = te; 102 | tl = tb.createTextLine(null, 100); 103 | var rect : Rectangle = tl.getAtomBounds(0); 104 | 105 | if(DEFAULT_TEXTURE_WIDTH < (last_w + rect.width)){ 106 | last_w = 0; 107 | last_h += DEFAULT_LINE_HEIGHT; 108 | if(last_h + DEFAULT_LINE_HEIGHT > DEFAULT_TEXTURE_HEIGHT){ 109 | isFull = true; 110 | return -1; 111 | } 112 | } 113 | 114 | mtx.tx = last_w; 115 | mtx.ty = DEFAULT_OFFSET_Y + last_h; 116 | 117 | var clip : Rectangle = new Rectangle(mtx.tx, last_h, rect.width, DEFAULT_LINE_HEIGHT); 118 | bd.draw(tl, mtx, null, null, clip, true); 119 | 120 | last_w += rect.width; 121 | bdInvalidated = true; 122 | 123 | clip.height = DEFAULT_LINE_HEIGHT; 124 | used.push(clip); 125 | dict[char] = used.length - 1; 126 | 127 | return dict[char]; 128 | } 129 | 130 | return -1; 131 | } 132 | 133 | public function getCharacterBounds(index : int) : Rectangle { 134 | return used[index]; 135 | } 136 | 137 | public function getTexture() : slTexture { 138 | if(!texture || bdInvalidated){ 139 | texture = world.textureFactory.createTextureFromBitmap(bm, "text_bm", false, true); 140 | 141 | for(var i : int = 0; i < used.length; i++){ 142 | var rect : Rectangle = used[i]; 143 | var l : Number = rect.x/w; 144 | var t : Number = rect.y/h; 145 | texture.setTextureCoord(i, Vector.([l, t, l + (rect.width/w), t + (rect.height/h), 0, 0, rect.width, rect.height])); 146 | } 147 | 148 | bdInvalidated = false; 149 | 150 | } 151 | 152 | return texture; 153 | } 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/sl2d/slTexture.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.display3D.textures.Texture; 3 | import flash.display3D.Context3DBlendFactor; 4 | import flash.display3D.Context3DCompareMode; 5 | 6 | import flash.display.Sprite; 7 | import flash.geom.Rectangle; 8 | 9 | public class slTexture { 10 | public static const FAILED : int = -1; 11 | public static const NOT_READY : int = 0; 12 | public static const PREPARING : int = 1; 13 | public static const READY : int = 2; 14 | public static const INITIALIZED : int = 3; 15 | 16 | public var status : uint = NOT_READY; 17 | public var root : Sprite; 18 | 19 | public var compareMode : String = Context3DCompareMode.GREATER_EQUAL; 20 | public var blendFactor_src : String = Context3DBlendFactor.ONE; 21 | public var blendFactor_dst : String = Context3DBlendFactor.ZERO; 22 | 23 | public var texture : Texture; 24 | 25 | public var width : uint = 0; 26 | public var height : uint = 0; 27 | 28 | public var frameCount : uint = 0; 29 | 30 | public function slTexture(texture : Texture = null, w : uint = 0, h : uint = 0, isReady : Boolean = true) : void { 31 | this.texture = texture; 32 | this.width = w; 33 | this.height = h; 34 | } 35 | 36 | public var vertexVector : Vector. = new Vector.(); 37 | public var uvVector : Vector. = new Vector.(); 38 | public var colorVector : Vector. = new Vector.(); 39 | 40 | protected var recycled : Vector. = new Vector.(); 41 | 42 | public function createBounds() : slBounds { 43 | if(recycled.length > 0) return recycled.shift(); 44 | 45 | var len : uint = vertexVector.push( 46 | 0, 0, 0, 47 | 1, 0, 0, 48 | 0, 1, 0, 49 | 1, 1, 0 50 | ); 51 | 52 | colorVector.push( 53 | 1, 1, 1, 54 | 1, 1, 1, 55 | 1, 1, 1, 56 | 1, 1, 1 57 | ); 58 | 59 | uvVector.push( 60 | 0, 0, 1, 61 | 1, 0, 1, 62 | 0, 1, 1, 63 | 1, 1, 1 64 | ); 65 | bc ++; 66 | vc = bc * 4; 67 | ic = bc * 2; 68 | 69 | return new slBounds(this, len - 12); 70 | } 71 | 72 | protected var bc : uint = 0; 73 | protected var vc : uint = 0; 74 | protected var ic : uint = 0; 75 | 76 | public function get bufferCount() : uint { 77 | return bc; 78 | } 79 | 80 | public function get vertexCount() : uint { 81 | return vc; 82 | } 83 | 84 | public function get indexCount() : uint { 85 | return ic; 86 | } 87 | 88 | public function recycle(bounds : slBounds) : void { 89 | resetVector(bounds.offset); 90 | recycled.push(bounds); 91 | } 92 | 93 | public function setV(offset : uint, position : uint, value : Number) : void { 94 | vertexVector[offset + position] = value; 95 | } 96 | 97 | public function getV(offset : uint, position : uint) : Number { 98 | return vertexVector[offset + position]; 99 | } 100 | 101 | public function setU(offset : uint, position : uint, value : Number) : void { 102 | uvVector[offset + position] = value; 103 | } 104 | 105 | public function getU(offset : uint, position : uint) : Number { 106 | return vertexVector[offset + position]; 107 | } 108 | 109 | 110 | public function resetVector(offset : uint) : void { 111 | vertexVector[offset + 0] = 0; 112 | vertexVector[offset + 1] = 0; 113 | vertexVector[offset + 2] = 0; 114 | 115 | vertexVector[offset + 3] = 1; 116 | vertexVector[offset + 4] = 0; 117 | vertexVector[offset + 5] = 0; 118 | 119 | vertexVector[offset + 6] = 0; 120 | vertexVector[offset + 7] = 1; 121 | vertexVector[offset + 8] = 0; 122 | 123 | vertexVector[offset + 9] = 1; 124 | vertexVector[offset + 10] = 1; 125 | vertexVector[offset + 11] = 0; 126 | 127 | uvVector[offset + 0] = 0; 128 | uvVector[offset + 1] = 0; 129 | uvVector[offset + 2] = 1; 130 | 131 | uvVector[offset + 3] = 1; 132 | uvVector[offset + 4] = 0; 133 | uvVector[offset + 5] = 1; 134 | 135 | uvVector[offset + 6] = 0; 136 | uvVector[offset + 7] = 1; 137 | uvVector[offset + 8] = 1; 138 | 139 | uvVector[offset + 9] = 1; 140 | uvVector[offset + 10] = 1; 141 | uvVector[offset + 11] = 1; 142 | } 143 | 144 | 145 | //UV Info 146 | public var uvIndex : Vector. = Vector.([0]); 147 | public var uvInfo : Vector. = Vector.([0, 0, 1, 1, 0, 0, 1, 1]);//left, top, right, bottom, offsetX, offsetY, width, height 148 | 149 | public function getTextureCoord(index_id : uint, vector : Vector.) : void { 150 | var index : int = uvIndex.indexOf(index_id); 151 | 152 | if(index >= 0){ 153 | index = index * 8; 154 | vector[0] = uvInfo[index]; 155 | vector[1] = uvInfo[index + 1]; 156 | vector[2] = uvInfo[index + 2]; 157 | vector[3] = uvInfo[index + 3]; 158 | vector[4] = uvInfo[index + 4]; 159 | vector[5] = uvInfo[index + 5]; 160 | vector[6] = uvInfo[index + 6]; 161 | vector[7] = uvInfo[index + 7]; 162 | }else{ 163 | throw new Error("Can't find the uv info:" + index_id); 164 | } 165 | } 166 | 167 | public function setTextureCoord(index_id : uint, vector : Vector.) : void { 168 | var index : int = uvIndex.indexOf(index_id); 169 | 170 | if(index >= 0){ 171 | uvInfo[index] = vector[0]; 172 | uvInfo[index + 1] = vector[1]; 173 | uvInfo[index + 2] = vector[2]; 174 | uvInfo[index + 3] = vector[3]; 175 | uvInfo[index + 4] = vector[4]; 176 | uvInfo[index + 5] = vector[5]; 177 | uvInfo[index + 6] = vector[6]; 178 | uvInfo[index + 7] = vector[7]; 179 | }else{ 180 | uvInfo = uvInfo.concat(vector); 181 | uvIndex.push(index_id); 182 | } 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/sl2d/slAGALHelper.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | import flash.display.*; 3 | import flash.display3D.*; 4 | import flash.display3D.textures.*; 5 | import flash.geom.*; 6 | 7 | import com.adobe.utils.AGALMiniAssembler; 8 | 9 | public class slAGALHelper { 10 | private var context : Context3D; 11 | 12 | public function slAGALHelper(context : Context3D) : void { 13 | this.context = context; 14 | initialize(); 15 | } 16 | 17 | private function initialize() : void { 18 | initVectors(); 19 | initBuffers(); 20 | initShader(); 21 | } 22 | 23 | private static const MAX_SIZE : uint = 1500; 24 | 25 | private static const VERTEX_SIZE : uint = MAX_SIZE * 4; 26 | private static const INDEX_SIZE : uint = MAX_SIZE * 6; 27 | 28 | private var vertexVector : Vector. = new Vector.(); 29 | private var uvVector : Vector. = new Vector.(); 30 | private var indexVector : Vector. = new Vector.(); 31 | 32 | private function initVectors() : void { 33 | for(var i:int = 0;i \n" + // sample texture 0 77 | "mul ft2, ft2, ft1 \n" + 78 | "mov oc, ft2"; 79 | 80 | private var shaderProgram : Program3D; 81 | 82 | private function initShader() : void { 83 | var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler(); 84 | vertexShaderAssembler.assemble( Context3DProgramType.VERTEX, vertex_shader ); 85 | 86 | var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler(); 87 | fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT, fragment_shader); 88 | 89 | shaderProgram = context.createProgram(); 90 | shaderProgram.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode ); 91 | 92 | context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, FRAGMENT_CONSTANT_0); 93 | context.setProgram( shaderProgram ); 94 | } 95 | 96 | private var camera : slCamera; 97 | 98 | public function setCamera(camera : slCamera) : void { 99 | this.camera = camera; 100 | } 101 | 102 | 103 | private static const FRAGMENT_CONSTANT_0 : Vector. = Vector. ( [0, 0, 0, -0.015] ); 104 | 105 | private var compareMode : String = Context3DCompareMode.GREATER_EQUAL; 106 | private var blendFactor_src : String = Context3DBlendFactor.ONE; 107 | private var blendFactor_dst : String = Context3DBlendFactor.ZERO; 108 | 109 | public function draw(textures : Vector.) : void { 110 | if(camera.invalidated) context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, camera.getViewProjectionMatrix(), true); 111 | 112 | var len : uint = textures.length; 113 | 114 | for(var i : uint = 0; i < len; i++){ 115 | var t : slTexture = textures[i]; 116 | 117 | if(t.status < slTexture.INITIALIZED){ 118 | continue; 119 | } 120 | 121 | if(compareMode != t.compareMode){ 122 | compareMode = t.compareMode; 123 | context.setDepthTest(true, compareMode); 124 | } 125 | 126 | if(blendFactor_src != t.blendFactor_src || blendFactor_dst != t.blendFactor_dst){ 127 | blendFactor_src = t.blendFactor_src; 128 | blendFactor_dst = t.blendFactor_dst; 129 | 130 | context.setBlendFactors(blendFactor_src, blendFactor_dst); 131 | } 132 | 133 | context.setTextureAt(0, t.texture); 134 | 135 | var count : uint = t.bufferCount * 4; 136 | vertexBuffer = context.createVertexBuffer(count, 3); 137 | uvBuffer = context.createVertexBuffer(count, 3); 138 | colorBuffer = context.createVertexBuffer(count, 3); 139 | 140 | //trace(t.bufferCount + "::" + t.indexCount); 141 | indexBuffer = context.createIndexBuffer( t.indexCount*3); 142 | indexBuffer.uploadFromVector(indexVector, 0, t.indexCount*3); 143 | 144 | vertexBuffer.uploadFromVector(t.vertexVector, 0, t.vertexCount); 145 | uvBuffer.uploadFromVector(t.uvVector, 0, t.vertexCount); 146 | colorBuffer.uploadFromVector(t.colorVector, 0, t.vertexCount); 147 | 148 | context.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3); 149 | context.setVertexBufferAt(1, uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_3); 150 | context.setVertexBufferAt(2, colorBuffer, 0, Context3DVertexBufferFormat.FLOAT_2); 151 | 152 | context.drawTriangles(indexBuffer, 0, t.indexCount); 153 | } 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/sl2d/slBounds.as: -------------------------------------------------------------------------------- 1 | package sl2d { 2 | public class slBounds { 3 | public var parent : slBoundsGroup; 4 | 5 | public var texture : slTexture; 6 | public var offset : uint; 7 | 8 | //public var parent : slBoundsGroup = null; 9 | 10 | public function slBounds(texture : slTexture, offset : uint) : void { 11 | this.texture = texture; 12 | this.offset = offset; 13 | } 14 | 15 | public function recycle() : void { 16 | texture.recycle(this); 17 | } 18 | 19 | //vX, vY, vWidth, vHeight, 20 | //uX, uY, uWidth, uHeight, 21 | //depth, alpha, offsetX, offsetY 22 | protected var bounds : Vector. = Vector.([0,0,0,0, 23 | 0,0,0,0, 24 | 0,0,0,0]); 25 | 26 | protected var vDirty : Boolean = false; //vertex dirty 27 | protected var uDirty : Boolean = false; //uv dirty 28 | protected var dDirty : Boolean = false; //depth dirty 29 | protected var cDirty : Boolean = false; //color dirty 30 | 31 | public function set x(value : Number) : void { 32 | bounds[0] = value; 33 | vDirty = true; 34 | } 35 | 36 | public function set y(value : Number) : void { 37 | bounds[1] = value; 38 | vDirty = true; 39 | } 40 | 41 | public function get x() : Number { 42 | return bounds[0]; 43 | } 44 | 45 | public function get y() : Number { 46 | return bounds[1]; 47 | } 48 | 49 | public function setPosition(x : Number, y : Number) : void { 50 | bounds[0] = x; 51 | bounds[1] = y; 52 | 53 | vDirty = true; 54 | } 55 | 56 | public function setOffset(x : Number, y : Number) : void { 57 | bounds[10] = x; 58 | bounds[11] = y; 59 | 60 | vDirty = true; 61 | } 62 | 63 | public function set width(value : Number) : void { 64 | bounds[2] = value; 65 | vDirty = true; 66 | } 67 | 68 | public function set height(value : Number) : void { 69 | bounds[3] = value; 70 | vDirty = true; 71 | } 72 | 73 | public function get width() : Number { 74 | return bounds[2]; 75 | } 76 | 77 | public function get height() : Number { 78 | return bounds[3]; 79 | } 80 | 81 | public function setSize(w : uint, h : uint) : void { 82 | bounds[2] = w; 83 | bounds[3] = h; 84 | 85 | vDirty = true; 86 | } 87 | 88 | public function set depth(value : Number) : void { 89 | if(bounds[8] == value) return; 90 | bounds[8] = value; 91 | dDirty = true; 92 | } 93 | 94 | public function get depth() : Number { 95 | return bounds[8]; 96 | } 97 | 98 | public function set color(value : Number) : void { 99 | bounds[9] = value; 100 | cDirty = true; 101 | } 102 | 103 | public function get color() : Number { 104 | return bounds[9]; 105 | } 106 | 107 | protected var uvCoord : Vector. = Vector.([0, 0, 1, 1]);//left, top, right, bottom 108 | 109 | private var coord_index : int = -1; 110 | 111 | public function setUV(coord_index : uint) : void { 112 | if(texture.status < slTexture.INITIALIZED || this.coord_index == coord_index) return; 113 | 114 | this.coord_index = coord_index; 115 | texture.getTextureCoord(coord_index, uvCoord); 116 | 117 | var uVector : Vector. = texture.uvVector; 118 | 119 | uVector[offset + 0] = uvCoord[0]; 120 | uVector[offset + 1] = uvCoord[1]; 121 | 122 | uVector[offset + 3] = uvCoord[2]; 123 | uVector[offset + 4] = uvCoord[1]; 124 | 125 | uVector[offset + 6] = uvCoord[0]; 126 | uVector[offset + 7] = uvCoord[3]; 127 | 128 | uVector[offset + 9] = uvCoord[2]; 129 | uVector[offset + 10] = uvCoord[3]; 130 | 131 | setPosition(uvCoord[4], uvCoord[5]); 132 | setSize(uvCoord[6], uvCoord[7]); 133 | } 134 | 135 | protected var oX : Number = 0; 136 | protected var oY : Number = 0; 137 | 138 | protected var invalidate : Boolean = true; 139 | 140 | public function update() : void { 141 | var vVector : Vector. = texture.vertexVector; 142 | var uVector : Vector. = texture.uvVector; 143 | var cVector : Vector. = texture.colorVector; 144 | 145 | var left : Number; 146 | var top : Number; 147 | var right : Number; 148 | var bottom : Number; 149 | 150 | if(vDirty){ 151 | left = bounds[0] + bounds[10]; 152 | top = bounds[1] + bounds[11]; 153 | 154 | right = left + bounds[2]; 155 | bottom = top + bounds[3]; 156 | 157 | vVector[offset + 0] = left; 158 | vVector[offset + 1] = top; 159 | 160 | vVector[offset + 3] = right; 161 | vVector[offset + 4] = top; 162 | 163 | vVector[offset + 6] = left; 164 | vVector[offset + 7] = bottom; 165 | 166 | vVector[offset + 9] = right; 167 | vVector[offset + 10] = bottom; 168 | 169 | vDirty = false; 170 | } 171 | 172 | if(dDirty){ 173 | vVector[offset + 2] = bounds[8]; 174 | vVector[offset + 5] = bounds[8]; 175 | vVector[offset + 8] = bounds[8]; 176 | vVector[offset + 11] = bounds[8]; 177 | 178 | dDirty = false; 179 | } 180 | 181 | if(cDirty){ 182 | var red : uint = color >> 16; 183 | var green : uint = (color & 0x00FF00) >> 8; 184 | var blue : uint = (color & 0x0000FF) ; 185 | 186 | //trace(red.toString(16) + " : " + green.toString(16) + " : " + blue.toString(16)); 187 | 188 | cVector[offset + 0] = red/0xFF; 189 | cVector[offset + 3] = red/0xFF; 190 | cVector[offset + 6] = red/0xFF; 191 | cVector[offset + 9] = red/0xFF; 192 | 193 | cVector[offset + 1] = green/0xFF; 194 | cVector[offset + 4] = green/0xFF; 195 | cVector[offset + 7] = green/0xFF; 196 | cVector[offset + 10] = green/0xFF; 197 | 198 | cVector[offset + 2] = blue/0xFF; 199 | cVector[offset + 5] = blue/0xFF; 200 | cVector[offset + 8] = blue/0xFF; 201 | cVector[offset + 11] = blue/0xFF; 202 | 203 | cDirty = false; 204 | } 205 | } 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /src/Text2Texture.as: -------------------------------------------------------------------------------- 1 | package 2 | { 3 | 4 | import flash.display.*; 5 | import flash.text.*; 6 | import flash.events.Event; 7 | import flash.geom.*; 8 | import flash.utils.*; 9 | import flash.text.engine.*; 10 | 11 | import com.bit101.components.*; 12 | import sl2d.*; 13 | 14 | public class Text2Texture extends Sprite 15 | { 16 | public function Text2Texture() : void { 17 | trace("Just a test to transform text to texture"); 18 | createView3D(); 19 | } 20 | 21 | private var w : slWorld; 22 | private var ticker : slTicker; 23 | 24 | public function createView3D() : void { 25 | w = new slWorld(stage, 0, null, contextReady); 26 | w.backgroundColor = 0x000000; 27 | 28 | ticker = new slTicker(stage); 29 | } 30 | 31 | private var t : slTextField; 32 | 33 | private var bd : BitmapData; 34 | private var bm : Bitmap; 35 | 36 | public function contextReady() : void { 37 | slTextTexture.world = w; 38 | t = slTextField.createTextField(12, 100); 39 | t.text = "AspirinP,史珉,你好 DoDo, Chris?!" 40 | 41 | ticker.addListener(t); 42 | ticker.addListener(w); 43 | 44 | ticker.start(); 45 | slTextField.textures[0].bm.y = 100; 46 | addChild(slTextField.textures[0].bm); 47 | 48 | var bg : slButtonGroup = new slButtonGroup(); 49 | //addChild(t.tl); 50 | 51 | //var fontDescriptionNormal : FontDescription = new FontDescription("Arial", FontWeight.NORMAL , FontPosture.NORMAL); 52 | //var formatNormal:ElementFormat = new ElementFormat(fontDescriptionNormal); 53 | //formatNormal.fontSize = 24; 54 | //formatNormal.color = 0xFFFFFFFF; 55 | 56 | //var textBlock : TextBlock = new TextBlock(); 57 | //var textElement : TextElement = new TextElement("您好,Text Engine!", formatNormal); 58 | //textBlock.content = textElement; 59 | 60 | //var textLine : TextLine = textBlock.createTextLine(null, 150); 61 | //textLine.y = 40; 62 | //addChild(textLine); 63 | 64 | //var mtx : Matrix = new Matrix(); 65 | //mtx.ty = 12; 66 | 67 | //var bd : BitmapData = new BitmapData(512, 512, true, 0x00000000); 68 | //bm = new Bitmap(bd); 69 | //bm.smoothing = true; 70 | //bd.draw(textLine, mtx, null, null, null, true); 71 | 72 | //addChild(bm); 73 | } 74 | 75 | /*private var bd : BitmapData; 76 | private var bm : Bitmap; 77 | 78 | public function createTexture() : void { 79 | bd = new BitmapData(512, 512, true, 0x00000000); 80 | bm = new Bitmap(bd); 81 | //addChild(bm); 82 | } 83 | 84 | private var tf : TextField; 85 | private var tft : TextFormat; 86 | 87 | private var tf_target : TextField; 88 | 89 | public function createTextField() : void { 90 | tf = new TextField(); 91 | tf.x = this.stage.stageWidth - 205; 92 | tf.y = 5; 93 | tf.background = true; 94 | tf.backgroundColor = 0x333333; 95 | tf.width = 200; 96 | tf.height = 60; 97 | //tf.multiline = true; 98 | tf.textColor = 0xFFFFFF; 99 | tf.type = TextFieldType.INPUT; 100 | addChild(tf); 101 | 102 | tft = new TextFormat(); 103 | tft.size = 12; 104 | tft.bold = true; 105 | //tft.italic = true; 106 | 107 | tf.text = "史珉,ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890"; 108 | tf.setTextFormat(tft); 109 | 110 | tf_target = new TextField(); 111 | //tf_target.background = true; 112 | //tf_target.backgroundColor = 0x000000; 113 | tf_target.width = 20; 114 | tf_target.height = 40; 115 | tf_target.multiline = true; 116 | tf_target.textColor = 0xFFFFFFFF; 117 | tf_target.gridFitType = "subpixel"; 118 | tf_target.defaultTextFormat = tft; 119 | 120 | 121 | //addChild(tf_target); 122 | 123 | tf.addEventListener(Event.CHANGE, onChange); 124 | } 125 | 126 | private var mtx : Matrix = new Matrix(); 127 | private var clip : Rectangle = new Rectangle(0, 0, 14, 16); 128 | 129 | private var dic : Dictionary = new Dictionary(); 130 | private var used : Vector. = new Vector.(1116); 131 | 132 | public function onChange(evt : Event = null) : void { 133 | var str : String = tf.text; 134 | var len : uint = str.length; 135 | for(var i : int = 0; i < len; i++){ 136 | var char : String = str.charAt(i); 137 | if(dic[char] == null){ 138 | var index : int = used.indexOf(0); 139 | 140 | dic[char] = index; 141 | used[index] = 1; 142 | 143 | var index_y : uint = uint(index/36); 144 | var index_x : uint = index%36; 145 | 146 | tf_target.text = "\n" + char; 147 | 148 | mtx.tx = index_x * 14; 149 | mtx.ty = -10 + index_y * 16; 150 | 151 | clip.y = mtx.ty + 10; 152 | clip.x = mtx.tx; 153 | bd.draw(tf_target, mtx, null, null, clip); 154 | } 155 | } 156 | } 157 | 158 | 159 | public function contextReady() : void { 160 | var root : slBoundsGroup = new slBoundsGroup(); 161 | onChange(); 162 | var bounds : slBounds = s.textureFactory.createTextureFromBitmap(bm,"bm", false).createBounds(); 163 | //root.addBounds(bounds); 164 | var fixed_w : uint = 512; 165 | var fixed_h : uint = 512; 166 | 167 | for(var i : int = 0; i < 1116; i++){ 168 | var w : Number = 14; 169 | var h : Number = 16; 170 | var x : int = i%36 * 14; 171 | var y : int = int(i/36) * 16; 172 | var l : Number = x/fixed_w; 173 | var t : Number = y/fixed_h; 174 | 175 | //trace(l + " " + t + " " + w + " " + h + " " + x + " " + y + " "); 176 | 177 | bounds.texture.setTextureCoord(i, Vector.([l, t, l + (w/fixed_w), t + (h/fixed_h), 0, 0, w, h])); 178 | } 179 | 180 | bounds.setUV(4); 181 | root.addBounds(bounds); 182 | 183 | var bounds1 : slBounds = s.textureFactory.createTextureFromBitmap(bm,"bm", false).createBounds(); 184 | bounds1.setUV(0); 185 | //root.addBounds(bounds1); 186 | 187 | 188 | ticker.addListener(root); 189 | ticker.addListener(s); 190 | 191 | ticker.start(); 192 | }*/ 193 | } 194 | 195 | } 196 | -------------------------------------------------------------------------------- /swfobject/swfobject.js: -------------------------------------------------------------------------------- 1 | /* SWFObject v2.2 2 | is released under the MIT License 3 | */ 4 | var swfobject=function(){var D="undefined",r="object",S="Shockwave Flash",W="ShockwaveFlash.ShockwaveFlash",q="application/x-shockwave-flash",R="SWFObjectExprInst",x="onreadystatechange",O=window,j=document,t=navigator,T=false,U=[h],o=[],N=[],I=[],l,Q,E,B,J=false,a=false,n,G,m=true,M=function(){var aa=typeof j.getElementById!=D&&typeof j.getElementsByTagName!=D&&typeof j.createElement!=D,ah=t.userAgent.toLowerCase(),Y=t.platform.toLowerCase(),ae=Y?/win/.test(Y):/win/.test(ah),ac=Y?/mac/.test(Y):/mac/.test(ah),af=/webkit/.test(ah)?parseFloat(ah.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,X=!+"\v1",ag=[0,0,0],ab=null;if(typeof t.plugins!=D&&typeof t.plugins[S]==r){ab=t.plugins[S].description;if(ab&&!(typeof t.mimeTypes!=D&&t.mimeTypes[q]&&!t.mimeTypes[q].enabledPlugin)){T=true;X=false;ab=ab.replace(/^.*\s+(\S+\s+\S+$)/,"$1");ag[0]=parseInt(ab.replace(/^(.*)\..*$/,"$1"),10);ag[1]=parseInt(ab.replace(/^.*\.(.*)\s.*$/,"$1"),10);ag[2]=/[a-zA-Z]/.test(ab)?parseInt(ab.replace(/^.*[a-zA-Z]+(.*)$/,"$1"),10):0}}else{if(typeof O.ActiveXObject!=D){try{var ad=new ActiveXObject(W);if(ad){ab=ad.GetVariable("$version");if(ab){X=true;ab=ab.split(" ")[1].split(",");ag=[parseInt(ab[0],10),parseInt(ab[1],10),parseInt(ab[2],10)]}}}catch(Z){}}}return{w3:aa,pv:ag,wk:af,ie:X,win:ae,mac:ac}}(),k=function(){if(!M.w3){return}if((typeof j.readyState!=D&&j.readyState=="complete")||(typeof j.readyState==D&&(j.getElementsByTagName("body")[0]||j.body))){f()}if(!J){if(typeof j.addEventListener!=D){j.addEventListener("DOMContentLoaded",f,false)}if(M.ie&&M.win){j.attachEvent(x,function(){if(j.readyState=="complete"){j.detachEvent(x,arguments.callee);f()}});if(O==top){(function(){if(J){return}try{j.documentElement.doScroll("left")}catch(X){setTimeout(arguments.callee,0);return}f()})()}}if(M.wk){(function(){if(J){return}if(!/loaded|complete/.test(j.readyState)){setTimeout(arguments.callee,0);return}f()})()}s(f)}}();function f(){if(J){return}try{var Z=j.getElementsByTagName("body")[0].appendChild(C("span"));Z.parentNode.removeChild(Z)}catch(aa){return}J=true;var X=U.length;for(var Y=0;Y0){for(var af=0;af0){var ae=c(Y);if(ae){if(F(o[af].swfVersion)&&!(M.wk&&M.wk<312)){w(Y,true);if(ab){aa.success=true;aa.ref=z(Y);ab(aa)}}else{if(o[af].expressInstall&&A()){var ai={};ai.data=o[af].expressInstall;ai.width=ae.getAttribute("width")||"0";ai.height=ae.getAttribute("height")||"0";if(ae.getAttribute("class")){ai.styleclass=ae.getAttribute("class")}if(ae.getAttribute("align")){ai.align=ae.getAttribute("align")}var ah={};var X=ae.getElementsByTagName("param");var ac=X.length;for(var ad=0;ad'}}aa.outerHTML='"+af+"";N[N.length]=ai.id;X=c(ai.id)}else{var Z=C(r);Z.setAttribute("type",q);for(var ac in ai){if(ai[ac]!=Object.prototype[ac]){if(ac.toLowerCase()=="styleclass"){Z.setAttribute("class",ai[ac])}else{if(ac.toLowerCase()!="classid"){Z.setAttribute(ac,ai[ac])}}}}for(var ab in ag){if(ag[ab]!=Object.prototype[ab]&&ab.toLowerCase()!="movie"){e(Z,ab,ag[ab])}}aa.parentNode.replaceChild(Z,aa);X=Z}}return X}function e(Z,X,Y){var aa=C("param");aa.setAttribute("name",X);aa.setAttribute("value",Y);Z.appendChild(aa)}function y(Y){var X=c(Y);if(X&&X.nodeName=="OBJECT"){if(M.ie&&M.win){X.style.display="none";(function(){if(X.readyState==4){b(Y)}else{setTimeout(arguments.callee,10)}})()}else{X.parentNode.removeChild(X)}}}function b(Z){var Y=c(Z);if(Y){for(var X in Y){if(typeof Y[X]=="function"){Y[X]=null}}Y.parentNode.removeChild(Y)}}function c(Z){var X=null;try{X=j.getElementById(Z)}catch(Y){}return X}function C(X){return j.createElement(X)}function i(Z,X,Y){Z.attachEvent(X,Y);I[I.length]=[Z,X,Y]}function F(Z){var Y=M.pv,X=Z.split(".");X[0]=parseInt(X[0],10);X[1]=parseInt(X[1],10)||0;X[2]=parseInt(X[2],10)||0;return(Y[0]>X[0]||(Y[0]==X[0]&&Y[1]>X[1])||(Y[0]==X[0]&&Y[1]==X[1]&&Y[2]>=X[2]))?true:false}function v(ac,Y,ad,ab){if(M.ie&&M.mac){return}var aa=j.getElementsByTagName("head")[0];if(!aa){return}var X=(ad&&typeof ad=="string")?ad:"screen";if(ab){n=null;G=null}if(!n||G!=X){var Z=C("style");Z.setAttribute("type","text/css");Z.setAttribute("media",X);n=aa.appendChild(Z);if(M.ie&&M.win&&typeof j.styleSheets!=D&&j.styleSheets.length>0){n=j.styleSheets[j.styleSheets.length-1]}G=X}if(M.ie&&M.win){if(n&&typeof n.addRule==r){n.addRule(ac,Y)}}else{if(n&&typeof j.createTextNode!=D){n.appendChild(j.createTextNode(ac+" {"+Y+"}"))}}}function w(Z,X){if(!m){return}var Y=X?"visible":"hidden";if(J&&c(Z)){c(Z).style.visibility=Y}else{v("#"+Z,"visibility:"+Y)}}function L(Y){var Z=/[\\\"<>\.;]/;var X=Z.exec(Y)!=null;return X&&typeof encodeURIComponent!=D?encodeURIComponent(Y):Y}var d=function(){if(M.ie&&M.win){window.attachEvent("onunload",function(){var ac=I.length;for(var ab=0;ab