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