This commit is contained in:
Felipe Alfonso 2017-02-07 12:47:39 -03:00
commit b8524200c3
14 changed files with 294 additions and 63 deletions

View file

@ -60,6 +60,10 @@ var Config = function (config)
// Callbacks
this.preBoot = GetObjectValue(config, 'callbacks.preBoot', NOOP);
this.postBoot = GetObjectValue(config, 'callbacks.postBoot', NOOP);
// Default / Missing Images
this.defaultImage = GetObjectValue(config, 'images.default', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQMAAABJtOi3AAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAABVJREFUeF7NwIEAAAAAgKD9qdeocAMAoAABm3DkcAAAAABJRU5ErkJggg==');
this.missingImage = GetObjectValue(config, 'images.missing', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg==');
};
Config.prototype.constructor = Config;

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: 'b209de10-eaff-11e6-83d3-cdfcfc553d04'
build: '42e0cd50-ed34-11e6-9a71-679050a34535'
};
module.exports = CHECKSUM;

View file

@ -41,7 +41,7 @@ var Blitter = function (state, x, y, key, frame)
Blitter.prototype = Object.create(GameObject.prototype);
Blitter.prototype.constructor = Blitter;
// Blitter.prototype.renderCanvas = require('./BlitterCanvasRenderer');
Blitter.prototype.renderCanvas = require('./BlitterCanvasRenderer');
Blitter.prototype.renderWebGL = require('./BlitterWebGLRenderer');
// frame MUST be part of the Blitter texture

View file

@ -0,0 +1,33 @@
var BlitterCanvasRenderer = function (renderer, src, interpolationPercentage)
{
var worldAlpha = src.color.worldAlpha;
var len = src.children.list.length;
// Skip rendering?
if (src.skipRender || !src.visible || worldAlpha === 0 || len === 0)
{
return;
}
renderer.resetTransform();
renderer.setBlendMode(src.blendMode);
renderer.setAlpha(worldAlpha);
// Render bobs
for (var i = 0; i < len; i++)
{
var bob = src.children.list[i];
var frame = bob.frame;
// if (!bob.visible)
// {
// continue;
// }
renderer.blitImage(bob.x, bob.y, frame);
}
};
module.exports = BlitterCanvasRenderer;

View file

@ -12,7 +12,6 @@ var BlitterWebGLRenderer = function (renderer, src, interpolationPercentage)
}
// Render bobs
//for (var i = len; i >= 0; i--)
for (var i = 0; i <= len; ++i)
{
var bob = src.children.list[i];

View file

@ -271,6 +271,9 @@ BaseLoader.prototype = {
this.queue.clear();
this.storage.clear();
this.events.removeAll('LOADER_START_EVENT');
this.events.removeAll('LOADER_COMPLETE_EVENT');
this.tag = '';
this.path = '';
this.baseURL = '';

View file

@ -26,6 +26,8 @@ var Phaser = {
},
State: require('./state/State'),
Loader: {
ImageFile: require('./loader/filetypes/ImageFile')

View file

@ -1,5 +1,6 @@
var CONST = require('../../const');
var DrawImage = require('./utils/DrawImage');
var BlitImage = require('./utils/BlitImage');
var GetBlendModes = require('./utils/GetBlendModes');
var CanvasRenderer = function (game)
@ -43,6 +44,7 @@ var CanvasRenderer = function (game)
// Map to the required function
this.drawImage = DrawImage;
this.blitImage = BlitImage;
this.blendModes = GetBlendModes();
@ -125,6 +127,29 @@ CanvasRenderer.prototype = {
ctx.fillRect(0, 0, this.width, this.height);
},
resetTransform: function ()
{
this.context.setTransform(1, 0, 0, 1, 0, 0);
},
setBlendMode: function (blendMode)
{
if (this.currentBlendMode !== blendMode)
{
this.context.globalCompositeOperation = blendMode;
this.currentBlendMode = blendMode;
}
},
setAlpha: function (alpha)
{
if (this.currentAlpha !== alpha)
{
this.context.globalAlpha = alpha;
this.currentAlpha = alpha;
}
},
/**
* Renders the State.
*

View file

@ -0,0 +1,11 @@
// No scaling, anchor, rotation or effects, literally draws the frame directly to the canvas
var BlitImage = function (dx, dy, frame)
{
var ctx = this.context;
var cd = frame.canvasData;
ctx.drawImage(frame.source.image, cd.sx, cd.sy, cd.sWidth, cd.sHeight, dx, dy, cd.dWidth, cd.dHeight);
};
module.exports = BlitImage;

View file

@ -2,45 +2,66 @@ var CONST = require('./const');
var ScaleModes = require('../renderer/ScaleModes');
var GetObjectValue = require('../utils/object/GetObjectValue');
var Settings = function (config, gameConfig)
{
if (typeof config === 'string')
var Settings = {
create: function (config)
{
config = { key: config };
}
else if (config === undefined)
if (typeof config === 'string')
{
config = { key: config };
}
else if (config === undefined)
{
// Pass the 'hasOwnProperty' checks
config = {};
}
return {
status: CONST.PENDING,
op: CONST.BOOT,
key: GetObjectValue(config, 'key', ''),
active: GetObjectValue(config, 'active', false),
visible: GetObjectValue(config, 'visible', true),
scaleMode: GetObjectValue(config, 'scaleMode', ScaleModes.DEFAULT),
// -1 means the State Manager will set it to be the Game dimensions
x: GetObjectValue(config, 'x', 0),
y: GetObjectValue(config, 'y', 0),
width: GetObjectValue(config, 'width', -1),
height: GetObjectValue(config, 'height', -1),
// Renderer Settings
clearBeforeRender: GetObjectValue(config, 'clearBeforeRender', true),
transparent: GetObjectValue(config, 'transparent', false),
autoResize: GetObjectValue(config, 'autoResize', false),
roundPixels: GetObjectValue(config, 'roundPixels', false),
drawToPrimaryCanvas: GetObjectValue(config, 'drawToPrimaryCanvas', false),
// Loader payload array
files: GetObjectValue(config, 'files', false)
};
},
init: function (config, gameConfig)
{
// Pass the 'hasOwnProperty' checks
config = {};
if (config.width === -1)
{
config.width = gameConfig.width;
}
if (config.height === -1)
{
config.height = gameConfig.height;
}
}
return {
status: CONST.PENDING,
op: CONST.BOOT,
key: GetObjectValue(config, 'key', ''),
active: GetObjectValue(config, 'active', false),
visible: GetObjectValue(config, 'visible', true),
scaleMode: GetObjectValue(config, 'scaleMode', ScaleModes.DEFAULT),
// -1 means the State Manager will set it to be the Game dimensions
x: GetObjectValue(config, 'x', 0),
y: GetObjectValue(config, 'y', 0),
width: GetObjectValue(config, 'width', gameConfig.width),
height: GetObjectValue(config, 'height', gameConfig.height),
// Renderer Settings
clearBeforeRender: GetObjectValue(config, 'clearBeforeRender', true),
transparent: GetObjectValue(config, 'transparent', false),
autoResize: GetObjectValue(config, 'autoResize', false),
roundPixels: GetObjectValue(config, 'roundPixels', false),
drawToPrimaryCanvas: GetObjectValue(config, 'drawToPrimaryCanvas', false)
};
};
module.exports = Settings;

View file

@ -137,6 +137,8 @@ StateManager.prototype = {
return;
}
// console.log('StateManager.add', key, stateConfig, autoStart);
key = this.getKey(key, stateConfig);
var newState;
@ -212,9 +214,6 @@ StateManager.prototype = {
}
else
{
newState.game = this.game;
newState.settings = new Settings(newState, key);
newState.sys = new Systems(newState);
newState.sys.init(this.game);
@ -222,7 +221,38 @@ StateManager.prototype = {
this.createStateDisplay(newState);
// Default required functions
return this.setupCallbacks(newState);
if (!newState.init)
{
newState.init = NOOP;
}
if (!newState.preload)
{
newState.preload = NOOP;
}
if (!newState.create)
{
newState.create = NOOP;
}
if (!newState.shutdown)
{
newState.shutdown = NOOP;
}
if (!newState.update)
{
newState.update = NOOP;
}
if (!newState.render)
{
newState.render = NOOP;
}
return newState;
}
},
@ -339,42 +369,83 @@ StateManager.prototype = {
state.settings.active = true;
// + arguments
if (state.init)
var loader = state.sys.load;
// Files payload?
if (loader && Array.isArray(state.sys.settings.files))
{
state.init.call(state);
}
loader.reset();
if (state.preload && state.sys.load)
{
state.sys.load.reset();
state.preload.call(state, this.game);
// Is the loader empty?
if (state.sys.load.list.size === 0)
if (loader.loadArray(state.sys.settings.files))
{
this.startCreate(state);
loader.events.once('LOADER_COMPLETE_EVENT', this.payloadComplete.bind(this));
loader.start();
}
else
{
// Start the loader going as we have something in the queue
state.sys.load.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
state.sys.load.start();
this.bootState(state);
}
}
else
{
// No preload? Then there was nothing to load either
this.bootState(state);
}
}
},
payloadComplete: function (event)
{
console.log('payloadComplete');
var state = event.loader.state;
this.bootState(state);
},
bootState: function (state)
{
console.log('bootState', state);
// + arguments
if (state.init)
{
state.init.call(state);
}
var loader = state.sys.load;
if (state.preload && loader)
{
loader.reset();
state.preload.call(state, this.game);
// Is the loader empty?
if (loader.list.size === 0)
{
this.startCreate(state);
}
else
{
// Start the loader going as we have something in the queue
loader.events.once('LOADER_COMPLETE_EVENT', this.loadComplete.bind(this));
loader.start();
}
}
else
{
// No preload? Then there was nothing to load either
this.startCreate(state);
}
},
loadComplete: function (event)
{
console.log('loadComplete');
var state = event.loader.state;
// Make sure to do load-update one last time before state is set to _created

View file

@ -23,7 +23,7 @@ var Systems = function (state, config)
this.config = config;
this.settings;
this.settings = Settings.create(config);
// CORE SYSTEMS / PROPERTIES
@ -58,7 +58,7 @@ Systems.prototype = {
this.game = game;
this.settings = Settings(this.config, this.game.config);
Settings.init(this.settings, this.game.config);
this.cache = this.game.cache;
this.textures = this.game.textures;

View file

@ -27,6 +27,50 @@ var Loader = function (state)
Loader.prototype = Object.create(BaseLoader.prototype);
Loader.prototype.constructor = Loader;
Loader.prototype.loadArray = function (files)
{
if (Array.isArray(files))
{
for (var i = 0; i < files.length; i++)
{
this.file(files[i]);
}
}
return (this.list.size > 0);
};
Loader.prototype.file = function (file)
{
var entry;
switch (file.type)
{
case 'image':
case 'json':
case 'xml':
case 'binary':
case 'text':
case 'glsl':
entry = this[file.type](file.key, file.url, file.xhrSettings);
break;
case 'spritesheet':
entry = this.spritesheet(file.key, file.url, file.config, file.xhrSettings);
break;
case 'atlas':
entry = this.atlas(file.key, file.textureURL, file.atlasURL, file.textureXhrSettings, file.atlasXhrSettings);
break;
case 'multiatlas':
entry = this.multiatlas(file.key, file.textureURLs, file.atlasURLs, file.textureXhrSettings, file.atlasXhrSettings);
break;
}
return entry;
};
Loader.prototype.image = function (key, url, xhrSettings)
{
var file = new ImageFile(key, url, this.path, xhrSettings);

View file

@ -23,12 +23,30 @@ var TextureManager = function (game)
this.game = game;
this.list = {};
this.addBase64('__DEFAULT', game.config.defaultImage);
this.addBase64('__MISSING', game.config.missingImage);
};
TextureManager.prototype.constructor = TextureManager;
TextureManager.prototype = {
addBase64: function (key, data)
{
var _this = this;
var image = new Image();
image.onload = function ()
{
var texture = _this.create(key, image);
Parser.Image(texture, 0);
};
image.src = data;
},
addImage: function (key, source)
{
var texture = this.create(key, source);