Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  help to read a level file  (Read 2218 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2009-01-27 01:49:48 »

I have the following scenario:

Created a level file composed by bytes:

1  
2  
    byte[] levels = { 72, 116, -15, 7, 60, -31, -123, 74, 46, 109 
     ... (I have 255 bytes)


The file was created using the following code:

1  
2  
      FileOutputStream fos = new FileOutputStream("l");
      fos.write(levels);


In my game I'm reading using (HERE IS MY PROBLEM):

1  
      getClass().getClassLoader().getResourceAsStream("l").read(levels);


The code above read bytes  0-255 range which return a different result from my original array (byte type -128-127 range). The values -15 that was previously saved is read as 113... and then some items in my game are not displayed because of this issue...

How can I fix it???
Offline Hsaka
« Reply #1 - Posted 2009-01-27 02:58:49 »

Not sure if there is an easier way, but..

Since the range is now 0-255, then :
-128 (in the original -128-127 range) = 0 (in the 0-255 range) .
0 (in the original -128-127 range) = 128 (in the 0-255 range) .
127 (in the original -128-127 range) = 255 (in the 0-255 range) .

So to convert, subtract 128 from the values is in the level array before you use them to generate your level. [113-128=-15]
Offline moogie

JGO Knight


Medals: 7
Projects: 5


Java games rock!


« Reply #2 - Posted 2009-01-27 06:04:17 »

I find that quite odd... the byte array saved should be identical to the byte array read in.

perhaps you should provide some more relevant code, incase you are doing something odd..
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Jono

Senior Member


Medals: 1
Projects: 1



« Reply #3 - Posted 2009-01-27 06:59:46 »

The code above read bytes  0-255 range which return a different result from my original array (byte type -128-127 range). The values -15 that was previously saved is read as 113... and then some items in my game are not displayed because of this issue...

How can I fix it???
Java bytes only come in the -128-127 flavour.. are you sure they haven't been read in as ints?

There might be something funny going on with the highest order bit: its 1 for -15 and 0 for 113, but otherwise they're the same.
Offline Markus_Persson

JGO Wizard


Medals: 8
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2009-01-27 09:22:00 »

Pardon the tangent, but..

I have the following scenario:

Created a level file composed by bytes:

1  
2  
    byte[] levels = { 72, 116, -15, 7, 60, -31, -123, 74, 46, 109 
     ... (I have 255 bytes)


That code takes up a whole bunch of space, as it gets converted into something like this:

1  
2  
3  
4  
5  
6  
byte[] levels = new byte[255];
levels[0] = 72;
levels[1] = 116;
levels[2] = -15;
levels[3] = 7;
[...]


My version of javac turns each of those assignments into
1  
2  
3  
4  
dup
bipush  7
bipush  74
bastore


That's six bytes of (uncompressed) class file per byte in the array, or 1530 bytes for a 255 entry byte array.
(Actually, the first six entries only take up five bytes each thanks to iconst_n)

This would all probably compress really well, but I thought you'd probably want to know anyway.

Play Minecraft!
Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #5 - Posted 2009-01-27 13:05:05 »

Nothing strange about it; for convenience the write & read methods of Streams accept & return integer types, however only the lowest 8 bits of the integer are used.

Regarding your question, you simply need to cast the returned value to a byte.

FYI the rules for integer casting are below:
Narrowing conversions are performed by truncation.
Widening conversions are performed by sign extension.

Ergo:
1  
2  
3  
int i1 = 255; //0x000000FF
byte b = (byte)i1; // 0xFF (-1)
int i2 = (int)b; // 0xFFFFFFFF (-1), The explicit (int) cast is unnecessary, but included for clarity

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #6 - Posted 2009-01-27 14:32:23 »

@Markus, please note that the level information is stored in a file which size is 255 bytes (I have 255 levels, 1 byte per level). And I'm using a read(byte[]) function to read the file. So I won't be losing all those bytes.

@Abuse, I'm not sure how to get the cast to work. Take this example: in the original byte array that I use to save the file I have a value -15 but when I read it already returns as 113 so the cast won't work because I don't have the original value (-15).  If I change my array to integer then the read function won't accept it because it's expecting a byte array.

I need to store them as bytes to save space. I need a function to read byte and not integer but I was not able to find such function in the IO package.

Not sure if I was clear enough. I'll post an example code when I get back to home...
Offline Markus_Persson

JGO Wizard


Medals: 8
Projects: 19


Mojang Specifications


« Reply #7 - Posted 2009-01-27 14:40:47 »

@Markus, please note that the level information is stored in a file which size is 255 bytes (I have 255 levels, 1 byte per level). And I'm using a read(byte[]) function to read the file. So I won't be losing all those bytes.

Oh, I see. =D

Although the overhead of an extra file might be bad anyway, since zip compresses files individually and you need to reference several long class and method names to be able to load the data.

Play Minecraft!
Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #8 - Posted 2009-01-27 14:47:28 »

Oh, you'e using the byte[] read/write methods - I should have read more carefully  Wink

You must have a bug somewhere; I doubt it's coincidence that -15 in a byte is 11110001, and 113 is 01110001

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #9 - Posted 2009-01-27 16:14:32 »

yep.. that was a bug in my automated ant script to generate the obsfuscated version of the file... it was getting the previous version of my level file... Lips Sealed

sorry... persecutioncomplex
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (117 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.125 seconds with 23 queries.