Results 1 to 16 of 16
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Efficient 2D Game Mapping

    I am trying to make a map for a 2D game.
    I have tried to make it using an XML file but it takes a while to load.
    All I want it to store is a bunch of tiles which are seperated by spaces and lines.
    It also must contain a size.
    This is what my XML file looked like:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <map>
    	<size>5 5</size>
    	<layer>
    		<tiles>
    		1 1 1 1 1 
    		1 1 1 1 1 
    		1 1 1 1 1 
    		1 1 1 1 1 
    		1 1 1 1 1 
    		</tiles>
    	</layer>
    	<layer>
    		<tiles>
    		-1 -1 -1 -1 -1 
    		-1 -1 -1 -1 -1 
    		-1 -1 5 4 7 
    		-1 -1 3 -1 3 
    		-1 -1 6 4 2 
    		</tiles>
    	</layer>
    </map>
    This just small example. I would want maps like 100 by 100 in size. But this would take up a lot of space. Because I would have like 5 layers for each map. And also it would take hours to load a map.
    What would be an easy way using a similar layout to make/parse maps in java?
    Maybe there is a way to make it parse the files quicker?
    EDIT:: Also is there a way to make the file sizes smaller?
    Last edited by PhQ; 11-28-2011 at 07:28 PM.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    up up.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    Does the disk file have to contain ASCII text? Could it be in another format like binary for example?

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Quote Originally Posted by Norm View Post
    Does the disk file have to contain ASCII text? Could it be in another format like binary for example?
    Yes. It can be a binary.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    That could speed up reading the data. There wouldn't be any conversions from String.
    You should write a small test program to compare the different times it takes to read an XML file of data vs reading a binary file, just to see if the extra effort is worth the trouble.
    Another possibilty would be a special purpose program that would read the XML file and then serialize that data for faster loading in the game program. Again, write a timing test program.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Quote Originally Posted by Norm View Post
    That could speed up reading the data. There wouldn't be any conversions from String.
    You should write a small test program to compare the different times it takes to read an XML file of data vs reading a binary file, just to see if the extra effort is worth the trouble.
    Another possibilty would be a special purpose program that would read the XML file and then serialize that data for faster loading in the game program. Again, write a timing test program.
    Ok. Thanks.

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Nevermind, I think i got it.

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Ok.
    I'm getting errors.
    I'm trying to separate layers.
    I do this to write my layers:
    Java Code:
    private void Write() throws Exception {
    		DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data.bin"));
    		// -1 = new line | 0 = blank space
    		for (int x = 0; x < mapLayers.size(); x++) {
    			for (int i = 0; i < mapLayers.get(x).tiles.size(); i++) {
    				dos.writeInt(mapLayers.get(x).tiles.get(i));
    				dos.writeChar(' ');
    				dos.writeInt(mapLayers.get(x).tilesX.get(i));
    				dos.writeChar(' ');
    				dos.writeInt(mapLayers.get(x).tilesY.get(i));
    				dos.writeChar('\t');
    			}
    			dos.writeChar('\n');
    		}
    		dos.flush();
    	}
    and I am using this to read:
    Java Code:
    DataInputStream dis = new DataInputStream(new FileInputStream("Data.bin"));
    		while(true){
    			System.out.println(dis.readInt());
    			dis.readChar();
    			System.out.println(dis.readInt());
    			dis.readChar();
    			System.out.println(dis.readInt());
    			dis.readChar();
    			if(dis.readChar() == '\n'){
    				System.out.println("New layer.");
    			}
    		}
    		
    		//System.out.println("Got to the end. Total layers: " + recievedMapLayers.size());
    However I get an error and some weird values.
    This is the error:
    Java Code:
    Exception in thread "main" java.io.EOFException
    	at java.io.DataInputStream.readInt(Unknown Source)
    	at ReadAndWrite.Read(ReadAndWrite.java:32)
    	at ReadAndWrite.main(ReadAndWrite.java:22)
    What's wrong with my code?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    What's wrong with my code?
    It tries to read past the end of the data in the file.

    What stops the forever loop(while(true))?
    Perhaps you need to write an special value at the end of your data that the reader could recognize.
    Or write a count at the beginning of the data that says how much data follows.

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    If I do at the end of my writing method
    Java Code:
    dos.writeChar('\u039A');
    and this for my reading loop
    Java Code:
    while(dis.readChar() != '\u039A'){
    I get weird values.
    Last edited by PhQ; 11-29-2011 at 12:18 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    I get weird values.
    I guess that could happen.

    What are "weird values"? With binary every byte can have one of 256 different values.

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Java Code:
    2162687
    2097152
    63
    -1
    99
    4194336
    -65504
    6488096
    2162687
    2097152
    2097152
    -1
    99
    67
    -65504
    6488096
    4456480
    2097152
    2097152
    2162687
    99
    70
    -1
    6488096
    4653088
    -65504
    2097152
    2162687
    2097152
    73
    -1
    99
    4849696
    -65504
    6488096
    2162687
    2097152
    2097152
    -1
    99
    77
    -65504
    6488096
    5111840
    2097152
    2097152
    2162687
    99
    80
    -1
    6488096
    5308448
    -65504
    2097152
    2162687
    2097152
    83
    -1
    99
    5505056
    -65504
    6488096
    2162687
    2097152
    2097152
    -1
    99
    87
    -65504
    6488096
    5767200
    2097152
    2097152
    2162687
    99
    90
    -1
    6488096
    5963808
    -65504
    2097152
    2162687
    2097152
    93
    -1
    99
    6160416
    -65504
    6488096
    2162687
    2097152
    2097152
    -1
    99
    97
    -65504
    6488096
    6422560
    2097152
    2097152
    2097162
    Exception in thread "main" java.io.EOFException
    	at java.io.DataInputStream.readChar(Unknown Source)
    	at ReadAndWrite.Read(ReadAndWrite.java:36)
    	at ReadAndWrite.main(ReadAndWrite.java:23)
    There are more values at the top though. I'm reading integers (System.out.println(dis.readInt());)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    Do a simpler test with easily identifiable numbers like 1,2,3,4, etc
    Write and read back a small number of these: 10 should be enough.

    Why are you writing a space character to the file? Also the \n and ]t characters?

  14. #14
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Quote Originally Posted by Norm View Post
    Do a simpler test with easily identifiable numbers like 1,2,3,4, etc
    Write and read back a small number of these: 10 should be enough.

    Why are you writing a space character to the file? Also the \n and ]t characters?
    I thought that you have to separate the values.
    I use a \n to separate the layers.
    But now that I think about it - I can calculate the total tiles and do it that way. It works fine now without the chars.
    But I still need a way to stop the loop.
    Isn't there a way to check whether or not the next line or something is not null?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Efficient 2D Game Mapping

    Can you write a field count at the beginning and use that to control when the loop ends?

  16. #16
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Efficient 2D Game Mapping

    Quote Originally Posted by Norm View Post
    Can you write a field count at the beginning and use that to control when the loop ends?
    You're a genius.
    Thank you.

Similar Threads

  1. Game Object mapping
    By Hikilaka in forum Java Gaming
    Replies: 2
    Last Post: 10-03-2011, 12:55 AM
  2. Tell me efficient way
    By lkalaivanan in forum New To Java
    Replies: 3
    Last Post: 08-11-2011, 11:36 AM
  3. is there a more efficient way?
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 05:32 PM
  4. Replies: 2
    Last Post: 10-20-2010, 12:29 PM
  5. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 03:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •