PageRenderTime 34ms CodeModel.GetById 31ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/Demo/util.js

http://github.com/mbebenita/Broadway
JavaScript | 68 lines | 45 code | 7 blank | 16 comment | 4 complexity | 14ba3741bec7c658191602c856aef7b9 MD5 | raw file
 1'use strict';
 2
 3function error(message) {
 4  console.error(message);
 5  console.trace();
 6}
 7
 8function assert(condition, message) {
 9  if (!condition) {
10    error(message);
11  }
12}
13
14function isPowerOfTwo(x) {
15  return (x & (x - 1)) == 0;
16}
17
18/**
19 * Joins a list of lines using a newline separator, not the fastest
20 * thing in the world but good enough for initialization code. 
21 */
22function text(lines) {
23  return lines.join("\n");
24}
25
26/**
27 * Rounds up to the next highest power of two.
28 */
29function nextHighestPowerOfTwo(x) {
30  --x;
31  for (var i = 1; i < 32; i <<= 1) {
32      x = x | x >> i;
33  }
34  return x + 1;
35}
36
37/**
38 * Represents a 2-dimensional size value. 
39 */
40var Size = (function size() {
41  function constructor(w, h) {
42    this.w = w;
43    this.h = h;
44  }
45  constructor.prototype = {
46    toString: function () {
47      return "(" + this.w + ", " + this.h + ")";
48    },
49    getHalfSize: function() {
50      return new Size(this.w >>> 1, this.h >>> 1);
51    }
52  }
53  return constructor;
54})();
55
56/**
57 * Creates a new prototype object derived from another objects prototype along with a list of additional properties.
58 *
59 * @param base object whose prototype to use as the created prototype object's prototype
60 * @param properties additional properties to add to the created prototype object
61 */
62function inherit(base, properties) {
63  var prot = Object.create(base.prototype);
64  for (var p in properties) {
65    prot[p] = properties[p];
66  }
67  return prot;
68}