Make this packer to work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PeterDoh
    New Member
    • Jul 2008
    • 2

    Make this packer to work?

    im in the creation of an sprite packer for a game, but i can't get it to run?
    a friend sent me the source for it, so i have no idea what to do from this?
    so far:
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.io.FileOutputStream;
    import java.io.BufferedInputStream;
    import java.nio.ByteBuffer;
    import java.awt.Toolkit;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.imageio.ImageIO;
    import java.util.TreeMap;
    import java.util.Map.Entry;
    import java.util.Enumeration;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    public class SpriteEditor {
    	public static final String IMG_DIR = "./img/";
    
    	public static final void main(String[] args) {
    		SpriteEditor editor = new SpriteEditor(new File("./sprites.Tx"));
    	}
    
    	public SpriteEditor(File file) {
    		TreeMap<Integer, Sprite> sprites = readZip(file);
    
    		sprites.put(2382, createSprite(new File("./new/1370.png"), true, 6, 2, 41, 29));
    		System.out.println("Successfully packed new sprites!");
    
    		writeZip(sprites, file);
    	}
    
    	public Sprite createSprite(File file, boolean requiresShift, int xShift, int yShift, int something1, int something2) {
    		try {
    			Sprite sprite = Sprite.fromImage(loadImage(file));
    
    			sprite.setRequiresShift(requiresShift);
    			sprite.setShift(xShift, yShift);
    			sprite.setSomething(something1, something2);
    
    			return sprite;
    		}
    		catch(Exception ioe) {
    			System.out.println(ioe);
    			return null;
    		}
    	}
    
    	public TreeMap<Integer, Sprite> readZip(File file) {
    		try {
    			TreeMap<Integer, Sprite> sprites = new TreeMap<Integer, Sprite>();
    
    			ZipFile zip = new ZipFile(file);
    			for(Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();entries.hasMoreElements();) {
    				ZipEntry entry = entries.nextElement();
    				BufferedInputStream in = new BufferedInputStream(zip.getInputStream(entry));
    				ByteBuffer buffer = streamToBuffer(in);
    				in.close();
    
    				Sprite sprite = Sprite.unpack(buffer);
    				sprites.put(Integer.parseInt(entry.getName()), sprite);
    			}
    			return sprites;
    		}
    		catch(IOException ioe) {
    			System.err.println(ioe);
    			return null;
    		}
    	}
    
    	public boolean writeZip(TreeMap<Integer, Sprite> sprites, File file) {
    		try {
    			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
    			out.setLevel(9);
    
    			for(Entry<Integer, Sprite> e : sprites.entrySet()) {
    				String name = String.valueOf(e.getKey());
    				Sprite sprite = e.getValue();
    
    				out.putNextEntry(new ZipEntry(name));
    				out.write(sprite.pack().array());
    				out.closeEntry();
    			}
    			out.close();
    			return true;
    		}
    		catch(IOException ioe) {
    			System.err.println(ioe);
    			return false;
    		}
    	}
    
    	public static final ByteBuffer streamToBuffer(BufferedInputStream in) throws IOException {
    		byte[] buffer = new byte[in.available()];
    		in.read(buffer, 0, buffer.length);
    		return ByteBuffer.wrap(buffer);
    	}
    
    	public static BufferedImage loadImage(File file) throws IOException {
    		Image image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath())).getImage();
    		BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
    
    		Graphics g = buffImage.createGraphics();
    		g.drawImage(image, 0, 0, null);
    		g.dispose();
    
    		return buffImage;
    	}
    
    }
    and:
    Code:
    sprites.put(1370, createSprite(new File("./new/1370.png"), true, 6, 2, 41, 29));
    any help is appriciated.

    Sincerly.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What did he say you should do with it?
    What did you try to do with it and what error did it give?

    Comment

    • PeterDoh
      New Member
      • Jul 2008
      • 2

      #3
      thats the thing, he just gave me the packer source (wich will pack the sprites)

      and:
      "Ok you will need to make a file with this named Sprites.Tx"
      Code:
      sprites.put(1370, createSprite(new File("./new/1370.png"), true, 6, 2, 41, 29));
      "Basicly this is what makes ur png into rsc format for sprites
      6, 2, 41, 29
      6 > xshift
      2 > yshift
      41 > something1
      29 > somtthing2"

      and, from here im stuck, i guess it needs an .bat to run it with like;
      java -classpath spriteeditor.ja va
      pause

      but this gives me options that has nothing to do with the packer method.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        What you have is source code which needs to be compiled into a class file
        Best thing to do is to get your friend and ask him for instructions on how to use that source code. e.g the tool for compiling the source file is javac not java.

        Comment

        Working...