Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Saving a "map" into a single file  (Read 854 times)
0 Members and 1 Guest are viewing this topic.
Offline Anju_Maaka

Junior Devvie


Medals: 1
Exp: 2 years



« Posted 2017-10-19 21:56:10 »

FINAL UPDATE: I managed to work it out. Am still interested in case anyone has a nice way of putting together a large number of objects into a single file in a way that is suitable for saving game map data

I am working on a Tile-based labyrinth game. I want to be able to save an entire Map (or level, you might also call it) into a single file.
Currently I can save all the information about the Map structure as text and use that to recreate the Map later, but I have to save/load all the images I use to individual files which are loaded separately. If possible, I would like to be able to save all the images I use into the same file as the map itself. Maybe in a structure similar to how a folder looks like, only as a single file. This is to guarantee that if the Map exists, all of the resources are there as well.

So, my question; is there a way to make a single file that can contain multiple files, similar to how a folder would be like? The file would start with a set of text (a number of lines) and it'd be fine if I have to put the information about the images that are stored there as text as well if I need to do that in order to load the images that I want to save after the text in the file

UPDATE: I am trying to use Zip Files for this. This is my code so far (first part seems to work, see bottom of the post for my current problem; reading the saved image back)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public static void saveAllToFile(String path, String textData, String[] imageNames, BufferedImage[] images) throws Exception{
      File saveFile = new File(path);
      if(!saveFile.exists()) saveFile.createNewFile();
      FileOutputStream fos = new FileOutputStream(saveFile);
      ZipOutputStream zos = new ZipOutputStream(fos);
     
      addToZipFile("TEXT_DATA", textData.getBytes(), zos);   //First store the general Text Data that I want to access later as a single String
     
      int limit = Math.min(imageNames.length, images.length);
      ByteArrayOutputStream baos;
      for(int i=0; i<limit; i++){
         baos = new ByteArrayOutputStream();   //Using this method to convert the BufferedImage to a byte[]
         ImageIO.write( images[i], "jpg", baos );
         baos.flush();
         addToZipFile(imageNames[i], baos.toByteArray(), zos);   //Then I add that image data to the Zip File
         baos.close();
      }
     
      zos.close();
      fos.close();
   }
   
   private static void addToZipFile(String entryName, byte[] data, ZipOutputStream zos) throws Exception {
      if(DEBUG) System.out.println("Writing '" + entryName + "' to zip file");
     
      ZipEntry zipEntry = new ZipEntry(entryName);   //Create an Entry to store the data in
      zos.putNextEntry(zipEntry);
      zos.write(data, 0, data.length);   //Write the Entry to the Zip File
     
      zos.closeEntry();
   }


The issue I am facing now is how to properly read it back later.
SECOND UPDATE: I managed to figure it out. All is well!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   public static BufferedImage[] loadImagesFromZipFile(String path) throws Exception{
      File file = new File(path);
      if(!file.exists()) return null;

      ZipFile zFile = new ZipFile(file);
     
      ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
     
      FileInputStream fis = new FileInputStream(file);
      ZipInputStream zis = new ZipInputStream(fis);
     
      ZipEntry entry;
      BufferedImage img;
      while((entry = zis.getNextEntry()) != null){
         if(entry.isDirectory()) continue;
         
         img = ImageIO.read(zFile.getInputStream(entry));

         if(img != null) images.add(img);
      }
     
      return images.toArray(new String[images.size()]);
   }
Offline Gornova
« Reply #1 - Posted 2017-10-20 07:23:19 »

I am working on a Tile-based labyrinth game. I want to be able to save an entire Map (or level, you might also call it) into a single file.
Currently I can save all the information about the Map structure as text and use that to recreate the Map later, but I have to save/load all the images I use to individual files which are loaded separately. If possible, I would like to be able to save all the images I use into the same file as the map itself. Maybe in a structure similar to how a folder looks like, only as a single file. This is to guarantee that if the Map exists, all of the resources are there as well.

So, my question; is there a way to make a single file that can contain multiple files, similar to how a folder would be like? The file would start with a set of text (a number of lines) and it'd be fine if I have to put the information about the images that are stored there as text as well if I need to do that in order to load the images that I want to save after the text in the file

Why save all information about a level, even resources? If resources are static, you don't need to save images, or level structure, but only entities state (position, hp, etc...)

Blog | Last game A droid story
Offline Anju_Maaka

Junior Devvie


Medals: 1
Exp: 2 years



« Reply #2 - Posted 2017-10-23 15:57:08 »

Quote
Why save all information about a level, even resources? If resources are static, you don't need to save images, or level structure, but only entities state (position, hp, etc...)

I'm afraid I don't quite understand what you mean. If I don't save these resources (images, level structure, etc.) then how is the program meant to "remember" how it looks like after the program is shut down? If I have an image with a nice texture of a rock, which I use as an obstacle in the game, I need to save that as a file right? If I compile a JAR file with the game I'm making and I send it to a friend for testing, the game will need that image to render the map, right?
What I see as a potential problem is if I have hundreds of texture images and I just put them inside folders, if some disappears the game will have problems. On the other hand, if all are in a single file then you either have a fully functional map, or you have nothing at all.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zngga
« Reply #3 - Posted 2017-10-23 20:19:55 »

Quote
Why save all information about a level, even resources? If resources are static, you don't need to save images, or level structure, but only entities state (position, hp, etc...)

I'm afraid I don't quite understand what you mean. If I don't save these resources (images, level structure, etc.) then how...[snip]

This is not what Gornova meant. Your game should have resources shipped with the game itself. The images, sounds, and the like should come with the game, where as a save file like a map should only refer to those resources and keep information like positions, states, and other variables. The Game and all the resources needed are distributed as one package/zip/jar and saves are separate, and if you would like to share a save file the recipient would need a copy of the base game with all the resources first. I hope that makes sense.

My code never has bugs... it just develops unexpected features!
Offline meva

Senior Newbie


Exp: 3 years



« Reply #4 - Posted 2017-10-23 21:02:47 »

Hi,

You can choose serialization mechanism.
You can find a lot of tutorials online.

I agree with previous posts. You dont need to store pictures.
Consider saving a colored map e.g. 1024x1024 where each pixel is represented by an int (4 bytes in java). This is wasting of time and storage space for a single map.

You can bind textures to objects (e.g by numbers or names of textures) and store only the bind table.
Offline Emmsii
« Reply #5 - Posted 2017-10-24 08:37:06 »

If I don't save these resources (images, level structure, etc.) then how is the program meant to "remember" how it looks like after the program is shut down?


Are you referring to bundling all your game assets into one file to distribute along side your jar file? You can put all your game assets inside the final .jar file, so the only thing you will send to players is a single jar file.

If you mean saving all the game assets when the player saves the game, then that's a bad idea. Say your game is 10mb, with the .jar file and all assets. If you save the player's progress you'll end up with a save file that's 10mb, there's no need to duplicate all the game assets.
Offline Gornova
« Reply #6 - Posted 2017-10-24 16:11:44 »

I'll try to elaborate.

Let's think to your level as 100x100 grid. Each element of that grid referes to a specific texture, for example water.png
If you save your level as is, you save 100x100xwater.png size => a lot of wasted space!
If you save the reference to a resource, for example save a bit like tileType = 0 ( meaning water ) You can store a level with 100x100x1 bit => a lot of space not wasted Cheesy

Hope you can understand what I mean Cheesy

Blog | Last game A droid story
Offline JaredBGreat
« Reply #7 - Posted 2017-10-25 18:50:05 »

Building on what others have said, I would probably not save any graphics in level or save files -- I'd save them in the jar (for small games) or as their own separate part of a large zip for large games.  Either way, I'd keep the graphics separate from the level data.  The only reason I might not do that would be if the tiles were generated procedurally or I wanted an option to support extensive modding.

What I do is save all graphics in a kind of registry (an ArrayList works for this) where they have IDs equal to there array index.  Different tile types hold the ID's of their graphic for drawing, and would in turn have their own idea for placing them in the map.  I handle sounds, 3d meshes, and just about any resource type that could be attached to more than one thing or put in more than one place that way.  Each resource type then has its own folder (and possibly sub-folders), with a text file listing and name all other files.  This way the loading system can get the list of files to load as data as well, only the general types have to be hard-coded.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!