From e150efea1f37894a2c24d5d6c49d5df05c740e42 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Mon, 14 Nov 2016 23:38:41 +0000 Subject: [PATCH] Added Blitter Game Object, for fast drawing of texture frames, with single level transforms. --- src/gameobjects/blitter/Blitter.js | 61 +++++++++++++++++++ src/gameobjects/blitter/BlitterFactory.js | 19 ++++++ .../blitter/BlitterWebGLRenderer.js | 28 +++++++++ src/gameobjects/blitter/Bob.js | 38 ++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 src/gameobjects/blitter/Blitter.js create mode 100644 src/gameobjects/blitter/BlitterFactory.js create mode 100644 src/gameobjects/blitter/BlitterWebGLRenderer.js create mode 100644 src/gameobjects/blitter/Bob.js diff --git a/src/gameobjects/blitter/Blitter.js b/src/gameobjects/blitter/Blitter.js new file mode 100644 index 000000000..03f02be2a --- /dev/null +++ b/src/gameobjects/blitter/Blitter.js @@ -0,0 +1,61 @@ +/** +* @author Richard Davey +* @copyright 2016 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +Phaser.GameObject.Blitter = function (state, parent) +{ + Phaser.GameObject.call(this, state, 0, 0, null, null, parent); + + this.type = Phaser.BLITTER; + + this.children = new Phaser.Component.Children(this); +}; + +Phaser.GameObject.Blitter.prototype = Object.create(Phaser.GameObject.prototype); +Phaser.GameObject.Blitter.prototype.constructor = Phaser.GameObject.Blitter; + +Phaser.GameObject.Blitter.prototype.preUpdate = function () +{ + if (this.parent) + { + this.color.worldAlpha = this.parent.color.worldAlpha; + } +}; + +Phaser.GameObject.Blitter.prototype.create = function (frame, x, y, visible, index) +{ + if (x === undefined) { x = 0; } + if (y === undefined) { y = 0; } + if (visible === undefined) { visible = true; } + if (index === undefined) { index = 0; } + + var bob = new Phaser.GameObject.Blitter.Bob(this, frame, x, y, visible); + + this.children.addAt(bob, index, true); + + return bob; +}; + +Phaser.GameObject.Blitter.prototype.createMultiple = function (quantity, frame, visible) +{ + if (visible === undefined) { visible = true; } + + if (!Array.isArray(frame)) + { + frame = [ frame ]; + } + + var bobs = []; + + for (var f = 0; f < frame.length; f++) + { + for (var i = 0; i < quantity; i++) + { + bobs.push(this.create(frame[f], 0, 0, visible)); + } + } + + return bobs; +}; diff --git a/src/gameobjects/blitter/BlitterFactory.js b/src/gameobjects/blitter/BlitterFactory.js new file mode 100644 index 000000000..e7f39c9b1 --- /dev/null +++ b/src/gameobjects/blitter/BlitterFactory.js @@ -0,0 +1,19 @@ +/** +* @author Richard Davey +* @copyright 2016 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +Phaser.GameObject.Blitter.FACTORY_KEY = 'blitter'; + +Phaser.GameObject.Blitter.FACTORY_ADD = function (parent) +{ + if (parent === undefined) { parent = this.state; } + + return parent.children.add(new Phaser.GameObject.Blitter(this.state, parent)); +}; + +Phaser.GameObject.Blitter.FACTORY_MAKE = function (parent) +{ + return new Phaser.GameObject.Blitter(this.state, parent); +}; diff --git a/src/gameobjects/blitter/BlitterWebGLRenderer.js b/src/gameobjects/blitter/BlitterWebGLRenderer.js new file mode 100644 index 000000000..8515ee346 --- /dev/null +++ b/src/gameobjects/blitter/BlitterWebGLRenderer.js @@ -0,0 +1,28 @@ +Phaser.Renderer.WebGL.GameObjects.Blitter = { + + TYPES: [ + Phaser.GameObject.Blitter.prototype + ], + + render: function (renderer, src) + { + var alpha = src.color.worldAlpha * 255 << 24; + + // Skip rendering? + // if (src.skipRender || !src.visible || alpha === 0 || src.children.list.length === 0) + if (src.skipRender || !src.visible || alpha === 0) + { + return; + } + + // Render children + // for (var i = 0; i < src.children.list.length; i++) + // { + // var child = src.children.list[i]; + + // child.render(renderer, child); + // } + + } + +}; diff --git a/src/gameobjects/blitter/Bob.js b/src/gameobjects/blitter/Bob.js new file mode 100644 index 000000000..bebdcf5dc --- /dev/null +++ b/src/gameobjects/blitter/Bob.js @@ -0,0 +1,38 @@ +/** +* @author Richard Davey +* @copyright 2016 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +Phaser.GameObject.Blitter.Bob = function (parent, frame, x, y, visible) +{ + if (x === undefined) { x = 0; } + if (y === undefined) { y = 0; } + if (visible === undefined) { visible = true; } + + this.parent = parent; + + this.texture = frame.texture; + + this.frame = frame; + + this.type = 0; + + this.x = x; + this.y = y; + this.scaleX = 1; + this.scaleY = 1; + this.rotation = 0; + this.pivotX = 0; + this.pivotY = 0; + this.anchorX = 0; + this.anchorY = 0; + + this.alpha = 1; + + this.scaleMode = Phaser.scaleModes.DEFAULT; + + this.visible = visible; +}; + +Phaser.GameObject.Blitter.Bob.prototype.constructor = Phaser.GameObject.Blitter.Bob;