Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 2779 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2009-01-27 00: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 01: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 Ninja


Medals: 15
Projects: 6
Exp: 10 years


Java games rock!


« Reply #2 - Posted 2009-01-27 05: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..

Java4k RIP 2014
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jono
« Reply #3 - Posted 2009-01-27 05: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: 16
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2009-01-27 08: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 Knight


Medals: 15


falling into the abyss of reality


« Reply #5 - Posted 2009-01-27 12: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! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #6 - Posted 2009-01-27 13: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: 16
Projects: 19


Mojang Specifications


« Reply #7 - Posted 2009-01-27 13: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 Knight


Medals: 15


falling into the abyss of reality


« Reply #8 - Posted 2009-01-27 13: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! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #9 - Posted 2009-01-27 15: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.

 

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 (33 views)
2014-12-15 09:26:44

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

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

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

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

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

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

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

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

toopeicgaming1999 (32 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!