6

I'm creating a basic 2D game (well a game engine) and I've currently been developing the file formats for my data. Of course, for this game to run, I will need cache. I find it quite unprofessional to leave all the data for the game to not be in a single file (well not necessarily, as long as the files are in a cache format of some kind).

So that's why I've came here to ask. I was thinking of doing a zip file but I feel like that isn't the best way at all. I was also thinking of doing another binary writer which will have headers (type of file, "location") and an enclosing tag for each of the files so it would be easy to interpret. But I felt like that was too inefficient.

So please, do you have any ideas?

Note: This game engine is really just for learning purposes.

tl;dr I need an efficient way to store data such as images for my game I'm making.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Fellixombc
  • 123
  • 2
  • 10
  • Do you want to write to the cache as well, or are you simply asking about packaging up all your static content? – Dilum Ranatunga Jul 13 '11 at 18:39
  • Plenty of games store images and sounds as basic image and sound files. If you really want to do this, add it after the game is done, otherwise it's going to slow down development as you have to compile code AND assets. – thedaian Jul 13 '11 at 18:45
  • @Dilum Ranatunga - I want to be able to write to the cache as well, or at least be able to add new content. – Fellixombc Jul 13 '11 at 19:54

3 Answers3

8

You can store any object as .dat file:

public class MyGame implements Serializable 
{ 
    private static void saveGame(ObjectType YourObject, String filePath) throws IOException 
    { 
        ObjectOutputStream outputStream = null; 
        try 
        { 
            outputStream = new ObjectOutputStream(new FileOutputStream(filePath)); 
            outputStream.writeObject(YourObject); 
        } 
        catch(FileNotFoundException ex) 
        { 
            ex.printStackTrace(); 
        } 
        catch(IOException ex) 
        { 
            ex.printStackTrace(); 
        } 
        finally 
        { 
            try 
            { 
                if(outputStream != null) 
                { 
                    outputStream.flush(); 
                    outputStream.close(); 
                } 
            } 
            catch(IOException ex) 
            { 
                ex.printStackTrace(); 
            } 
        } 
    } 

    public static ObjectType loadGame(String filePath) throws IOException 
    { 
        try 
        { 
            FileInputStream fileIn = new FileInputStream(filePath); 
            ObjectInputStream in = new ObjectInputStream(fileIn); 
            return (ObjectType) in.readObject(); 
        } 
        catch(FileNotFoundException ex) 
        { 
            ex.printStackTrace(); 
        } 
        catch(IOException ex) 
        { 
            ex.printStackTrace(); 
        } 
    } 
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

I don't think that zip is the wrong way. It is a rather good way. Just put everything in a jar-File. You will even be able to use a classloader to load it.

marc
  • 6,103
  • 1
  • 28
  • 33
  • +1: Jar is a better choice than Zip, and using getResourceAsInputStream() to get any resource from the classloader means you can have the file sin a directory for development, later in a jar without changing the code – Peter Lawrey Jul 13 '11 at 18:26
  • You cannot write to a JAR that is pegged by a classloader. So this solution would fail as a writeable cache. – Dilum Ranatunga Jul 13 '11 at 18:36
  • That sounds rather interesting actually, and would be a good learning experience, thank you! – Fellixombc Jul 13 '11 at 19:51
1

For pragmatic reasons, I would recommend using multiple files but putting them all in a subdirectory in temp.

But if part of your learning exercise is related to this topic, here are my suggestions:

First, stay away from any format like ZIP, where there is a heavy penalty for deleting, updating, adding, jumping. Even with the index files of a JAR, they are poor for anything other than compression.

So you can read up on file systems. Essentially, you are writing a file system where the 'disk' is just a binary file. That should give you a high level idea of allocation tables, etc. In order to implement such a design, you need read/write access to the underlying file; look at java.io.RandomAccessFile

Finally, if you are willing to use Java7, you can play with the new filesystem SPIs.

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • Thank you very much! I will go ahead and look at RandomAccess File and see what I can do. And yes, I'm trying to learn new ways to do things along with exploring java a bit more! – Fellixombc Jul 13 '11 at 19:53