Files
cocos4/CCBoot.js
T
2015-12-09 14:46:42 +08:00

339 lines
9.5 KiB
JavaScript

/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2015 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var cc = cc || {};
cc._tmp = cc._tmp || {};
cc._LogInfos = cc._LogInfos || {};
var _p = window;
/** @expose */
_p.gl;
/** @expose */
_p.WebGLRenderingContext;
/** @expose */
_p.DeviceOrientationEvent;
/** @expose */
_p.DeviceMotionEvent;
/** @expose */
_p.AudioContext;
if (!_p.AudioContext) {
/** @expose */
_p.webkitAudioContext;
}
/** @expose */
_p.mozAudioContext;
_p = Object.prototype;
/** @expose */
_p._super;
/** @expose */
_p.ctor;
_p = null;
/**
* Device oriented vertically, home button on the bottom
* @constant
* @type {Number}
*/
cc.ORIENTATION_PORTRAIT = 0;
/**
* Device oriented vertically, home button on the top
* @constant
* @type {Number}
*/
cc.ORIENTATION_PORTRAIT_UPSIDE_DOWN = 1;
/**
* Device oriented horizontally, home button on the right
* @constant
* @type {Number}
*/
cc.ORIENTATION_LANDSCAPE_LEFT = 2;
/**
* Device oriented horizontally, home button on the left
* @constant
* @type {Number}
*/
cc.ORIENTATION_LANDSCAPE_RIGHT = 3;
/**
* drawing primitive of game engine
* @type {cc.DrawingPrimitive}
*/
cc._drawingUtil = null;
/**
* main Canvas 2D/3D Context of game engine
* @type {CanvasRenderingContext2D|WebGLRenderingContext}
*/
cc._renderContext = null;
cc._supportRender = false;
/**
* Main canvas of game engine
* @type {HTMLCanvasElement}
*/
cc._canvas = null;
/**
* The element contains the game canvas
* @type {HTMLDivElement}
*/
cc.container = null;
cc._gameDiv = null;
cc.isEditor = typeof Editor !== 'undefined';
/**
* Iterate over an object or an array, executing a function for each matched element.
* @param {object|array} obj
* @param {function} iterator
* @param {object} [context]
*/
cc.each = function (obj, iterator, context) {
if (!obj)
return;
if (obj instanceof Array) {
for (var i = 0, li = obj.length; i < li; i++) {
if (iterator.call(context, obj[i], i) === false)
return;
}
} else {
for (var key in obj) {
if (iterator.call(context, obj[key], key) === false)
return;
}
}
};
/**
* Check the url whether cross origin
* @param {String} url
* @returns {boolean}
*/
cc.isCrossOrigin = function (url) {
if (!url) {
cc.log("invalid URL");
return false;
}
var startIndex = url.indexOf("://");
if (startIndex === -1)
return false;
var endIndex = url.indexOf("/", startIndex + 3);
var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex);
return urlOrigin !== location.origin;
};
/**
* A string tool to construct a string with format string.
* for example:
* cc.formatStr("a: %d, b: %b", a, b);
* cc.formatStr(a, b, c);
* @returns {String}
*/
cc.formatStr = function(){
var args = arguments;
var l = args.length;
if(l < 1)
return "";
var str = args[0];
var needToFormat = true;
if(typeof str === "object"){
needToFormat = false;
}
for(var i = 1; i < l; ++i){
var arg = args[i];
if(needToFormat){
while(true){
var result = null;
if(typeof arg === "number"){
result = str.match(/(%d)|(%s)/);
if(result){
str = str.replace(/(%d)|(%s)/, arg);
break;
}
}
result = str.match(/%s/);
if(result)
str = str.replace(/%s/, arg);
else
str += " " + arg;
break;
}
}else
str += " " + arg;
}
return str;
};
require('../cocos2d/core/utils');
require('../cocos2d/core/platform/CCLoader');
require('../cocos2d/core/platform/CCSys');
require('../cocos2d/core/CCGame');
//+++++++++++++++++++++++++Engine initialization function begin+++++++++++++++++++++++++++
(function () {
var _jsAddedCache = {}, //cache for js and module that has added into jsList to be loaded.
_engineInitCalled = false,
_engineLoadedCallback = null;
cc._engineLoaded = false;
function _determineRenderType(config) {
var CONFIG_KEY = cc.game.CONFIG_KEY,
userRenderMode = parseInt(config[CONFIG_KEY.renderMode]) || 0,
shieldOs = [cc.sys.OS_ANDROID],
shieldBrowser = [];
// Adjust RenderType
if (isNaN(userRenderMode) || userRenderMode > 2 || userRenderMode < 0)
config[CONFIG_KEY.renderMode] = 0;
// Determine RenderType
cc._renderType = cc.game.RENDER_TYPE_CANVAS;
cc._supportRender = true;
if ( userRenderMode === 2 ||
( userRenderMode === 0 &&
shieldOs.indexOf(cc.sys.os) === -1 &&
shieldBrowser.indexOf(cc.sys.browserType) === -1 )) {
if (cc.sys.capabilities["opengl"]) {
cc._renderType = cc.game.RENDER_TYPE_WEBGL;
cc._supportRender = true;
}
else {
cc._supportRender = false;
}
}
if (userRenderMode === 1
|| (userRenderMode === 0 && !cc._supportRender)) {
if (cc.sys.capabilities["canvas"]) {
cc._renderType = cc.game.RENDER_TYPE_CANVAS;
cc._supportRender = true;
}
else {
cc._supportRender = false;
}
}
}
function _getJsListOfModule(moduleMap, moduleName, dir) {
if (_jsAddedCache[moduleName]) return null;
dir = dir || "";
var jsList = [];
var tempList = moduleMap[moduleName];
if (!tempList) throw new Error("can not find module [" + moduleName + "]");
var ccPath = cc.path;
for (var i = 0, li = tempList.length; i < li; i++) {
var item = tempList[i];
if (_jsAddedCache[item]) continue;
var extname = ccPath.extname(item);
if (!extname) {
var arr = _getJsListOfModule(moduleMap, item, dir);
if (arr) jsList = jsList.concat(arr);
} else if (extname.toLowerCase() === ".js") jsList.push(ccPath.join(dir, item));
_jsAddedCache[item] = 1;
}
return jsList;
}
function _afterEngineLoaded(config) {
cc._initDebugSetting(config[cc.game.CONFIG_KEY.debugMode]);
cc._engineLoaded = true;
cc.log(cc.ENGINE_VERSION);
if (_engineLoadedCallback) _engineLoadedCallback();
}
function _load(config) {
var self = this;
var CONFIG_KEY = cc.game.CONFIG_KEY, engineDir = config[CONFIG_KEY.engineDir], loader = cc.loader;
if (cc._Class) {
// Single file loaded
_afterEngineLoaded(config);
} else {
// Load cocos modules
var ccModulesPath = cc.path.join(engineDir, "moduleConfig.json");
loader.loadJson(ccModulesPath, function (err, modulesJson) {
if (err) throw new Error(err);
var modules = config["modules"] || [];
var moduleMap = modulesJson["module"];
var jsList = [];
if (cc.sys.capabilities["opengl"] && modules.indexOf("base4webgl") < 0) modules.splice(0, 0, "base4webgl");
else if (modules.indexOf("core") < 0) modules.splice(0, 0, "core");
for (var i = 0, li = modules.length; i < li; i++) {
var arr = _getJsListOfModule(moduleMap, modules[i], engineDir);
if (arr) jsList = jsList.concat(arr);
}
cc.loader.loadJsWithImg(jsList, function (err) {
if (err) throw err;
_afterEngineLoaded(config);
});
});
}
}
function _windowLoaded() {
window.removeEventListener('load', _windowLoaded, false);
_load(cc.game.config);
}
cc.initEngine = function (config, cb) {
if (_engineInitCalled) {
var previousCallback = _engineLoadedCallback;
_engineLoadedCallback = function () {
previousCallback && previousCallback();
cb && cb();
}
return;
}
_engineLoadedCallback = cb;
// Config uninitialized and given, initialize with it
if (!cc.game.config && config) {
cc.game.config = config;
}
// No config given and no config set before, load it
else if (!cc.game.config) {
cc.game._loadConfig();
}
config = cc.game.config;
_determineRenderType(config);
document.body ? _load(config) : window.addEventListener('load', _windowLoaded, false);
_engineInitCalled = true;
}
})();
//+++++++++++++++++++++++++Engine initialization function end+++++++++++++++++++++++++++++