Skip to content

Commit fb33be2

Browse files
committed
Added in the Create functions and exposed via Texture Manager
1 parent e7465bb commit fb33be2

13 files changed

Lines changed: 390 additions & 1 deletion

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '927d4eb0-509e-11e7-8474-0fbd363270fe'
2+
build: '51369a60-5161-11e7-bea9-fdb861265841'
33
};
44
module.exports = CHECKSUM;

v3/src/create/GenerateTexture.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var GetValue = require('../utils/object/GetValue');
2+
var Arne16 = require('./palettes/Arne16');
3+
var CanvasPool = require('../dom/CanvasPool');
4+
5+
var GenerateTexture = function (config)
6+
{
7+
var data = GetValue(config, 'data', []);
8+
var canvas = GetValue(config, 'canvas', null);
9+
var palette = GetValue(config, 'palette', Arne16);
10+
var pixelWidth = GetValue(config, 'pixelWidth', 1);
11+
var pixelHeight = GetValue(config, 'pixelHeight', pixelWidth);
12+
var resizeCanvas = GetValue(config, 'resizeCanvas', true);
13+
var clearCanvas = GetValue(config, 'clearCanvas', true);
14+
var preRender = GetValue(config, 'preRender', null);
15+
var postRender = GetValue(config, 'postRender', null);
16+
17+
var width = Math.floor(Math.abs(data[0].length * pixelWidth));
18+
var height = Math.floor(Math.abs(data.length * pixelHeight));
19+
20+
if (!canvas)
21+
{
22+
canvas = CanvasPool.create2D(this, width, height);
23+
resizeCanvas = false;
24+
clearCanvas = false;
25+
}
26+
27+
if (resizeCanvas)
28+
{
29+
canvas.width = width;
30+
canvas.height = height;
31+
}
32+
33+
var ctx = canvas.getContext('2d');
34+
35+
if (clearCanvas)
36+
{
37+
ctx.clearRect(0, 0, width, height);
38+
}
39+
40+
// preRender Callback?
41+
if (preRender)
42+
{
43+
preRender(canvas, ctx);
44+
}
45+
46+
// Draw it
47+
for (var y = 0; y < data.length; y++)
48+
{
49+
var row = data[y];
50+
51+
for (var x = 0; x < row.length; x++)
52+
{
53+
var d = row[x];
54+
55+
if (d !== '.' && d !== ' ')
56+
{
57+
ctx.fillStyle = palette[d];
58+
ctx.fillRect(x * pixelWidth, y * pixelHeight, pixelWidth, pixelHeight);
59+
}
60+
}
61+
}
62+
63+
// postRender Callback?
64+
if (postRender)
65+
{
66+
postRender(canvas, ctx);
67+
}
68+
69+
return canvas;
70+
};
71+
72+
module.exports = GenerateTexture;

v3/src/create/Grid.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
import Canvas from 'canvas/Canvas.js';
3+
import GetContext from 'canvas/GetContext.js';
4+
5+
export default function Grid (
6+
{
7+
canvas = undefined,
8+
width = 256,
9+
height = width,
10+
cellWidth = 32,
11+
cellHeight = cellWidth,
12+
color1 = '#fff',
13+
color2 = '#000',
14+
drawLines = false,
15+
lineColor = '#ff0000',
16+
alternate = true,
17+
resizeCanvas = true,
18+
clear = true,
19+
preRender = undefined,
20+
postRender = undefined
21+
} = {}
22+
) {
23+
24+
if (!canvas)
25+
{
26+
canvas = Canvas(width, height);
27+
resizeCanvas = false;
28+
clear = false;
29+
}
30+
else
31+
{
32+
// They provided own canvas, so we use its dimensions
33+
if (!resizeCanvas)
34+
{
35+
width = canvas.width;
36+
height = canvas.height;
37+
}
38+
}
39+
40+
let ctx = GetContext(canvas);
41+
42+
if (resizeCanvas)
43+
{
44+
Resize(canvas, width, height);
45+
}
46+
47+
if (clear)
48+
{
49+
ctx.clearRect(0, 0, width, height);
50+
}
51+
52+
if (drawLines)
53+
{
54+
ctx.lineWidth = 1;
55+
ctx.strokeStyle = lineColor;
56+
}
57+
58+
// preRender Callback?
59+
if (preRender)
60+
{
61+
preRender(canvas, ctx);
62+
}
63+
64+
// Draw the grid cells first (the lines go on top)
65+
66+
let cx = Math.ceil(width / cellWidth);
67+
let cy = Math.ceil(height / cellHeight);
68+
let c = 0;
69+
let color = color1;
70+
71+
for (let y = 0; y < cy; y++)
72+
{
73+
for (let x = 0; x < cx; x++)
74+
{
75+
if (c === 0)
76+
{
77+
color = color1;
78+
c = 1;
79+
}
80+
else
81+
{
82+
color = color2;
83+
c = 0;
84+
}
85+
86+
if (color)
87+
{
88+
ctx.fillStyle = color;
89+
ctx.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight);
90+
}
91+
92+
if (drawLines)
93+
{
94+
// +- 0.5 because we're using stroke, and will get anti-aliased line strokes without
95+
let ox = 0.5;
96+
let oy = 0.5;
97+
98+
if (x === cx - 1)
99+
{
100+
ox = -0.5;
101+
}
102+
103+
if (y === cy - 1)
104+
{
105+
oy = -0.5;
106+
}
107+
108+
ctx.strokeRect((x * cellWidth) + ox, (y * cellHeight) + oy, cellWidth, cellHeight);
109+
}
110+
}
111+
112+
if (alternate)
113+
{
114+
if (c === 0)
115+
{
116+
c = 1;
117+
}
118+
else
119+
{
120+
c = 0;
121+
}
122+
}
123+
124+
}
125+
126+
// postRender Callback?
127+
if (postRender)
128+
{
129+
postRender(canvas, ctx);
130+
}
131+
132+
return canvas;
133+
134+
}
135+
*/

v3/src/create/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Phaser.Create
2+
3+
module.exports = {
4+
5+
GenerateTexture: require('./GenerateTexture'),
6+
Grid: require('./Grid'),
7+
Palettes: require('./palettes')
8+
9+
};

v3/src/create/palettes/Arne16.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* A 16 color palette by [Arne](http://androidarts.com/palette/16pal.htm)
3+
* @constant
4+
* @type {Array}
5+
*/
6+
7+
module.exports = {
8+
0: '#000',
9+
1: '#9D9D9D',
10+
2: '#FFF',
11+
3: '#BE2633',
12+
4: '#E06F8B',
13+
5: '#493C2B',
14+
6: '#A46422',
15+
7: '#EB8931',
16+
8: '#F7E26B',
17+
9: '#2F484E',
18+
A: '#44891A',
19+
B: '#A3CE27',
20+
C: '#1B2632',
21+
D: '#005784',
22+
E: '#31A2F2',
23+
F: '#B2DCEF'
24+
};

v3/src/create/palettes/C64.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* A 16 color C64 inspired palette.
3+
* @constant
4+
* @type {Array}
5+
*/
6+
7+
module.exports = {
8+
0: '#000',
9+
1: '#fff',
10+
2: '#8b4131',
11+
3: '#7bbdc5',
12+
4: '#8b41ac',
13+
5: '#6aac41',
14+
6: '#3931a4',
15+
7: '#d5de73',
16+
8: '#945a20',
17+
9: '#5a4100',
18+
A: '#bd736a',
19+
B: '#525252',
20+
C: '#838383',
21+
D: '#acee8b',
22+
E: '#7b73de',
23+
F: '#acacac'
24+
};

v3/src/create/palettes/CGA.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* A 16 color CGA inspired palette by [Arne](http://androidarts.com/palette/16pal.htm)
3+
* @constant
4+
* @type {Array}
5+
*/
6+
7+
module.exports = {
8+
0: '#000',
9+
1: '#2234d1',
10+
2: '#0c7e45',
11+
3: '#44aacc',
12+
4: '#8a3622',
13+
5: '#5c2e78',
14+
6: '#aa5c3d',
15+
7: '#b5b5b5',
16+
8: '#5e606e',
17+
9: '#4c81fb',
18+
A: '#6cd947',
19+
B: '#7be2f9',
20+
C: '#eb8a60',
21+
D: '#e23d69',
22+
E: '#ffd93f',
23+
F: '#fff'
24+
};

v3/src/create/palettes/JMP.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* A 16 color JMP palette by [Arne](http://androidarts.com/palette/16pal.htm)
3+
* @constant
4+
* @type {Array}
5+
*/
6+
7+
module.exports = {
8+
0: '#000',
9+
1: '#191028',
10+
2: '#46af45',
11+
3: '#a1d685',
12+
4: '#453e78',
13+
5: '#7664fe',
14+
6: '#833129',
15+
7: '#9ec2e8',
16+
8: '#dc534b',
17+
9: '#e18d79',
18+
A: '#d6b97b',
19+
B: '#e9d8a1',
20+
C: '#216c4b',
21+
D: '#d365c8',
22+
E: '#afaab9',
23+
F: '#f5f4eb'
24+
};

v3/src/create/palettes/MSX.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* A 16 color palette inspired by Japanese computers like the MSX.
3+
* @constant
4+
* @type {Array}
5+
*/
6+
7+
module.exports = {
8+
0: '#000',
9+
1: '#191028',
10+
2: '#46af45',
11+
3: '#a1d685',
12+
4: '#453e78',
13+
5: '#7664fe',
14+
6: '#833129',
15+
7: '#9ec2e8',
16+
8: '#dc534b',
17+
9: '#e18d79',
18+
A: '#d6b97b',
19+
B: '#e9d8a1',
20+
C: '#216c4b',
21+
D: '#d365c8',
22+
E: '#afaab9',
23+
F: '#fff'
24+
};

v3/src/create/palettes/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
3+
ARNE16: require('./Arne16'),
4+
C64: require('./C64'),
5+
CGA: require('./CGA'),
6+
JMP: require('./JMP'),
7+
MSX: require('./MSX')
8+
9+
};

0 commit comments

Comments
 (0)