Skip to content

Commit 559d75e

Browse files
committed
When Phaser loads images they are now added to the PIXI.BaseTextureCache. Also when it loads atlas data (regardless of the 3 formats) they are converted into PIXI TextureCache entries using UUIDs to avoid name clashes and to support index based atlases.
1 parent 4c1dacf commit 559d75e

5 files changed

Lines changed: 123 additions & 35 deletions

File tree

examples/stage 2.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,33 @@ function preload() {
2222

2323
function create() {
2424

25-
var base = new PIXI.BaseTexture(game.cache.getImage('monsters'));
26-
var texture = new PIXI.Texture(base);
27-
bunny = new PIXI.Sprite(texture);
25+
//var texture = PIXI.TextureCache['skully.png'];
26+
27+
// PIXI.BaseTextureCache['monsters'] = new PIXI.BaseTexture(game.cache.getImage('monsters'));
28+
29+
// every image loaded should go into the BaseTextureCache, unique by URL (or maybe key)
30+
// var base = new PIXI.BaseTexture(game.cache.getImage('monsters'));
31+
32+
// Every FRAME needs a PIXI.Texture, related to the base (the source image) and the frame data
33+
// var texture = new PIXI.Texture(base, { x: 0, y: 0, width: 100, height: 100 });
34+
// var texture2 = new PIXI.Texture(base, { x: 0, y: 0, width: 100, height: 300 });
35+
36+
// PIXI.Sprite.fromFrame(key) pulls the texture from the TextureCache and returns a new Sprite made from it...
37+
// var texture = PIXI.TextureCache[frameId];
38+
39+
40+
// console.log(PIXI.TextureCache);
41+
42+
// console.log(game.cache.getFrameData('monsters'));
43+
44+
var frameData = game.cache.getFrameData('monsters');
45+
46+
// bunny = new PIXI.Sprite(texture2);
47+
48+
// bunny = PIXI.Sprite.fromFrame(frameData.getFrame(0).uuid);
49+
bunny = PIXI.Sprite.fromFrame(frameData.getFrameByName('skully.png').uuid);
50+
51+
// bunny = PIXI.Sprite.fromImage('monsters');
2852

2953
bunny.anchor.x = 0.5;
3054
bunny.anchor.y = 0.5;

src/animation/Frame.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
* @copyright 2013 Photon Storm Ltd.
99
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
1010
*/
11-
Phaser.Animation.Frame = function (x, y, width, height, name) {
11+
Phaser.Animation.Frame = function (x, y, width, height, name, uuid) {
1212

1313
this.x = x;
1414
this.y = y;
1515
this.width = width;
1616
this.height = height;
1717
this.name = name;
18+
this.uuid = uuid;
1819

1920
};
2021

2122
Phaser.Animation.Frame.prototype = {
2223

24+
/**
25+
* A link to the PIXI.TextureCache entry
26+
*/
27+
uuid: '',
28+
2329
/**
2430
* X position within the image to cut from.
2531
* @type {number}

src/animation/Parser.js

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Phaser.Animation.Parser = {
6969
* @param json {object} Json data you want to parse.
7070
* @return {FrameData} Generated FrameData object.
7171
*/
72-
JSONData: function (game, json) {
72+
JSONData: function (game, json, cacheKey) {
7373

7474
// Malformed?
7575
if (!json['frames']) {
@@ -84,25 +84,41 @@ Phaser.Animation.Parser = {
8484
var frames = json['frames'];
8585
var newFrame;
8686

87-
for (var i = 0; i < frames.length; i++) {
87+
for (var i = 0; i < frames.length; i++)
88+
{
89+
var uuid = game.rnd.uuid();
8890

8991
newFrame = data.addFrame(new Phaser.Animation.Frame(
9092
frames[i].frame.x,
9193
frames[i].frame.y,
9294
frames[i].frame.w,
9395
frames[i].frame.h,
94-
frames[i].filename
96+
frames[i].filename,
97+
uuid
9598
));
9699

97-
newFrame.setTrim(
98-
frames[i].trimmed,
99-
frames[i].sourceSize.w,
100-
frames[i].sourceSize.h,
101-
frames[i].spriteSourceSize.x,
102-
frames[i].spriteSourceSize.y,
103-
frames[i].spriteSourceSize.w,
104-
frames[i].spriteSourceSize.h
105-
);
100+
PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[cacheKey], {
101+
x: frames[key].frame.x,
102+
y: frames[key].frame.y,
103+
width: frames[key].frame.w,
104+
height: frames[key].frame.h
105+
});
106+
107+
if (frames[i].trimmed)
108+
{
109+
newFrame.setTrim(
110+
frames[i].trimmed,
111+
frames[i].sourceSize.w,
112+
frames[i].sourceSize.h,
113+
frames[i].spriteSourceSize.x,
114+
frames[i].spriteSourceSize.y,
115+
frames[i].spriteSourceSize.w,
116+
frames[i].spriteSourceSize.h
117+
);
118+
119+
PIXI.TextureCache[uuid].realSize = frames[key].spriteSourceSize;
120+
PIXI.TextureCache[uuid].trim.x = 0;
121+
}
106122
}
107123

108124
return data;
@@ -114,7 +130,7 @@ Phaser.Animation.Parser = {
114130
* @param json {object} Json data you want to parse.
115131
* @return {FrameData} Generated FrameData object.
116132
*/
117-
JSONDataHash: function (game, json) {
133+
JSONDataHash: function (game, json, cacheKey) {
118134

119135
// Malformed?
120136
if (!json['frames']) {
@@ -129,25 +145,41 @@ Phaser.Animation.Parser = {
129145
var frames = json['frames'];
130146
var newFrame;
131147

132-
for (var key in frames) {
148+
for (var key in frames)
149+
{
150+
var uuid = game.rnd.uuid();
133151

134152
newFrame = data.addFrame(new Phaser.Animation.Frame(
135153
frames[key].frame.x,
136154
frames[key].frame.y,
137155
frames[key].frame.w,
138156
frames[key].frame.h,
139-
key
157+
key,
158+
uuid
140159
));
141160

142-
newFrame.setTrim(
143-
frames[key].trimmed,
144-
frames[key].sourceSize.w,
145-
frames[key].sourceSize.h,
146-
frames[key].spriteSourceSize.x,
147-
frames[key].spriteSourceSize.y,
148-
frames[key].spriteSourceSize.w,
149-
frames[key].spriteSourceSize.h
150-
);
161+
PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[cacheKey], {
162+
x: frames[key].frame.x,
163+
y: frames[key].frame.y,
164+
width: frames[key].frame.w,
165+
height: frames[key].frame.h
166+
});
167+
168+
if (frames[key].trimmed)
169+
{
170+
newFrame.setTrim(
171+
frames[key].trimmed,
172+
frames[key].sourceSize.w,
173+
frames[key].sourceSize.h,
174+
frames[key].spriteSourceSize.x,
175+
frames[key].spriteSourceSize.y,
176+
frames[key].spriteSourceSize.w,
177+
frames[key].spriteSourceSize.h
178+
);
179+
180+
PIXI.TextureCache[uuid].realSize = frames[key].spriteSourceSize;
181+
PIXI.TextureCache[uuid].trim.x = 0;
182+
}
151183
}
152184

153185
return data;
@@ -159,7 +191,7 @@ Phaser.Animation.Parser = {
159191
* @param xml {object} XML data you want to parse.
160192
* @return {FrameData} Generated FrameData object.
161193
*/
162-
XMLData: function (game, xml, format) {
194+
XMLData: function (game, xml, cacheKey) {
163195

164196
// Malformed?
165197
if (!xml.getElementsByTagName('TextureAtlas')) {
@@ -171,7 +203,9 @@ Phaser.Animation.Parser = {
171203
var frames = xml.getElementsByTagName('SubTexture');
172204
var newFrame;
173205

174-
for (var i = 0; i < frames.length; i++) {
206+
for (var i = 0; i < frames.length; i++)
207+
{
208+
var uuid = game.rnd.uuid();
175209

176210
var frame = frames[i].attributes;
177211

@@ -180,9 +214,17 @@ Phaser.Animation.Parser = {
180214
frame.y.nodeValue,
181215
frame.width.nodeValue,
182216
frame.height.nodeValue,
183-
frame.name.nodeValue
217+
frame.name.nodeValue,
218+
uuid
184219
));
185220

221+
PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[cacheKey], {
222+
x: frame.x.nodeValue,
223+
y: frame.y.nodeValue,
224+
width: frame.width.nodeValue,
225+
height: frame.height.nodeValue
226+
});
227+
186228
// Trimmed?
187229
if (frame.frameX.nodeValue != '-0' || frame.frameY.nodeValue != '-0') {
188230
newFrame.setTrim(
@@ -194,6 +236,16 @@ Phaser.Animation.Parser = {
194236
frame.frameWidth.nodeValue,
195237
frame.frameHeight.nodeValue
196238
);
239+
240+
PIXI.TextureCache[uuid].realSize = {
241+
x: Math.abs(frame.frameX.nodeValue),
242+
y: Math.abs(frame.frameY.nodeValue),
243+
w: frame.frameWidth.nodeValue,
244+
h: frame.frameHeight.nodeValue
245+
};
246+
247+
PIXI.TextureCache[uuid].trim.x = 0;
248+
197249
}
198250
}
199251

src/core/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ Phaser.Game.prototype = {
258258

259259
this.device = new Phaser.Device();
260260
this.math = Phaser.Math;
261+
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
261262

262263
this.setUpRenderer();
263264

@@ -270,7 +271,6 @@ Phaser.Game.prototype = {
270271
this.tweens = new Phaser.TweenManager(this);
271272
// this.input = new Phaser.InputManager(this);
272273
// this.sound = new Phaser.SoundManager(this);
273-
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
274274
// this.physics = new Phaser.Physics.PhysicsManager(this);
275275
this.plugins = new Phaser.PluginManager(this, this);
276276
this.net = new Phaser.Net(this);

src/loader/Cache.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Phaser.Cache.prototype = {
7171
this._images[key] = { url: url, data: data, spriteSheet: true, frameWidth: frameWidth, frameHeight: frameHeight };
7272
this._images[key].frameData = Phaser.Animation.Parser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax);
7373

74+
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
75+
7476
},
7577

7678
/**
@@ -84,17 +86,19 @@ Phaser.Cache.prototype = {
8486

8587
this._images[key] = { url: url, data: data, spriteSheet: true };
8688

89+
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
90+
8791
if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY)
8892
{
89-
this._images[key].frameData = Phaser.Animation.Parser.JSONData(this.game, atlasData);
93+
this._images[key].frameData = Phaser.Animation.Parser.JSONData(this.game, atlasData, key);
9094
}
9195
else if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH)
9296
{
93-
this._images[key].frameData = Phaser.Animation.Parser.JSONDataHash(this.game, atlasData);
97+
this._images[key].frameData = Phaser.Animation.Parser.JSONDataHash(this.game, atlasData, key);
9498
}
9599
else if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
96100
{
97-
this._images[key].frameData = Phaser.Animation.Parser.XMLData(this.game, atlasData, format);
101+
this._images[key].frameData = Phaser.Animation.Parser.XMLData(this.game, atlasData, key);
98102
}
99103

100104
},
@@ -109,6 +113,8 @@ Phaser.Cache.prototype = {
109113

110114
this._images[key] = { url: url, data: data, spriteSheet: false };
111115

116+
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
117+
112118
},
113119

114120
/**

0 commit comments

Comments
 (0)