• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    22
  • comments
    30
  • views
    42480

Sprite Packing in Python

Sign in to follow this  
Followers 0
jwezorek

1774 views

I've been working on my puzzle game Syzygy again, after a long hiatus, and am now writing to iOS/cocos2d-x rather than just working on the prototype I had implemented to Win32.

The way that you get sprite data into cocos2d is by including as a resource a sprite sheet image and a .plist file which is XML that specifies which sprite is where. Plists are apparently an old Mac thing -- I had never heard of this format. .plists describing a lot of sprites would be a chore to write by hand so there is a cottage industry of sprite packing applications.

I tried out one called TexturePacker and liked it a lot -- except that it is crippleware; I need a few features that are only in the full version; plus I can't stand crippleware; and I think $30 is too much for something that I can write myself over the weekend. So I decided to write my own sprite packer over the weekend.

The result is pypacker, a python script: source code here. Usage is like

pypacker -i -o -m -p

where

  • = a path to a directory containing image files. (In any format supported by the python PIL module.)
  • = a path + filename prefix for the two output files e.g. given C:\foo\bar the script will generate C:\foo\bar.png and c:\foo\bar.plist
  • = the packing mode. Can be either "grow" or fixed dimensions such as "256x256?. "grow" tells the algorithm to begin packing rectangles from a blank slate expanding the packing as necessary. "256x256? et. al. tell the algorithm to start with the given image size and pack sprites into it by subdivision, throwing an error if they all won't fit.
  • -p = optional flag indicating you want the output image file dimensions padded to the nearest power-of-two-sized square.

    The algorithm I used is a recursive bin packing algorithm in which sprites are placed one-by-one into a binary tree. I based it directly on Jake Gordon's work in Javascript for generating sprite sheets for use in CSS, described here, only my algorithm is sort of like version 2 of his i.e. I fixed an issue that bugged me about his algorithm.

    The core of the algorithm is a function that looks like this:[code=code:1]def pack_images( named_images, grow_mode, max_dim): root=() while named_images: named_image = named_images.pop() if not root: if (grow_mode): root = rect_node((), rectangle(0, 0, named_image.img.size[0], named_image.img.size[1])) else: root = rect_node((), rectangle(0, 0, max_dim[0], max_dim[1])) root.split_node(named_image) continue leaf = find_empty_leaf(root, named_image.img) if (leaf): leaf.split_node(named_image) else: if (grow_mode): root.grow_node(named_image) else: raise Exception("Can't pack images into a %d by %d rectangle." % max_dim) return root
    We iterate through the images we want to pack. For each image, try to find a rectangular node in the tree that can contain the image. If one exists, place the image in the node and subdivide the node such that the remaining space, not taken up by the image, is available in the tree (this is what 'split_node' does). If such a node cannot be found, throw an exception if we are not in 'grow' mode or expand the root rectangle node to accommodate the new image if we are in 'grow' mode.

    This routine is very similar to the Javascript implementation I linked to above. The difference is in the details about the structure of the binary tree. Jake Gordon's Javascript implementation uses a node type that stores an image in the upper left and has children that he calls 'right' and 'down' like this:
    pack_illus1.png
    Since actual data is always burnt into the upper left, it means that the tree can never subdivide into this space; we can never recurse into the upper left. This results in the grow_node routine being awkward to write. When we grow the root we either want to extend to the right or extend down, if the upper left can be a node and not image data this is a simple matter of creating a new node and making the the existing root its upper or left child. Anyway, Jake Gordon's implementation results in a packing tree that cannot both grow right and grow down simultaneously because it would have been complicated to implement this. This limitation is not a problem practically as long as you sort the images from largest to smallest before running the packing algorithm -- a standard heuristic from the bin packing literature.

    I however wanted to see if the standard sorting heuristic is really accomplishing anything. I wanted to be able to pack rectangles in random order. I therefore simplified the trinary node structure of the Javascript implementation into true binary nodes either oriented horizontally or vertically like this:
    pack_illus2.png
    Further now only leafs can contain images and if a node is not a leaf it always has two valid, that is non-null, children. Using this type of tree structure makes the full grow_node routine more or less trivial.

    Beyond that, I'm using the following heuristics:

    • if the orientation (horizontally or vertically) of a split is not forced, split with the orientation that will result in the new empty node having the largest area
    • If the orientation of growing the root rect is not forced, grow in the direction that leads to the smallest increase in the maximum side length of the root rectangle. (This heuristic enforces squarishness and is extremely important. Without doing this the grow version of the algorithm is basically unusable, and in this sense this grow heuristic can be considered part of the algorithm rather than a heuristic that can be swapped out)

      Sorting by size (max side length) turns out be about a 6% improvement with this algorithm. Here's 500 rects packed with sorting (top) and without (bottom):
      pypack_samp1.png

      pypack_samp2.png

      EDIT: Actually the version I posted isn't the one that can handle expanding right and down simultaneously. (Although it doesn't effect results because sorting by size is hardcoded into the script). Will post the newer version soon...

      Source

1
Sign in to follow this  
Followers 0


0 Comments


There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now