p3nGu1nZz commited on
Commit
ff401af
1 Parent(s): d502220

upgrade gameLoop

Browse files
Files changed (1) hide show
  1. index.js +8 -2
index.js CHANGED
@@ -75,11 +75,17 @@ async function main() {
75
  state.width = width;
76
  state.height = height;
77
 
 
 
 
 
78
  let lastTime = performance.now();
 
79
  function update() {
80
  const currentTime = performance.now();
81
- const deltaTime = (currentTime - lastTime) / 1000; //seconds
82
  state.time += deltaTime;
 
83
  const fov = 60 * Math.PI / 180;
84
  const aspect = canvas.clientWidth / canvas.clientHeight;
85
  const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
@@ -100,7 +106,7 @@ async function main() {
100
  state.device.queue.submit([encoder.finish()]);
101
 
102
  lastTime = currentTime;
103
- setTimeout(update, 16); //60fps
104
  }
105
 
106
  update();
 
75
  state.width = width;
76
  state.height = height;
77
 
78
+ gameLoop(context);
79
+ }
80
+
81
+ function gameLoop(context) {
82
  let lastTime = performance.now();
83
+
84
  function update() {
85
  const currentTime = performance.now();
86
+ const deltaTime = (currentTime - lastTime) / 1000; // Convert to seconds
87
  state.time += deltaTime;
88
+
89
  const fov = 60 * Math.PI / 180;
90
  const aspect = canvas.clientWidth / canvas.clientHeight;
91
  const projectionMatrix = mat4.perspective(fov, aspect, config.render.zNear, config.render.zFar);
 
106
  state.device.queue.submit([encoder.finish()]);
107
 
108
  lastTime = currentTime;
109
+ setTimeout(update, 16); // Approximate 60 FPS
110
  }
111
 
112
  update();