Search in sources :

Example 51 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class FileWrapper method list.

/** Returns the paths to the children of this directory with the specified suffix. Returns an empty list if this file handle
	 * represents a file and not a directory. On the desktop, an {@link FileType#Internal} handle to a directory on the classpath
	 * will return a zero length array.
	 * @throw GdxRuntimeException if this file is an {@link FileType#Classpath} file. */
public FileWrapper[] list(String suffix) {
    if (type == FileType.Classpath)
        throw new GdxRuntimeException("Cannot list a classpath directory: " + file);
    String[] relativePaths = file().list();
    if (relativePaths == null)
        return new FileWrapper[0];
    FileWrapper[] handles = new FileWrapper[relativePaths.length];
    int count = 0;
    for (int i = 0, n = relativePaths.length; i < n; i++) {
        String path = relativePaths[i];
        if (!path.endsWith(suffix))
            continue;
        handles[count] = child(path);
        count++;
    }
    if (count < relativePaths.length) {
        FileWrapper[] newHandles = new FileWrapper[count];
        System.arraycopy(handles, 0, newHandles, 0, count);
        handles = newHandles;
    }
    return handles;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 52 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class Controllers method initialize.

private static void initialize() {
    if (managers.containsKey(Gdx.app))
        return;
    String className = null;
    ApplicationType type = Gdx.app.getType();
    ControllerManager manager = null;
    if (type == ApplicationType.Android) {
        if (Gdx.app.getVersion() >= 12) {
            className = "com.badlogic.gdx.controllers.android.AndroidControllers";
        } else {
            Gdx.app.log(TAG, "No controller manager is available for Android versions < API level 12");
            manager = new ControllerManagerStub();
        }
    } else if (type == ApplicationType.Desktop) {
        if (Gdx.graphics.getType() == GraphicsType.LWJGL3) {
            className = "com.badlogic.gdx.controllers.lwjgl3.Lwjgl3ControllerManager";
        } else {
            className = "com.badlogic.gdx.controllers.desktop.DesktopControllerManager";
        }
    } else if (type == ApplicationType.WebGL) {
        className = "com.badlogic.gdx.controllers.gwt.GwtControllers";
    } else {
        Gdx.app.log(TAG, "No controller manager is available for: " + Gdx.app.getType());
        manager = new ControllerManagerStub();
    }
    if (manager == null) {
        try {
            Class controllerManagerClass = ClassReflection.forName(className);
            manager = (ControllerManager) ClassReflection.newInstance(controllerManagerClass);
        } catch (Throwable ex) {
            throw new GdxRuntimeException("Error creating controller manager: " + className, ex);
        }
    }
    managers.put(Gdx.app, manager);
    final Application app = Gdx.app;
    Gdx.app.addLifecycleListener(new LifecycleListener() {

        @Override
        public void resume() {
        }

        @Override
        public void pause() {
        }

        @Override
        public void dispose() {
            managers.remove(app);
            Gdx.app.log(TAG, "removed manager for application, " + managers.size + " managers active");
        }
    });
    Gdx.app.log(TAG, "added manager for application, " + managers.size + " managers active");
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ApplicationType(com.badlogic.gdx.Application.ApplicationType) LifecycleListener(com.badlogic.gdx.LifecycleListener) Application(com.badlogic.gdx.Application)

Example 53 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class FreeType method initFreeType.

public static Library initFreeType() {
    new SharedLibraryLoader().load("gdx-freetype");
    long address = initFreeTypeJni();
    if (address == 0)
        throw new GdxRuntimeException("Couldn't initialize FreeType library, FreeType error code: " + getLastErrorCode());
    else
        return new Library(address);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SharedLibraryLoader(com.badlogic.gdx.utils.SharedLibraryLoader)

Example 54 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class FreeTypeFontGenerator method generateData.

/** Generates a new {@link BitmapFontData} instance, expert usage only. Throws a GdxRuntimeException if something went wrong.
	 * @param parameter configures how the font is generated */
public FreeTypeBitmapFontData generateData(FreeTypeFontParameter parameter, FreeTypeBitmapFontData data) {
    parameter = parameter == null ? new FreeTypeFontParameter() : parameter;
    char[] characters = parameter.characters.toCharArray();
    int charactersLength = characters.length;
    boolean incremental = parameter.incremental;
    int flags = getLoadingFlags(parameter);
    setPixelSizes(0, parameter.size);
    // set general font data
    SizeMetrics fontMetrics = face.getSize().getMetrics();
    data.flipped = parameter.flip;
    data.ascent = FreeType.toInt(fontMetrics.getAscender());
    data.descent = FreeType.toInt(fontMetrics.getDescender());
    data.lineHeight = FreeType.toInt(fontMetrics.getHeight());
    float baseLine = data.ascent;
    // if bitmapped
    if (bitmapped && (data.lineHeight == 0)) {
        for (int c = 32; c < (32 + face.getNumGlyphs()); c++) {
            if (loadChar(c, flags)) {
                int lh = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
                data.lineHeight = (lh > data.lineHeight) ? lh : data.lineHeight;
            }
        }
    }
    data.lineHeight += parameter.spaceY;
    // determine space width
    if (loadChar(' ', flags) || loadChar('l', flags)) {
        data.spaceWidth = FreeType.toInt(face.getGlyph().getMetrics().getHoriAdvance());
    } else {
        // Possibly very wrong.
        data.spaceWidth = face.getMaxAdvanceWidth();
    }
    // determine x-height
    for (char xChar : data.xChars) {
        if (!loadChar(xChar, flags))
            continue;
        data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
        break;
    }
    if (data.xHeight == 0)
        throw new GdxRuntimeException("No x-height character found in font");
    // determine cap height
    for (char capChar : data.capChars) {
        if (!loadChar(capChar, flags))
            continue;
        data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
        break;
    }
    if (!bitmapped && data.capHeight == 1)
        throw new GdxRuntimeException("No cap character found in font");
    data.ascent -= data.capHeight;
    data.down = -data.lineHeight;
    if (parameter.flip) {
        data.ascent = -data.ascent;
        data.down = -data.down;
    }
    boolean ownsAtlas = false;
    PixmapPacker packer = parameter.packer;
    if (packer == null) {
        // Create a packer.
        int size;
        PackStrategy packStrategy;
        if (incremental) {
            size = maxTextureSize;
            packStrategy = new GuillotineStrategy();
        } else {
            int maxGlyphHeight = (int) Math.ceil(data.lineHeight);
            size = MathUtils.nextPowerOfTwo((int) Math.sqrt(maxGlyphHeight * maxGlyphHeight * charactersLength));
            if (maxTextureSize > 0)
                size = Math.min(size, maxTextureSize);
            packStrategy = new SkylineStrategy();
        }
        ownsAtlas = true;
        packer = new PixmapPacker(size, size, Format.RGBA8888, 1, false, packStrategy);
        packer.setTransparentColor(parameter.color);
        packer.getTransparentColor().a = 0;
        if (parameter.borderWidth > 0) {
            packer.setTransparentColor(parameter.borderColor);
            packer.getTransparentColor().a = 0;
        }
    }
    if (incremental)
        data.glyphs = new Array(charactersLength + 32);
    Stroker stroker = null;
    if (parameter.borderWidth > 0) {
        stroker = library.createStroker();
        stroker.set((int) (parameter.borderWidth * 64f), parameter.borderStraight ? FreeType.FT_STROKER_LINECAP_BUTT : FreeType.FT_STROKER_LINECAP_ROUND, parameter.borderStraight ? FreeType.FT_STROKER_LINEJOIN_MITER_FIXED : FreeType.FT_STROKER_LINEJOIN_ROUND, 0);
    }
    Glyph missingGlyph = createGlyph('\0', data, parameter, stroker, baseLine, packer);
    if (missingGlyph != null && missingGlyph.width != 0 && missingGlyph.height != 0) {
        data.setGlyph('\0', missingGlyph);
        if (incremental)
            data.glyphs.add(missingGlyph);
    }
    // Create glyphs largest height first for best packing.
    int[] heights = new int[charactersLength];
    for (int i = 0, n = charactersLength; i < n; i++) {
        int height = loadChar(characters[i], flags) ? FreeType.toInt(face.getGlyph().getMetrics().getHeight()) : 0;
        heights[i] = height;
    }
    int heightsCount = heights.length;
    while (heightsCount > 0) {
        int best = 0, maxHeight = heights[0];
        for (int i = 1; i < heightsCount; i++) {
            int height = heights[i];
            if (height > maxHeight) {
                maxHeight = height;
                best = i;
            }
        }
        char c = characters[best];
        Glyph glyph = createGlyph(c, data, parameter, stroker, baseLine, packer);
        if (glyph != null) {
            data.setGlyph(c, glyph);
            if (incremental)
                data.glyphs.add(glyph);
        }
        heightsCount--;
        heights[best] = heights[heightsCount];
        char tmpChar = characters[best];
        characters[best] = characters[heightsCount];
        characters[heightsCount] = tmpChar;
    }
    if (stroker != null && !incremental)
        stroker.dispose();
    if (incremental) {
        data.generator = this;
        data.parameter = parameter;
        data.stroker = stroker;
        data.packer = packer;
    }
    // Generate kerning.
    parameter.kerning &= face.hasKerning();
    if (parameter.kerning) {
        for (int i = 0; i < charactersLength; i++) {
            char firstChar = characters[i];
            Glyph first = data.getGlyph(firstChar);
            if (first == null)
                continue;
            int firstIndex = face.getCharIndex(firstChar);
            for (int ii = i; ii < charactersLength; ii++) {
                char secondChar = characters[ii];
                Glyph second = data.getGlyph(secondChar);
                if (second == null)
                    continue;
                int secondIndex = face.getCharIndex(secondChar);
                int kerning = face.getKerning(firstIndex, secondIndex, 0);
                if (kerning != 0)
                    first.setKerning(secondChar, FreeType.toInt(kerning));
                kerning = face.getKerning(secondIndex, firstIndex, 0);
                if (kerning != 0)
                    second.setKerning(firstChar, FreeType.toInt(kerning));
            }
        }
    }
    // Generate texture regions.
    if (ownsAtlas) {
        data.regions = new Array();
        packer.updateTextureRegions(data.regions, parameter.minFilter, parameter.magFilter, parameter.genMipMaps);
    }
    // Set space glyph.
    Glyph spaceGlyph = data.getGlyph(' ');
    if (spaceGlyph == null) {
        spaceGlyph = new Glyph();
        spaceGlyph.xadvance = (int) data.spaceWidth + parameter.spaceX;
        spaceGlyph.id = (int) ' ';
        data.setGlyph(' ', spaceGlyph);
    }
    if (spaceGlyph.width == 0)
        spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight);
    return data;
}
Also used : SizeMetrics(com.badlogic.gdx.graphics.g2d.freetype.FreeType.SizeMetrics) Stroker(com.badlogic.gdx.graphics.g2d.freetype.FreeType.Stroker) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) GuillotineStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.GuillotineStrategy) SkylineStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.SkylineStrategy) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph) PackStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.PackStrategy)

Example 55 with GdxRuntimeException

use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.

the class FreeTypeFontGenerator method createGlyph.

/** @return null if glyph was not found. */
Glyph createGlyph(char c, FreeTypeBitmapFontData data, FreeTypeFontParameter parameter, Stroker stroker, float baseLine, PixmapPacker packer) {
    boolean missing = face.getCharIndex(c) == 0 && c != 0;
    if (missing)
        return null;
    if (!loadChar(c, getLoadingFlags(parameter)))
        return null;
    GlyphSlot slot = face.getGlyph();
    FreeType.Glyph mainGlyph = slot.getGlyph();
    try {
        mainGlyph.toBitmap(parameter.mono ? FreeType.FT_RENDER_MODE_MONO : FreeType.FT_RENDER_MODE_NORMAL);
    } catch (GdxRuntimeException e) {
        mainGlyph.dispose();
        Gdx.app.log("FreeTypeFontGenerator", "Couldn't render char: " + c);
        return null;
    }
    Bitmap mainBitmap = mainGlyph.getBitmap();
    Pixmap mainPixmap = mainBitmap.getPixmap(Format.RGBA8888, parameter.color, parameter.gamma);
    if (mainBitmap.getWidth() != 0 && mainBitmap.getRows() != 0) {
        int offsetX = 0, offsetY = 0;
        if (parameter.borderWidth > 0) {
            // execute stroker; this generates a glyph "extended" along the outline
            int top = mainGlyph.getTop(), left = mainGlyph.getLeft();
            FreeType.Glyph borderGlyph = slot.getGlyph();
            borderGlyph.strokeBorder(stroker, false);
            borderGlyph.toBitmap(parameter.mono ? FreeType.FT_RENDER_MODE_MONO : FreeType.FT_RENDER_MODE_NORMAL);
            offsetX = left - borderGlyph.getLeft();
            offsetY = -(top - borderGlyph.getTop());
            // Render border (pixmap is bigger than main).
            Bitmap borderBitmap = borderGlyph.getBitmap();
            Pixmap borderPixmap = borderBitmap.getPixmap(Format.RGBA8888, parameter.borderColor, parameter.borderGamma);
            // Draw main glyph on top of border.
            for (int i = 0, n = parameter.renderCount; i < n; i++) borderPixmap.drawPixmap(mainPixmap, offsetX, offsetY);
            mainPixmap.dispose();
            mainGlyph.dispose();
            mainPixmap = borderPixmap;
            mainGlyph = borderGlyph;
        }
        if (parameter.shadowOffsetX != 0 || parameter.shadowOffsetY != 0) {
            int mainW = mainPixmap.getWidth(), mainH = mainPixmap.getHeight();
            int shadowOffsetX = Math.max(parameter.shadowOffsetX, 0), shadowOffsetY = Math.max(parameter.shadowOffsetY, 0);
            int shadowW = mainW + Math.abs(parameter.shadowOffsetX), shadowH = mainH + Math.abs(parameter.shadowOffsetY);
            Pixmap shadowPixmap = new Pixmap(shadowW, shadowH, mainPixmap.getFormat());
            Color shadowColor = parameter.shadowColor;
            float a = shadowColor.a;
            if (a != 0) {
                byte r = (byte) (shadowColor.r * 255), g = (byte) (shadowColor.g * 255), b = (byte) (shadowColor.b * 255);
                ByteBuffer mainPixels = mainPixmap.getPixels();
                ByteBuffer shadowPixels = shadowPixmap.getPixels();
                for (int y = 0; y < mainH; y++) {
                    int shadowRow = shadowW * (y + shadowOffsetY) + shadowOffsetX;
                    for (int x = 0; x < mainW; x++) {
                        int mainPixel = (mainW * y + x) * 4;
                        byte mainA = mainPixels.get(mainPixel + 3);
                        if (mainA == 0)
                            continue;
                        int shadowPixel = (shadowRow + x) * 4;
                        shadowPixels.put(shadowPixel, r);
                        shadowPixels.put(shadowPixel + 1, g);
                        shadowPixels.put(shadowPixel + 2, b);
                        shadowPixels.put(shadowPixel + 3, (byte) ((mainA & 0xff) * a));
                    }
                }
            }
            // Draw main glyph (with any border) on top of shadow.
            for (int i = 0, n = parameter.renderCount; i < n; i++) shadowPixmap.drawPixmap(mainPixmap, Math.max(-parameter.shadowOffsetX, 0), Math.max(-parameter.shadowOffsetY, 0));
            mainPixmap.dispose();
            mainPixmap = shadowPixmap;
        } else if (parameter.borderWidth == 0) {
            // No shadow and no border, draw glyph additional times.
            for (int i = 0, n = parameter.renderCount - 1; i < n; i++) mainPixmap.drawPixmap(mainPixmap, 0, 0);
        }
    }
    GlyphMetrics metrics = slot.getMetrics();
    Glyph glyph = new Glyph();
    glyph.id = c;
    glyph.width = mainPixmap.getWidth();
    glyph.height = mainPixmap.getHeight();
    glyph.xoffset = mainGlyph.getLeft();
    glyph.yoffset = parameter.flip ? -mainGlyph.getTop() + (int) baseLine : -(glyph.height - mainGlyph.getTop()) - (int) baseLine;
    glyph.xadvance = FreeType.toInt(metrics.getHoriAdvance()) + (int) parameter.borderWidth + parameter.spaceX;
    if (bitmapped) {
        mainPixmap.setColor(Color.CLEAR);
        mainPixmap.fill();
        ByteBuffer buf = mainBitmap.getBuffer();
        int whiteIntBits = Color.WHITE.toIntBits();
        int clearIntBits = Color.CLEAR.toIntBits();
        for (int h = 0; h < glyph.height; h++) {
            int idx = h * mainBitmap.getPitch();
            for (int w = 0; w < (glyph.width + glyph.xoffset); w++) {
                int bit = (buf.get(idx + (w / 8)) >>> (7 - (w % 8))) & 1;
                mainPixmap.drawPixel(w, h, ((bit == 1) ? whiteIntBits : clearIntBits));
            }
        }
    }
    Rectangle rect = packer.pack(mainPixmap);
    // Glyph is always packed into the last page for now.
    glyph.page = packer.getPages().size - 1;
    glyph.srcX = (int) rect.x;
    glyph.srcY = (int) rect.y;
    // If a page was added, create a new texture region for the incrementally added glyph.
    if (parameter.incremental && data.regions != null && data.regions.size <= glyph.page)
        packer.updateTextureRegions(data.regions, parameter.minFilter, parameter.magFilter, parameter.genMipMaps);
    mainPixmap.dispose();
    mainGlyph.dispose();
    return glyph;
}
Also used : GlyphSlot(com.badlogic.gdx.graphics.g2d.freetype.FreeType.GlyphSlot) GlyphMetrics(com.badlogic.gdx.graphics.g2d.freetype.FreeType.GlyphMetrics) Color(com.badlogic.gdx.graphics.Color) Rectangle(com.badlogic.gdx.math.Rectangle) ByteBuffer(java.nio.ByteBuffer) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Bitmap(com.badlogic.gdx.graphics.g2d.freetype.FreeType.Bitmap) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)202 IOException (java.io.IOException)40 FileHandle (com.badlogic.gdx.files.FileHandle)14 Array (com.badlogic.gdx.utils.Array)13 Texture (com.badlogic.gdx.graphics.Texture)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)9 InputStream (java.io.InputStream)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)7 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 OutputStream (java.io.OutputStream)7 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)6 LifecycleListener (com.badlogic.gdx.LifecycleListener)5 ByteBuffer (java.nio.ByteBuffer)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4