qqplay-adapter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated engine source code (the "Software"), a limited,
  6. worldwide, royalty-free, non-assignable, revocable and non-exclusive license
  7. to use Cocos Creator solely to develop games on your target platforms. You shall
  8. not use Cocos Creator software for developing other software or tools that's
  9. used for developing games. You are not granted to publish, distribute,
  10. sublicense, and/or sell copies of Cocos Creator.
  11. The software or tools in this License Agreement are licensed, not sold.
  12. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. // BK.Script.loadlib('GameRes://libs/qqPlayCore.js');
  22. var gl;
  23. var window = this;
  24. window.addEventListener = function () {};
  25. window.removeEventListener = function () {};
  26. var navigator = window.navigator = {
  27. userAgent: 'qqplay ' + GameStatusInfo.platform + ' QQ/' + GameStatusInfo.QQVer,
  28. appVersion: ''
  29. };
  30. BK.Script.loadlib('GameRes://libs/xmldom/dom-parser.js');
  31. BK.Script.loadlib('GameRes://src/assets/scripts/lib/Underscore.js');
  32. // import element node
  33. BK.Script.loadlib('GameRes://libs/element/Utils.js');
  34. BK.Script.loadlib('GameRes://libs/element/HTMLElemenet.js');
  35. BK.Script.loadlib('GameRes://libs/element/HTMLAudioElement.js');
  36. BK.Script.loadlib('GameRes://libs/element/HTMLImageElement.js');
  37. BK.Script.loadlib('GameRes://libs/element/HTMLScriptElement.js');
  38. BK.Script.loadlib('GameRes://libs/element/HTMLVideoElement.js');
  39. BK.Script.loadlib('GameRes://libs/element/Document.js');
  40. BK.Script.loadlib('GameRes://libs/element/Canvas.js');
  41. BK.Script.loadlib('GameRes://libs/element/XMLHttpRequest.js');
  42. window['XMLHttpRequest'] = XMLHttpRequest;
  43. HTMLElement = _HTMLBaseElemenet;
  44. Image = window.Image = HTMLImageElement;
  45. document = window.document = new HTMLDocumentElement();
  46. var canvas = new HTMLMainCanvasElement();
  47. canvas.id = 'GameCanvas';
  48. document.body.appendChild(canvas);
  49. var HTMLCanvasElement = BK.Canvas;
  50. var location = window.location = {
  51. href: "",
  52. };
  53. var console = window.console = {
  54. log: function (msg) {
  55. BK.Script.log(1, 0, msg);
  56. },
  57. warn: function (msg) {
  58. BK.Script.log(1, 0, msg);
  59. },
  60. error: function (msg) {
  61. BK.Script.log(1, 0, msg);
  62. },
  63. info: function(msg) {
  64. BK.Script.log(1, 0, msg);
  65. },
  66. debug: function(msg) {
  67. BK.Script.log(1, 0, msg);
  68. },
  69. };
  70. window["BK"] = BK;
  71. window["isQQPlay"] = true;
  72. alert = window.alert = console.warn;
  73. window["pageXOffset"] = 0;
  74. window["pageYOffset"] = 0;
  75. var WebGLRenderingContext = function () {};
  76. // 用于在加载引擎后对一些代码适配
  77. function initAdapter () {
  78. var sps = BK.Director.screenPixelSize;
  79. window.innerWidth = sps.width;
  80. window.innerHeight = sps.height;
  81. canvas.width = sps.width;
  82. canvas.height = sps.height;
  83. // adapt _runMainLoop
  84. cc.game._setAnimFrame = function () {
  85. this._lastTime = new Date();
  86. var frameRate = this.config.frameRate;
  87. this._frameTime = 1000 / frameRate;
  88. window.requestAnimFrame = window.requestAnimationFrame;
  89. window.cancelAnimFrame = window.cancelAnimationFrame;
  90. };
  91. cc.game._runMainLoop = function () {
  92. var self = this, callback, config = self.config,
  93. director = cc.director,
  94. frameRate = config.frameRate;
  95. cc.debug.setDisplayStats(config.showFPS);
  96. callback = function () {
  97. if (!self._paused) {
  98. self._intervalId = window.requestAnimFrame(callback);
  99. director.mainLoop();
  100. }
  101. };
  102. self._intervalId = window.requestAnimFrame(callback);
  103. self._paused = false;
  104. };
  105. // adapt engine component
  106. BK.Script.loadlib('GameRes://libs/engine/index.js');
  107. }
  108. var rendererAdapterInited = false;
  109. function initRendererAdapter () {
  110. if (rendererAdapterInited) {
  111. return;
  112. }
  113. rendererAdapterInited = true;
  114. cc.renderer.__render = cc.renderer.render;
  115. cc.renderer.render = function (ecScene) {
  116. this.__render(ecScene);
  117. gl.glCommit();
  118. };
  119. gl.canvas = canvas;
  120. }
  121. Float32Array.prototype.subarray = function (begin, end) {
  122. return new Float32Array(this.buffer, begin, end)
  123. };
  124. Uint16Array.prototype.subarray = function (begin, end) {
  125. return new Uint16Array(this.buffer, begin, end)
  126. };
  127. //--BK.Canvas------------------------------------------------
  128. var prototype = BK.Canvas.prototype;
  129. prototype.addEventListener = function () {};
  130. prototype.createLinearGradient = function () {};
  131. prototype.setTransform = prototype.transforms;
  132. var _fillText = prototype.strokeText = prototype.fillText;
  133. prototype.fillText = function () {
  134. this.lineWidth = 0;
  135. _fillText.apply(this, arguments);
  136. };
  137. prototype.getImageData = function(){
  138. return {data : [1, 0, 1, 0]};
  139. };
  140. prototype.focus = function(){};
  141. prototype.getContext = function () {
  142. return this;
  143. };
  144. Object.defineProperty(prototype, "width", {
  145. get: function () {
  146. return this.contentSize.width;
  147. },
  148. set: function (val) {
  149. var size = this.contentSize;
  150. size.width = val;
  151. this.contentSize = size;
  152. this.font = "";
  153. this.strokeColor = {r:0,g:0,b:0,a:0};
  154. this.strokewidth = 0;
  155. },
  156. enumerable: true,
  157. configurable: true
  158. });
  159. Object.defineProperty(prototype, "height", {
  160. get: function () {
  161. return this.contentSize.height;
  162. },
  163. set: function (val) {
  164. var size = this.contentSize;
  165. size.height = val;
  166. this.contentSize = size;
  167. this.font = "";
  168. this.strokeColor = {r:0,g:0,b:0,a:0};
  169. this.strokewidth = 0;
  170. },
  171. enumerable: true,
  172. configurable: true
  173. });
  174. var BOLD_REGEX = /bold/g;
  175. Object.defineProperty(prototype, "font", {
  176. get: function () {
  177. return this._font || '';
  178. },
  179. set: function (val) {
  180. this._font = val;
  181. if (val) {
  182. var matchRet = val.match(/(\d*)px/);
  183. var size = (matchRet && !isNaN(matchRet[1])) ? parseInt(matchRet[1]) : 20;
  184. //console.log("size" + size)
  185. this.setTextSize(size);
  186. var isBold = BOLD_REGEX.test(val);
  187. this.setTextBold(isBold);
  188. }
  189. },
  190. enumerable: true,
  191. configurable: true
  192. });
  193. var tempColor = { r: 0, g: 0, b: 0, a: 1.0};
  194. function rgbToColor (rgbStr) {
  195. if (/^\#/.test(rgbStr)) {
  196. if (rgbStr.length === 4) {
  197. return {
  198. r: parseInt(rgbStr[1] + rgbStr[1], 16) / 255,
  199. g: parseInt(rgbStr[2] + rgbStr[2], 16) / 255,
  200. b: parseInt(rgbStr[3] + rgbStr[3], 16) / 255,
  201. a: parseInt(rgbStr[4] + rgbStr[4], 16),
  202. }
  203. } else if (rgbStr.length === 7) {
  204. return {
  205. r: parseInt(rgbStr.substr(1, 2), 16) / 255,
  206. g: parseInt(rgbStr.substr(3, 2), 16) / 255,
  207. b: parseInt(rgbStr.substr(5, 2), 16) / 255,
  208. a: parseInt(rgbStr.substr(7, 2), 16),
  209. };
  210. } else {
  211. return {r: 0, g: 0, b: 0, a: 1};
  212. }
  213. }
  214. var strArr = rgbStr.match(/(\d|\.)+/g);
  215. if (!strArr || strArr.length > 4 || strArr.length < 3) {
  216. return {r: 0, g: 0, b: 0, a: 1};
  217. }
  218. tempColor.r = strArr[0] / 255 || 0;
  219. tempColor.g = strArr[1] / 255 || 0;
  220. tempColor.b = strArr[2] / 255 || 0;
  221. tempColor.a = strArr[3] || 1;
  222. return tempColor;
  223. }
  224. Object.defineProperty(prototype, "fillStyle", {
  225. get: function () {
  226. return this._fillStyle || '';
  227. },
  228. set: function (val) {
  229. this._fillStyle = val;
  230. this.fillColor = rgbToColor(val);
  231. },
  232. enumerable: true,
  233. configurable: true
  234. });
  235. Object.defineProperty(prototype, "strokeStyle", {
  236. get: function () {
  237. return this._strokeStyle || '';
  238. },
  239. set: function (val) {
  240. this._strokeStyle = val;
  241. this.strokeColor = rgbToColor(val);
  242. },
  243. enumerable: true,
  244. configurable: true
  245. });
  246. // requestAnimationFrame requestAnimationFrame
  247. var _mainTicker;
  248. var requestAnimationFrame = window.requestAnimationFrame = function (callback) {
  249. if (!_mainTicker) {
  250. _mainTicker = new BK.Ticker();
  251. var frameRate = cc.game.config.frameRate;
  252. _mainTicker.interval = 60 / frameRate;
  253. _mainTicker.setTickerCallBack(function (ts, duration) {
  254. if (!cc.game._paused) {
  255. BK.inputManager && BK.inputManager.detectGesture();
  256. callback();
  257. }
  258. });
  259. }
  260. return 1;
  261. };
  262. var cancelAnimationFrame = window.cancelAnimationFrame = function () {
  263. if (_mainTicker) {
  264. _mainTicker.dispose();
  265. _mainTicker = null;
  266. }
  267. };
  268. // setTimeout, clearTimeout
  269. var _windowTimeIntervalId = 0;
  270. var _windowTimeFunHash = {};
  271. var WindowTimeFun = function (code) {
  272. this._intervalId = _windowTimeIntervalId++;
  273. this._ticker = new BK.Ticker();
  274. this._ticker.interval = 1;
  275. this._code = code;
  276. };
  277. WindowTimeFun.prototype.fun = function () {
  278. if (!this._code) return;
  279. var code = this._code;
  280. if (typeof code === 'string') {
  281. Function(code)();
  282. }
  283. else if (typeof code === 'function') {
  284. code.apply(null, this._args);
  285. }
  286. };
  287. var setTimeout = window.setTimeout = function (code, delay) {
  288. var target = new WindowTimeFun(code);
  289. if (arguments.length > 2)
  290. target._args = Array.prototype.slice.call(arguments, 2);
  291. var original = target.fun;
  292. target.fun = function () {
  293. clearTimeout(this.target._intervalId);
  294. original.apply(this.target, arguments);
  295. };
  296. if (!delay) {
  297. delay = 0.001;
  298. }
  299. target._ticker.setTimeout(function(){
  300. try{
  301. target.fun.apply(this, arguments);
  302. }catch(e){
  303. debugger;
  304. console.error(e);
  305. }
  306. }, delay, target);
  307. _windowTimeFunHash[target._intervalId] = target;
  308. return target._intervalId;
  309. };
  310. var setInterval = window.setInterval = function(code, delay){
  311. var target = new WindowTimeFun(code);
  312. if (arguments.length > 2)
  313. target._args = Array.prototype.slice.call(arguments, 2);
  314. var original = target.fun;
  315. target.fun = function () {
  316. original.apply(this.target, arguments);
  317. };
  318. if (!delay) {
  319. delay = 0.001;
  320. }
  321. target._ticker.setInterval(target.fun, delay, target);
  322. _windowTimeFunHash[target._intervalId] = target;
  323. return target._intervalId;
  324. };
  325. var clearTimeout = window.clearTimeout = function (intervalId) {
  326. var target = _windowTimeFunHash[intervalId];
  327. if (target) {
  328. target._ticker.removeTimeout(target);
  329. target._ticker.dispose();
  330. delete _windowTimeFunHash[intervalId];
  331. }
  332. };
  333. var clearInterval = window.clearInterval = function (intervalId) {
  334. var target = _windowTimeFunHash[intervalId];
  335. if (target) {
  336. target._ticker.removeInterval(target);
  337. target._ticker.dispose();
  338. delete _windowTimeFunHash[intervalId];
  339. }
  340. };
  341. // WebSocket.js
  342. BK.Script.loadlib('GameRes://libs/engine/CCWebSocket.js');
  343. // Local Storage
  344. BK.Script.loadlib('GameRes://libs/engine/CCLocalStorage.js');
  345. // other adapter
  346. var performance = { now: function() { return BK.Time.timestamp * 1000; } };