private SpriteState generateSpriteState(int frames, int directions, List<Integer> delays) throws IOException
	{
		SpriteState state = new SpriteState();
		state.frames = new ArrayList<SpriteFrame>();
		
    	// Extract the width of the full image(not the tiles)
        int base_width = buffer.getWidth();

        for(int framei = 0; framei < frames; framei++)
        {
            // Prepare a new frame to insert into our state
            SpriteFrame frame = new SpriteFrame();
            frame.directions = new HashMap<Integer,BufferedImage>();
            
            // This array simply maps tile indices to directions.
            // For example, the first tile in the frame would be the direction SOUTH.
            int dirs[] = {SOUTH, NORTH, EAST, WEST, SOUTHEAST, SOUTHWEST, NORTHEAST, NORTHWEST};

            if(directions != 1 && directions != 4 && directions != 8) {
            	throw new IOException(".DMI metadata malformed");
            }
            
            // Go over all directions and insert them into our frame
            for(int i=0; i < directions; i++) {
            	// Find out which direction the next tile will be
                int next_dir = dirs[i];
                
            	// If we're at the end of the buffer on the right, go to the next line
                if(pixel_x >= base_width)
                {
                    pixel_x = 0;
                    pixel_y = pixel_y + height;
                }
                
                BufferedImage image = buffer.getSubimage(pixel_x, pixel_y, width, height);
                frame.directions.put(next_dir, image);
                pixel_x += width; // push to the next "row"

            }
            
            // Set the proper delay of the frame
            frame.delay = delays.get(framei);
            
            // Add the frame to our state
            state.frames.add(frame);
        }
        
        return state;
	}