2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-25 00:48:48 +00:00
|
|
|
var CanvasInterpolation = require('../display/canvas/CanvasInterpolation');
|
2017-10-11 16:05:59 +00:00
|
|
|
var CanvasPool = require('../display/canvas/CanvasPool');
|
2018-01-25 00:48:48 +00:00
|
|
|
var CONST = require('../const');
|
2016-11-29 11:26:30 +00:00
|
|
|
var Features = require('../device/Features');
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 00:48:48 +00:00
|
|
|
* Called automatically by Phaser.Game and responsible for creating the renderer it will use.
|
|
|
|
*
|
|
|
|
* Relies upon two webpack global flags to be defined: `WEBGL_RENDERER` and `CANVAS_RENDERER` during build time, but not at run-time.
|
2017-10-04 22:48:16 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Boot.CreateRenderer
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-01-25 00:48:48 +00:00
|
|
|
* @param {Phaser.Game} game - The Phaser.Game instance on which the renderer will be set.
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2016-11-29 11:26:30 +00:00
|
|
|
var CreateRenderer = function (game)
|
|
|
|
{
|
|
|
|
var config = game.config;
|
|
|
|
|
|
|
|
// Game either requested Canvas,
|
|
|
|
// or requested AUTO or WEBGL but the browser doesn't support it, so fall back to Canvas
|
2018-02-28 21:57:32 +00:00
|
|
|
|
|
|
|
if (config.renderType !== CONST.HEADLESS)
|
2016-11-29 11:26:30 +00:00
|
|
|
{
|
2018-02-28 21:57:32 +00:00
|
|
|
if (config.renderType === CONST.CANVAS || (config.renderType !== CONST.CANVAS && !Features.webGL))
|
2016-11-29 11:26:30 +00:00
|
|
|
{
|
2018-02-28 21:57:32 +00:00
|
|
|
if (Features.canvas)
|
|
|
|
{
|
|
|
|
// They requested Canvas and their browser supports it
|
|
|
|
config.renderType = CONST.CANVAS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Error('Cannot create Canvas or WebGL context, aborting.');
|
|
|
|
}
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-28 21:57:32 +00:00
|
|
|
// Game requested WebGL and browser says it supports it
|
|
|
|
config.renderType = CONST.WEBGL;
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:05:07 +00:00
|
|
|
// Pixel Art mode?
|
2018-06-27 14:15:00 +00:00
|
|
|
if (config.pixelArt || !config.antialias)
|
2017-06-08 14:05:07 +00:00
|
|
|
{
|
|
|
|
CanvasPool.disableSmoothing();
|
|
|
|
}
|
|
|
|
|
2016-11-29 11:26:30 +00:00
|
|
|
// Does the game config provide its own canvas element to use?
|
|
|
|
if (config.canvas)
|
|
|
|
{
|
|
|
|
game.canvas = config.canvas;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
game.canvas = CanvasPool.create(game, config.width, config.height, config.renderType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the game config provide some canvas css styles to use?
|
|
|
|
if (config.canvasStyle)
|
|
|
|
{
|
|
|
|
game.canvas.style = config.canvasStyle;
|
|
|
|
}
|
|
|
|
|
2017-02-04 18:02:31 +00:00
|
|
|
// Pixel Art mode?
|
2018-06-27 14:15:00 +00:00
|
|
|
if (config.pixelArt || !config.antialias)
|
2017-02-04 18:02:31 +00:00
|
|
|
{
|
|
|
|
CanvasInterpolation.setCrisp(game.canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zoomed?
|
|
|
|
if (config.zoom !== 1)
|
|
|
|
{
|
|
|
|
game.canvas.style.width = (config.width * config.zoom).toString() + 'px';
|
|
|
|
game.canvas.style.height = (config.height * config.zoom).toString() + 'px';
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:57:32 +00:00
|
|
|
if (config.renderType === CONST.HEADLESS)
|
|
|
|
{
|
|
|
|
// Nothing more to do here
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:27:43 +00:00
|
|
|
var CanvasRenderer;
|
|
|
|
var WebGLRenderer;
|
|
|
|
|
2018-04-19 23:23:24 +00:00
|
|
|
if (typeof WEBGL_RENDERER && typeof CANVAS_RENDERER)
|
2017-09-13 14:27:43 +00:00
|
|
|
{
|
|
|
|
CanvasRenderer = require('../renderer/canvas/CanvasRenderer');
|
|
|
|
WebGLRenderer = require('../renderer/webgl/WebGLRenderer');
|
|
|
|
|
2018-05-10 11:25:33 +00:00
|
|
|
// Let the config pick the renderer type, as both are included
|
2017-09-13 14:27:43 +00:00
|
|
|
if (config.renderType === CONST.WEBGL)
|
|
|
|
{
|
|
|
|
game.renderer = new WebGLRenderer(game);
|
2018-05-10 11:25:33 +00:00
|
|
|
|
|
|
|
// The WebGL Renderer sets this value during its init, not on construction
|
2017-09-13 14:27:43 +00:00
|
|
|
game.context = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
game.renderer = new CanvasRenderer(game);
|
|
|
|
game.context = game.renderer.gameContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 23:23:24 +00:00
|
|
|
if (typeof WEBGL_RENDERER && !typeof CANVAS_RENDERER)
|
2016-11-29 11:26:30 +00:00
|
|
|
{
|
2017-09-13 14:27:43 +00:00
|
|
|
WebGLRenderer = require('../renderer/webgl/WebGLRenderer');
|
|
|
|
|
|
|
|
// Force the type to WebGL, regardless what was requested
|
|
|
|
config.renderType = CONST.WEBGL;
|
2018-05-10 11:25:33 +00:00
|
|
|
|
2016-12-07 02:28:22 +00:00
|
|
|
game.renderer = new WebGLRenderer(game);
|
2018-05-10 11:25:33 +00:00
|
|
|
|
|
|
|
// The WebGL Renderer sets this value during its init, not on construction
|
2016-12-07 02:28:22 +00:00
|
|
|
game.context = null;
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
2017-09-13 14:27:43 +00:00
|
|
|
|
2018-04-19 23:23:24 +00:00
|
|
|
if (!typeof WEBGL_RENDERER && typeof CANVAS_RENDERER)
|
2016-11-29 11:26:30 +00:00
|
|
|
{
|
2017-09-13 14:27:43 +00:00
|
|
|
CanvasRenderer = require('../renderer/canvas/CanvasRenderer');
|
|
|
|
|
|
|
|
// Force the type to Canvas, regardless what was requested
|
|
|
|
config.renderType = CONST.CANVAS;
|
2018-05-10 11:25:33 +00:00
|
|
|
|
2017-01-12 17:11:58 +00:00
|
|
|
game.renderer = new CanvasRenderer(game);
|
2018-05-10 11:25:33 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
game.context = game.renderer.gameContext;
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CreateRenderer;
|