@@ -163,77 +163,83 @@ public void close() {
163163 * @param clientWindow
164164 */
165165 private void loop (ClientWindow clientWindow ) {
166+ final double TICKS_PER_SECOND = 20.0 ;
167+ final double NS_PER_TICK = 1_000_000_000.0 / TICKS_PER_SECOND ; // 50_000_000 ns
168+ final float FIXED_DELTA_SECONDS = 1.0f / (float )TICKS_PER_SECOND ; // 0.05 seconds
169+
166170 int fps = 0 ;
167- int ticks = 0 ;
168- long start = System .currentTimeMillis ();
171+ int tps = 0 ;
172+ long fpsTimer = System .currentTimeMillis ();
169173
170- // Set the clear color
171- glClearColor ( 0.529f , 0.808f , 0.922f , 1.0f ) ;
174+ long lastTime = System . nanoTime ();
175+ double accumulator = 0.0 ;
172176
173- // Run the rendering loop until the user has attempted to close
174177 double currentFrame = glfwGetTime ();
175178 double lastFrame = currentFrame ;
176179 double deltaTime ;
177180
178- while (!glfwWindowShouldClose (clientWindow .getWindow ())) {
179- long tickLength = 50 ;
180- long endOfTick = System .currentTimeMillis () + tickLength ;
181- long currentTime = System .currentTimeMillis ();
182- ticks ++;
183-
184- while (endOfTick >= currentTime ) {
185- currentFrame = glfwGetTime ();
186- deltaTime = currentFrame - lastFrame ;
187- lastFrame = currentFrame ;
188-
189- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the framebuffer
190-
191- // Blending
192- glEnable (GL_BLEND );
193- glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
181+ // Set the clear color once
182+ glClearColor (0.529f , 0.808f , 0.922f , 1.0f );
194183
195- // 3D Depth
196- glEnable (GL_DEPTH_TEST );
184+ while (!glfwWindowShouldClose (clientWindow .getWindow ())) {
185+ currentFrame = glfwGetTime ();
186+ deltaTime = currentFrame - lastFrame ;
187+ lastFrame = currentFrame ;
197188
198- // Disable back faces
199- glEnable (GL_CULL_FACE ); // Enable face culling
200189
201- // Inputs etc
202- clientWindow .poll ();
190+ long now = System .nanoTime ();
191+ long elapsedNanos = now - lastTime ;
192+ lastTime = now ;
193+ accumulator += (double ) elapsedNanos ;
203194
204- // Render all pending objects
205- RenderManager . render ();
195+ // Process input/events every frame
196+ glfwPollEvents (); // make sure to poll events each loop
206197
207- //TODO Temp I think
208- if (this .localPlayer != null ) {
209- this .localPlayer .render ();
198+ // Run fixed ticks while we've accumulated one-or-more tick durations
199+ boolean didTick = false ;
200+ while (accumulator >= NS_PER_TICK ) {
201+ // --- TICK: update game logic at fixed rate ---
202+ if (!BlockGame .getInstance ().getConfig ().isPaused ()) {
203+ world .update (); // game logic should use FIXED_DELTA_SECONDS where appropriate
210204 }
211205
212- if (!BlockGame .getInstance ().getConfig ().isPaused ()) {
213- this .world .update ();
206+ accumulator -= NS_PER_TICK ;
207+ tps ++;
208+ didTick = true ;
209+ }
210+
211+ if (!BlockGame .getInstance ().getConfig ().isPaused ()) {
212+ if (this .localPlayer != null ) {
214213 this .localPlayer .update (deltaTime );
215214 }
215+ }
216216
217- //Run a single main thread queue
218- ThreadUtil .runMainQueue ();
219-
220- // Render UI
221- ScreenManager .render ();
222-
223- // Swap the buffers
224- glfwSwapBuffers (clientWindow .getWindow ());
225-
226- // FPS Calculator
227- currentTime = System .currentTimeMillis ();
228- if (currentTime - start >= 1000 ) {
229- BlockGame .getInstance ().getConfig ().setFPS (fps );
230- BlockGame .getInstance ().getConfig ().setTicks (ticks );
231- start = currentTime ;
232- fps = 0 ;
233- ticks = 0 ;
234- } else {
235- fps ++;
236- }
217+ ThreadUtil .runMainQueue (); // run main-thread queued tasks on tick
218+
219+ // Prepare GL state and render
220+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
221+ glEnable (GL_BLEND );
222+ glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
223+ glEnable (GL_DEPTH_TEST );
224+ glEnable (GL_CULL_FACE );
225+
226+ // Inputs, render managers and UI (per-frame)
227+ clientWindow .poll (); // your code — keep per-frame input/polling
228+ RenderManager .render ();
229+ if (this .localPlayer != null ) this .localPlayer .render (); // rendering uses alpha if desired
230+
231+ ScreenManager .render ();
232+
233+ glfwSwapBuffers (clientWindow .getWindow ());
234+ fps ++;
235+
236+ // FPS / TPS reporting (once per second)
237+ if (System .currentTimeMillis () - fpsTimer >= 1000 ) {
238+ BlockGame .getInstance ().getConfig ().setFPS (fps );
239+ BlockGame .getInstance ().getConfig ().setTicks (tps );
240+ fps = 0 ;
241+ tps = 0 ;
242+ fpsTimer += 1000 ;
237243 }
238244 }
239245 }
0 commit comments