Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  File Input  (Read 1960 times)
0 Members and 1 Guest are viewing this topic.
Offline krypto

Junior Devvie




while(true) { self.caffeinate (); }


« Posted 2003-08-05 09:04:29 »

I know this is probably the wrong forum, but here goes.
I have code that reads my 2d world from a file into a hacked version of the GAGE 2d API. Iy seems vary slow in doing so. I know nio is supposed to be the fasted io around, but I don't really know where to start in porting this. Here it is as it currently stands:

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
/*
 * ParallaxReader.java
 *
 * Created on June 4, 2003, 12:01 PM
 */


package com.krypto.k2.editor.io;

import com.dnsalias.java.gage.collision.CollisionMap;
import com.dnsalias.java.gage.map.*;
import com.dnsalias.java.gage.map.Tile;
import com.krypto.k2.editor.Grid;
import com.krypto.k2.editor.GridCell;
import com.krypto.k2.graphics.*;
import com.krypto.k2.graphics.GFXEngine;
import com.krypto.k2.graphics.sprites.SpriteManager;
import java.awt.*;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;


/**
 *
 * @author  Krypto
 */

public final class GageParallaxReader
{
    private static Hashtable managers = new Hashtable(4);
    /** Creates a new instance of ParallaxWriter */
    public GageParallaxReader()
    {
    }
       
    public static Parallax read(final File _file)
    {
     
       System.out.println("Reading File: " + _file.getName ());
       Parallax parallax = new Parallax();
        try
        {

            RandomAccessFile file = new RandomAccessFile(_file,"r");
           
           
            file.readUTF();
            file.readUTF();
            int numMaps = file.readInt();
            int numManagers = file.readInt();
           
            readManagerSections(numManagers,file);
            readMapSections(numMaps,file,parallax);
            SpriteManager.get().read(file);
            file.close ();      
        }
        catch (IOException e)
        {
            System.out.println("Error Writing to File");
        }
        return parallax;
    }
   
 private static void readManagerSections( final int numManagers,
                                          final RandomAccessFile file )
 {
     System.out.println("attempting to read in " + numManagers + " Managers");
     TileManager[] manager = new TileManager[numManagers];
     try
     {        
         for(int i=0;i<numManagers;i++)
         {

             String name = file.readUTF ();

             int tileWidth = file.readInt ();
             int tileHeight = file.readInt ();
             String tileSetName = file.readUTF();
             String tileName = null;
             manager[i] = new TileManager(tileWidth,tileHeight);
             BufferedImage tileSetImage = ImageManager.getImage(tileSetName);
             int col = (int)Math.floor(tileSetImage.getWidth()/tileWidth);
             int row = (int)Math.ceil(tileSetImage.getHeight()/tileHeight);
             Grid grid = new Grid(col,row,tileWidth,tileHeight);
             GridCell cell = null;
             int count = grid.getLength();

             for(int j=0;j<count;j++)
             {
                 //  Point point = grid.getPoint(i);
                 cell = grid.getCell(j);
                 
                 //BufferedImage tileImage = new BufferedImage(tileWidth,tileHeight,BufferedImage.TYPE_INT_ARGB);
                 BufferedImage tileImage = GFXEngine.getGC().createCompatibleImage(tileWidth,tileHeight,Transparency.BITMASK);
                 Graphics2D g = (Graphics2D)tileImage.getGraphics();
                 
                 g.drawImage(tileSetImage,0,0,tileWidth,tileHeight,cell.getX(),cell.getY(),cell.getX()+tileWidth,tileHeight+cell.getY(),null);
                 Tile tile = new Tile(tileImage);
                 
                 manager[i].addTile(tile);
                 //System.out.println("added tile: " + j);
             }
             

             managers.put(name, manager[i]);
             //TileTool.get().addTileset (manager[i]);
         }
     }
     catch(final java.io.IOException e)
     {
         System.out.println("Failed writing TileManager Section!!");
     }
 }
   
 private static void readMapSections(final int numMaps,
                                     final RandomAccessFile file,
                                     final Parallax parallax)
 {
      System.out.println("attempting to read in " + numMaps + " Maps");
     for(int i=0;i<4;i++)
     {
         try
         {
           //  file.readShort ();
             String name = file.readUTF ();
              String managerName = file.readUTF ();
             int cellsX = file.readInt ();
              int cellsY = file.readInt ();
              int count = file.readInt ();
              int width = file.readInt ();
              int height = file.readInt ();
             String background = file.readUTF();
             int[] tiles = new int[count];//maps[i].getTiles ();
           System.out.println("attempting to read in " + count + " Tiles");
             for(int j=0;j<count;j++)
             {              
                 tiles[j] = file.readInt ();
                 //System.out.println("Tile: " + tiles[j]);
             }
             
             TileManager manager = (TileManager)managers.get(managerName);
             //Map map = new Map(manager,cellsX,cellsY,640,380);
             Map map = new Map(manager,cellsX,cellsY,640,380);
             System.out.println("Background is: " + background);
            // map.setName(name);
             
            //for(int i=0; i<images.length; i++) ids[i] = manager.addTile(images[i]);
//             Tile[] ids = manager.getTiles();
//            for(int k=0; k<tiles.length; k++)
//            {
//                if(tiles[k] > 0) tiles[k] = ids[tiles[k]-1].;
//            }
             map.copyMap (tiles);
            // map.setBackground (Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(background)));
             parallax.addMap (map);
         }
         catch(final java.io.IOException e)
         {
             System.out.println("failed Writing Map Section");
         }
     }
     
      try
      {
      System.out.println("Reading Collision Map");
      String name = file.readUTF ();
              String managerName = file.readUTF ();
             int cellsX = file.readInt ();
              int cellsY = file.readInt ();
              int count = file.readInt ();
              int width = file.readInt ();
              int height = file.readInt ();
             String background = file.readUTF();
             int[] grid = new int[count];//maps[i].getTiles ();
          // System.out.println("attempting to read in " + count + " Tiles");
             for(int j=0;j<count;j++)
             {              
                 grid[j] = file.readInt ();
                 //System.out.println("Tile: " + tiles[j]);
             }
             
          TileManager manager = (TileManager)managers.get(managerName);
           CollisionMap cmap = new CollisionMap(manager,cellsX,cellsY,640,380);            
           cmap.copyGrid(grid);
           //cmap.copyMap(grid);
           parallax.setCollisionMap(cmap);
      }
      catch(final IOException e)
      {
          System.out.println("Failed to initialize CollisionMap");
      }
 }
}


Any suggestions on how th speed this up?

JRPG Users -  General Users Site
JRPG Developers -  The JRPG Project's Home
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2003-08-05 09:25:48 »

If the file isn't _too_ big then you could just read it all into a byte array (in one go) and then process it from that. It'd be considerably faster most probably...

Kev

Offline krypto

Junior Devvie




while(true) { self.caffeinate (); }


« Reply #2 - Posted 2003-08-05 09:56:52 »

Kevin;
Could you post an example?
Thanks. Grin

JRPG Users -  General Users Site
JRPG Developers -  The JRPG Project's Home
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2003-08-05 10:07:26 »

Ok, bearing in mind I'm not really "into" using the Readers/Writers and I tend to go for InputStream/OutputStream I'd do something like this:

Note: This is typed from memory, so I haven't caught exceptions, and there may be a few issues hanging around..

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// find out how big the file is going to be and reserve
// some memory
File file = new File("mydatafile.dat");
byte[] data = new byte[file.length()];

// read the complete file into memory
FileInputStream fin = new FileInputStream(file);
fin.read(data);
fin.close();

// process the file as though its any other stream
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);

// do my data reads
String charName = din.readUTF();
int charAge = din.readInt();
int charHeight = din.readDouble();

// tidy up
din.close();


Hope this is of some help,

Kev

Offline krypto

Junior Devvie




while(true) { self.caffeinate (); }


« Reply #4 - Posted 2003-08-05 10:24:20 »

Thanks Kevin, I'll give it a try.

JRPG Users -  General Users Site
JRPG Developers -  The JRPG Project's Home
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

theagentd (1425 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

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
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!