Java-Gaming.org
Java4K - to go         Javadoc:
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, peek at the official java tutorials or join us at irc #jgo.
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Is this timer any good?  (Read 646 times)
0 Members and 1 Guest are viewing this topic.
Offline Kramer

JGO n00b
*

Posts: 11



« on: 2006-07-30 23:03:40 »

Hi All,

I've been thinking about timers a bit lately and came up with this...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
package org.kramer.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Internally uses nanoseconds due to Java 1.5's System.nanoTime() being more
 * accurate. The public interface is in milliseconds though for convenience.
 *
 * TODO: add support for one-off events, look into threading
 *
 * @author Kramer
 *
 */

public class Timer
{
   private long lastLoopTime = System.nanoTime();
   private List<Event> events = new ArrayList<Event>();

   /**
    * Call this in the display() loop
    */

   public void update()
   {
//      long deltaMillis = (System.nanoTime() - lastLoopTime) / 1000000;
     
      Iterator<Event> it = events.iterator();
      while (it.hasNext())
      {
         Event event = it.next();
         if (event.nextExecution <= System.nanoTime())
         {
            event.nextExecution = System.nanoTime() + (event.frequency * 1000000);
            event.doEvent();
         }
      }
      lastLoopTime = System.nanoTime();
   }
   
   /**
    * @param frequency Frequency in milliseconds
    * @param event
    */

   public void scheduleEvent(long frequency, Event event)
   {
      event.frequency = frequency;
      event.nextExecution = System.nanoTime() + (frequency * 1000000);
      events.add(event);
   }
   

   /**
    *
    */

   public abstract class Event
   {
      long nextExecution;
      long frequency;
     
      public abstract void doEvent();
   }

}


...and in your init() you register events and how frequently you want them to happen as:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      Timer timer = new Timer();
      timer.scheduleEvent(500, timer.new Event(){
         public void doEvent() {
            System.out.println("every 500 milliseconds");
         }
      });
      timer.scheduleEvent(100, timer.new Event(){
         public void doEvent() {
            System.out.println("every 100 milliseconds");
         }
      });      


Then call timer.update() in display().

So... does anyone think this is any good?  Would you use it?  If not, please let me know why.

Thanks,
Kramer
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5509
Medals: 204


Hand over your head.


« Reply #1 on: 2006-07-31 05:08:03 »

Replace this:
event.nextExecution = System.nanoTime() + (event.frequency * 1000000);

with:
event.nextExecution += (event.frequency * 1000000);


to ensure it tries to execute at that interval, and can catch up if the cpu was too busy earlier.

Further 'frequency' is poorly named, it should be 'interval'

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Kramer

JGO n00b
*

Posts: 11



« Reply #2 on: 2006-07-31 22:03:23 »

Thanks Riven.  I've taken your advice.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.152 seconds with 22 queries.