public void initManagers() { assetManager = new AssetManager(new LocalFileHandleResolver(), true); imagePacks = new ObjectMap<String, Array<String>>(); for (String name : new String[] {"characters", "obstacles", "grounds", "clouds", "bushes", "buildings"}) { imagePacks.put(DATA_PATH + "/" + name, new Array<String>()); } stateManager = new StateManager(this); stateManager.addState("loading", new LoadingState("menu", this)); stateManager.addState("menu", new MenuState(this)); stateManager.addState("game", new GameState(this)); stateManager.addState("credits", new CreditsState(this)); spriteBatch = new SpriteBatch(); pixmapPacker = new PixmapPacker(1024, 1024, Pixmap.Format.RGBA8888, 5, true, new PixmapPacker.GuillotineStrategy()); }
@Override protected Sprite loadResource(FileReference ref) { FileHandle f; String pathPrefix; if(super.root == null || super.root.equals("")) { pathPrefix = ""; } else { pathPrefix = super.root + File.separator; } String path = pathPrefix + data.getFile(ref).name; switch(Gdx.app.getType()){ case iOS: f = Gdx.files.absolute(path); break; default: f = Gdx.files.internal(path); break; } if(!f.exists()) throw new GdxRuntimeException("Could not find file handle "+ path + "! Please check your paths."); if(this.packer == null && this.pack) this.packer = new PixmapPacker(this.atlasWidth, this.atlasHeight, Pixmap.Format.RGBA8888, 2, true); final Pixmap pix = new Pixmap(f); this.pixmaps.put(ref, pix); return null; }
@Override public void create () { batch = new SpriteBatch(); camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0); camera.update(); Pixmap pixmap1 = new Pixmap(Gdx.files.internal("data/badlogic.jpg")); Pixmap pixmap2 = new Pixmap(Gdx.files.internal("data/wheel.png")); Pixmap pixmap3 = new Pixmap(Gdx.files.internal("data/egg.png")); PixmapPacker packer = new PixmapPacker(1024, 1024, Format.RGBA8888, 2, true); packer.pack("badlogic", pixmap1); packer.pack("wheel", pixmap1); packer.pack("egg", pixmap1); pixmap1.dispose(); pixmap2.dispose(); pixmap3.dispose(); atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, false); Gdx.app.log("PixmaPackerTest", "Number of textures: " + atlas.getTextures().size); }
/** * Convenience method for generating a font, and then writing the fnt and * png files. Writing a generated font to files allows the possibility of * only generating the fonts when they are missing, otherwise loading from a * previously generated file. * * @param fontFile * @param fontSize * @param destiny */ private void generateFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight, FileHandle destiny) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontParameter param = new FreeTypeFontParameter(); param.packer = packer; param.characters = FreeTypeFontGenerator.DEFAULT_CHARS; param.size = fontSize; param.flip = false; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator .generateData(param); saveFontToFile(fontData, fontSize, fontName, packer, destiny); generator.dispose(); packer.dispose(); }
private void saveFontToFile(FreeTypeBitmapFontData data, int fontSize, String fontName, PixmapPacker packer, FileHandle destiny) { FileHandle fontFile = Gdx.files.absolute(destiny.file() .getAbsolutePath() + File.separator + fontName + ".fnt"); // .fnt // path BitmapFontWriter.setOutputFormat(BitmapFontWriter.OutputFormat.Text); String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), destiny, fontName); // png dir path Gdx.app.debug(TAG, String.format( "Saving font [%s]: fontfile: %s, pixmapDir: %s\n", fontName, fontFile, destiny)); BitmapFontWriter.writeFont(data, pageRefs, fontFile, new BitmapFontWriter.FontInfo(fontName, fontSize), 1, 1); }
@Override protected Sprite loadResource(FileReference ref) { FileHandle f; String path = super.root+"/"+data.getFile(ref).name; switch(Gdx.app.getType()){ case iOS: f = Gdx.files.absolute(path); break; default: f = Gdx.files.internal(path); break; } if(!f.exists()) throw new GdxRuntimeException("Could not find file handle "+ path + "! Please check your paths."); if(this.packer == null && this.pack) this.packer = new PixmapPacker(this.atlasWidth, this.atlasHeight, Pixmap.Format.RGBA8888, 2, true); final Pixmap pix = new Pixmap(f); this.pixmaps.put(ref, pix); return null; }
/** Convenience method for generating a font, and then writing the fnt and png files. * Writing a generated font to files allows the possibility of only generating the fonts when they are missing, otherwise * loading from a previously generated file. * @param fontFile * @param fontSize */ private BitmapFont generateFontWriteFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator.generateData(fontSize, FreeTypeFontGenerator.DEFAULT_CHARS, false, packer); Array<PixmapPacker.Page> pages = packer.getPages(); TextureRegion[] texRegions = new TextureRegion[pages.size]; for (int i=0; i<pages.size; i++) { PixmapPacker.Page p = pages.get(i); Texture tex = new Texture(new PixmapTextureData(p.getPixmap(), p.getPixmap().getFormat(), false, false, true)) { @Override public void dispose () { super.dispose(); getTextureData().consumePixmap().dispose(); } }; tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); texRegions[i] = new TextureRegion(tex); } BitmapFont font = new BitmapFont(fontData, texRegions, false); saveFontToFile(font, fontSize, fontName, packer); generator.dispose(); packer.dispose(); return font; }
@Override protected Sprite loadResource (FileReference ref) { FileHandle f; String path = super.root + "/" + data.getFile(ref).name; switch (Gdx.app.getType()) { case iOS: f = Gdx.files.absolute(path); break; default: f = Gdx.files.internal(path); break; } if (!f.exists()) throw new GdxRuntimeException("Could not find file handle " + path + "! Please check your paths."); if (this.packer == null && this.pack) this.packer = new PixmapPacker(this.atlasWidth, this.atlasHeight, Pixmap.Format.RGBA8888, 2, true); final Pixmap pix = new Pixmap(f); this.pixmaps.put(ref, pix); return null; }
/** A convenience method to write pixmaps by page; typically returned from a PixmapPacker when used alongside * FreeTypeFontGenerator. * * @param pages the pages containing the Pixmaps * @param outputDir the output directory * @param fileName the file name * @return the file refs */ public static String[] writePixmaps (Array<PixmapPacker.Page> pages, FileHandle outputDir, String fileName) { Pixmap[] pix = new Pixmap[pages.size]; for (int i=0; i<pages.size; i++) { pix[i] = pages.get(i).getPixmap(); } return writePixmaps(pix, outputDir, fileName); }
/** Convenience method for generating a font, and then writing the fnt and png files. * Writing a generated font to files allows the possibility of only generating the fonts when they are missing, otherwise * loading from a previously generated file. * @param fontFile * @param fontSize */ private BitmapFont generateFontWriteFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = fontSize; parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS; parameter.packer = packer; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator.generateData(parameter);//(fontSize, FreeTypeFontGenerator.DEFAULT_CHARS, false, packer); Array<PixmapPacker.Page> pages = packer.getPages(); Array<TextureRegion> texRegions = new Array<TextureRegion>(pageSize); for (int i=0; i<pages.size; i++) { PixmapPacker.Page p = pages.get(i); Texture tex = new Texture(new PixmapTextureData(p.getPixmap(), p.getPixmap().getFormat(), false, false, true)) { @Override public void dispose () { super.dispose(); getTextureData().consumePixmap().dispose(); } }; tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); texRegions.add(new TextureRegion(tex)); } BitmapFont font = new BitmapFont(fontData,texRegions,false); //fontData, texRegions, false); saveFontToFile(font, fontSize, fontName, packer); generator.dispose(); packer.dispose(); return font; }
private void saveFontToFile(BitmapFont font, int fontSize, String fontName, PixmapPacker packer) { FileHandle fontFile = getFontFile(fontName + ".fnt"); // .fnt path FileHandle pixmapDir = getFontFile(fontName); // png dir path BitmapFontWriter.setOutputFormat(BitmapFontWriter.OutputFormat.Text); String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), pixmapDir, fontName); Gdx.app.debug(TAG, String.format("Saving font [%s]: fontfile: %s, pixmapDir: %s\n", fontName, fontFile, pixmapDir)); // here we must add the png dir to the page refs for (int i = 0; i < pageRefs.length; i++) { pageRefs[i] = fontName + "/" + pageRefs[i]; } BitmapFontWriter.writeFont(font.getData(), pageRefs, fontFile, new BitmapFontWriter.FontInfo(fontName, fontSize), 1, 1); }
/** * Convenience method for generating a font, and then writing the fnt and * png files. Writing a generated font to files allows the possibility of * only generating the fonts when they are missing, otherwise loading from a * previously generated file. * * @param fontFile * @param fontSize */ private BitmapFont generateFontWriteFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontParameter freeTypeFontParameter = new FreeTypeFontParameter(); freeTypeFontParameter.size = fontSize; freeTypeFontParameter.packer = packer; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator.generateData(freeTypeFontParameter); Array<PixmapPacker.Page> pages = packer.getPages(); Array<TextureRegion> texRegions = new Array<TextureRegion>(); for (int i = 0; i < pages.size; i++) { PixmapPacker.Page p = pages.get(i); Texture tex = new Texture( new PixmapTextureData(p.getPixmap(), p.getPixmap().getFormat(), false, false, true)) { @Override public void dispose() { super.dispose(); getTextureData().consumePixmap().dispose(); } }; tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); texRegions.add(new TextureRegion(tex)); } BitmapFont font = new BitmapFont(fontData, texRegions, false); saveFontToFile(font, fontSize, fontName, packer); generator.dispose(); packer.dispose(); return font; }
@Override protected Sprite loadResource (FileReference ref) { FileHandle f; String pathPrefix; if (super.root == null || super.root.equals("")) { pathPrefix = ""; } else { pathPrefix = super.root + File.separator; } String path = pathPrefix + data.getFile(ref).name; switch (Gdx.app.getType()) { case iOS: f = Gdx.files.absolute(path); break; default: f = RavTech.files.getAssetHandle(path); break; } if (!f.exists()) throw new GdxRuntimeException("Could not find file handle " + path + "! Please check your paths."); if (this.packer == null && this.pack) this.packer = new PixmapPacker(this.atlasWidth, this.atlasHeight, Pixmap.Format.RGBA8888, 2, true); final Pixmap pix = new Pixmap(f); this.pixmaps.put(ref, pix); return null; }
/** Generates a new {@link BitmapFontData} instance, expert usage only. Throws a GdxRuntimeException in case something went * wrong. * * @param size the size in pixels * @param characters the characters the font should contain * @param flip whether to flip the font vertically, see {@link BitmapFont#BitmapFont(FileHandle, TextureRegion, boolean)} * @param packer the optional PixmapPacker to use * @deprecated use {@link #generateData(FreeTypeFontParameter)} instead */ public FreeTypeBitmapFontData generateData (int size, String characters, boolean flip, PixmapPacker packer) { FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = size; parameter.characters = characters; parameter.flip = flip; parameter.packer = packer; return generateData(parameter); }
/** Convenience method for generating a font, and then writing the fnt and png files. * Writing a generated font to files allows the possibility of only generating the fonts when they are missing, otherwise * loading from a previously generated file. * @param fontFile * @param fontSize */ private BitmapFont generateFontWriteFiles(String fontName, FileHandle fontFile, int fontSize, int pageWidth, int pageHeight) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); PixmapPacker packer = new PixmapPacker(pageWidth, pageHeight, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = fontSize; parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS; parameter.flip = false; parameter.packer = packer; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator.generateData(parameter); Array<PixmapPacker.Page> pages = packer.getPages(); Array<TextureRegion> texRegions = new Array<>(); for (int i = 0; i < pages.size; i++) { PixmapPacker.Page p = pages.get(i); Texture tex = new Texture( new PixmapTextureData(p.getPixmap(), p.getPixmap().getFormat(), false, false, true)) { @Override public void dispose() { super.dispose(); getTextureData().consumePixmap().dispose(); } }; tex.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest); texRegions.add(new TextureRegion(tex)); } BitmapFont font = new BitmapFont((BitmapFont.BitmapFontData) fontData, texRegions, false); saveFontToFile(font, fontSize, fontName, packer); generator.dispose(); packer.dispose(); return font; }
private void saveFontToFile(BitmapFont font, int fontSize, String fontName, PixmapPacker packer) { FileHandle fontFile = getFontFile(fontName + ".fnt", fontSize); // .fnt path FileHandle pixmapDir = getFontFile(fontName, fontSize); // png dir path BitmapFontWriter.setOutputFormat(BitmapFontWriter.OutputFormat.Text); String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), pixmapDir, fontName); Gdx.app.debug(TAG, String.format("Saving font [%s]: fontfile: %s, pixmapDir: %s\n", fontName, fontFile, pixmapDir)); // here we must add the png dir to the page refs for (int i = 0; i < pageRefs.length; i++) { pageRefs[i] = fontSize + "_" + fontName + "/" + pageRefs[i]; } BitmapFontWriter.writeFont(font.getData(), pageRefs, fontFile, new BitmapFontWriter.FontInfo(fontName, fontSize), 1, 1); }
static void initialize() { texturePacker = new PixmapPacker(2048, 2048, Format.RGBA8888, 2, false); textureAtlas = new TextureAtlas(); terrariaAssets = Gdx.files.local("assets-terraria"); loadPlayerAssets(); }
private void saveFontToFile( FreeTypeFontGenerator.FreeTypeBitmapFontData data, int fontSize, String fontName, PixmapPacker packer, FileHandle destiny) { FileHandle fontFile = destiny.child(fontName + ".fnt"); // .fnt path BitmapFontWriter.setOutputFormat(BitmapFontWriter.OutputFormat.Text); String[] pageRefs = BitmapFontWriter.writePixmaps(packer.getPages(), destiny, fontName); // png dir path BitmapFontWriter.writeFont(data, pageRefs, fontFile, new BitmapFontWriter.FontInfo(fontName, fontSize), 1, 1); }
public PixmapPacker getPixmapPacker() { return pixmapPacker; }
public ImageFileWalker(PixmapPacker packer) { this.packer = packer; }
/** * Convenience method for generating a font, and then writing the fnt and * png files. Writing a generated font to files allows the possibility of * only generating the fonts when they are missing, otherwise loading from a * previously generated file. * * @param fontFile * @param fontSize * @param destiny */ private void generateFiles(String fontName, FileHandle fontFile, int fontSize, FileHandle destiny) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); // compute the minimum page size for a square png FreeType.Library library = FreeType.initFreeType(); FreeType.Face face = FreeType.newFace(library, fontFile, 0); FreeType.setPixelSizes(face, 0, fontSize); FreeType.SizeMetrics fontMetrics = face.getSize().getMetrics(); float scale = FreeType.toInt(fontMetrics.getHeight()); for (int c = 32; c < (32 + face.getNumGlyphs()); c++) { if (FreeType.loadChar(face, c, FreeType.FT_LOAD_DEFAULT)) { int lh = FreeType.toInt(face.getGlyph().getMetrics() .getHeight()); scale = (lh > scale) ? lh : scale; } } // generate the glyphs int maxGlyphHeight = (int) Math.ceil(scale); float area = maxGlyphHeight * maxGlyphHeight * FreeTypeFontGenerator.DEFAULT_CHARS.length(); int pageWidth = MathUtils.nextPowerOfTwo((int) Math.sqrt(area)); pageWidth = Math.min(pageWidth, 1024); PixmapPacker packer = new PixmapPacker(pageWidth, pageWidth, Pixmap.Format.RGBA8888, 2, false); FreeTypeFontGenerator.FreeTypeFontParameter param = new FreeTypeFontGenerator.FreeTypeFontParameter(); param.packer = packer; param.characters = FreeTypeFontGenerator.DEFAULT_CHARS; param.size = fontSize; param.flip = false; FreeTypeFontGenerator.FreeTypeBitmapFontData fontData = generator .generateData(param); saveFontToFile(fontData, fontSize, fontName, packer, destiny); generator.dispose(); packer.dispose(); }