2016-11-29 11:26:30 +00:00
|
|
|
var CONST = require('../const');
|
|
|
|
var CanvasPool = require('../dom/CanvasPool');
|
|
|
|
var Features = require('../device/Features');
|
2017-01-12 17:11:58 +00:00
|
|
|
var CanvasRenderer = require('../renderer/canvas/CanvasRenderer');
|
2017-01-18 23:16:57 +00:00
|
|
|
var WebGLRenderer = require('../renderer/webgl/WebGLRenderer');
|
2017-02-04 18:02:31 +00:00
|
|
|
var CanvasInterpolation = require('../dom/CanvasInterpolation');
|
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
|
|
|
|
if (config.renderType === CONST.CANVAS || (config.renderType !== CONST.CANVAS && !Features.webGL))
|
|
|
|
{
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Game requested WebGL and browser says it supports it
|
|
|
|
config.renderType = CONST.WEBGL;
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:05:07 +00:00
|
|
|
// Pixel Art mode?
|
|
|
|
if (config.pixelArt)
|
|
|
|
{
|
|
|
|
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?
|
|
|
|
if (config.pixelArt)
|
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2016-11-29 11:26:30 +00:00
|
|
|
// Create the renderer
|
|
|
|
if (config.renderType === CONST.WEBGL)
|
|
|
|
{
|
2016-12-07 02:28:22 +00:00
|
|
|
game.renderer = new WebGLRenderer(game);
|
|
|
|
game.context = null;
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-12 17:11:58 +00:00
|
|
|
game.renderer = new CanvasRenderer(game);
|
2017-02-07 18:44:26 +00:00
|
|
|
game.context = game.renderer.gameContext;
|
2017-02-07 20:47:41 +00:00
|
|
|
|
2017-02-07 19:53:04 +00:00
|
|
|
// debug
|
|
|
|
game.canvas.id = 'game';
|
2016-11-29 11:26:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CreateRenderer;
|