p3nGu1nZz commited on
Commit
e8a6c79
·
1 Parent(s): 6802c16
Files changed (1) hide show
  1. index.js +10 -3
index.js CHANGED
@@ -8,11 +8,16 @@ import { generateGlyphVerticesForText } from './wgpu-text.js';
8
  import { config } from './wgpu-config.js';
9
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
10
 
 
11
  const canvas = document.querySelector('canvas');
 
 
12
  const state = createState(config);
13
 
14
  const FIXED_DELTA_TIME = 1 / 60; // Fixed time step of 60 FPS for physics and game logic
15
  const MAX_FRAME_TIME = 0.25; // Maximum time to simulate per frame to prevent spiral of death
 
 
16
 
17
  async function Main() {
18
  const adapter = await navigator.gpu?.requestAdapter();
@@ -25,10 +30,10 @@ async function Main() {
25
  const shaderCode = await fetchShaderCode('shaders.wgsl');
26
  const vertexSize = config.floatsPerVertex * 4;
27
 
28
- // Create pipeline
29
  state.pipeline = await createPipeline(state.device, presentationFormat, vertexSize, shaderCode);
30
 
31
- // Generate and display glyph texture atlas
32
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
33
  document.body.appendChild(glyphCanvas);
34
  glyphCanvas.style.backgroundColor = '#222';
@@ -62,6 +67,7 @@ function CreateBuffers() {
62
  }
63
 
64
  function GenerateIndices(maxGlyphs) {
 
65
  return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
66
  const ndx = Math.floor(i / 6) * 4;
67
  return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
@@ -122,7 +128,8 @@ function GameLoop(context) {
122
  const alpha = accumulator / FIXED_DELTA_TIME;
123
  Render(alpha, context);
124
 
125
- requestAnimationFrame(Tick);
 
126
  }
127
 
128
  Tick();
 
8
  import { config } from './wgpu-config.js';
9
  import { CANVAS, CTX, COLORS, RENDER_PASS_DESCRIPTOR } from './wgpu-constants.js';
10
 
11
+ // Canvas element for rendering
12
  const canvas = document.querySelector('canvas');
13
+
14
+ // State initialization
15
  const state = createState(config);
16
 
17
  const FIXED_DELTA_TIME = 1 / 60; // Fixed time step of 60 FPS for physics and game logic
18
  const MAX_FRAME_TIME = 0.25; // Maximum time to simulate per frame to prevent spiral of death
19
+ const TARGET_FPS = 60; // Target frames per second
20
+ const FRAME_DURATION = 1000 / TARGET_FPS; // Duration of a single frame in milliseconds
21
 
22
  async function Main() {
23
  const adapter = await navigator.gpu?.requestAdapter();
 
30
  const shaderCode = await fetchShaderCode('shaders.wgsl');
31
  const vertexSize = config.floatsPerVertex * 4;
32
 
33
+ // Create rendering pipeline
34
  state.pipeline = await createPipeline(state.device, presentationFormat, vertexSize, shaderCode);
35
 
36
+ // Generate glyph texture atlas
37
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, config);
38
  document.body.appendChild(glyphCanvas);
39
  glyphCanvas.style.backgroundColor = '#222';
 
67
  }
68
 
69
  function GenerateIndices(maxGlyphs) {
70
+ // Generate index array for glyphs
71
  return Array.from({ length: maxGlyphs * 6 }, (_, i) => {
72
  const ndx = Math.floor(i / 6) * 4;
73
  return (i % 6 < 3 ? [ndx, ndx + 1, ndx + 2] : [ndx + 2, ndx + 1, ndx + 3])[i % 3];
 
128
  const alpha = accumulator / FIXED_DELTA_TIME;
129
  Render(alpha, context);
130
 
131
+ // Schedule the next frame update
132
+ setTimeout(Tick, FRAME_DURATION);
133
  }
134
 
135
  Tick();