Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can somebody explain this loop?  (Read 3097 times)
0 Members and 1 Guest are viewing this topic.
Offline Dane

Senior Newbie





« Posted 2013-07-01 16:03:46 »

So here it is:

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  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
package org.dane.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JApplet;
import javax.swing.JFrame;

public class TestLoop extends JApplet implements Runnable {

   private Thread thread;
   private boolean active;
   private BufferedImage image;

   // Other Variables
   int operationPos = 0;
   int ratio = 256;
   int delay = 1;
   int count = 0;
   int interruptionCount = 0;
   int interval = 1000 / 50; // 50 is the goal fps.
   int fps = 0;
   long[] operationTime = new long[10];
   long currentTime;

   public static void main(String[] args) {
      JFrame frame = new JFrame("Test");
      frame.setLayout(new BorderLayout());
      frame.setResizable(false);
      frame.add(new TestLoop(256, 256), BorderLayout.CENTER);
      frame.pack();
      frame.setVisible(true);
   }

   public TestLoop(int width, int height) {
      this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      this.setPreferredSize(new Dimension(width, height));
      this.thread = new Thread(this);
      this.thread.start();
   }

   @Override
   public void run() {

      this.active = true;

      for (int i = 0; i < operationTime.length; i++) {
         operationTime[i] = System.currentTimeMillis();
      }

      while (this.active) {

         ratio = 300;
         delay = 1;
         currentTime = System.currentTimeMillis();

         // What's this doing?
         if (currentTime > operationTime[operationPos]) {
            ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
         }

         if (ratio < 25) {
            ratio = 25;
         }

         if (ratio > 256) {
            ratio = 256;
            delay = (int) (interval - (currentTime - operationTime[operationPos]) / 10L);
         }

         if (delay > interval) {
            delay = interval;
         }

         operationTime[operationPos] = currentTime;
         operationPos = (operationPos + 1) % 10;

         // ???
         if (delay > 1) {
            for (int i = 0; i < 10; i++) {
               if (operationTime[i] != 0L) {
                  operationTime[i] += delay;
               }
            }
         }

         if (delay < 1) {
            delay = 1;
         }

         try {
            Thread.sleep(delay);
         } catch (InterruptedException e) {
            interruptionCount++;
         }

         for (; count < 256; count += ratio) {
            // TODO: logic();
         }

         fps = (1000 * ratio) / (interval * 256);

         this.repaint();

      }

   }

   @Override
   public void paint(Graphics graphics) {
      Graphics g = this.image.getGraphics();

      g.setColor(Color.WHITE);
      g.fillRect(0, 0, this.getWidth(), this.getHeight());

      g.setColor(Color.BLACK);

      int x = 4;
      int y = 16;

      for (int i = 0; i < 10; i++) {
         int optim = (operationPos - i + 20) % 10;
         g.drawString("optim" + optim + ": " + operationTime[optim], x, y);
         y += 16;
      }

      g.drawString("fps: " + fps + " ratio: " + ratio + " count: " + count, x, y);
      y += 16;
      g.drawString("delay: " + delay + " interval: " + interval, x, y);
      y += 16;
      g.drawString("interruptions: " + interruptionCount + " opos: " + operationPos, x, y);

      graphics.drawImage(this.image, 0, 0, null);
      graphics.dispose();
   }

   private static final long serialVersionUID = 3633102351301301398L;

}

Doing it wrong since 0x7CC.
Offline Dane

Senior Newbie





« Reply #1 - Posted 2013-07-02 14:39:53 »

There's a lack of replies, and it's probably because of my lack of context, sorry. Specifically what I'd like to know is what exactly 'ratio' is suppose to be doing, as well as the 'operationTime(s)'.

Is the ratio used to tell whether or not it should wait a little longer or go faster in-case of lag?

Are the operation times used for lag compensation as well?

Doing it wrong since 0x7CC.
Offline Danny02
« Reply #2 - Posted 2013-07-02 18:12:49 »

i guess this is not your code, becasue I would advice you to look for some other game loop code.

Perhaps the author has some ├╝bercool time measuring logic in this code, but when he can't code it in any sane way so that other can understand it just remotly, it is crap.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dane

Senior Newbie





« Reply #3 - Posted 2013-07-02 21:02:05 »

It's actually the loop that was used in a game called RuneScape, but this is from their #317 revision client which is from 2006. It was originally obfuscated so all the names were like 'anInt4' 'anLong1'. So the variable names are most likely nowhere near the original.

If you read closely to what it's doing though, it makes somewhat sense. It's just a matter of figuring out what exactly they were trying to do. The original source probably had documentation on what was going on and an explanation.

Here are some points that I found could contain some information

1  
2  
3  
4  
5  
         // 2560 is 256 * 10
         // 2560 is also half of what ( interval * 256 ) should be.
         if (currentTime > operationTime[operationPos]) {
            ratio = (int) (2560 * (interval / (currentTime - operationTime[operationPos])));
         }


1  
2  
3  
4  
5  
6  
         // At this point, ratio should be 256 if everything went smoothly.
         // (1000 * 256) = 256000
         // (20 * 256) = 5120
         // 256000 / 5120 = 50 (Goal FPS)
         
         fps = (1000 * ratio) / (interval * 256);

Doing it wrong since 0x7CC.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2013-08-31 20:49:55 »

Well, this code was also obfuscated quite a bit. It may be that it is intentionally cluttered to make its intention unclear. I know for a fact that the 317 client is not the most easily understandable piece of work ever written, and it could be for reasons other than insane british programmers.

Just throwing it out there. It could be something else entirely.

Offline lcass
« Reply #5 - Posted 2013-10-22 20:08:36 »

Ahh us british eh?
Offline kramin42
« Reply #6 - Posted 2013-10-23 00:05:07 »

I'm not 100% sure what all the code is doing, but it seems to be adjusting the number of logic updates per frame between 1 and 10 to try and keep the framerate constant.

EDIT:

I believe the loop below adjusts the operation times so they don't count the thread.sleep(delay)
1  
2  
3  
4  
5  
6  
7  
         if (delay > 1) {
            for (int i = 0; i < 10; i++) {
               if (operationTime[i] != 0L) {
                  operationTime[i] += delay;
               }
            }
         }


Also, it seems something is missing because the variable 'count' is never reset to zero or decreased Huh

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline philfrei
« Reply #7 - Posted 2013-10-23 02:44:44 »

I'm guessing some sort of smoothing is going on, like a 10 element moving average.

I think a delay is calculated, constrained by upper & lower limits mysteriously (to me), then 1/10th of that delay is added into each of 10 elements of operationTime. The amount of delay is aimed at giving a target rate of 50fps.

The main pointer into operationTime is operationPos, which rotates circularly through this array via an increment and mod 10, and is involved in calculating an amount of delay based on the current difference between the actual and the target times.  Huh


 

"It's after the end of the world! Don't you know that yet?"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!