Create a gist now

Instantly share code, notes, and snippets.

module PixenToGameQuery
require 'RMagick'
include Magick
FPR = 12 # Frames per row in a sprite sheet generated by Pixen
COLPAD = 2 # Pix between each column of images on the sprite sheet
ROWPAD = 2 # Pix between each row of images on the sprite sheet
LEFTPAD = 4 # Extra pix on left of sprite sheet
RIGHTPAD = 20 # Extra pix on right of sprite sheet
BOTTOMPAD = 2 # Extra pix on bottom of sprite sheet
def PixenToGameQuery.convert_sprite(image_path, width, height, frames, new_image_path)
original = ImageList.new(image_path)
img = Image.new((width + COLPAD) * frames, height)
sprite_rows = (frames.to_f / FPR).ceil
0.upto(sprite_rows - 1) do |rownum|
rowframes = (rownum == sprite_rows - 1) ? frames % FPR : FPR
row_width = rowframes * (width + COLPAD)
pix = original.get_pixels(LEFTPAD,
ROWPAD + (height + ROWPAD) * rownum,
row_width,
height)
img.store_pixels(rownum * FPR * (width + COLPAD),
0,
row_width,
height,
pix)
end
img.write(new_image_path)
end
end
@robesris
Owner

This is a simple script that uses RMagick to convert a sprite sheet created by Pixen and convert it to a single-row sprite suitable for use with the gameQuery library. Pixen automatically creates rows of 12 frames each and puts some extra padding in. This script accounts for that and trims the image down and puts all the frames in a single row.

image_path: path to the file generated by Pixen
width: the width of a single frame
height: the height of a single frame
frames: the total number of frames in the animation
new_image_path: the path to write the new file to

@robesris
Owner

Minor changes to encapsulate the code in a Module and change filename to match module name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment