Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 2013-07-02 23: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);
2  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 2013-07-02 16: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?
3  Game Development / Performance Tuning / Can somebody explain this loop? on: 2013-07-01 18: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;

}
4  Game Development / Newbie & Debugging Questions / Re: Keyboard input wont work on: 2013-02-28 09:58:55
Also if you're going to use more than 3 if statements, you should use a switch:

1  
2  
3  
4  
5  
6  
7  
8  
9  
switch( keyCode )
{
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


For multiple keys doing the same thing:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
switch( keyCode )
{
   case KeyCode.VK_W:
   case KeyCode.VK_UP:
      ypos++;
      break;
   case KeyCode.VK_S:
   case KeyCode.VK_DOWN:
      ypos--;
      break;
}


Good night.  Grin
5  Game Development / Game Mechanics / Re: Single Thread Scheduled Executor For Processing on: 2013-02-28 00:41:55
Well actually I never said this was superior to anything, I just asked for your opinions of it.  Grin
6  Game Development / Game Mechanics / Single Thread Scheduled Executor For Processing on: 2013-02-27 22:28:48
I was wondering what your opinions are of this:

 Grin

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  
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class Game implements Runnable
{

   private ScheduledExecutorService service;
   private boolean running;

   public Game()
   {
      this.service = Executors.newSingleThreadScheduledExecutor();
      this.start();
   }

   public void start() {
      if (!running) {
         this.service.submit(this);
      }
   }

   public void stop() {
      this.running = false;
   }

   @Override
   public void run() {
      try {

         // Do stuff here.

         // Sleep
        Thread.sleep(50);

         // If we're still active, re-submit.
        if (this.running) {
            this.service.submit(this);
         }

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}
7  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 04:05:29
Huh

When I said writing your own rasterizer, I meant it as a joke. It will lead to pretty terrible performance compared to LibGDX and you will be nowhere closer to making a 2D game. Can I ask why you didn't like the library and/or how you feel it could be improved? 

Just curious. Wink

LibGDX may have an upper hand on performance, but that doesn't necessarily mean the rasterizer would be bad. Smiley

I didn't enjoy using the library because I feel like I should be learning how to create my own tools and use those tools to make my own content.

I've got a severe case of NIH and so far nothing has cured me of it. Sad


Edit: Actually, I'm thinking of using either LWJGL or some basic wrapper for OpenGL.

I know LibGDX is derived from LWJGL but it's just the way LibGDX is setup that I don't like very much.
8  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 02:42:30
After using libGDX, I've determined that it just isn't for me, instead I'll be checking out Relminator's link and just learning to create my own 3D rasterizer.

Thanks everybody for your input! Smiley
9  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-21 07:04:07
The main reason why I don't like using third party libraries is because I like to know how everything works.

For example; I've always wanted to create my own really basic 3D rendering. Similar to what RuneScape used back in 2003-2006. (Software Rendering)
But their client's rendering is so obfuscated that I couldn't learn from it.
10  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 22:35:41
I know what forms data can take.  Roll Eyes

I was just giving an idea of what he could do; which is send the image's pixels in the form of an integer and interpret them on the client as an integer.

Anyways, I don't think sending images from the client to the server is a great idea unless they're being cached/saved somewhere where they can be re-loaded, and if you plan on having those same images change then somehow a way to check if they're the same version as the servers. (Like a CRC check or something.)
11  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 22:22:11
An integer is a primitive data type that is made up of 32 bits. (4 bytes)   Shocked
12  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 17:38:12
After checking LibGDX's Wiki, I've decided to move on from tinkering with my 'while (running)' statement and spamming draw().
I'm comfortable and confident with saying I think this is going to go well. Smiley

You've all helped convinced me, I really appreciate it.  Grin
13  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 16:07:25
There is no spoon image. There are no integers or files or music files or whatever.

If you can send bytes from A to B you can send anything.

If you create a BufferedImage with the type INT_RGB/ARGB then the data is an integer, like a white pixel would be 0xFFFFFF.

Plus an integer is 4 bytes, so I don't see how that wouldn't be applicable.
14  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 07:40:41
Then try lwjgl. I am sure davedes will post his great tutorials on it.  Wink

If you don't like the idea of using libs then why are you using any of the built in java libs?

Haha you got me there, what I meant specifically were libraries other than the stock ones. Tongue
15  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 07:21:26
Here's an idea:

(Assuming the image is a BufferedImage)
Writing:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int[] data = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

// Write the length of the image.
net.writeInteger( data.length );
net.writeShort( img.getWidth() );
net.writeShort( img.getHeight() );

// Write the image's data.
for (int i : data) {
   net.writeInteger(i);
}


Reading:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int available = net.readInteger();
int width = net.readShort();
int height = net.readShort();

BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

for (int i = 0; i < available; i++)
{
   data[i] = net.readInteger();
}

16  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 06:52:15
Thanks, but I was looking for more of an alternative that wouldn't include using libraries that weren't made by myself.
Although- It does look interesting so I will definitely check it out and give it a go! Smiley

The reason why I'm looking for an alternative that doesn't use a library; is because I've got this thing where I don't feel as accomplished if I didn't make 100% of it.
17  Game Development / Performance Tuning / 2D Drawing on: 2013-02-19 02:14:14
I'm trying to figure out what the best way to draw a 2d game is, and I'm pretty much stumped.

I use a StrategyBuffer in a canvas at the moment, and this is basically what I'm doing right now:

Updating method: http://pastebin.com/zrmmTGEg
Drawing method: http://pastebin.com/924nhQkp
Image2D.java: http://pastebin.com/T16CrRq2

Educate me.  Grin
18  Game Development / Newbie & Debugging Questions / Re: Bitwise Operation Issues on: 2013-02-17 12:39:01
You clean your payload on every get, since the Pair constructor calls clean() on it...

1  
2  
3  
4  
5  
6  
      public Pair(Payload payload, T value)
      {
         this.payload = payload;
         this.value = value;
        this.payload.clean().reset();
      }


Unoffensive tip: learn how stepping and variable inspection work in your IDEs debugger Wink

Btw. this code produces a lot of garbage (memory management wise), since you effectively creating multiple temporary Pair objects on every get call. Better refactor the gets to some equivalents of your put mechanism.

Thanks for this, I don't know what I was thinking when I put that in the constructor. Completely my fault for working on this at 3AM in the morning.

Also I hope I refactored properly, I've edited the original post with the updated source. Smiley


Edit: Also it appears getDouble or putDouble may not be working properly.

1  
2  
3  
4  
5  
6  
7  
8  
         Payload p = new Payload(32);
         p.putDouble(123.10d);
         p.putFloat(123.10f);
         p.putLong(0x73FD73FDL);

         p.reset();

         System.out.println("Double: " + p.getDouble() + ", Float: " + p.getFloat() + ", Long: " + Long.toHexString(p.getLong()));


Output:
1  
Double: 8.49591604E-315, Float: 123.1, Long: 73fd73fd
19  Game Development / Newbie & Debugging Questions / Bitwise Operation Issues on: 2013-02-17 11:27:01
I can't figure out why this isn't writing/reading properly.

Basically what I'm trying to do is write numbers to an array of bytes, and then read them. It's pretty self-explanatory if you take a look.

Any help would be greatly appreciated.


Test.java:
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  
import org.valhalla.io.Payload;

public class Test
{

   public Test()
   {
      try {
         Payload p = new Payload(32);
         p.putLong(0x7fffffffffffffffL);
         p.putInt(0xFFFFFF);
         p.putShort(0xFFFF);
         p.put(0xFF);
         p.putFloat(1.234f);
         
         p.reset(); // Resets the position back to zero.
       
         System.out.println("Value: " + p.getLong() + ", " + p.getInt() + ", " + p.getShort() + ", " + p.get() + ", " + p.getFloat());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      new Test();
   }

}


Output:
1  
Value: -1, 16777215, 65535, 255, 1.234


Payload.java:
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  
package org.valhalla.io;

import java.io.IOException;
import java.io.OutputStream;

public class Payload
{

   private byte[] data;
   private int position = 0;
   private OutputStream out;

   public Payload(int size)
   {
      this.data = new byte[size];
   }

   public Payload(OutputStream out, int size)
   {
      this.out = out;
      this.data = new byte[size];
   }

   public Payload(byte[] data)
   {
      this.data = data;
   }

   public int get() {
      return (data[position++] & 0xFF);
   }

   public Payload put(int i) {
      this.data[position++] = (byte) (i & 0xFF);
      return this;
   }

   public boolean getBoolean() {
      return get() == 1;
   }

   public Payload putBoolean(boolean flag) {
      this.put(flag ? 1 : 0);
      return this;
   }

   public int getShort() {
      return get() << 8 | get();
   }

   public Payload putShort(int i) {
      put(i >>> 8);
      put(i);
      return this;
   }

   public int getInt() {
      return getShort() << 16 | getShort();
   }

   public Payload putInt(int i) {
      putShort(i >>> 16);
      putShort(i);
      return this;
   }

   public long getLong() {
      return getInt() << 32L | getInt();
   }

   public Payload putLong(long l) {
      putInt((int) (l >>> 32L));
      putInt((int) l);
      return this;
   }

   public double getDouble() {
      return Double.longBitsToDouble(getLong());
   }

   public Payload putDouble(double d) {
      this.putLong(Double.doubleToLongBits(d));
      return this;
   }

   public float getFloat() {
      return Float.intBitsToFloat(getInt());
   }

   public Payload putFloat(float f) {
      this.putInt(Float.floatToIntBits(f));
      return this;
   }

   public Payload reset() {
      this.position = 0;
      return this;
   }

   public Payload clean() {
      for (int i = 0; i < this.data.length; i++) {
         this.data[i] = 0;
      }
      return this;
   }

   public Payload flush() throws IOException {
      if (this.getOutputStream() == null) {
         System.out.println("Warning: Attempted to flush payload with no output stream!");
         return this;
      }
      out.write(this.data);
      return this.clean().reset();
   }

   public Payload setData(byte[] data) {
      this.data = data;
      return this;
   }

   public final byte[] getData() {
      return this.data;
   }

   public Payload setOutputStream(OutputStream out) {
      this.out = out;
      return this;
   }

   public OutputStream getOutputStream() {
      return this.out;
   }

}
20  Game Development / Articles & tutorials / Re: Playing Sound on: 2013-02-14 01:19:14
I'm not sure if I'm just confused or not; but isn't that 512kb, not 128kb?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (62 views)
2014-04-15 18:08:23

BurntPizza (60 views)
2014-04-15 03:46:01

UprightPath (73 views)
2014-04-14 17:39:50

UprightPath (56 views)
2014-04-14 17:35:47

Porlus (73 views)
2014-04-14 15:48:38

tom_mai78101 (99 views)
2014-04-10 04:04:31

BurntPizza (159 views)
2014-04-08 23:06:04

tom_mai78101 (254 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!