forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateRenderer.js
More file actions
138 lines (115 loc) · 3.93 KB
/
Copy pathCreateRenderer.js
File metadata and controls
138 lines (115 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CanvasInterpolation = require('../display/canvas/CanvasInterpolation');
var CanvasPool = require('../display/canvas/CanvasPool');
var CONST = require('../const');
var Features = require('../device/Features');
/**
* 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.
*
* @function Phaser.Core.CreateRenderer
* @since 3.0.0
*
* @param {Phaser.Game} game - The Phaser.Game instance on which the renderer will be set.
*/
var CreateRenderer = function (game)
{
var config = game.config;
if ((config.customEnvironment || config.canvas) && config.renderType === CONST.AUTO)
{
throw new Error('Must set explicit renderType in custom environment');
}
// Not a custom environment, didn't provide their own canvas and not headless, so determine the renderer:
if (!config.customEnvironment && !config.canvas && config.renderType !== CONST.HEADLESS)
{
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;
}
}
// Pixel Art mode?
if (!config.antialias)
{
CanvasPool.disableSmoothing();
}
var baseSize = game.scale.baseSize;
var width = baseSize.width;
var height = baseSize.height;
// Does the game config provide its own canvas element to use?
if (config.canvas)
{
game.canvas = config.canvas;
game.canvas.width = width;
game.canvas.height = height;
}
else
{
game.canvas = CanvasPool.create(game, width, height, config.renderType);
}
// Does the game config provide some canvas css styles to use?
if (config.canvasStyle)
{
game.canvas.style = config.canvasStyle;
}
// Pixel Art mode?
if (!config.antialias)
{
CanvasInterpolation.setCrisp(game.canvas);
}
if (config.renderType === CONST.HEADLESS)
{
// Nothing more to do here
return;
}
var CanvasRenderer;
var WebGLRenderer;
if (typeof WEBGL_RENDERER && typeof CANVAS_RENDERER)
{
CanvasRenderer = require('../renderer/canvas/CanvasRenderer');
WebGLRenderer = require('../renderer/webgl/WebGLRenderer');
// Let the config pick the renderer type, as both are included
if (config.renderType === CONST.WEBGL)
{
game.renderer = new WebGLRenderer(game);
}
else
{
game.renderer = new CanvasRenderer(game);
game.context = game.renderer.gameContext;
}
}
if (typeof WEBGL_RENDERER && !typeof CANVAS_RENDERER)
{
WebGLRenderer = require('../renderer/webgl/WebGLRenderer');
// Force the type to WebGL, regardless what was requested
config.renderType = CONST.WEBGL;
game.renderer = new WebGLRenderer(game);
}
if (!typeof WEBGL_RENDERER && typeof CANVAS_RENDERER)
{
CanvasRenderer = require('../renderer/canvas/CanvasRenderer');
// Force the type to Canvas, regardless what was requested
config.renderType = CONST.CANVAS;
game.renderer = new CanvasRenderer(game);
game.context = game.renderer.gameContext;
}
};
module.exports = CreateRenderer;