While prototyping different ideas for J4K, I’ve written a couple of code generators that some might find useful.
Image Encoding Tool
This tool generates Java code that contains a String-encoded sprite sheet. It generates simple GIF/PNG output for ImageIO, but also includes a slightly more optimal means of encoding strings, especially if you are handling images yourself without BufferedImages.
Below is an input, where our “sprites” folder (in our class-path) contains a series of equal size images. It will compile a color table of all the images, but for the sake of optimization you should try to use the same colors among many sprites.
Keep your total number of colors less than 95 for indexed colors to work.
ImageTool.create(ImageTool.dir("/res/sprites/"));
You can see the output below, which gives us a couple different ways of encoding our data (indexed color, or simple ImageIO string):
http://www.java-gaming.org/?action=pastebin&id=365
You simply copy + paste the generated code (printed to System.out) into your game, and then you can render images like so:
//Filenames are transformed into constants
//e.g. "res/sprites/pirate2_attack.png" --> SPRITE_PIRATE2_ATTACK
g.drawImage(SPRITES[SPRITE_PIRATE2_ATTACK],
50, 50,
null);
Testing a 128x128 opaque sprite sheet, then JShrink -> pack200:
GIF Encoding: 2,875 bytes
Indexed Color Tables: 2,630 bytes
Testing six 11x9 transparent sprites, then JShrink -> pack200:
PNG Encoding: 1,931 bytes
Indexed Color Tables: 1,816 bytes
Without BufferedImages: 1,689 bytes (i.e. for tiled map data)
The files don’t have any dependencies, and each should be self-contained.
ImageTool.java (excuse the messy code)
ImageToolTest.java (test case, shows proper usage)
Here is an example 4K applet template using the resulting output:
T.java
I have another little tool I’ll post about later. If you’ve got any 4K tools of your own, post 'em here.