Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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 / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 12:53:08
I will.... I think I'll call it..... Lua!

But really, it's not thaaaat bad, riiiight?
2  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:34:38
I could add listeners to when you set variables too though!  Roll Eyes

Think about the possibilities!  Clueless
3  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:32:19
Because I always prematurely optimize things, I want to do the opposite just to mess around. I could make a component system like .add(Component.class, instance) and .get() etc.
4  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:27:52
HashMaps are pretty bad for cache locality, good ol' ArrayList has your back 99% of the time. And you know what they say about premature optimization. Have you attempted to time ArrayList<Point> etc. vs. Map? I'll bet List is faster.
It's also almost guaranteed that you don't need every last drop of perf anyway. Don't think too hard about it until you need to.

Actually I was trying to do the opposite of optimization. Giving myself the seemingly worst optimization case scenario for a 2d game I want to make. The point of using a Map is so I could freely add/remove keys and values of any type to my entities.
5  Game Development / Newbie & Debugging Questions / Re: How does this work? on: 2014-11-17 04:09:52
Oh. I switch them around and get about the same exact results. The long string in place of the short string, and vice versa. I guess this means I will have to put my hope on the optimizer then. Thanks for letting me know.

Do you have any opinion on using this KeyValue class for things like storing Entity X/Y which would be constantly read/written to? I imagine it would be more optimized in that case if I were to encapsulate the x/y coordinates in an Object like a Point or something as well. I know it would be muuuch better to just define a field, but I also want to be able to add/remove any variable with any name from an entity.
6  Game Development / Newbie & Debugging Questions / How does this work? on: 2014-11-17 03:58:25
Am I doing something wrong? I wouldn't expect the second one to have a faster result..

Output:
1  
2  
Average 1: 7.33694838
Average 2: 4.06036178


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  
package dane.game.model;

import java.util.HashMap;
import java.util.Map;

public class KeyValue {

   private Map<Object, Object> keyvalues = new HashMap<>();

   public <T> T set(Object key, T value) {
      keyvalues.put(key, value);
      return value;
   }

   public <T> T get(Object key) {
      return get(key, null);
   }

   @SuppressWarnings("unchecked")
   public <T> T get(Object key, T defaultValue) {
      if (!keyvalues.containsKey(key)) {
         keyvalues.put(key, defaultValue);
      }
      return (T) keyvalues.get(key);
   }

}


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  
package dane.game.world;

import dane.game.model.KeyValue;

public class Entity extends KeyValue {

   static final int ITERATIONS = 1_000_000;
   static final double NS_TO_MS = 1_000_000.0;

   public static void main(String[] args) {
      Entity e = new Entity();

      // set values
      e.set("x", 0);
      e.set("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", 0);

      double total1 = 0, total2 = 0;

      for (int n = 0; n < 100; n++) {
         long time = System.nanoTime();

         for (int i = 0; i < ITERATIONS; i++) {
            e.get("x");
         }

         total1 += (System.nanoTime() - time) / NS_TO_MS;

         time = System.nanoTime();

         for (int i = 0; i < ITERATIONS; i++) {
            e.get("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
         }

         total2 += (System.nanoTime() - time) / NS_TO_MS;
      }

      System.out.println("Average 1: " + (total1 / 100));
      System.out.println("Average 2: " + (total2 / 100));
   }

}
7  Game Development / Newbie & Debugging Questions / Re: Diagonal Movement on 2D Tilemap on: 2014-11-15 23:24:02
1  
2  
3  
4  
5  
6  
   boolean isSolid(final int x, final int y) {
      if (x < 0 || x >= MAP_SIZE || y < 0 || y >= MAP_SIZE) {
         return true;
      }
      return solid[x][y];
   }


The thing is, I'm trying to have a tile map with pixel movement. My goal is to have Legend of Zelda styled movement. The reason I overly complicated it was because I'm trying to also make it work for entities that are larger than just one tile.

I appreciate the input though.
8  Game Development / Newbie & Debugging Questions / Diagonal Movement on 2D Tilemap on: 2014-11-15 16:37:42
I need ideas on what I should be doing if I'm walking perfectly diagonal into a tile with my logic.

Here's the entire snippet processing the movement:

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  
   public void updatePlayerPosition() {
      int destX = playerX;
      int destY = playerY;

      // hardcoded for now
      final int speed = 2;

      if (Keyboard.isDown(KeyEvent.VK_W)) {
         destY -= speed;
      } else if (Keyboard.isDown(KeyEvent.VK_S)) {
         destY += speed;
      }

      if (Keyboard.isDown(KeyEvent.VK_A)) {
         destX -= speed;
      } else if (Keyboard.isDown(KeyEvent.VK_D)) {
         destX += speed;
      }

      // movement differences
      int dx = destX - playerX;
      int dy = destY - playerY;

      // we're not moving, so we're gunna stop here.
      if (dx == 0 && dy == 0) {
         return;
      }

      // Tile boundaries our player occupies
      final int minTileX = playerX / TILE_SIZE;
      final int minTileY = playerY / TILE_SIZE;

      final int maxTileX = (playerX + (TILE_SIZE - 1)) / TILE_SIZE;
      final int maxTileY = (playerY + (TILE_SIZE - 1)) / TILE_SIZE;

      // TODO: larger sized players
      // TODO: higher movement speeds?

      int srcX = dx < 0 ? maxTileX : minTileX;
      int srcY = dy < 0 ? maxTileY : minTileY;
      int dstX = srcX + (dx < 0 ? -1 : 1);
      int dstY = srcY + (dy < 0 ? -1 : 1);

      // we're moving diagonally
      if (dx != 0 && dy != 0) {
         // is that next diagonal tile solid?
         if (isSolid(dstX, dstY)) {
            // TODO: what can we do to prevent glitching on corners?
         }
      }

      // we're moving horizontally
      if (dx != 0) {
         boolean blocked = false;

         // check every tile vertically in that direction.
         for (int y = minTileY; y < maxTileY + 1; y++) {
            if (isSolid(dstX, y)) {
               blocked = true;
            }
         }

         if (blocked) {
            // we were blocked from going right, set ourselfs to the left of
            // the blocker.
            if (dx > 0) {
               playerX = (dstX - 1) * TILE_SIZE;
            }
            // vice versa
            else {
               playerX = (dstX + 1) * TILE_SIZE;
            }
         } else {
            playerX = destX;
         }
      }

      // we're moving vertically
      if (dy != 0) {
         boolean blocked = false;

         // check every tile horizontally in that direction
         for (int x = minTileX; x < maxTileX + 1; x++) {
            if (isSolid(x, dstY)) {
               blocked = true;
               break;
            }
         }

         if (blocked) {
            if (dy > 0) {
               playerY = (dstY - 1) * TILE_SIZE;
            } else {
               playerY = (dstY + 1) * TILE_SIZE;
            }
         } else {
            playerY = destY;
         }
      }
   }


This works perfectly both horizontally and vertically, but when you go diagonally then you phase into that tile. Here's an example of that happening:


This isn't an incredible problem, as you can't actually continue moving through the tile you've phased into. But it's not beautiful to see the flickering of your character as it phases in and out diagonally each tick.

This is the position you would be in right before this happens.


The orange rectangle is outlining your current tile boundaries. My collision detection only checks if you're moving either vertically or horizontally, and doesn't handle when you're going both. This means that when I'm going diagonal towards the top right, it checks the tile to my right, and the tile above me. Those are both translucent, so the code runs true and sets my position into that diagonal tile.

Are there any solutions you guys could think of? I thought of just making it so I don't move horizontally when I collide diagonally, which fixes it. But then that breaks movement for running against a wall while trying to move towards it parallel.
9  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 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);
10  Game Development / Performance Tuning / Re: Can somebody explain this loop? on: 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?
11  Game Development / Performance Tuning / Can somebody explain this loop? on: 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;

}
12  Game Development / Newbie & Debugging Questions / Re: Keyboard input wont work on: 2013-02-28 08: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
13  Game Development / Game Mechanics / Re: Single Thread Scheduled Executor For Processing on: 2013-02-27 23:41:55
Well actually I never said this was superior to anything, I just asked for your opinions of it.  Grin
14  Game Development / Game Mechanics / Single Thread Scheduled Executor For Processing on: 2013-02-27 21: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();
      }
   }

}
15  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 03: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.
16  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-25 01: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
17  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-21 06: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.
18  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 21: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.)
19  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 21:22:11
An integer is a primitive data type that is made up of 32 bits. (4 bytes)   Shocked
20  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 16: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
21  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 15: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.
22  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 06: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
23  Game Development / Networking & Multiplayer / Re: Images over java? on: 2013-02-19 06: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();
}

24  Game Development / Performance Tuning / Re: 2D Drawing on: 2013-02-19 05: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.
25  Game Development / Performance Tuning / 2D Drawing on: 2013-02-19 01: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
26  Game Development / Newbie & Debugging Questions / Re: Bitwise Operation Issues on: 2013-02-17 11: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
27  Game Development / Newbie & Debugging Questions / Bitwise Operation Issues on: 2013-02-17 10: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;
   }

}
28  Game Development / Articles & tutorials / Re: Playing Sound on: 2013-02-14 00: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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!