Java Code Examples for com.badlogic.gdx.math.MathUtils#nextPowerOfTwo()

The following examples show how to use com.badlogic.gdx.math.MathUtils#nextPowerOfTwo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectSet.java    From collider with Apache License 2.0 6 votes vote down vote up
/** Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity * loadFactor items
 * before growing the backing table. */
public ObjectSet (int initialCapacity, float loadFactor) {
	if (initialCapacity < 0) throw new IllegalArgumentException("initialCapacity must be >= 0: " + initialCapacity);
	if (initialCapacity > 1 << 30) throw new IllegalArgumentException("initialCapacity is too large: " + initialCapacity);
	capacity = MathUtils.nextPowerOfTwo(initialCapacity);

	if (loadFactor <= 0) throw new IllegalArgumentException("loadFactor must be > 0: " + loadFactor);
	this.loadFactor = loadFactor;

	threshold = (int)(capacity * loadFactor);
	mask = capacity - 1;
	hashShift = 31 - Integer.numberOfTrailingZeros(capacity);
	stashCapacity = Math.max(3, (int)Math.ceil(Math.log(capacity)) * 2);
	pushIterations = Math.max(Math.min(capacity, 8), (int)Math.sqrt(capacity) / 8);

	keyTable = (T[])new Object[capacity + stashCapacity];
}
 
Example 2
Source File: ImageHelper.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms byte[] to Texture Region.
 * <p>
 * If you are going to call this method inside firebase callback remember to wrap it<p>
 * into {@code Gdx.app.postRunnable(Runnable)}.
 * The texture will be changed so that it has sides with length of power of 2.
 *
 * @param bytes Byte array with image description
 * @return Texture region representation of given byte array
 */
public TextureRegion createTextureFromBytes(byte[] bytes) {
    Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
    final int orgWidth = pixmap.getWidth();
    final int orgHeight = pixmap.getHeight();
    int width = MathUtils.nextPowerOfTwo(orgWidth);
    int height = MathUtils.nextPowerOfTwo(orgHeight);
    final Pixmap potPixmap = new Pixmap(width, height, pixmap.getFormat());
    potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight());
    pixmap.dispose();
    TextureRegion region = new TextureRegion(new Texture(potPixmap), 0, 0, orgWidth, orgHeight);
    potPixmap.dispose();
    return region;
}
 
Example 3
Source File: TexturePacker.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
/** @param rootDir See {@link #setRootDir(File)}.  */
public TexturePacker(Settings settings, PageFileWriter pageFileWriter, File rootDir) {
	this.pageFileWriter = pageFileWriter;
	this.settings = settings;

	if (settings.pot) {
		if (settings.maxWidth != MathUtils.nextPowerOfTwo(settings.maxWidth))
			throw new RuntimeException("If pot is true, maxWidth must be a power of two: " + settings.maxWidth);
		if (settings.maxHeight != MathUtils.nextPowerOfTwo(settings.maxHeight))
			throw new RuntimeException("If pot is true, maxHeight must be a power of two: " + settings.maxHeight);
	}

	if (settings.multipleOfFour) {
		if (settings.maxWidth % 4 != 0)
			throw new RuntimeException("If mod4 is true, maxWidth must be evenly divisible by 4: " + settings.maxWidth);
		if (settings.maxHeight % 4 != 0)
			throw new RuntimeException("If mod4 is true, maxHeight must be evenly divisible by 4: " + settings.maxHeight);
	}

	if (settings.grid)
		packer = new OrderedGridPacker(settings);
	else
		packer = new MaxRectsPacker(settings);

	imageProcessor = new ImageProcessor(settings);
	setRootDir(rootDir);
}
 
Example 4
Source File: ObjectSet.java    From collider with Apache License 2.0 5 votes vote down vote up
/** Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is
 * done. If the map contains more items than the specified capacity, the next highest power of two capacity is used instead. */
public void shrink (int maximumCapacity) {
	if (maximumCapacity < 0) throw new IllegalArgumentException("maximumCapacity must be >= 0: " + maximumCapacity);
	if (size > maximumCapacity) maximumCapacity = size;
	if (capacity <= maximumCapacity) return;
	maximumCapacity = MathUtils.nextPowerOfTwo(maximumCapacity);
	resize(maximumCapacity);
}
 
Example 5
Source File: ImageUtils.java    From bladecoder-adventure-engine with Apache License 2.0 4 votes vote down vote up
public static int getRecommendedAtlasSize() {
	int wWidth = Ctx.project.getWorld().getWidth();

	return MathUtils.nextPowerOfTwo((int) (wWidth * 2f));
}