Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Engine Offload/System Cycle Rate?  (Read 320 times)
0 Members and 2 Guests are viewing this topic.
Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« on: 2012-02-05 21:00:42 »

Looking for a nice way to keep my games cycle under 6 seconds, and every 45 seconds outprint the following: (If it can be calculated)

Average Cycle Time: 0.0ms. (Under control =D)
Engine Offload: 0.0%. (Possibly out of 100%?)

Right now how my game's loop looks is like the following:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public void run() {
      try {
         if (sleepTime > 0) {
            Thread.sleep(getCycle());
            mainProcessHandler();
            cycles++;
            debug();
         }
      } catch (InterruptedException e) {
         System.out.println("A Fatal Exception has occured during Runtime!");
         e.printStackTrace(System.err);
         System.exit(0);
         return;
      }
   }


And at the end it calls the debugging method (where i'd like this to outprint every 45 seconds).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   private void debug() {
      try {
         if ((debugTimer.elapsed() > averageDebugTime)  && (debugging())) {
            cycleTime = cycleTimer.elapsed();
            System.out.println("Average Cycle Time: "+cycleTime+"ms.");
            System.out.println("Total Cycles: "+getCycles());
            cycles = 0;
            sleepTime = getCycle();
            cycleTimer.reset();
            debugTimer.reset();
            System.gc();
            System.runFinalization();
         }
      } catch (Exception e1) {
         System.err.println("A Fatal Exception has occured while processing debug!");
         e1.printStackTrace(System.err);
         System.exit(0);
         return;
      }
   }


I really don't like what i've came up with before this, or the example above.

Right now it just outprints:
1  
2  
[2/5/12 6:05 PM]: Average Cycle Time: 30007ms.
[2/5/12 6:05 PM]: Total Cycles: 1938


So i'm asking if anyone could come up or share some nice way of keeping track of these variables, and adjusting them through out my main game class.

Offline ra4king

JGO Kernel
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #1 on: 2012-02-05 22:03:20 »

Now what exactly is a cycle? The call to update() then draw()? Just update()?

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #2 on: 2012-02-05 22:33:24 »

A 'Cycles' variable is added++ everytime "mainProcessHandler();" is called & the initial "Cycle" is completed:
So a cycle in this would be the "mainProcessHandler()" method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   private void mainProcessHandler() {
      try {
         Player.process();      
         WalkingCheck.process();
         WalkingHandler.process();
         repaint();
         updateFps();
      } catch (Exception e1) {
         System.err.println("A Fatal Exception has occured while processing the mainHandler!");
         e1.printStackTrace(System.err);
         System.exit(0);
         return;
      }
   }


Sorry for posting so much code, it's just that i've never been good with System.currentTimeMillis() and getting results to return accurately.
The Cycles variable i don't care about (I added that just for looks after all else failed to return accurate results).
I'd just like some fancy debugging/engine offload variables that when printed out give me accurate results of usage out of 100(percent). <(keyboard failure).

(The average time between debugs i've already handled via a SimpleTimer class).

This is what I was using before, and since I can't comprehend on how to change those variables to adjust to a much smaller sleepTime  (3 to six) ms. I bring my question here asking for help D=

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   /**
    * Main Server Tick
    */

   try {
      if (sleepTime > 0)
         Thread.sleep(sleepTime);
      engineTimer.reset();

      if(cycleTime < 575)
         sleepTime = cycleRate - cycleTime;
      else
         sleepTime = 0;
      cycleTime = engineTimer.elapsed();            
      sleepTime = cycleRate - cycleTime;
      totalCycleTime += cycleTime;
      cycles++;
      debug();
   } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println("A fatal exception has been thrown!");
      System.exit(0);
   }


Everytime that got printed out it would just say usage is = 0.0, simply because the hop between sleepTime was to great for anything to even build up/redraw intime for a smaller cycle (Sprite based game).

Here is that fancy methods debug() (math/results portion)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   private static void debug() {
      if (debugTimer.elapsed() > 360 * 1000) {
         long averageCycleTime = totalCycleTime / cycles;
         System.out.println("Average Cycle Time: " + averageCycleTime + "ms");
         double engineLoad = ((double) averageCycleTime / (double) cycleRate);
         System.out.println("Engine load: "+ debugPercentFormat.format(engineLoad));
         totalCycleTime = 0;
         cycles = 0;
         debugTimer.reset();
      }
   }

Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #3 on: 2012-02-06 01:35:55 »

Ah that sounds like the normal inaccuracy of currentTimeMillis() Smiley

This should help explain things quite a bit and offer a quick hack to fix it Smiley

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.082 seconds with 19 queries.