Skip to content

Commit 167a498

Browse files
committed
Phaser.Game constructor can now be passed a single object containing game settings + Stage settings, useful for advanced configurations.
1 parent 0acef49 commit 167a498

7 files changed

Lines changed: 253 additions & 67 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Significant API changes:
5353

5454
New features:
5555

56+
* Phaser.Game constructor can now be passed a single object containing game settings + Stage settings, useful for advanced configurations.
57+
* The width/height given to Phaser.Game can now be percentages, i.e. "100%" will set the width to the maximum window innerWidth.
5658
* Added a stage.fullScreenScaleMode property to determine scaling when fullscreen (thanks oysterCrusher)
5759
* Added support for margin and spacing around a frame in Loader.spritesheet.
5860
* Added Device.vibration to check if the Vibration API is available or not.
@@ -63,6 +65,7 @@ New features:
6365
* You can now load any binary file via the Loader: game.load.binary(key, url, callback) - the optional callback allows for post-load processing before entering the Cache.
6466
* Group.set will let you deep set a new propery on a single child of the Group.
6567
* Stage.display property added. A direct reference to the root Pixi Stage object (very useful for RenderTexture manipulation)
68+
* Added Ejecta detection to Device (thanks endel)
6669

6770

6871
New Examples:

examples/wip/rendertexture1.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.WEBGL, 'phaser-example', { preload: preload, create: create, update: update });
2+
var config = {
3+
4+
width: "100%",
5+
height: "100%",
6+
renderer: Phaser.WEBGL,
7+
parent: 'phaser-example',
8+
state: { preload: preload, create: create, update: update },
9+
backgroundColor: '#ff0000'
10+
11+
};
12+
13+
var game = new Phaser.Game(config);
314

415
function preload() {
516

@@ -24,20 +35,20 @@ var count = 0;
2435
function create() {
2536

2637
// create two render textures.. these dynamic textures will be used to draw the scene into itself
27-
renderTexture = game.add.renderTexture('texture1', 800, 600);
28-
renderTexture2 = game.add.renderTexture('textur2e', 800, 600);
38+
renderTexture = game.add.renderTexture('texture1', game.width, game.height);
39+
renderTexture2 = game.add.renderTexture('textur2e', game.width, game.height);
2940
currentTexture = renderTexture;
3041

3142
// create a new sprite that uses the render texture we created above
32-
outputSprite = game.add.sprite(400, 300, currentTexture);
43+
outputSprite = game.add.sprite(game.width/2, game.height/2, currentTexture);
3344

3445
// align the sprite
3546
outputSprite.anchor.x = 0.5;
3647
outputSprite.anchor.y = 0.5;
3748

3849
stuffContainer = game.add.group();
39-
stuffContainer.x = 800/2;
40-
stuffContainer.y = 600/2
50+
stuffContainer.x = game.width/2;
51+
stuffContainer.y = game.height/2;
4152

4253
// now create some items and randomly position them in the stuff container
4354
for (var i = 0; i < 20; i++)

src/core/Game.js

Lines changed: 156 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,61 @@
2424
*/
2525
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias) {
2626

27-
width = width || 800;
28-
height = height || 600;
29-
renderer = renderer || Phaser.AUTO;
30-
parent = parent || '';
31-
state = state || null;
32-
33-
if (typeof transparent === 'undefined') { transparent = false; }
34-
if (typeof antialias === 'undefined') { antialias = true; }
35-
3627
/**
3728
* @property {number} id - Phaser Game ID (for when Pixi supports multiple instances).
3829
*/
3930
this.id = Phaser.GAMES.push(this) - 1;
4031

32+
/**
33+
* @property {object} config - The Phaser.Game configuration object.
34+
*/
35+
this.config = null;
36+
4137
/**
4238
* @property {HTMLElement} parent - The Games DOM parent.
39+
* @default
4340
*/
44-
this.parent = parent;
41+
this.parent = '';
4542

4643
/**
4744
* @property {number} width - The Game width (in pixels).
45+
* @default
4846
*/
49-
this.width = width;
47+
this.width = 800;
5048

5149
/**
5250
* @property {number} height - The Game height (in pixels).
51+
* @default
5352
*/
54-
this.height = height;
53+
this.height = 600;
5554

5655
/**
5756
* @property {boolean} transparent - Use a transparent canvas background or not.
57+
* @default
5858
*/
59-
this.transparent = transparent;
59+
this.transparent = false;
6060

6161
/**
6262
* @property {boolean} antialias - Anti-alias graphics (in WebGL this helps with edges, in Canvas2D it retains pixel-art quality).
63+
* @default
6364
*/
64-
this.antialias = antialias;
65+
this.antialias = true;
6566

6667
/**
6768
* @property {number} renderer - The Pixi Renderer
6869
* @default
6970
*/
70-
this.renderer = null;
71+
this.renderer = Phaser.AUTO;
72+
73+
/**
74+
* @property {number} renderType - The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL.
75+
*/
76+
this.renderType = Phaser.AUTO;
7177

7278
/**
7379
* @property {number} state - The StateManager.
7480
*/
75-
this.state = new Phaser.StateManager(this, state);
81+
this.state = null;
7682

7783
/**
7884
* @property {boolean} _paused - Is game paused?
@@ -81,11 +87,6 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
8187
*/
8288
this._paused = false;
8389

84-
/**
85-
* @property {number} renderType - The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL.
86-
*/
87-
this.renderType = renderer;
88-
8990
/**
9091
* @property {boolean} _loadComplete - Whether load complete loading or not.
9192
* @private
@@ -225,6 +226,47 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
225226
*/
226227
this.particles = null;
227228

229+
// Parse the configuration object (if any)
230+
if (arguments.length === 1 && typeof arguments[0] === 'object')
231+
{
232+
this.parseConfig(arguments[0]);
233+
}
234+
else
235+
{
236+
if (typeof width !== 'undefined')
237+
{
238+
this.width = width;
239+
}
240+
241+
if (typeof height !== 'undefined')
242+
{
243+
this.height = height;
244+
}
245+
246+
if (typeof renderer !== 'undefined')
247+
{
248+
this.renderer = renderer;
249+
this.renderType = renderer;
250+
}
251+
252+
if (typeof parent !== 'undefined')
253+
{
254+
this.parent = parent;
255+
}
256+
257+
if (typeof transparent !== 'undefined')
258+
{
259+
this.transparent = transparent;
260+
}
261+
262+
if (typeof antialias !== 'undefined')
263+
{
264+
this.antialias = antialias;
265+
}
266+
267+
this.state = new Phaser.StateManager(this, state);
268+
}
269+
228270
var _this = this;
229271

230272
this._onBoot = function () {
@@ -247,6 +289,99 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
247289

248290
Phaser.Game.prototype = {
249291

292+
/**
293+
* Parses a Game configuration object.
294+
*
295+
* @method Phaser.Game#parseConfig
296+
* @protected
297+
*/
298+
parseConfig: function (config) {
299+
300+
this.config = config;
301+
302+
if (config['width'])
303+
{
304+
this.width = this.parseDimension(config['width'], 0);
305+
}
306+
307+
if (config['height'])
308+
{
309+
this.height = this.parseDimension(config['height'], 1);
310+
}
311+
312+
if (config['renderer'])
313+
{
314+
this.renderer = config['renderer'];
315+
this.renderType = config['renderer'];
316+
}
317+
318+
if (config['parent'])
319+
{
320+
this.parent = config['parent'];
321+
}
322+
323+
if (config['transparent'])
324+
{
325+
this.transparent = config['transparent'];
326+
}
327+
328+
if (config['antialias'])
329+
{
330+
this.antialias = config['antialias'];
331+
}
332+
333+
var state = null;
334+
335+
if (config['state'])
336+
{
337+
state = config['state'];
338+
}
339+
340+
this.state = new Phaser.StateManager(this, state);
341+
342+
},
343+
344+
/**
345+
* Get dimension.
346+
*
347+
* @method Phaser.Game#parseDimension
348+
* @protected
349+
*/
350+
parseDimension: function (size, dimension) {
351+
352+
var f = 0;
353+
var px = 0;
354+
355+
if (typeof size === 'string')
356+
{
357+
// %?
358+
if (size.substr(-1) === '%')
359+
{
360+
f = parseInt(size, 10) / 100;
361+
362+
if (dimension === 0)
363+
{
364+
px = window.innerWidth * f;
365+
}
366+
else
367+
{
368+
px = window.innerHeight * f;
369+
}
370+
}
371+
else
372+
{
373+
px = parseInt(size, 10);
374+
}
375+
}
376+
else
377+
{
378+
px = size;
379+
}
380+
381+
return px;
382+
383+
},
384+
250385
/**
251386
* Initialize engine sub modules and start the game.
252387
*

src/core/Stage.js

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Phaser.Stage = function (game, width, height) {
2424
/**
2525
* @property {string} game - Background color of the stage (defaults to black). Set via the public backgroundColor property.
2626
* @private
27-
* @default 'rgb(0,0,0)'
2827
*/
2928
this._backgroundColor = 'rgb(0,0,0)';
3029

@@ -34,10 +33,9 @@ Phaser.Stage = function (game, width, height) {
3433
this.offset = new Phaser.Point();
3534

3635
/**
37-
* @property {HTMLCanvasElement} canvas - Reference to the newly created &lt;canvas&gt; element.
36+
* @property {HTMLCanvasElement} canvas - Reference to the newly created `canvas` element.
3837
*/
39-
this.canvas = Phaser.Canvas.create(width, height);
40-
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
38+
this.canvas = null;
4139

4240
/**
4341
* @property {PIXI.Stage} _stage - The Pixi Stage which is hooked to the renderer.
@@ -90,10 +88,73 @@ Phaser.Stage = function (game, width, height) {
9088
*/
9189
this.checkOffsetInterval = 2500;
9290

91+
if (game.config)
92+
{
93+
this.parseConfig(game.config);
94+
}
95+
else
96+
{
97+
this.canvas = Phaser.Canvas.create(width, height);
98+
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
99+
}
100+
93101
};
94102

95103
Phaser.Stage.prototype = {
96104

105+
/**
106+
* Parses a Game configuration object.
107+
*
108+
* @method Phaser.Stage#parseConfig
109+
* @protected
110+
*/
111+
parseConfig: function (config) {
112+
113+
if (config['canvasID'])
114+
{
115+
this.canvas = Phaser.Canvas.create(this.game.width, this.game.height, config['canvasID']);
116+
}
117+
else
118+
{
119+
this.canvas = Phaser.Canvas.create(this.game.width, this.game.height);
120+
}
121+
122+
if (config['canvasStyle'])
123+
{
124+
this.canvas.stlye = config['canvasStyle'];
125+
}
126+
else
127+
{
128+
this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
129+
}
130+
131+
if (config['checkOffsetInterval'])
132+
{
133+
this.checkOffsetInterval = config['checkOffsetInterval'];
134+
}
135+
136+
if (config['disableVisibilityChange'])
137+
{
138+
this.disableVisibilityChange = config['disableVisibilityChange'];
139+
}
140+
141+
if (config['fullScreenScaleMode'])
142+
{
143+
this.fullScreenScaleMode = config['fullScreenScaleMode'];
144+
}
145+
146+
if (config['scaleMode'])
147+
{
148+
this.scaleMode = config['scaleMode'];
149+
}
150+
151+
if (config['backgroundColor'])
152+
{
153+
this.backgroundColor = config['backgroundColor'];
154+
}
155+
156+
},
157+
97158
/**
98159
* Initialises the stage and adds the event listeners.
99160
* @method Phaser.Stage#boot

0 commit comments

Comments
 (0)