Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Best way to read/write tiles or a 2D integer set from a file? XML?  (Read 682 times)
0 Members and 1 Guest are viewing this topic.
Offline lolwutpear

Innocent Bystander





« Posted 2011-06-19 16:06:27 »

I'm working on a game that utilizes tiles in a weird way. It's not your average kind of game where I could just make a nice default texture and focus 'the camera' on a certain section.
Think of the game sort of like minecraft-- the terrain at the start is randomly generated and it's absolutely massive in terms of size. For example, I started getting "out of memory" errors when I wanted to make a 2D int array that had 10000 x 10000 indexes [is that the proper plural?]... I tried storing the tile number in an int[][] where the arrays were an int(x)(y) Sad. Thus, I can't store all the tiles I want in a memory array. How would I go about storing the tiles in a file?
Thus far they only have to be numbers, very basic. I could even write them as bytes at this point, but I wanted to keep integers to allow for more stuff later on.

Basically my game works like this-- or rather this is how I want it to work (if you know a better way I'll change it guaranteed, remember I am a noob Smiley):
- Interface draws tiles from top left to bottom right (2D flat game, no isometrics or anything). It draws 16 x 16 tiles on the screen.
- Lets say it wants to draw from (x, y) down to (x + 16, y + 16), the code would go like:
1  
2  
3  
 for (int a = x; a < x + 16; x++) 
    for (int b = y; b < y + 16; b++)
        // draw code here

Actually I probably did the height last and the width first, but either way: that the only way I can think of rendering a dynamic world.
- NPCs and stuff would be in a different file or loaded into the game as an object directly instead and are drawn on the tiles (should I also store these in a file somewhere?)


I don't know how to go about properly formatting the tiles for storage. I heard XML files are great, but I don't know how feasible it is for it to read 16x16 tiles every 20ish milliseconds.


Are there any better ways to go about reading/writing/printing 2D tiles on an interface than how I've proposed? Please note that I'm still learning java, though I've done 2-3 other languages so I understand how syntax works... hope that helps!
Offline cylab

JGO Ninja


Medals: 38



« Reply #1 - Posted 2011-06-19 22:31:47 »

XML files are not very good for massive amount of data because of the large markup overhead. Just use an RandomAccessFile, seek to the desired position and read bytes. If you need more than 256 possible values, read two bytes in a sequence, shift the first byte 8 bits and add the second to get an 16bit integer (65536 values).

Keep in mind that you get an OutOfMemory error because of the default max heap setting, not because of running out of "real" memory. See the -Xmx option of the java launcher.

Also if you really want to make a huge random level, you should think about splitting it in chunks, generate them on the fly and only store chunks on disk, that are already visited (thats afaik how minecraft does it)

Mathias - I Know What [you] Did Last Summer!
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2011-06-20 15:39:20 »

Since you mentioned minecraft

Minecraft breaks it into chunks of smallar arrays

I believe his 3D array is byte[16][16][256]

16x16 for x/y and 256 for height/z
But if you are keeping it in x/y only then id personally recommend something a little larger than 16x16, perhaps 256, 1024, or 2048 per chunk.  You can go higher, but just experiment.  Once its written for dynamic chunks, changing chunk size is easy

"Experience is what you get when you did not get what you wanted"
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.

Riven (16 views)
2014-07-29 18:09:19

Riven (11 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!