Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5161

Re: a tight game loop in Swing

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.gui
Subject Re: a tight game loop in Swing
Date 2012-06-04 11:01 -0400
Organization The Wasteland
Message-ID <nospam-E06F2C.11012604062012@news.aioe.org> (permalink)
References <tight-game-loop-20120604142027@ram.dialup.fu-berlin.de>

Show all headers | View raw


In article <tight-game-loop-20120604142027@ram.dialup.fu-berlin.de>,
 ram@zedat.fu-berlin.de (Stefan Ram) wrote:

> For many years I often have wondered, what the Swing equivalence of 
> a classic tight game loop might be. What I am thinking of is:
> 
>       as fast as possible, repeat this:
>         if there is user input pending, then handle it
>         update the state to the next generation and redraw it
>         end repeat
> 
> (redraws might be omitted when know to be invisble due to a
> limited screen refresh rate, this is the meaning of
> »possiblyRepaint«, below.)
> 
> Now I have heard that often 1 ms timer events are recommended
> with the idea that Swing will coalesc multiple timer events into
> a single one and that they have lower priority than user inputs.
> (Theoretically, there also is the possibility that 1 ms is to
> slow, so that the program might become idle.)

The available resolution varies by platform:

<http://mindprod.com/jgloss/time.html#ACCURACY>

>   Recently, I had another idea of:
> 
>       public class NextGeneration implements java.lang.Runnable
>       { public void run()
>         { calculateNextGeneration();
>           possiblyRepaint();
>           invokeLater( this ); }}
> 
> I am hoping that the recursion via »invokeLater( this )«
> will effectively give me a loop (but not eat stack, since
> it's not a real recursion) and at the same time user input
> events IIRC have a higher priority than invokeLater events,
> so that they will still be handled. What do you think about
> such an »invokeLater« loop?

This reminds of an example adduced by Knute Johnson:

<https://groups.google.com/d/msg/comp.lang.java.gui/aBy_DZFvg2M/-T9aWOwBM-QJ>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar


Thread

Re: a tight game loop in Swing "John B. Matthews" <nospam@nospam.invalid> - 2012-06-04 11:01 -0400
  Re: a tight game loop in Swing Lew <lewbloch@gmail.com> - 2012-06-04 16:39 -0700
    Re: a tight game loop in Swing Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2012-06-05 21:51 -0700
      Re: a tight game loop in Swing Lew <noone@lewscanon.com> - 2012-06-06 23:58 -0700
        Re: a tight game loop in Swing Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-07 09:20 -0700
          Re: a tight game loop in Swing Lew <lewbloch@gmail.com> - 2012-06-08 12:28 -0700
      Re: a tight game loop in Swing Lew <noone@lewscanon.com> - 2012-06-07 00:36 -0700

csiph-web