Skip to content

Commit 0dc8757

Browse files
committed
Updates.
1 parent 6a8e6a7 commit 0dc8757

1 file changed

Lines changed: 145 additions & 8 deletions

File tree

src/core/Create.js

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
*
99
* TODO: Gradient generator
10-
* TODO: Look at bsfxr for audio gen
10+
* TODO: Look at sfxr for audio gen
1111
* TODO: Dither support
1212
*
1313
* @class Phaser.Create
@@ -28,21 +28,27 @@ Phaser.Create = function (game) {
2828

2929
// http://androidarts.com/palette/16pal.htm
3030

31-
this.palettes = {
32-
'arne': { 0: '#000000', 1: '#9D9D9D', 2: '#FFFFFF', 3: '#BE2633', 4: '#E06F8B', 5: '#493C2B', 6: '#A46422', 7: '#EB8931', 8: '#F7E26B', 9: '#2F484E', A: '#44891A', B: '#A3CE27', C: '#1B2632', D: '#005784', E: '#31A2F2', F: '#B2DCEF' },
33-
'jmp': { 0: '#000000', 1: '#191028', 2: '#46af45', 3: '#a1d685', 4: '#453e78', 5: '#7664fe', 6: '#833129', 7: '#9ec2e8', 8: '#dc534b', 9: '#e18d79', A: '#d6b97b', B: '#e9d8a1', C: '#216c4b', D: '#d365c8', E: '#afaab9', F: '#f5f4eb' },
34-
'cga': { 0: '#000000', 1: '#2234d1', 2: '#0c7e45', 3: '#44aacc', 4: '#8a3622', 5: '#5c2e78', 6: '#aa5c3d', 7: '#b5b5b5', 8: '#5e606e', 9: '#4c81fb', A: '#6cd947', B: '#7be2f9', C: '#eb8a60', D: '#e23d69', E: '#ffd93f', F: '#fffff' }
35-
};
31+
this.palettes = [
32+
{ 0: '#000', 1: '#9D9D9D', 2: '#FFF', 3: '#BE2633', 4: '#E06F8B', 5: '#493C2B', 6: '#A46422', 7: '#EB8931', 8: '#F7E26B', 9: '#2F484E', A: '#44891A', B: '#A3CE27', C: '#1B2632', D: '#005784', E: '#31A2F2', F: '#B2DCEF' },
33+
{ 0: '#000', 1: '#191028', 2: '#46af45', 3: '#a1d685', 4: '#453e78', 5: '#7664fe', 6: '#833129', 7: '#9ec2e8', 8: '#dc534b', 9: '#e18d79', A: '#d6b97b', B: '#e9d8a1', C: '#216c4b', D: '#d365c8', E: '#afaab9', F: '#f5f4eb' },
34+
{ 0: '#000', 1: '#2234d1', 2: '#0c7e45', 3: '#44aacc', 4: '#8a3622', 5: '#5c2e78', 6: '#aa5c3d', 7: '#b5b5b5', 8: '#5e606e', 9: '#4c81fb', A: '#6cd947', B: '#7be2f9', C: '#eb8a60', D: '#e23d69', E: '#ffd93f', F: '#fff' },
35+
{ 0: '#000', 1: '#fff', 2: '#8b4131', 3: '#7bbdc5', 4: '#8b41ac', 5: '#6aac41', 6: '#3931a4', 7: '#d5de73', 8: '#945a20', 9: '#5a4100', A: '#bd736a', B: '#525252', C: '#838383', D: '#acee8b', E: '#7b73de', F: '#acacac' }
36+
];
3637

3738
};
3839

40+
Phaser.Create.PALETTE_ARNE = 0;
41+
Phaser.Create.PALETTE_JMP = 1;
42+
Phaser.Create.PALETTE_CGA = 2;
43+
Phaser.Create.PALETTE_C64 = 3;
44+
3945
Phaser.Create.prototype = {
4046

4147
texture: function (key, data, pixelWidth, pixelHeight, palette) {
4248

4349
if (typeof pixelWidth === 'undefined') { pixelWidth = 8; }
44-
if (typeof pixelHeight === 'undefined') { pixelHeight = 8; }
45-
if (typeof palette === 'undefined') { palette = 'arne'; }
50+
if (typeof pixelHeight === 'undefined') { pixelHeight = pixelWidth; }
51+
if (typeof palette === 'undefined') { palette = 0; }
4652

4753
var w = data[0].length * pixelWidth;
4854
var h = data.length * pixelHeight;
@@ -93,3 +99,134 @@ Phaser.Create.prototype = {
9399
};
94100

95101
Phaser.Create.prototype.constructor = Phaser.Create;
102+
103+
/*
104+
* RIFFWAVE.js v0.03 - Audio encoder for HTML5 <audio> elements.
105+
* Copyleft 2011 by Pedro Ladaria <pedro.ladaria at Gmail dot com>
106+
*
107+
* Public Domain
108+
*
109+
* Changelog:
110+
*
111+
* 0.01 - First release
112+
* 0.02 - New faster base64 encoding
113+
* 0.03 - Support for 16bit samples
114+
*
115+
* Notes:
116+
*
117+
* 8 bit data is unsigned: 0..255
118+
* 16 bit data is signed: −32,768..32,767
119+
*
120+
*/
121+
122+
var FastBase64 = {
123+
124+
chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
125+
encLookup: [],
126+
127+
Init: function() {
128+
for (var i=0; i<4096; i++) {
129+
this.encLookup[i] = this.chars[i >> 6] + this.chars[i & 0x3F];
130+
}
131+
},
132+
133+
Encode: function(src) {
134+
var len = src.length;
135+
var dst = '';
136+
var i = 0;
137+
while (len > 2) {
138+
n = (src[i] << 16) | (src[i+1]<<8) | src[i+2];
139+
dst+= this.encLookup[n >> 12] + this.encLookup[n & 0xFFF];
140+
len-= 3;
141+
i+= 3;
142+
}
143+
if (len > 0) {
144+
var n1= (src[i] & 0xFC) >> 2;
145+
var n2= (src[i] & 0x03) << 4;
146+
if (len > 1) n2 |= (src[++i] & 0xF0) >> 4;
147+
dst+= this.chars[n1];
148+
dst+= this.chars[n2];
149+
if (len == 2) {
150+
var n3= (src[i++] & 0x0F) << 2;
151+
n3 |= (src[i] & 0xC0) >> 6;
152+
dst+= this.chars[n3];
153+
}
154+
if (len == 1) dst+= '=';
155+
dst+= '=';
156+
}
157+
return dst;
158+
} // end Encode
159+
160+
}
161+
162+
FastBase64.Init();
163+
164+
var RIFFWAVE = function(data) {
165+
166+
this.data = []; // Array containing audio samples
167+
this.wav = []; // Array containing the generated wave file
168+
this.dataURI = ''; // http://en.wikipedia.org/wiki/Data_URI_scheme
169+
170+
this.header = { // OFFS SIZE NOTES
171+
chunkId : [0x52,0x49,0x46,0x46], // 0 4 "RIFF" = 0x52494646
172+
chunkSize : 0, // 4 4 36+SubChunk2Size = 4+(8+SubChunk1Size)+(8+SubChunk2Size)
173+
format : [0x57,0x41,0x56,0x45], // 8 4 "WAVE" = 0x57415645
174+
subChunk1Id : [0x66,0x6d,0x74,0x20], // 12 4 "fmt " = 0x666d7420
175+
subChunk1Size: 16, // 16 4 16 for PCM
176+
audioFormat : 1, // 20 2 PCM = 1
177+
numChannels : 1, // 22 2 Mono = 1, Stereo = 2...
178+
sampleRate : 8000, // 24 4 8000, 44100...
179+
byteRate : 0, // 28 4 SampleRate*NumChannels*BitsPerSample/8
180+
blockAlign : 0, // 32 2 NumChannels*BitsPerSample/8
181+
bitsPerSample: 8, // 34 2 8 bits = 8, 16 bits = 16
182+
subChunk2Id : [0x64,0x61,0x74,0x61], // 36 4 "data" = 0x64617461
183+
subChunk2Size: 0 // 40 4 data size = NumSamples*NumChannels*BitsPerSample/8
184+
};
185+
186+
function u32ToArray(i) {
187+
return [i&0xFF, (i>>8)&0xFF, (i>>16)&0xFF, (i>>24)&0xFF];
188+
}
189+
190+
function u16ToArray(i) {
191+
return [i&0xFF, (i>>8)&0xFF];
192+
}
193+
194+
function split16bitArray(data) {
195+
var r = [];
196+
var j = 0;
197+
var len = data.length;
198+
for (var i=0; i<len; i++) {
199+
r[j++] = data[i] & 0xFF;
200+
r[j++] = (data[i]>>8) & 0xFF;
201+
}
202+
return r;
203+
}
204+
205+
this.Make = function(data) {
206+
if (data instanceof Array) this.data = data;
207+
this.header.blockAlign = (this.header.numChannels * this.header.bitsPerSample) >> 3;
208+
this.header.byteRate = this.header.blockAlign * this.sampleRate;
209+
this.header.subChunk2Size = this.data.length * (this.header.bitsPerSample >> 3);
210+
this.header.chunkSize = 36 + this.header.subChunk2Size;
211+
212+
this.wav = this.header.chunkId.concat(
213+
u32ToArray(this.header.chunkSize),
214+
this.header.format,
215+
this.header.subChunk1Id,
216+
u32ToArray(this.header.subChunk1Size),
217+
u16ToArray(this.header.audioFormat),
218+
u16ToArray(this.header.numChannels),
219+
u32ToArray(this.header.sampleRate),
220+
u32ToArray(this.header.byteRate),
221+
u16ToArray(this.header.blockAlign),
222+
u16ToArray(this.header.bitsPerSample),
223+
this.header.subChunk2Id,
224+
u32ToArray(this.header.subChunk2Size),
225+
(this.header.bitsPerSample == 16) ? split16bitArray(this.data) : this.data
226+
);
227+
this.dataURI = 'data:audio/wav;base64,'+FastBase64.Encode(this.wav);
228+
};
229+
230+
if (data instanceof Array) this.Make(data);
231+
232+
}; // end RIFFWAVE

0 commit comments

Comments
 (0)