? ?转自:?http://www.riadev.com/flex-thread-1929-1-1.html ?? 学习:http://www.adobe.com/cn/devnet/flashplayer/articles/introducing_Starling.html
flashplayer11给我们带来了巨大的性能提升,这一点不言而喻,大家都讲GPU加速主要用户3D画面中,不错。这是adobe的初衷。不过我们也可以使用GPU来加速我们的2D画面,这样的操作很多人认为是多次一举,因为flash本身就对2D画面支持的非常好了,为什么还要用GPU来进行图像渲染操作呢?其实也是一个性能上的提升,关于这点我们就不再多说了,慢慢的,我们就可以在很多的商业项目中看到,基于GPU加速的2D flash应用在性能上也会有很大的提高。那么怎么样使用GPU来对2D画面进行加速呢?
? ? 可能很多人都还不明白,认为flashplayer11是adobe内置了一个3D的引擎,这个引擎用来调用GPU,其实不是的。adobe为我们提供的仅仅是一些非常底层的API,利用这些API我们可以实现对GPU的调用。但是请不要忘记,使用这些API我们无法生成任何图像,因为他们实在是太底层了。所以我们需要用到一些第三方的引擎。这里Starling就是一个基于GPU加速的2D引擎。 ? ? 可能很多人都还不明白,认为flashplayer11是adobe内置了一个3D的引擎,这个引擎用来调用GPU,其实不是的。adobe为我们提供的仅仅是一些非常底层的API,利用这些API我们可以实现对GPU的调用。但是请不要忘记,使用这些API我们无法生成任何图像,因为他们实在是太底层了。所以我们需要用到一些第三方的引擎。这里Starling就是一个基于GPU加速的2D引擎。 ? ? 在adobe MAX2011大会上,adobe为我们展示了一个使用starling引擎来制作的flash游戏,其名称是《愤怒的小鸟》。感兴趣的童鞋可以到我的博客中观看这段视频。在adobe 官方视频教程板块中。 ? ? ? ? 在flash中运行如此复杂或者说如此多运算的程序,对于以前的flash来说需要进行大量的优化工作以达到我们预期的效果,但是现在有了GPU,我们就可以大胆的放开手脚来进行制作了。那么starling到底有哪些方便之处,以至于adobe在这么重要的大会中抽出一部分时间来进行演示呢? ? ? 答案我想只有举办者才知道。但是我所知道的是,它不仅仅是方便好用,而且更易于以前项目的移植工作。例如,在这个引擎中我们可以看到Sprige、MovieClip或者Button这样熟悉的名字。不错。为了方便开发者移植工程,所有的类名称和以前相同,我们只需要将import 语句进行修改,然后小范围修改代码即可。虽然如此,但是笔者目前还没有找到将美工制作动画进行GPU加速的办法。目前不知道能不能行得通。至于starling,是使用程序来制作动画,非常适合游戏。在此忽略美工部分,说说咱程序员之间的事情吧! ? ? ? ? 首先,想让你的画面显示,就需要纹理渲染,换句话说就是贴图。和3D引擎的中的概念很相像。只不过这里的纹理是2D场景中的。在starling引擎中控制纹理的操作由Texture类来执行。Texture类的所有纹理数据基本上都是来自于位图,或者位图数据。这一点没有任何问题。因为我们不可能从flash中的矢量图形中进行数据获取。矢量图的渲染还需要CPU来进行操作。 ? ? ? ? 下面我们来看一个简单的demo,这个DEMO中我们使用loader对象来加载一张图片,然后使用starling引擎来对其进行GPU加速操作。 ? ? ? ? stex1.as
- ? ? package
- {
- ? ? import flash.display.Sprite;
- ? ? import flash.display.StageAlign;
- ? ? import flash.display.StageScaleMode;
- ? ? import flash.geom.Rectangle;
- ? ?
- ? ? import starling.core.Starling;
- ? ?
- ? ? [SWF(width="320",height="480",frameRate="60",backgroundColor="#000000")]
- ? ? public class stex1 extends Sprite
- ? ? {
- ? ?? ???private var mStarling:Starling;
- ? ?? ???
- ? ?? ???public function stex1()
- ? ?? ???{
- ? ?? ?? ?? ?stage.scaleMode = StageScaleMode.NO_SCALE;
- ? ?? ?? ?? ?stage.align = StageAlign.TOP_LEFT;
- ? ?? ?? ?? ?
- ? ?? ?? ?? ?//Starling.multitouchEnabled = true;
- ? ?? ?? ?? ?
- ? ?? ?? ?? ?mStarling = new Starling(mys,stage,new Rectangle(0,320,480));
- ? ?? ?? ?? ?//mStarling.simulateMultitouch = true;
- ? ?? ?? ?? ?//mStarling.enableErrorChecking = false;
- ? ?? ?? ?? ?mStarling.antiAliasing= 1;
- ? ?? ?? ?? ?mStarling.start();
- ? ?? ?? ?? ?
- ? ?? ???}
- ? ? }
- }
-
普通浏览
复制代码
保存代码
打印代码
-
????package
-
{
-
????
import?flash.display.Sprite
;
-
????
import?flash.display.StageAlign
;
-
????
import?flash.display.StageScaleMode
;
-
????
import?flash.geom.Rectangle
;
-
????
-
????
import?starling.core.Starling
;
-
????
-
????[SWF
(
width
=
"320",?
height
=
"480",?frameRate
=
"60",?
backgroundColor
=
"#000000"
)]
-
????
public?
class?stex1?
extends?Sprite
-
????{
-
????????
private?
var?mStarling
:Starling
;
-
????????
-
????????
public?
function?stex1
(
)
-
????????{
-
????????????stage.
scaleMode?
=?StageScaleMode.NO_SCALE
;
-
????????????stage.
align?
=?StageAlign.TOP_LEFT
;
-
????????????
-
????????????
-
????????????
-
????????????mStarling?
=?
new?Starling
(mys,?stage, new?Rectangle
(
0, 0, 320, 480
)
)
;
-
????????????
-
????????????
-
????????????mStarling.antiAliasing
=?
1
;
-
????????????mStarling.
start
(
)
;
-
????????????
-
????????}
-
????}
-
}
-
?
mys.as
- package
- {
- ? ? import flash.display.Bitmap;
- ? ? import flash.display.BitmapData;
- ? ? import flash.events.ProgressEvent;
- ? ?
- ? ? import starling.display.Button;
- ? ? import starling.display.Image;
- ? ? import starling.display.Sprite;
- ? ? import starling.events.Event;
- ? ? import starling.textures.Texture;
- ? ?
- ? ? public class mys extends Sprite
- ? ? {
- ? ?? ???private var d:data = new data();
- ? ?? ???public function mys()
- ? ?? ???{
- ? ?? ?? ?? ?
- ? ?? ?? ?? ?d.addEventListener(ProgressEvent.PROGRESS,ok);
- ? ?? ?? ?? ?
- ? ?? ???}
- ? ?? ???private function ok(evt:ProgressEvent):void
- ? ?? ???{
- ? ?? ?? ?? ?
- ? ?? ?? ?? ?var bg:Image = new Image(Texture.fromBitmap(d.bit) );
- ? ?? ?? ?? ?this.addChild(bg);
- ? ?? ???}
- ? ?? ???
- ? ? }
- }
-
普通浏览
复制代码
保存代码
打印代码
-
package
-
{
-
????
import?flash.display.Bitmap
;
-
????
import?flash.display.BitmapData
;
-
????
import?flash.events.ProgressEvent
;
-
????
-
????
import?starling.display.
Button
;
-
????
import?starling.display.Image
;
-
????
import?starling.display.Sprite
;
-
????
import?starling.events.Event
;
-
????
import?starling.textures.Texture
;
-
????
-
????
public?
class?mys?
extends?Sprite
-
????{
-
????????
private?
var?d
:
data?
=?
new?
data
(
)
;
-
????????
public?
function?mys
(
)
-
????????{
-
????????????
-
????????????d.addEventListener
(ProgressEvent.PROGRESS,ok
)
;
-
????????????
-
????????}
-
????????
private?
function?ok
(evt
:ProgressEvent
)
:
void
-
????????{
-
????????????
-
????????????
var?bg
:Image?
=?
new?Image
(Texture.fromBitmap
(d.bit
)?
)
;
-
????????????
this.addChild
(bg
)
;
-
????????}
-
????????
-
????}
-
}
-
?
data.as
- package
- {
- ? ? import flash.display.Bitmap;
- ? ? import flash.display.Loader;
- ? ? import flash.display.LoaderInfo;
- ? ? import flash.events.Event;
- ? ? import flash.events.EventDispatcher;
- ? ? import flash.net.URLRequest;
- ? ? import flash.events.ProgressEvent;
- ? ?
- ? ? public class data extends EventDispatcher
- ? ? {
- ? ?? ???public var bit:Bitmap;
- ? ?? ???
- ? ?? ???private var a:Loader = new Loader();
- ? ?? ???public function data()
- ? ?? ???{
- ? ?? ?? ?? ?a.load(new URLRequest("../media/textures/background.png") );
- ? ?? ?? ?? ?a.contentLoaderInfo.addEventListener(Event.COMPLETE,ok);
- ? ?? ???}
- ? ?? ???private function ok(evt:Event):void
- ? ?? ???{
- ? ?? ?? ?? ?trace(a.content + "*********");
- ? ?? ?? ?? ?this.bit = a.content as Bitmap;
- ? ?? ?? ?? ?
- ? ?? ?? ?? ?var shijian:Event = new ProgressEvent(ProgressEvent.PROGRESS);
- ? ?? ?? ?? ?this.dispatchEvent(shijian);
- ? ?? ???}
- ? ? }
- }
-
普通浏览
复制代码
保存代码
打印代码
-
package
-
{
-
????
import?flash.display.Bitmap
;
-
????
import?flash.display.Loader
;
-
????
import?flash.display.LoaderInfo
;
-
????
import?flash.events.Event
;
-
????
import?flash.events.EventDispatcher
;
-
????
import?flash.net.URLRequest
;
-
????
import?flash.events.ProgressEvent
;
-
????
-
????
public?
class?
data?
extends?EventDispatcher
-
????{
-
????????
public?
var?bit
:Bitmap
;
-
????????
-
????????
private?
var?a
:Loader?
=?
new?Loader
(
)
;
-
????????
public?
function?
data
(
)
-
????????{
-
????????????a.
load
(
new?URLRequest
(
"../media/textures/background.png"
)?
)
;
-
????????????a.contentLoaderInfo.addEventListener
(Event.COMPLETE,ok
)
;
-
????????}
-
????????
private?
function?ok
(evt
:Event
)
:
void
-
????????{
-
????????????
trace
(a.content?
+?
"*********"
)
;
-
????????????
this.bit?
=?a.content?as?Bitmap
;
-
????????????
-
????????????
var?shijian
:Event?
=?
new?ProgressEvent
(ProgressEvent.PROGRESS
)
;
-
????????????
this.dispatchEvent
(shijian
)
;
-
????????}
-
????}
-
}
-
-
?
??
|