Created
January 16, 2018 20:18
-
-
Save roipeker/e29f43a96ef835e276a9387fdcb74642 to your computer and use it in GitHub Desktop.
Draws a Flash MovieClip to make a Starling texture considering the texture scale.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================================================================================= | |
// | |
// Created by Rodrigo Lopez [roipeker™] on Jan 16 2019. | |
// | |
// ================================================================================================= | |
/* | |
== Sample screenshots == | |
http://bit.ly/2Dg30F0 | |
== Sample graphics.swc == (add as library, and use GuyGFX) | |
http://bit.ly/2FIKqmx | |
*/ | |
package { | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageQuality; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.geom.Rectangle; | |
import starling.core.Starling; | |
// -- CHECK DIFFERENT RESOLUTIONS HERE -- | |
[SWF(width="800", height="600", backgroundColor="#FFFFFF", frameRate="60")] | |
//[SWF(width="1024", height="768", backgroundColor="#FFFFFF", frameRate="60")] | |
//[SWF(width="1600", height="900", backgroundColor="#FFFFFF", frameRate="60")] | |
//[SWF(width="1200", height="900", backgroundColor="#FFFFFF", frameRate="60")] | |
public class DrawMovieClip extends Sprite { | |
private var starling:Starling; | |
public function DrawMovieClip() { | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.quality = StageQuality.LOW; | |
stage.align = StageAlign.TOP_LEFT; | |
loaderInfo.addEventListener( Event.COMPLETE, onLoaded ); | |
} | |
private function onLoaded( event:Event ):void { | |
var viewPortRect:Rectangle = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight ); | |
starling = new Starling( Game, stage, viewPortRect ); | |
starling.stage.stageWidth = 1024; | |
starling.stage.stageHeight = 768; | |
starling.supportHighResolutions = true; // comment this to see the difference in quality for HDPI displays. | |
starling.showStats = true ; | |
starling.stage.color = 0x999999 ; | |
starling.start(); | |
} | |
} | |
} | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.DisplayObject; | |
import flash.display.Stage; | |
import flash.geom.Matrix; | |
import starling.core.Starling; | |
import starling.display.Image; | |
import starling.display.Sprite; | |
import starling.events.Event; | |
import starling.textures.Texture; | |
class Game extends Sprite { | |
public function Game() { | |
addEventListener( Event.ADDED_TO_STAGE, onAdded ); | |
} | |
private function onAdded( event:Event ):void { | |
var nativeStage:Stage = Starling.current.nativeStage; | |
// vainilla Flash display list drawing, forget about Starling resizing for now ... | |
var csf:Number = nativeStage.contentsScaleFactor; | |
// pick an axis and scale based on stageWidth or stageHeight... | |
var stageRatioX:Number = nativeStage.stageWidth / stage.stageWidth; | |
var stageRatioY:Number = nativeStage.stageHeight / stage.stageHeight; | |
var stageRatio:Number = stageRatioY ; | |
var nativeDisplay:DisplayObject ; // temporal variable to position Starling Image. | |
var mc:GuyGFX = new GuyGFX(); // change the MovieClip Class from the SWF/SWC Library. | |
nativeDisplay = mc ; | |
/* opt1 */ | |
// Starling.current.nativeOverlay.addChild( mc ); // starling resizes the native Overlay to match the virtual stage, so no need to scale the mc there. | |
/* opt2 */ | |
mc.scaleX = mc.scaleY = stageRatio; // adjust to virtual stage size. | |
nativeStage.addChild( mc ); // just to display and compare | |
// now we draw the vectors CONSIDERING the stage.contentScaleMode... or calculate based on DPI. | |
var matrix:Matrix = mc.transform.matrix; // copy the mc scale (do not change x, y, or the draw will be off) | |
matrix.scale( csf, csf ); | |
var green_bd:BitmapData = new BitmapData( mc.width * csf, mc.height * csf, true, 0x2200ff00 ); // for the bitmap visualization | |
var red_bd:BitmapData = new BitmapData( mc.width * csf, mc.height * csf, true, 0x22ff0000 ); // for the Starling::Texture. | |
green_bd.draw( mc, matrix ); | |
red_bd.draw( mc, matrix ); | |
// the bmp code can be commented, is just to preview the Texture in Flash display list. | |
var bmp:Bitmap = nativeDisplay = new Bitmap( green_bd ); | |
Starling.current.nativeStage.addChild( bmp ); // show the actual texture size. | |
bmp.x = mc.width; | |
bmp.scaleX = bmp.scaleY = 1 / csf; // or you can match the original mc size now... | |
// now create the Starling::Texture. | |
var texture:Texture = Texture.fromBitmapData( red_bd, false, false, csf ); | |
var image:Image = new Image( texture ); | |
image.x = (nativeDisplay.x + nativeDisplay.width)/stageRatioX ; // position next to mc or bitmap | |
// image.scale = 1 / stageRatio; // if you wanna compensate the VIRTUAL stage scaling in Starling. | |
addChild( image ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment