Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (788)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (860)
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 1341 times)
0 Members and 1 Guest are viewing this topic.
Offline lolwutpear

Innocent Bystander

« Posted 2011-06-19 14: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:
 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 Kernel

Medals: 194

« Reply #1 - Posted 2011-06-19 20: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 13: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  

hadezbladez (2078 views)
2018-11-16 13:46:03

hadezbladez (796 views)
2018-11-16 13:41:33

hadezbladez (2047 views)
2018-11-16 13:35:35

hadezbladez (412 views)
2018-11-16 13:32:03

EgonOlsen (3441 views)
2018-06-10 19:43:48

EgonOlsen (3663 views)
2018-06-10 19:43:44

EgonOlsen (2270 views)
2018-06-10 19:43:20

DesertCoockie (2991 views)
2018-05-13 18:23:11

nelsongames (3059 views)
2018-04-24 18:15:36

nelsongames (3726 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 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‑
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!