use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.
the class BitmapFontWriter method writeFont.
/** Writes the given BitmapFontData to a file, using the specified <tt>pageRefs</tt> strings as the image paths for each
* texture page. The glyphs in BitmapFontData have a "page" id, which references the index of the pageRef you specify here.
*
* The FontInfo parameter is useful for cleaner output; such as including a size and font face name hint. However, it can be
* null to use default values. LibGDX ignores most of the "info" line when reading back fonts, only padding is used. Padding
* also affects the size, location, and offset of the glyphs that are output.
*
* Likewise, the scaleW and scaleH are only for cleaner output. They are currently ignored by LibGDX's reader. For maximum
* compatibility with other BMFont tools, you should use the width and height of your texture pages (each page should be the
* same size).
*
* @param fontData the bitmap font
* @param pageRefs the references to each texture page image file, generally in the same folder as outFntFile
* @param outFntFile the font file to save to (typically ends with '.fnt')
* @param info the optional info for the file header; can be null
* @param scaleW the width of your texture pages
* @param scaleH the height of your texture pages */
public static void writeFont(BitmapFontData fontData, String[] pageRefs, FileHandle outFntFile, FontInfo info, int scaleW, int scaleH) {
if (info == null) {
info = new FontInfo();
info.face = outFntFile.nameWithoutExtension();
}
int lineHeight = (int) fontData.lineHeight;
int pages = pageRefs.length;
int packed = 0;
int base = (int) ((fontData.capHeight) + (fontData.flipped ? -fontData.ascent : fontData.ascent));
OutputFormat fmt = BitmapFontWriter.getOutputFormat();
boolean xml = fmt == OutputFormat.XML;
StringBuilder buf = new StringBuilder();
if (xml) {
buf.append("<font>\n");
}
String xmlOpen = xml ? "\t<" : "";
String xmlCloseSelf = xml ? "/>" : "";
String xmlTab = xml ? "\t" : "";
String xmlClose = xml ? ">" : "";
String xmlQuote = xml ? "\"" : "";
String alphaChnlParams = xml ? " alphaChnl=\"0\" redChnl=\"0\" greenChnl=\"0\" blueChnl=\"0\"" : " alphaChnl=0 redChnl=0 greenChnl=0 blueChnl=0";
// INFO LINE
buf.append(xmlOpen).append("info face=\"").append(info.face == null ? "" : info.face.replaceAll("\"", "'")).append("\" size=").append(quote(info.size)).append(" bold=").append(quote(info.bold ? 1 : 0)).append(" italic=").append(quote(info.italic ? 1 : 0)).append(" charset=\"").append(info.charset == null ? "" : info.charset).append("\" unicode=").append(quote(info.unicode ? 1 : 0)).append(" stretchH=").append(quote(info.stretchH)).append(" smooth=").append(quote(info.smooth ? 1 : 0)).append(" aa=").append(quote(info.aa)).append(" padding=").append(xmlQuote).append(info.padding.up).append(",").append(info.padding.right).append(",").append(info.padding.down).append(",").append(info.padding.left).append(xmlQuote).append(" spacing=").append(xmlQuote).append(info.spacing.horizontal).append(",").append(info.spacing.vertical).append(xmlQuote).append(xmlCloseSelf).append("\n");
// COMMON line
buf.append(xmlOpen).append("common lineHeight=").append(quote(lineHeight)).append(" base=").append(quote(base)).append(" scaleW=").append(quote(scaleW)).append(" scaleH=").append(quote(scaleH)).append(" pages=").append(quote(pages)).append(" packed=").append(quote(packed)).append(alphaChnlParams).append(xmlCloseSelf).append("\n");
if (xml)
buf.append("\t<pages>\n");
// PAGES
for (int i = 0; i < pageRefs.length; i++) {
buf.append(xmlTab).append(xmlOpen).append("page id=").append(quote(i)).append(" file=\"").append(pageRefs[i]).append("\"").append(xmlCloseSelf).append("\n");
}
if (xml)
buf.append("\t</pages>\n");
// CHARS
Array<Glyph> glyphs = new Array<Glyph>(256);
for (int i = 0; i < fontData.glyphs.length; i++) {
if (fontData.glyphs[i] == null)
continue;
for (int j = 0; j < fontData.glyphs[i].length; j++) {
if (fontData.glyphs[i][j] != null) {
glyphs.add(fontData.glyphs[i][j]);
}
}
}
buf.append(xmlOpen).append("chars count=").append(quote(glyphs.size)).append(xmlClose).append("\n");
int padLeft = 0, padRight = 0, padTop = 0, padX = 0, padY = 0;
if (info != null) {
padTop = info.padding.up;
padLeft = info.padding.left;
padRight = info.padding.right;
padX = padLeft + padRight;
padY = info.padding.up + info.padding.down;
}
// CHAR definitions
for (int i = 0; i < glyphs.size; i++) {
Glyph g = glyphs.get(i);
boolean empty = g.width == 0 || g.height == 0;
buf.append(xmlTab).append(xmlOpen).append("char id=").append(quote(String.format("%-6s", g.id), true)).append("x=").append(quote(String.format("%-5s", empty ? 0 : g.srcX - padLeft), true)).append("y=").append(quote(String.format("%-5s", empty ? 0 : g.srcY - padRight), true)).append("width=").append(quote(String.format("%-5s", empty ? 0 : g.width + padX), true)).append("height=").append(quote(String.format("%-5s", empty ? 0 : g.height + padY), true)).append("xoffset=").append(quote(String.format("%-5s", g.xoffset - padLeft), true)).append("yoffset=").append(quote(String.format("%-5s", fontData.flipped ? g.yoffset + padTop : -(g.height + (g.yoffset + padTop))), true)).append("xadvance=").append(quote(String.format("%-5s", g.xadvance), true)).append("page=").append(quote(String.format("%-5s", g.page), true)).append("chnl=").append(quote(0, true)).append(xmlCloseSelf).append("\n");
}
if (xml)
buf.append("\t</chars>\n");
// KERNINGS
int kernCount = 0;
StringBuilder kernBuf = new StringBuilder();
for (int i = 0; i < glyphs.size; i++) {
for (int j = 0; j < glyphs.size; j++) {
Glyph first = glyphs.get(i);
Glyph second = glyphs.get(j);
int kern = first.getKerning((char) second.id);
if (kern != 0) {
kernCount++;
kernBuf.append(xmlTab).append(xmlOpen).append("kerning first=").append(quote(first.id)).append(" second=").append(quote(second.id)).append(" amount=").append(quote(kern, true)).append(xmlCloseSelf).append("\n");
}
}
}
// KERN info
buf.append(xmlOpen).append("kernings count=").append(quote(kernCount)).append(xmlClose).append("\n");
buf.append(kernBuf);
if (xml) {
buf.append("\t</kernings>\n");
buf.append("</font>");
}
String charset = info.charset;
if (charset != null && charset.length() == 0)
charset = null;
outFntFile.writeString(buf.toString(), false, charset);
}
use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.
the class FreeTypeFontGenerator method generateFont.
/** Generates a new {@link BitmapFont}. The size is expressed in pixels. Throws a GdxRuntimeException if the font could not be
* generated. Using big sizes might cause such an exception.
* @param parameter configures how the font is generated */
public BitmapFont generateFont(FreeTypeFontParameter parameter, FreeTypeBitmapFontData data) {
generateData(parameter, data);
if (data.regions == null && parameter.packer != null) {
data.regions = new Array();
parameter.packer.updateTextureRegions(data.regions, parameter.minFilter, parameter.magFilter, parameter.genMipMaps);
}
BitmapFont font = new BitmapFont(data, data.regions, true);
font.setOwnsTexture(parameter.packer == null);
return font;
}
use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.
the class Lwjgl3Application method loop.
private void loop() {
Array<Lwjgl3Window> closedWindows = new Array<Lwjgl3Window>();
while (running && windows.size > 0) {
// FIXME put it on a separate thread
if (audio instanceof OpenALAudio) {
((OpenALAudio) audio).update();
}
boolean haveWindowsRendered = false;
closedWindows.clear();
for (Lwjgl3Window window : windows) {
window.makeCurrent();
currentWindow = window;
synchronized (lifecycleListeners) {
haveWindowsRendered |= window.update();
}
if (window.shouldClose()) {
closedWindows.add(window);
}
}
GLFW.glfwPollEvents();
boolean shouldRequestRendering;
synchronized (runnables) {
shouldRequestRendering = runnables.size > 0;
executedRunnables.clear();
executedRunnables.addAll(runnables);
runnables.clear();
}
for (Runnable runnable : executedRunnables) {
runnable.run();
}
if (shouldRequestRendering) {
// in the following render.
for (Lwjgl3Window window : windows) {
if (!window.getGraphics().isContinuousRendering())
window.requestRendering();
}
}
for (Lwjgl3Window closedWindow : closedWindows) {
if (windows.size == 1) {
// when there is only 1 window left, which is in the process of being disposed.
for (int i = lifecycleListeners.size - 1; i >= 0; i--) {
LifecycleListener l = lifecycleListeners.get(i);
l.pause();
l.dispose();
}
lifecycleListeners.clear();
}
closedWindow.dispose();
windows.removeValue(closedWindow, false);
}
if (!haveWindowsRendered) {
// with continuous rendering disabled.
try {
Thread.sleep(1000 / config.idleFPS);
} catch (InterruptedException e) {
// ignore
}
}
}
}
use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.
the class AtlasTmxMapLoader method getDependencies.
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle tmxFile, AtlasTiledMapLoaderParameters parameter) {
Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
try {
root = xml.parse(tmxFile);
Element properties = root.getChildByName("properties");
if (properties != null) {
for (Element property : properties.getChildrenByName("property")) {
String name = property.getAttribute("name");
String value = property.getAttribute("value");
if (name.startsWith("atlas")) {
FileHandle atlasHandle = getRelativeFileHandle(tmxFile, value);
dependencies.add(new AssetDescriptor(atlasHandle, TextureAtlas.class));
}
}
}
} catch (IOException e) {
throw new GdxRuntimeException("Unable to parse .tmx file.");
}
return dependencies;
}
use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.
the class AtlasTmxMapLoader method loadTileset.
protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver) {
if (element.getName().equals("tileset")) {
String name = element.get("name", null);
int firstgid = element.getIntAttribute("firstgid", 1);
int tilewidth = element.getIntAttribute("tilewidth", 0);
int tileheight = element.getIntAttribute("tileheight", 0);
int spacing = element.getIntAttribute("spacing", 0);
int margin = element.getIntAttribute("margin", 0);
String source = element.getAttribute("source", null);
int offsetX = 0;
int offsetY = 0;
String imageSource = "";
int imageWidth = 0, imageHeight = 0;
FileHandle image = null;
if (source != null) {
FileHandle tsx = getRelativeFileHandle(tmxFile, source);
try {
element = xml.parse(tsx);
name = element.get("name", null);
tilewidth = element.getIntAttribute("tilewidth", 0);
tileheight = element.getIntAttribute("tileheight", 0);
spacing = element.getIntAttribute("spacing", 0);
margin = element.getIntAttribute("margin", 0);
Element offset = element.getChildByName("tileoffset");
if (offset != null) {
offsetX = offset.getIntAttribute("x", 0);
offsetY = offset.getIntAttribute("y", 0);
}
Element imageElement = element.getChildByName("image");
if (imageElement != null) {
imageSource = imageElement.getAttribute("source");
imageWidth = imageElement.getIntAttribute("width", 0);
imageHeight = imageElement.getIntAttribute("height", 0);
image = getRelativeFileHandle(tsx, imageSource);
}
} catch (IOException e) {
throw new GdxRuntimeException("Error parsing external tileset.");
}
} else {
Element offset = element.getChildByName("tileoffset");
if (offset != null) {
offsetX = offset.getIntAttribute("x", 0);
offsetY = offset.getIntAttribute("y", 0);
}
Element imageElement = element.getChildByName("image");
if (imageElement != null) {
imageSource = imageElement.getAttribute("source");
imageWidth = imageElement.getIntAttribute("width", 0);
imageHeight = imageElement.getIntAttribute("height", 0);
image = getRelativeFileHandle(tmxFile, imageSource);
}
}
String atlasFilePath = map.getProperties().get("atlas", String.class);
if (atlasFilePath == null) {
FileHandle atlasFile = tmxFile.sibling(tmxFile.nameWithoutExtension() + ".atlas");
if (atlasFile.exists())
atlasFilePath = atlasFile.name();
}
if (atlasFilePath == null) {
throw new GdxRuntimeException("The map is missing the 'atlas' property");
}
// get the TextureAtlas for this tileset
FileHandle atlasHandle = getRelativeFileHandle(tmxFile, atlasFilePath);
atlasHandle = resolve(atlasHandle.path());
TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
String regionsName = name;
for (Texture texture : atlas.getTextures()) {
trackedTextures.add(texture);
}
TiledMapTileSet tileset = new TiledMapTileSet();
MapProperties props = tileset.getProperties();
tileset.setName(name);
props.put("firstgid", firstgid);
props.put("imagesource", imageSource);
props.put("imagewidth", imageWidth);
props.put("imageheight", imageHeight);
props.put("tilewidth", tilewidth);
props.put("tileheight", tileheight);
props.put("margin", margin);
props.put("spacing", spacing);
if (imageSource != null && imageSource.length() > 0) {
int lastgid = firstgid + ((imageWidth / tilewidth) * (imageHeight / tileheight)) - 1;
for (AtlasRegion region : atlas.findRegions(regionsName)) {
// handle unused tile ids
if (region != null) {
int tileid = region.index + 1;
if (tileid >= firstgid && tileid <= lastgid) {
StaticTiledMapTile tile = new StaticTiledMapTile(region);
tile.setId(tileid);
tile.setOffsetX(offsetX);
tile.setOffsetY(flipY ? -offsetY : offsetY);
tileset.putTile(tileid, tile);
}
}
}
}
for (Element tileElement : element.getChildrenByName("tile")) {
int tileid = firstgid + tileElement.getIntAttribute("id", 0);
TiledMapTile tile = tileset.getTile(tileid);
if (tile == null) {
Element imageElement = tileElement.getChildByName("image");
if (imageElement != null) {
// Is a tilemap with individual images.
String regionName = imageElement.getAttribute("source");
regionName = regionName.substring(0, regionName.lastIndexOf('.'));
AtlasRegion region = atlas.findRegion(regionName);
if (region == null)
throw new GdxRuntimeException("Tileset region not found: " + regionName);
tile = new StaticTiledMapTile(region);
tile.setId(tileid);
tile.setOffsetX(offsetX);
tile.setOffsetY(flipY ? -offsetY : offsetY);
tileset.putTile(tileid, tile);
}
}
if (tile != null) {
String terrain = tileElement.getAttribute("terrain", null);
if (terrain != null) {
tile.getProperties().put("terrain", terrain);
}
String probability = tileElement.getAttribute("probability", null);
if (probability != null) {
tile.getProperties().put("probability", probability);
}
Element properties = tileElement.getChildByName("properties");
if (properties != null) {
loadProperties(tile.getProperties(), properties);
}
}
}
Array<Element> tileElements = element.getChildrenByName("tile");
Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();
for (Element tileElement : tileElements) {
int localtid = tileElement.getIntAttribute("id", 0);
TiledMapTile tile = tileset.getTile(firstgid + localtid);
if (tile != null) {
Element animationElement = tileElement.getChildByName("animation");
if (animationElement != null) {
Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
IntArray intervals = new IntArray();
for (Element frameElement : animationElement.getChildrenByName("frame")) {
staticTiles.add((StaticTiledMapTile) tileset.getTile(firstgid + frameElement.getIntAttribute("tileid")));
intervals.add(frameElement.getIntAttribute("duration"));
}
AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
animatedTile.setId(tile.getId());
animatedTiles.add(animatedTile);
tile = animatedTile;
}
String terrain = tileElement.getAttribute("terrain", null);
if (terrain != null) {
tile.getProperties().put("terrain", terrain);
}
String probability = tileElement.getAttribute("probability", null);
if (probability != null) {
tile.getProperties().put("probability", probability);
}
Element properties = tileElement.getChildByName("properties");
if (properties != null) {
loadProperties(tile.getProperties(), properties);
}
}
}
for (AnimatedTiledMapTile tile : animatedTiles) {
tileset.putTile(tile.getId(), tile);
}
Element properties = element.getChildByName("properties");
if (properties != null) {
loadProperties(tileset.getProperties(), properties);
}
map.getTileSets().addTileSet(tileset);
}
}
Aggregations