1 /* Name: Core Module 2 Date: April 2011 3 Description: Core functionality such as device detection is implemeted in this module 4 Dependencies: <none> 5 Children: <none> */ 6 7 /* Properties GET FROM JSLINT */ 8 9 /** 10 * Creates an instance of the Core module 11 * 12 * @constructor 13 * @this {Core} 14 * @see core.js 15 */ 16 17 function Core() { 18 19 /** 20 * Device name. 21 * 22 * @type String 23 */ 24 this.deviceName = "Unknown Device"; 25 26 /** 27 * Platform type. 28 * 29 * @type String 30 */ 31 this.platformType = "Unknown"; 32 33 /** 34 * Canvas element width. 35 * 36 * @type Number 37 */ 38 this.canvasWidth = 0; 39 40 /** 41 * Canvas element height. 42 * 43 * @type Number 44 */ 45 this.canvasHeight = 0; 46 47 /** 48 * Reference to Canvas. 49 * 50 * @type [object CanvasRenderingContext2D] 51 */ 52 this.ctx = null; 53 54 /** 55 * FPS counter draw interval. 56 * 57 * @type Number 58 */ 59 this.drawInterval = 0; 60 61 /** 62 * FPS counter frame counter. 63 * 64 * @type Number 65 */ 66 this.frameCounter = 0; 67 68 /** 69 * FPS counter max frames per second counter. 70 * 71 * @type Number 72 */ 73 this.maxFramesPerSecond = 0; 74 75 /** 76 * FPS counter last time value. 77 * 78 * @type Number 79 */ 80 this.lastTime = new Date(); 81 82 /** 83 * FPS counter frames per second value. 84 * 85 * @type Number 86 */ 87 this.framesPerSecond = 0; 88 89 /** 90 * FPS counter current time value. 91 * 92 * @type Number 93 */ 94 this.nowTime = 0; 95 96 /** 97 * FPS counter difference in time value. 98 * 99 * @type Number 100 */ 101 this.diffTime = 0; 102 103 /** 104 * Detect device and store in {@link #deviceName}. 105 * 106 * @return {String} String denoting device type. 107 * <i>"Firefox", "Chrome", "Opera", "IE", "Safari", "SafariMobile", "Android", "AndroidMobile" or "Incompatible".</i> 108 * @this {Core} 109 */ 110 this.detectDevice = function() { 111 var browserAgent = navigator.userAgent.toLowerCase(); 112 113 if (browserAgent.indexOf("firefox") != -1){ 114 this.deviceName = "Firefox"; 115 return "Firefox"; 116 } 117 118 if (browserAgent.indexOf("chrome") != -1 && 119 browserAgent.indexOf("android") == -1){ 120 this.deviceName = "Chrome"; 121 return "Chrome"; 122 } 123 124 if (browserAgent.indexOf("opera") != -1 && 125 browserAgent.indexOf("firefox") == -1){ 126 this.deviceName = "Opera"; 127 return "Opera"; 128 } 129 130 if (browserAgent.indexOf("msie 7") != -1 || 131 browserAgent.indexOf("msie 8") != -1 || 132 browserAgent.indexOf("msie 9") != -1){ 133 this.deviceName = "IE"; 134 return "IE"; 135 } else if (browserAgent.indexOf("msie") != -1){ 136 this.deviceName = "Incompatible"; 137 return "Incompatible"; 138 } 139 140 if (browserAgent.indexOf("safari") != -1 && 141 browserAgent.indexOf("chrome") == -1 && 142 browserAgent.indexOf("mobile") == -1){ 143 this.deviceName = "Safari"; 144 return "Safari"; 145 } 146 147 if (browserAgent.indexOf("safari") != -1 || 148 browserAgent.indexOf("iphone") != -1 || 149 browserAgent.indexOf("ipod") != -1 || 150 browserAgent.indexOf("ipad") != -1 && 151 browserAgent.indexOf("mobile") != -1){ 152 this.deviceName = "SafariMobile"; 153 return "SafariMobile"; 154 } 155 156 if (browserAgent.indexOf("android") != -1 && 157 browserAgent.indexOf("mobile") == -1){ 158 this.deviceName = "Android"; 159 return "Android"; 160 } 161 162 if (browserAgent.indexOf("android") != -1 && 163 browserAgent.indexOf("mobile") != -1){ 164 this.deviceName = "AndroidMobile"; 165 return "AndroidMobile"; 166 } 167 }; 168 169 /** 170 * Detect if we are running on a Desktop or Mobile device and store in {@link #platformType}. 171 * @return {String} Returns String denoting platform: "Desktop" or "Mobile". 172 * @this {Core} 173 */ 174 this.detectPlatform = function() { 175 var d = this.detectDevice(); 176 177 switch (d){ 178 case "Firefox": 179 this.platformType = "Desktop"; 180 return "Desktop"; 181 break; 182 case "Chrome": 183 this.platformType = "Desktop"; 184 return "Desktop"; 185 break; 186 case "Opera": 187 this.platformType = "Desktop"; 188 return "Desktop"; 189 break; 190 case "IE": 191 this.platformType = "Desktop"; 192 return "Desktop"; 193 break; 194 case "Safari": 195 this.platformType = "Desktop"; 196 return "Desktop"; 197 break; 198 case "SafariMobile": 199 this.platformType = "Mobile"; 200 return "Mobile"; 201 break; 202 case "Android": 203 this.platformType = "Mobile"; 204 return "Mobile"; 205 break; 206 case "AndroidMobile": 207 this.platformType = "Mobile"; 208 return "Mobile"; 209 break; 210 default: 211 return "Unknown"; 212 } 213 }; 214 215 /** 216 * Get reference to Canvas and 2D context. 217 * <i>The first canvas object to be found is taken as the main canvas.</i> 218 * @param {[object HTMLCanvasElement]} canvas Canvas object. 219 * @param {[object CanvasRenderingContext2D]} ctx 2D Canvas Context object. 220 * @return {[object HTMLCanvasElement,object CanvasRenderingContext2D]} canvas and ctx object. 221 * @this {Core} 222 */ 223 this.getReference = function(canvas,ctx) { 224 var canvasId = document.getElementsByTagName('canvas')[0].id; 225 canvas = document.getElementById(canvasId); 226 ctx = canvas.getContext('2d'); 227 this.ctx = ctx; 228 return [canvas,ctx]; 229 }; 230 231 /** 232 * Resize the Canvas frame on window resize. 233 * 234 * @this {Core} 235 */ 236 this.resize = function() { 237 if (this.platformType == "Mobile"){ 238 this.ctx.canvas.width = window.innerWidth; 239 this.ctx.canvas.height = window.innerHeight; 240 241 this.canvasWidth = window.innerWidth; 242 this.canvasHeight = window.innerHeight; 243 } else if (this.platformType == "Desktop"){ 244 this.canvasWidth = this.ctx.canvas.width; 245 this.canvasHeight = this.ctx.canvas.height; 246 } 247 } 248 249 /** 250 * Set the Canvas frame properties. 251 * 252 * @this {Core} 253 */ 254 this.setFrameProperties = function(){ 255 if (this.platformType == "Mobile"){ 256 this.ctx.canvas.width = window.innerWidth; 257 this.ctx.canvas.height = window.innerHeight; 258 259 this.canvasWidth = window.innerWidth; 260 this.canvasHeight = window.innerHeight; 261 262 window.scrollTo(0, 1); 263 264 } else if (this.platformType == "Desktop"){ 265 this.canvasWidth = this.ctx.canvas.width; 266 this.canvasHeight = this.ctx.canvas.height; 267 } 268 } 269 270 /** 271 * Return canvas width stored in {@link #canvasWidth}. 272 * @return {Number} Number denoting width of Canvas. 273 * @this {Core} 274 */ 275 this.getCanvasWidth = function(){ 276 return this.canvasWidth; 277 } 278 279 280 /** 281 * Return canvas height stored in {@link #canvasHeight}. 282 * @return {Number} Number denoting height of Canvas. 283 * @this {Core} 284 */ 285 this.getCanvasHeight = function(){ 286 return this.canvasHeight; 287 } 288 289 /** 290 * Set the millisecond drawing interval rate, and maximum frames per second based on this value. 291 * @param {Number} interval The drawing interval value. 292 * @this {Core} 293 */ 294 this.setDrawInterval = function(interval){ 295 this.drawInterval = interval; 296 297 // Set max FPS 298 this.maxFramesPerSecond = 1 / (this.drawInterval / 1000); 299 } 300 301 /** 302 * Return the drawing interval rate stored in {@link #drawInterval}. 303 * @return {Number} Number denoting drawing interval rate. 304 * @this {Core} 305 */ 306 this.getDrawInterval = function(){ 307 return this.drawInterval; 308 } 309 310 /** 311 * Calculate the frames per second and store in {@link #framesPerSecond}. 312 * 313 * @this {Core} 314 */ 315 this.fpsCalc = function(){ 316 this.nowTime = new Date(); 317 this.diffTime = Math.ceil((this.nowTime.getTime() - this.lastTime.getTime())); 318 319 if (this.diffTime >= 1000) { 320 this.framesPerSecond = this.frameCounter; 321 this.frameCounter = 0; 322 this.lastTime = this.nowTime; 323 } 324 } 325 326 /** 327 * Increment the frame counter stored in {@link #frameCounter}. 328 * 329 * @this {Core} 330 */ 331 this.fpsInc = function(){ 332 this.frameCounter++; 333 } 334 335 /** 336 * Return the game frames per second stored in {@link #framesPerSecond}. 337 * @return {Number} Number denoting game frames per second. 338 * @this {Core} 339 */ 340 this.getFPS = function(){ 341 return this.framesPerSecond; 342 } 343 344 /** 345 * Return the game maximum frames per second stored in {@link #maxFramesPerSecond}. 346 * @return {Number} Number denoting maximum game frames per second. 347 * @this {Core} 348 */ 349 this.getMaximumFPS = function(){ 350 return this.maxFramesPerSecond; 351 } 352 }