I think the system timer or new jqt form timers are going to be part of the solution. You can schedule them for 34 ms. In Jqt you can also have independent timers, but that is unlikely to be a useful experiment, unless there are time based game data updates independent of frames. Your keyboard and mouse handlers will run uninterrupted.
System timers are not guaranteed to trigger on time, but I think that you do not get 2 of them bunched up. That is if 60 ms elapse (instead of 34) from the last one, Im not 100% sure whether you get another 34 ms later or 4ms later, but I do know that if you missed 2 timers, you do not get 2 timers one after each other. This generally works for a game loop, where frame rate slows down adaptively. one way to avoid thinking about time-based (separate system timer) game data updates is exactly through your game tick "java" approach. Calculate things that are updated by keyboard/mouse events separately, and then combine these with time (tick)-based updates within the render system timer call. ----- Original Message ----- From: Neill Robson <[email protected]> To: [email protected] Cc: Sent: Friday, August 29, 2014 7:23:18 PM Subject: [Jprogramming] Running a video game loop Hello to all on the forum! If any you happen to know Mr. (Henry) Rich, I'm one of his students. The most direct way to create a constantly updating screen in J is to run the relevant gl2 commands in a sys_timer verb, set to repeat at a certain interval by the window driver. However, for a video game with any more than rudimentary complexity, one would wish to separate "frames" (drawing the image to the screen) from "ticks" (computing the game logic). Preferably, ticks would happen at a constant rate per second, and then the remaining computation time would be spent rendering frames. In Java, that would be implemented somehow similar to this: long lastTime = System.nanoTime(); double nsPerTick = 1000000000D / 60D; double delta = 0; while (running) { long now = System.nanoTime(); // Determine how long it has been since the last tick delta += (now - lastTime) / nsPerTick; lastTime = now; // If more than 1/60th of a second has passed, tick for each 60th. while (delta >= 1) { ticks++; tick(); delta -= 1; } render(); } Of course, J is *much* different than Java in many respects. "While" loops are frowned upon in this situation, because functional languages get hung up in the while loop until it is "finished;" however, for video games this "hanging up" seems to be exactly what is needed (without the side effects of freezing the terminal, of course). I have been directed towards sockets for this sort of situation in the past, but I've never progressed far enough down that route to understand the gritty details of how to implement such a system. Would someone on the forum be able to guide me towards a solution? To restate the problem, I'm looking for the functional (J) equivalent of a "while" loop that repeats one function at a fixed rate and runs the other as frequently as the host machine safely allows. Thank you for your assistance, and apologies for the protracted & ugly Java example! -- -Neill ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
