GameHandler.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /************************************************************************
  2. * GameHandler.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. const GameHandler = {
  22. nextId: 0,
  23. rootObjects: [],
  24. renderMode: null,
  25. bDrawDebugFPS: false,
  26. debugFpsLabel: null,
  27. prevMillis: 0,
  28. delta: 0,
  29. db: null,
  30. dbWidth: 1920,
  31. dbHeight: 1080,
  32. setRenderMode: function(mode)
  33. {
  34. this.renderMode = mode;
  35. },
  36. setDoubleBufferSize: function(w, h)
  37. {
  38. this.dbWidth = w;
  39. this.dbHeight = h;
  40. },
  41. drawDebugFPS(val)
  42. {
  43. this.bDrawDebugFPS = val;
  44. },
  45. init: function(fps = 60)
  46. {
  47. if (!this.renderMode) this.renderMode = RENDER_MODES.P2D;
  48. switch (this.renderMode)
  49. {
  50. case RENDER_MODES.P2D:
  51. createCanvas(windowWidth, windowHeight);
  52. this.db = createGraphics(this.dbWidth, this.dbHeight);
  53. break;
  54. case RENDER_MODES.WEBGL:
  55. createCanvas(windowWidth, windowHeight, WEBGL);
  56. this.db = createGraphics(this.dbWidth, this.dbHeight, WEBGL);
  57. this.db.smooth();
  58. break;
  59. }
  60. frameRate(fps);
  61. smooth();
  62. if (this.renderMode == RENDER_MODES.WEBGL)
  63. {
  64. translate(-windowWidth / 2, -windowHeight / 2);
  65. db.translate(-this.dbWidth / 2, -this.dbHeight / 2);
  66. }
  67. if (this.bDrawDebugFPS)
  68. {
  69. this.debugFpsLabel = new Label("debugFps", `FPS: ${frameRate()}`);
  70. this.addRootObject(this.debugFpsLabel);
  71. }
  72. },
  73. instanceGameObject: function(obj)
  74. {
  75. obj.id = this.nextId;
  76. this.nextId++;
  77. },
  78. addRootObject: function(obj)
  79. {
  80. this.rootObjects.push(obj);
  81. obj.setup();
  82. },
  83. upframecount: 0,
  84. upframenum: 20,
  85. update: function()
  86. {
  87. if (this.bDrawDebugFPS)
  88. {
  89. if (frameCount % this.upframenum == 0)
  90. {
  91. this.debugFpsLabel.setText(`FPS: ${
  92. Math.round(this.upframecount * 1000) / 1000
  93. }`);
  94. this.upframecount = 0;
  95. }
  96. else
  97. this.upframecount = max(this.upframecount, frameRate());
  98. }
  99. this.delta = (millis() - this.prevMillis) / 1000;
  100. for (let i = 0; i < this.rootObjects.length; i++)
  101. this.rootObjects[i].update(this.delta);
  102. },
  103. draw: function()
  104. {
  105. this.db.clear();
  106. for (let i = 0; i < this.rootObjects.length; i++)
  107. this.rootObjects[i].draw(this.delta, this.db);
  108. this.db.push();
  109. this.db.strokeWeight(5);
  110. this.db.noFill();
  111. this.db.rect(0, 0, this.dbWidth, this.dbHeight);
  112. this.db.pop();
  113. imageMode(CENTER);
  114. if (windowWidth / windowHeight < this.dbWidth / this.dbHeight)
  115. {
  116. this.db.screenWidth = windowWidth;
  117. this.db.screenHeight = windowWidth * (this.dbHeight / this.dbWidth);
  118. }
  119. else
  120. {
  121. this.db.screenHeight = windowHeight;
  122. this.db.screenWidth = windowHeight * (this.dbWidth / this.dbHeight);
  123. }
  124. image(this.db, windowWidth / 2, windowHeight / 2, this.db.screenWidth, this.db.screenHeight);
  125. this.prevMillis = millis();
  126. }
  127. }
  128. function windowResized()
  129. {
  130. resizeCanvas(windowWidth, windowHeight);
  131. }