Skip to content

Commit 1da9599

Browse files
committed
First pass of the newly re-structured Canvas Renderer (still using old texture system though).
1 parent 42b8118 commit 1da9599

13 files changed

Lines changed: 560 additions & 132 deletions

File tree

build/config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@
133133
<script src="$path/src/textures/parsers/JSONHash.js"></script>
134134
<script src="$path/src/textures/parsers/SpriteSheet.js"></script>
135135
136+
<script src="$path/src/renderer/canvas/CanvasRenderer.js"></script>
137+
<script src="$path/src/renderer/canvas/gameobjects/Container.js"></script>
138+
<script src="$path/src/renderer/canvas/gameobjects/Sprite.js"></script>
139+
136140
137141
EOL;
138142

src/Phaser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,13 @@ var Phaser = Phaser || { // jshint ignore:line
542542
NEAREST: 1
543543
},
544544

545-
PIXI: PIXI || {},
545+
Renderer: {
546+
547+
},
548+
549+
PIXI: PIXI || {
550+
551+
},
546552

547553
// Used to create IDs for various Pixi objects.
548554
_UID: 0

src/core/Game.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,12 @@ Phaser.Game.prototype = {
685685
return;
686686
}
687687

688+
var c = (this.renderType === Phaser.CANVAS) ? 'Canvas' : 'WebGL';
689+
688690
if (!this.device.ie)
689691
{
690692
var args = [
691-
'%c %c %c %c %c Phaser v' + Phaser.VERSION + ' / Pixi.js %c http://phaser.io',
693+
'%c %c %c %c %c Phaser v' + Phaser.VERSION + ' / Pixi.js / ' + c + ' %c http://phaser.io',
692694
'background: #ff0000',
693695
'background: #ffff00',
694696
'background: #00ff00',
@@ -739,7 +741,8 @@ Phaser.Game.prototype = {
739741
// They requested Canvas and their browser supports it
740742
this.renderType = Phaser.CANVAS;
741743

742-
this.renderer = new PIXI.CanvasRenderer(this);
744+
// this.renderer = new PIXI.CanvasRenderer(this);
745+
this.renderer = new Phaser.Renderer.Canvas(this);
743746

744747
this.context = this.renderer.context;
745748
}

src/gameobjects/components/Children.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Phaser.Component.Children.prototype = {
4646

4747
addAt: function (child, index) {
4848

49+
if (this.list.length === 0)
50+
{
51+
return this.add(child);
52+
}
53+
4954
if (index >= 0 && index <= this.list.length)
5055
{
5156
if (child.parent)

src/pixi/display/DisplayObjectContainer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ PIXI.DisplayObjectContainer = function () {
3333
* @default
3434
*/
3535
this.ignoreChildInput = false;
36+
37+
this.render = Phaser.Renderer.Canvas.GameObjects.Container.render;
3638

3739
};
3840

src/pixi/display/Sprite.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ PIXI.Sprite = function (texture) {
116116

117117
this.renderable = true;
118118

119+
this.render = Phaser.Renderer.Canvas.GameObjects.Sprite.render;
120+
119121
};
120122

121123
// constructor

src/pixi/renderers/webgl/WebGLRenderer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ PIXI.WebGLRenderer.prototype.updateCompressedTexture = function (texture) {
499499
};
500500

501501
/**
502+
* Note: Moved to TextureManager class.
503+
*
502504
* Updates and Creates a WebGL texture for the renderers context.
503505
*
504506
* @method updateTexture
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Mat Groves (@Doormat23)
4+
* @copyright 2016 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
8+
/**
9+
* A Camera is your view into the game world. It has a position and size and renders only those objects within its field of view.
10+
* The game automatically creates a single Stage sized camera on boot. Move the camera around the world with Phaser.Camera.x/y
11+
*
12+
* @class Phaser.Camera
13+
* @constructor
14+
* @param {Phaser.Game} game - Game reference to the currently running game.
15+
*/
16+
Phaser.Renderer.Canvas = function (game)
17+
{
18+
console.log('CanvasRenderer Alive');
19+
20+
/**
21+
* @property {Phaser.Game} game - A reference to the currently running Game.
22+
*/
23+
this.game = game;
24+
25+
this.type = Phaser.CANVAS;
26+
27+
// this.resolution = game.resolution;
28+
29+
/**
30+
* This sets if the CanvasRenderer will clear the canvas or not before the new render pass.
31+
* If the Stage is NOT transparent Pixi will use a canvas sized fillRect operation every frame to set the canvas background color.
32+
* If the Stage is transparent Pixi will use clearRect to clear the canvas every frame.
33+
* Disable this by setting this to false. For example if your game has a canvas filling background image you often don't need this set.
34+
*
35+
* @property clearBeforeRender
36+
* @type Boolean
37+
* @default
38+
*/
39+
this.clearBeforeRender = game.clearBeforeRender;
40+
41+
/**
42+
* Whether the render view is transparent
43+
*
44+
* @property transparent
45+
* @type Boolean
46+
*/
47+
this.transparent = game.transparent;
48+
49+
/**
50+
* Whether the render view should be resized automatically
51+
*
52+
* @property autoResize
53+
* @type Boolean
54+
*/
55+
this.autoResize = false;
56+
57+
/**
58+
* The width of the canvas view
59+
*
60+
* @property width
61+
* @type Number
62+
* @default 800
63+
*/
64+
this.width = game.width * game.resolution;
65+
66+
/**
67+
* The height of the canvas view
68+
*
69+
* @property height
70+
* @type Number
71+
* @default 600
72+
*/
73+
this.height = game.height * game.resolution;
74+
75+
/**
76+
* The canvas element that everything is drawn to.
77+
*
78+
* @property view
79+
* @type HTMLCanvasElement
80+
*/
81+
this.view = game.canvas;
82+
83+
/**
84+
* The canvas 2d context that everything is drawn with
85+
* @property context
86+
* @type CanvasRenderingContext2D
87+
*/
88+
this.context = this.view.getContext('2d', {
89+
alpha: this.transparent
90+
});
91+
92+
this.smoothProperty = Phaser.Canvas.getSmoothingPrefix(this.context);
93+
94+
this.roundPixels = false;
95+
96+
var so = 'source-over';
97+
98+
this.blendModes = [ so, 'lighter', so, so, so, so, so, so, so, so, so, so, so, so, so, so, so ];
99+
100+
this.currentBlendMode = 0;
101+
this.currentScaleMode = 0;
102+
103+
if (this.game.device.canUseMultiply)
104+
{
105+
this.mapBlendModes();
106+
}
107+
108+
this.resize(this.width, this.height);
109+
110+
// this.renderTypes = [];
111+
// this.renderTypes[Phaser.GROUP] = Phaser.Renderer.Canvas.GameObjects.Container;
112+
// this.renderTypes[Phaser.SPRITE] = Phaser.Renderer.Canvas.GameObjects.Sprite;
113+
114+
};
115+
116+
Phaser.Renderer.Canvas.GameObjects = {
117+
// Populated by the GameObjects classes
118+
};
119+
120+
Phaser.Renderer.Canvas.prototype.constructor = Phaser.Renderer.Canvas;
121+
122+
Phaser.Renderer.Canvas.prototype = {
123+
124+
/**
125+
* Maps Blend modes to Canvas blend modes.
126+
*
127+
* @method mapBlendModes
128+
* @private
129+
*/
130+
mapBlendModes: function ()
131+
{
132+
var modes = Phaser.blendModes;
133+
134+
this.blendModes[modes.MULTIPLY] = 'multiply';
135+
this.blendModes[modes.SCREEN] = 'screen';
136+
this.blendModes[modes.OVERLAY] = 'overlay';
137+
this.blendModes[modes.DARKEN] = 'darken';
138+
this.blendModes[modes.LIGHTEN] = 'lighten';
139+
this.blendModes[modes.COLOR_DODGE] = 'color-dodge';
140+
this.blendModes[modes.COLOR_BURN] = 'color-burn';
141+
this.blendModes[modes.HARD_LIGHT] = 'hard-light';
142+
this.blendModes[modes.SOFT_LIGHT] = 'soft-light';
143+
this.blendModes[modes.DIFFERENCE] = 'difference';
144+
this.blendModes[modes.EXCLUSION] = 'exclusion';
145+
this.blendModes[modes.HUE] = 'hue';
146+
this.blendModes[modes.SATURATION] = 'saturation';
147+
this.blendModes[modes.COLOR] = 'color';
148+
this.blendModes[modes.LUMINOSITY] = 'luminosity';
149+
150+
},
151+
152+
resize: function (width, height)
153+
{
154+
this.width = width * this.game.resolution;
155+
this.height = height * this.game.resolution;
156+
157+
this.view.width = this.width;
158+
this.view.height = this.height;
159+
160+
if (this.autoResize)
161+
{
162+
this.view.style.width = (this.width / this.game.resolution) + 'px';
163+
this.view.style.height = (this.height / this.game.resolution) + 'px';
164+
}
165+
166+
if (this.smoothProperty)
167+
{
168+
this.context[this.smoothProperty] = (this.scaleMode === Phaser.scaleModes.LINEAR);
169+
}
170+
171+
},
172+
173+
/**
174+
* Renders the Phaser.Stage to the canvas view, then iterates through its children.
175+
*
176+
* @method render
177+
* @param stage {Phaser.Stage}
178+
*/
179+
render: function (stage)
180+
{
181+
this.context.setTransform(1, 0, 0, 1, 0, 0);
182+
this.context.globalAlpha = 1;
183+
this.context.globalCompositeOperation = 'source-over';
184+
185+
this.currentBlendMode = 0;
186+
this.currentScaleMode = 0;
187+
188+
// Add Pre-render hook
189+
190+
// this.renderSession.currentBlendMode = 0;
191+
// this.renderSession.shakeX = this.game.camera._shake.x;
192+
// this.renderSession.shakeY = this.game.camera._shake.y;
193+
194+
// Is this needed any longer?
195+
/*
196+
if (navigator.isCocoonJS && this.view.screencanvas)
197+
{
198+
this.context.fillStyle = "black";
199+
this.context.clear();
200+
}
201+
*/
202+
203+
if (this.clearBeforeRender)
204+
{
205+
if (this.transparent)
206+
{
207+
this.context.clearRect(0, 0, this.width, this.height);
208+
}
209+
else if (stage._bgColor)
210+
{
211+
this.context.fillStyle = stage._bgColor.rgba;
212+
this.context.fillRect(0, 0, this.width , this.height);
213+
}
214+
}
215+
216+
stage.render(this, stage);
217+
218+
// Add Post-render hook
219+
220+
},
221+
222+
/**
223+
* This method adds it to the current stack of masks.
224+
*
225+
* @method pushMask
226+
* @param maskData {Object} the maskData that will be pushed
227+
* @param renderSession {Object} The renderSession whose context will be used for this mask manager.
228+
*/
229+
pushMask: function (maskData)
230+
{
231+
this.context.save();
232+
233+
var cacheAlpha = maskData.alpha;
234+
var transform = maskData.worldTransform;
235+
236+
var resolution = this.game.resolution;
237+
238+
this.context.setTransform(
239+
transform.a * resolution,
240+
transform.b * resolution,
241+
transform.c * resolution,
242+
transform.d * resolution,
243+
transform.tx * resolution,
244+
transform.ty * resolution
245+
);
246+
247+
// PIXI.CanvasGraphics.renderGraphicsMask(maskData, context);
248+
249+
this.context.clip();
250+
251+
maskData.worldAlpha = cacheAlpha;
252+
253+
},
254+
255+
popMask: function ()
256+
{
257+
this.context.restore();
258+
},
259+
260+
/**
261+
* Removes everything from the renderer and optionally removes the Canvas DOM element.
262+
*
263+
* @method destroy
264+
* @param [removeView=true] {boolean} Removes the Canvas element from the DOM.
265+
*/
266+
destroy: function ()
267+
{
268+
// CanvasPool
269+
270+
this.view = null;
271+
this.context = null;
272+
this.maskManager = null;
273+
274+
}
275+
276+
};
277+

0 commit comments

Comments
 (0)