Skip to content

Instantly share code, notes, and snippets.

@mhulse mhulse/shprite
Last active Jun 13, 2016

Embed
What would you like to do?
Simple bash command line script to make sprite textures for video games.
#!/usr/bin/env bash
function init() {
clear
# Display instructions:
echo -e "\033[1mSIMPLE SPRITE SHEET CREATION SCRIPT\033[0m"
echo "-----------------------------------"
echo "1. Navigate to the folder that contains your tile images."
echo "2. Run “shprite” and follow the on-screen instructions."
echo
echo -e "For more information, visit: \e[4mhttps://git.io/vocLj\e[24m"
echo
name="sheet.png"
# Check if previous sprite sheet exists:
if [ -e ${name} ]; then
# Remove it!
rm ${name}
fi
# Creates a list of file names:
pngs=(*.png)
# Get lenght of array:
count=${#pngs[@]}
# Make sure we have some images to work with:
if [ ${count} -gt 0 ]; then
# Provide options and get optional user input:
read -e -p "Output rows (e.g., 2): " -i "1" SHEET_ROWS
read -e -p "Output columns (e.g., 4): " -i "${count}" SHEET_COLS
echo
echo "Generating sprite sheet …"
# http://www.imagemagick.org/Usage/montage/
# http://www.imagemagick.org/script/montage.php
montage \
*.png \
-tile ${SHEET_COLS}x${SHEET_ROWS} \
-geometry +0+0 \
-background transparent \
${name}
# Show the path to the generated image:
echo -e "\e[4m$(pwd)/${name}\e[24m"
echo "Done!"
else
echo "Oops, something went wrong …"
echo "Please try again."
fi
}
# Start program:
init
@mhulse

This comment has been minimized.

Copy link
Owner Author

mhulse commented Jun 13, 2016

To get this script on your $PATH, add this to your .bash_profile:

# Custom scripts:
[[ -d "$HOME/scripts" ]] && export PATH=$HOME/scripts:$PATH

Next, create a “scripts” folder in your home directory.

Copy the code from this gist and put it in a file named shprite.

Make it executable:

$ chmod u+x shprite

Restart your machine for good measure.

Now you can navigate to the folder containing your tile images and run:

$ shprite

Follow the on-screen instructions.

Note: It is assumed that you have ImageMagick installed on your system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.