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


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

Re: a tight game loop in Swing

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.gui
Subject Re: a tight game loop in Swing
Date 2012-06-07 00:36 -0700
Organization albasani.net
Message-ID <jqpll7$mee$1@news.albasani.net> (permalink)
References <tight-game-loop-20120604142027@ram.dialup.fu-berlin.de> <nospam-E06F2C.11012604062012@news.aioe.org> <01353eab-26e6-442c-9bf1-38ccf92b19ad@googlegroups.com> <jqmnk0$2jm$1@dont-email.me>

Show all headers | View raw


On 06/05/2012 09:51 PM, Knute Johnson wrote:
>    public void run() {
>          try {
>              long now = 0;
>              long then = System.nanoTime();
>
>              while (true) {
>                  render();
>                  try {
>                      EventQueue.invokeAndWait(new Runnable() {
>                          public void run() {
>                              paintImmediately(getBounds());
>                          }
>                      });
>                  } catch (InvocationTargetException ite) {
>                      System.out.println(ite);
>                  }
>
>                  /*
>                  while (now < then + 10000000)
>                      now = System.nanoTime();
>                  then = now;
>                  */
>              }
>          } catch (InterruptedException ie) {
>              System.out.println(ie);
>          }
>      }
>

How about this formulation?

   @Override
   public void run() {
     for (boolean active = true; active; ) {
       try {
         render();
         try {
           EventQueue.invokeAndWait(new Runnable() {
               public void run() {
                 paintImmediately(getBounds());
               }
             });
         } catch (InvocationTargetException ite) {
           System.out.println(ite);
         }

         /*
         for (long pause = System.nanoTime() + 10000000L;
              System.nanoTime() < pause;
              ) {
         }
         */
       } catch (InterruptedException ie) {
         System.out.println(ie);
         active = false;
       }
     }
   }

I base this on vague, hence possibly superstitiously encoded memories of 
Goetz's (et al.) _Java Concurrency in Practice_ and other sources.

He goes to some length to re-educate about 'InterruptedException'. I may have 
got it wrong, but I seem to recall something about setting a loop condition 
rather than leaping out pell-mell, and a tickle about a rethrow of the 
interruption but that might be for specialized circumstances. Like when the 
interruptee isn't the end of the line for handling that interrupt as it is here.

I read a book. I'm not an authority.

I loathe 'System.out' for logging. OTOH, in this case the point of the program 
arguably is the exception output, so it's legit, right?

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.gui | Previous | NextPrevious 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