Skip to content

Commit 0f1e0a3

Browse files
committed
Updated the Device little / big endianess check.
1 parent b6cc150 commit 0f1e0a3

5 files changed

Lines changed: 133 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Version 2.0.4 - "Mos Shirare" - in development
7575
* All Game Objects have a new property: destroyPhase (boolean) which is true if the object is in the process of being destroyed, otherwise false.
7676
* If Tween.yoyo was true but repeat was 0 then it wouldn't yoyo. Now if yoyo is set, but not repeat, the repeat count gets set to 1 (thanks @hilts-vaughan, fix #744)
7777
* RandomDataGenerator.integerInRange uses a new method of rounding the value to an integer to avoid distribution probability issues (thanks PhaserFan)
78+
* Updated the Device little / big endianess check.
7879

7980

8081
### New Features

build/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ declare module Phaser {
11321132
add(object: any): void;
11331133
alphaMask(source: any, mask: any): void;
11341134
clear(): void;
1135+
cls(): void;
11351136
copyPixels(source: any, area: Phaser.Rectangle, destX: number, destY: number): void;
11361137
draw(source: any, destX: number, destY: number): void;
11371138
getPixel(x: number, y: number): number;

src/core/Game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
208208
this.particles = null;
209209

210210
/**
211-
* @property {Phaser.BitmapData} scratch - A handy BitmapData scratch-pad that can be used for off-screen bitmap manipulation.
211+
* @property {Phaser.BitmapData} scratch - A handy BitmapData scratch-pad that can be used for off-screen bitmap manipulation. Defaults to 1024 x 1024 in size and doesn't upload to the GPU.
212212
*/
213213
this.scratch = null;
214214

@@ -450,7 +450,7 @@ Phaser.Game.prototype = {
450450
this.plugins = new Phaser.PluginManager(this);
451451
this.net = new Phaser.Net(this);
452452
this.debug = new Phaser.Utils.Debug(this);
453-
this.scratch = new Phaser.BitmapData(this, '__root', this.width, this.height);
453+
this.scratch = new Phaser.BitmapData(this, '__root', 1024, 1024);
454454

455455
this.time.boot();
456456
this.stage.boot();

src/gameobjects/BitmapData.js

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ Phaser.BitmapData = function (game, key, width, height) {
106106
*/
107107
this.dirty = false;
108108

109+
/**
110+
* @property {function} cls - Alias for BitmapData.clear.
111+
*/
112+
this.cls = this.clear;
113+
109114
};
110115

111116
Phaser.BitmapData.prototype = {
@@ -183,6 +188,59 @@ Phaser.BitmapData.prototype = {
183188

184189
/**
185190
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
191+
*
192+
* @method Phaser.BitmapData#replaceRGB
193+
* @param {number} x - The X coordinate of the pixel to be set.
194+
* @param {number} y - The Y coordinate of the pixel to be set.
195+
* @param {number} red - The red color value, between 0 and 0xFF (255).
196+
* @param {number} green - The green color value, between 0 and 0xFF (255).
197+
* @param {number} blue - The blue color value, between 0 and 0xFF (255).
198+
* @param {number} alpha - The alpha color value, between 0 and 0xFF (255).
199+
*/
200+
replaceRGB: function (sourceR, sourceG, sourceB, sourceA, destR, destG, destB, destA, region) {
201+
202+
var x = 0;
203+
var y = 0;
204+
var w = this.width;
205+
var h = this.height;
206+
207+
if (region instanceof Phaser.Rectangle)
208+
{
209+
x = region.x;
210+
y = region.y;
211+
w = region.width;
212+
h = region.height;
213+
}
214+
215+
216+
217+
if (x >= 0 && x <= this.width && y >= 0 && y <= this.height)
218+
{
219+
this.pixels[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
220+
221+
/*
222+
if (this.isLittleEndian)
223+
{
224+
this.data32[y * this.width + x] = (alpha << 24) | (blue << 16) | (green << 8) | red;
225+
}
226+
else
227+
{
228+
this.data32[y * this.width + x] = (red << 24) | (green << 16) | (blue << 8) | alpha;
229+
}
230+
*/
231+
232+
// this.imageData.data.set(this.data8);
233+
234+
this.context.putImageData(this.imageData, 0, 0);
235+
236+
this.dirty = true;
237+
}
238+
239+
},
240+
241+
/**
242+
* Sets the color of the given pixel to the specified red, green, blue and alpha values.
243+
*
186244
* @method Phaser.BitmapData#setPixel32
187245
* @param {number} x - The X coordinate of the pixel to be set.
188246
* @param {number} y - The Y coordinate of the pixel to be set.
@@ -252,6 +310,7 @@ Phaser.BitmapData.prototype = {
252310
/**
253311
* Get the color of a specific pixel including its alpha value.
254312
*
313+
* @method Phaser.BitmapData#getPixel32
255314
* @param {number} x - The X coordinate of the pixel to get.
256315
* @param {number} y - The Y coordinate of the pixel to get.
257316
* @return {number} A native color value integer (format: 0xAARRGGBB)
@@ -268,6 +327,7 @@ Phaser.BitmapData.prototype = {
268327
/**
269328
* Gets all the pixels from the region specified by the given Rectangle object.
270329
*
330+
* @method Phaser.BitmapData#getPixels
271331
* @param {Phaser.Rectangle} rect - The Rectangle region to get.
272332
* @return {array} CanvasPixelArray.
273333
*/
@@ -280,6 +340,7 @@ Phaser.BitmapData.prototype = {
280340
/**
281341
* Copies the pixels from the source image to this BitmapData based on the given area and destination.
282342
*
343+
* @method Phaser.BitmapData#copyPixels
283344
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
284345
* @param {Phaser.Rectangle} area - The Rectangle region to copy from the source image.
285346
* @param {number} destX - The destination x coordinate to copy the image to.
@@ -302,11 +363,15 @@ Phaser.BitmapData.prototype = {
302363
/**
303364
* Draws the given image to this BitmapData at the coordinates specified. If you need to only draw a part of the image use BitmapData.copyPixels instead.
304365
*
305-
* @param {HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
306-
* @param {number} destX - The destination x coordinate to draw the image to.
307-
* @param {number} destY - The destination y coordinate to draw the image to.
366+
* @method Phaser.BitmapData#draw
367+
* @param {HTMLImage|string} source - The Image to draw. If you give a string it will try and find the Image in the Game.Cache.
368+
* @param {number} [x=0] - The x coordinate to draw the image to.
369+
* @param {number} [y=0] - The y coordinate to draw the image to.
308370
*/
309-
draw: function (source, destX, destY) {
371+
draw: function (source, x, y) {
372+
373+
if (typeof x === 'undefined') { x = 0; }
374+
if (typeof y === 'undefined') { y = 0; }
310375

311376
if (typeof source === 'string')
312377
{
@@ -315,11 +380,30 @@ Phaser.BitmapData.prototype = {
315380

316381
if (source)
317382
{
318-
this.context.drawImage(source, 0, 0, source.width, source.height, destX, destY, source.width, source.height);
383+
this.context.drawImage(source, 0, 0, source.width, source.height, x, y, source.width, source.height);
319384
}
320385

321386
},
322387

388+
/**
389+
* Draws the given image to this BitmapData at the coordinates specified. If you need to only draw a part of the image use BitmapData.copyPixels instead.
390+
*
391+
* @method Phaser.BitmapData#drawSprite
392+
* @param {Phaser.Sprite|Phaser.Image} sprite - The Sprite to draw. Must have a loaded texture and frame.
393+
* @param {number} [x=0] - The x coordinate to draw the Sprite to.
394+
* @param {number} [y=0] - The y coordinate to draw the Sprite to.
395+
*/
396+
drawSprite: function (sprite, x, y) {
397+
398+
if (typeof x === 'undefined') { x = 0; }
399+
if (typeof y === 'undefined') { y = 0; }
400+
401+
var frame = sprite.texture.frame;
402+
403+
this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height);
404+
405+
},
406+
323407
/**
324408
* Draws the given image onto this BitmapData using an image as an alpha mask.
325409
*

src/system/Device.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ Phaser.Device.prototype = {
663663
},
664664

665665
/**
666-
* Check PixelRatio of devices.
666+
* Check PixelRatio, iOS device, Vibration API, ArrayBuffers and endianess.
667667
* @method Phaser.Device#_checkDevice
668668
* @private
669669
*/
@@ -676,15 +676,18 @@ Phaser.Device.prototype = {
676676

677677
if (typeof Int8Array !== 'undefined')
678678
{
679-
this.littleEndian = new Int8Array(new Int16Array([1]).buffer)[0] > 0;
680679
this.typedArray = true;
681680
}
682681
else
683682
{
684-
this.littleEndian = false;
685683
this.typedArray = false;
686684
}
687685

686+
if (typeof ArrayBuffer !== 'undefined' && typeof Uint8Array !== 'undefined' && typeof Uint32Array !== 'undefined')
687+
{
688+
this.littleEndian = this._checkIsLittleEndian();
689+
}
690+
688691
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
689692

690693
if (navigator.vibrate)
@@ -694,6 +697,40 @@ Phaser.Device.prototype = {
694697

695698
},
696699

700+
/**
701+
* Check Little or Big Endian system.
702+
* @author Matt DesLauriers (@mattdesl)
703+
* @method Phaser.Device#_checkIsLittleEndian
704+
* @private
705+
*/
706+
_checkIsLittleEndian: function () {
707+
708+
var a = new ArrayBuffer(4);
709+
var b = new Uint8Array(a);
710+
var c = new Uint32Array(a);
711+
712+
b[0] = 0xa1;
713+
b[1] = 0xb2;
714+
b[2] = 0xc3;
715+
b[3] = 0xd4;
716+
717+
if (c[0] == 0xd4c3b2a1)
718+
{
719+
return true;
720+
}
721+
722+
if (c[0] == 0xa1b2c3d4)
723+
{
724+
return false;
725+
}
726+
else
727+
{
728+
// Could not determine endianness
729+
return null;
730+
}
731+
732+
},
733+
697734
/**
698735
* Check whether the host environment support 3D CSS.
699736
* @method Phaser.Device#_checkCSS3D

0 commit comments

Comments
 (0)