Example usage for com.badlogic.gdx.utils Pool free

List of usage examples for com.badlogic.gdx.utils Pool free

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Pool free.

Prototype

public void free(T object) 

Source Link

Document

Puts the specified object in the pool, making it eligible to be returned by #obtain() .

Usage

From source file:com.android.ringfly.common.Colliders.java

License:Apache License

/** Removes game objects that are marked as in collision, calling a removal handler for each one that is marked. Note that the
 * "U extends T" part of this method's signature means, for example, that PlayerShot and RobotShot can both have the same
 * handler if they are both derived from BaseShot.
 * /*from   www. j a v a  2s  .  co  m*/
 * @param <T> the base class of the object.
 * @param <U> a more specific class of the object.
 * @param pool the pool to which the object belongs.
 * @param gos the array of game objects to check.
 * @param handler the collision handler callback. */
public static <T extends GameObject, U extends T> void removeMarkedCollisions(Pool<U> pool, Array<U> gos,
        RemovalHandler<T> handler) {
    // The "U extends T" allows for shotHandler to cope with both PlayerShot and RobotShot, because they both
    // extend BaseShot.
    for (int i = gos.size - 1; i >= 0; i--) {
        U go = gos.get(i);
        if (go.inCollision) {
            handler.onRemove(go);
            gos.removeIndex(i);
            pool.free(go);
        }
    }
}

From source file:com.android.ringfly.common.Colliders.java

License:Apache License

/** Removes game objects that are outside of the given rectangular bounds.
 * @param <T> the object's class./*from  www  .  j  a  va  2  s .c  om*/
 * @param pool the pool to which the object belongs.
 * @param gos the array of game objects to check.
 * @param bounds the rectangular bounds. */
public static <T extends GameObject> void removeOutOfBounds(Pool<T> pool, Array<T> gos, Rectangle bounds) {
    float minX = bounds.x;
    float maxX = minX + bounds.width;
    float minY = bounds.y;
    float maxY = minY + bounds.height;
    for (int i = gos.size - 1; i >= 0; i--) {
        T go = gos.get(i);
        if (go.x >= maxX || go.x + go.width <= minX || go.y >= maxY || go.y + go.height <= minY) {
            gos.removeIndex(i);
            pool.free(go);
        }
    }
}

From source file:com.badlydrawngames.general.Pools.java

License:Apache License

/** Creates an array from a pool, freeing its items if it already exists.
 * @param <T> the type of item allocated in the array.
 * @param array the array of items to (re)create.
 * @param pool the pool that the items are to be allocated from / released to.
 * @param size the array's capacity.//from  w  ww . j a  v  a2  s.  c  o m
 * @return the input array */
@SuppressWarnings("unchecked")
public static <T> Array<T> makeArrayFromPool(Array<T> array, Pool<T> pool, int size) {
    if (array == null) {
        // Do this so that array.items can be used.
        T t = pool.obtain();
        array = new Array<T>(false, size, (Class<T>) t.getClass());
        pool.free(t);
    } else {
        freeArrayToPool(array, pool);
    }
    return array;
}

From source file:com.github.antag99.retinazer.Mapper.java

License:Open Source License

void flushComponentRemoval() {
    final Bag<T> components = this.components;
    final IntArray remove = this.remove;
    final Mask componentsMask = this.componentsMask;
    final Mask removeMask = this.removeMask;
    final int removeCount = this.removeCount;

    final Pool<T> pool = this.componentPool;
    if (pool == null) {
        //Version for standard components
        for (int i = 0; i < removeCount; i++) {
            final int entity = remove.get(i);
            components.remove(entity);/*from ww  w  . j  a  v  a  2 s  . co  m*/
            componentsMask.clear(entity);
            removeMask.clear(entity);
        }
    } else {
        //Version for pooled components
        for (int i = 0; i < removeCount; i++) {
            final int entity = remove.get(i);

            final T component = components.remove(entity);
            if (component != null)
                pool.free(component);

            componentsMask.clear(entity);
            removeMask.clear(entity);
        }
    }

    if (removeCount > 0)
        remove.removeRange(0, removeCount - 1);
}

From source file:com.github.antag99.retinazer.Mapper.java

License:Open Source License

/** Should be called on components created by {@link #createComponent()} which do not participate in
 * entity system and are no longer needed.
 * For pooled components, this returns them into the pool.
 * For non-pooled components this is a no-op, which can be safely omitted. */
public void destroyComponent(T component) {
    final Pool<T> pool = this.componentPool;
    if (pool != null) {
        pool.free(component);
    }// ww w  . j  a  va2 s . c  om
}

From source file:com.kotcrab.vis.ui.widget.HighlightTextArea.java

License:Apache License

@Override
protected void calculateOffsets() {
    super.calculateOffsets();
    if (chunkUpdateScheduled == false)
        return;/*w w w  . j  a v a2  s. c  o  m*/
    chunkUpdateScheduled = false;
    highlights.sort();
    renderChunks.clear();

    String text = getText();

    Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
    GlyphLayout layout = layoutPool.obtain();
    boolean carryHighlight = false;
    for (int lineIdx = 0, highlightIdx = 0; lineIdx < linesBreak.size; lineIdx += 2) {
        int lineStart = linesBreak.items[lineIdx];
        int lineEnd = linesBreak.items[lineIdx + 1];
        int lineProgress = lineStart;
        float chunkOffset = 0;

        for (; highlightIdx < highlights.size;) {
            Highlight highlight = highlights.get(highlightIdx);
            if (highlight.getStart() > lineEnd) {
                break;
            }

            if (highlight.getStart() == lineProgress || carryHighlight) {
                renderChunks.add(new Chunk(text.substring(lineProgress, Math.min(highlight.getEnd(), lineEnd)),
                        highlight.getColor(), chunkOffset, lineIdx));
                lineProgress = Math.min(highlight.getEnd(), lineEnd);

                if (highlight.getEnd() > lineEnd) {
                    carryHighlight = true;
                } else {
                    carryHighlight = false;
                    highlightIdx++;
                }
            } else {
                //protect against overlapping highlights
                boolean noMatch = false;
                while (highlight.getStart() <= lineProgress) {
                    highlightIdx++;
                    if (highlightIdx >= highlights.size) {
                        noMatch = true;
                        break;
                    }
                    highlight = highlights.get(highlightIdx);
                    if (highlight.getStart() > lineEnd) {
                        noMatch = true;
                        break;
                    }
                }
                if (noMatch)
                    break;
                renderChunks.add(new Chunk(text.substring(lineProgress, highlight.getStart()), defaultColor,
                        chunkOffset, lineIdx));
                lineProgress = highlight.getStart();
            }

            Chunk chunk = renderChunks.peek();
            layout.setText(style.font, chunk.text);
            chunkOffset += layout.width;
            //current highlight needs to be applied to next line meaning that there is no other highlights that can be applied to currently parsed line
            if (carryHighlight)
                break;
        }

        if (lineProgress < lineEnd) {
            renderChunks
                    .add(new Chunk(text.substring(lineProgress, lineEnd), defaultColor, chunkOffset, lineIdx));
        }
    }

    maxAreaWidth = 0;
    for (String line : text.split("\\n")) {
        layout.setText(style.font, line);
        maxAreaWidth = Math.max(maxAreaWidth, layout.width + 30);
    }

    layoutPool.free(layout);
    updateScrollLayout();
}

From source file:com.kotcrab.vis.ui.widget.VisTextArea.java

License:Apache License

@Override
protected void calculateOffsets() {
    super.calculateOffsets();
    if (!this.text.equals(lastText)) {
        this.lastText = text;
        BitmapFont font = style.font;//from   w w  w .jav a2s. c om
        float maxWidthLine = this.getWidth()
                - (style.background != null ? style.background.getLeftWidth() + style.background.getRightWidth()
                        : 0);
        linesBreak.clear();
        int lineStart = 0;
        int lastSpace = 0;
        char lastCharacter;
        Pool<GlyphLayout> layoutPool = Pools.get(GlyphLayout.class);
        GlyphLayout layout = layoutPool.obtain();
        for (int i = 0; i < text.length(); i++) {
            lastCharacter = text.charAt(i);
            if (lastCharacter == ENTER_DESKTOP || lastCharacter == ENTER_ANDROID) {
                linesBreak.add(lineStart);
                linesBreak.add(i);
                lineStart = i + 1;
            } else {
                lastSpace = (continueCursor(i, 0) ? lastSpace : i);
                layout.setText(font, text.subSequence(lineStart, i + 1));
                if (layout.width > maxWidthLine && softwrap) {
                    if (lineStart >= lastSpace) {
                        lastSpace = i - 1;
                    }
                    linesBreak.add(lineStart);
                    linesBreak.add(lastSpace + 1);
                    lineStart = lastSpace + 1;
                    lastSpace = lineStart;
                }
            }
        }
        layoutPool.free(layout);
        // Add last line
        if (lineStart < text.length()) {
            linesBreak.add(lineStart);
            linesBreak.add(text.length());
        }
        showCursor();
    }
}

From source file:com.todoroo.zxzx.general.Pools.java

License:Apache License

/** Frees the items in an array to a pool.
 * @param <T> the type of item allocated in the array.
 * @param array the array of items to free.
 * @param pool the pool that the items are to be released to. */
public static <T> void freeArrayToPool(Array<T> array, Pool<T> pool) {
    pool.free(array);
    array.clear();//from w w w.j av  a2s .c  om
}

From source file:com.wlys.player.Colliders.java

License:Apache License

/**
 * Removes game objects that are marked as in collision, calling a removal
 * handler for each one that is marked. Note that the "U extends T" part of
 * this method's signature means, for example, that PlayerShot and RobotShot
 * can both have the same handler if they are both derived from BaseShot.
 * /*ww w  .java2  s.co m*/
 * @param <T>
 *            the base class of the object.
 * @param <U>
 *            a more specific class of the object.
 * @param pool
 *            the pool to which the object belongs.
 * @param gos
 *            the array of game objects to check.
 * @param handler
 *            the collision handler callback.
 */
public static <T extends GameObject, U extends T> void removeMarkedCollisions(Pool<U> pool, Array<U> gos,
        RemovalHandler<T> handler) {
    // The "U extends T" allows for shotHandler to cope with both PlayerShot
    // and RobotShot, because they both
    // extend BaseShot.
    for (int i = gos.size - 1; i >= 0; i--) {
        U go = gos.get(i);
        if (go.isDemandRemove()) {
            handler.onRemove(go);
            gos.removeIndex(i);
            pool.free(go);
            logger.info("" + go.toString());
        }
    }
}

From source file:com.wlys.player.Colliders.java

License:Apache License

/**
 * Removes game objects that are outside of the given rectangular bounds.
 * //from ww w.  j  a v a2s .co  m
 * @param <T>
 *            the object's class.
 * @param pool
 *            the pool to which the object belongs.
 * @param gos
 *            the array of game objects to check.
 * @param bounds
 *            the rectangular bounds.
 */
public static <T extends GameObject> void removeOutOfBounds(Pool<T> pool, Array<T> gos, Rectangle bounds) {
    float minX = bounds.x;
    float maxX = minX + bounds.width;
    float minY = bounds.y;
    float maxY = minY + bounds.height;
    for (int i = gos.size - 1; i >= 0; i--) {
        T go = gos.get(i);
        if (go.getX() >= maxX || go.getX() + go.getWidth() <= minX || go.getY() >= maxY
                || go.getY() + go.getHeight() <= minY) {
            gos.removeIndex(i);
            pool.free(go);
            logger.info("" + go.toString());
        }
    }
}