Skip to content

Commit

Permalink
Changes in nifty-gui required to implement AWT-backed TTF font render…
Browse files Browse the repository at this point in the history
…ing from jglfont side
  • Loading branch information
iamtakingiteasy committed Dec 29, 2013
1 parent 815bb0c commit 472dbdf
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 106 deletions.
2 changes: 1 addition & 1 deletion nifty-core/pom.xml
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>org.jglfont</groupId>
<artifactId>jglfont-core</artifactId>
<version>1.3</version>
<version>1.4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Expand Up @@ -11,13 +11,14 @@
import de.lessvoid.nifty.tools.Color;
import de.lessvoid.nifty.tools.ColorValueParser;
import de.lessvoid.nifty.tools.resourceloader.NiftyResourceLoader;
import org.jglfont.BitmapFontFactory;
import org.jglfont.spi.BitmapFontRenderer;
import org.jglfont.JGLFontFactory;
import org.jglfont.spi.JGLFontRenderer;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class BatchRenderDevice implements RenderDevice {
private int completeClippedCounter;

@Nonnull
private final BitmapFontFactory factory;
private final JGLFontFactory factory;
@Nonnull
private final BatchRenderBackend renderBackend;
@Nonnull
Expand Down Expand Up @@ -88,7 +89,7 @@ public BatchRenderDevice(
frames = 0;
generator = new TextureAtlasGenerator(atlasWidth, atlasHeight);
fontRenderer = new FontRenderer(this);
factory = new BitmapFontFactory(fontRenderer);
factory = new JGLFontFactory(fontRenderer);
renderBackend.createAtlasTexture(atlasWidth, atlasHeight);
}

Expand Down Expand Up @@ -584,7 +585,7 @@ private boolean isInsideClippingRectangle(final float x, final float y, final fl
return false;
}

private class FontRenderer implements BitmapFontRenderer {
private class FontRenderer implements JGLFontRenderer {
private final Map<String, BitmapInfo> textureInfos = new HashMap<String, BitmapInfo>();
private final ColorValueParser colorValueParser = new ColorValueParser();
private final BatchRenderDevice batchRenderDevice;
Expand All @@ -609,10 +610,26 @@ public void registerBitmap(
textureInfos.put(bitmapId, new BitmapInfo((BatchRenderImage) batchRenderDevice.createImage(filename, true)));
}

@Override
public void registerBitmap(
@Nonnull final String bitmapId,
@Nonnull final ByteBuffer data,
final int width,
final int height,
@Nonnull final String filename
) throws IOException {
BatchRenderImage batchRenderImage = null;
Image image = renderBackend.loadImage(data, width, height);
if (image != null) {
batchRenderImage = new BatchRenderImage(image, generator, filename, renderBackend);
}
textureInfos.put(bitmapId, new BitmapInfo(batchRenderImage));
}

@Override
public void registerGlyph(
final String bitmapId,
final char c,
final int c,
final int xoff,
final int yoff,
final int w,
Expand Down Expand Up @@ -663,7 +680,7 @@ public void render(
final String bitmapId,
final int x,
final int y,
final char c,
final int c,
final float sx,
final float sy,
final float r,
Expand Down Expand Up @@ -750,7 +767,7 @@ public void renderQuad(

private static class BitmapInfo {
private final BatchRenderImage image;
private final Map<Character, CharRenderInfo> characterIndices = new HashMap<Character, CharRenderInfo>();
private final Map<Integer, CharRenderInfo> characterIndices = new HashMap<Integer, CharRenderInfo>();
private Result result;

public BitmapInfo(final BatchRenderImage image) {
Expand All @@ -769,15 +786,15 @@ private void unload() {
image.markAsUnloaded();
}

public void renderCharacter(char c, int x, int y, float sx, float sy, @Nonnull Color textColor) {
public void renderCharacter(int c, int x, int y, float sx, float sy, @Nonnull Color textColor) {
int atlasX0 = result.getX();
int atlasY0 = result.getY();
int atlasImageW = result.getOriginalImageWidth();
int atlasImageH = result.getOriginalImageHeight();
characterIndices.get(c).renderQuad(x, y, sx, sy, textColor, atlasX0, atlasY0, atlasImageW, atlasImageH);
}

public void addCharRenderInfo(final Character c, final CharRenderInfo renderInfo) {
public void addCharRenderInfo(final Integer c, final CharRenderInfo renderInfo) {
this.characterIndices.put(c, renderInfo);
}
}
Expand Down
Expand Up @@ -2,20 +2,20 @@

import de.lessvoid.nifty.spi.render.RenderFont;
import de.lessvoid.nifty.tools.resourceloader.NiftyResourceLoader;
import org.jglfont.BitmapFont;
import org.jglfont.BitmapFontFactory;
import org.jglfont.JGLFont;
import org.jglfont.JGLFontFactory;

import javax.annotation.Nonnull;
import java.io.IOException;

public class BatchRenderFont implements RenderFont {
private final BatchRenderDevice batchRenderDevice;
private final BitmapFont font;
private final JGLFont font;

public BatchRenderFont(
final BatchRenderDevice batchRenderDevice,
@Nonnull final String name,
@Nonnull final BitmapFontFactory factory,
@Nonnull final JGLFontFactory factory,
@Nonnull final NiftyResourceLoader resourceLoader) throws IOException {
this.batchRenderDevice = batchRenderDevice;
this.font = factory.loadFont(resourceLoader.getResourceAsStream(name), name);
Expand Down Expand Up @@ -46,7 +46,7 @@ public void dispose() {
batchRenderDevice.disposeFont(this);
}

public BitmapFont getBitmapFont() {
public JGLFont getBitmapFont() {
return font;
}
}
Expand Up @@ -8,6 +8,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
* Nifty BatchRenderBackend used to interface any graphics backend to the new BatchRenderDevice. This looks like the
Expand Down Expand Up @@ -118,6 +119,17 @@ public interface BatchRenderBackend {
@Nullable
Image loadImage(@Nonnull String filename);

/**
* Wraps given buffer into Image interface
*
* @param data image data
* @param w width of the image
* @param h height of the image
* @return wrapped image
*/
@Nullable
Image loadImage(@Nonnull ByteBuffer data, int w, int h);

/**
* Adds the given image to the main texture atlas at the given position.
*
Expand Down Expand Up @@ -196,4 +208,38 @@ public interface Image {

int getHeight();
}

/**
* Generic implementation of Image interface backed up with byte buffer as main or optional storage.
* @author iamtakingiteasy
*/
public static class ByteBufferedImage implements Image {
protected final ByteBuffer buffer;
protected final int width;
protected final int height;

public ByteBufferedImage() {
this(null, 0, 0);
}

public ByteBufferedImage(ByteBuffer buffer, int width, int height) {
this.buffer = buffer;
this.width = width;
this.height = height;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}

public ByteBuffer getBuffer() {
return buffer;
}
}
}
Expand Up @@ -177,7 +177,7 @@ public Image loadImage(@Nonnull final String filename) {
image.rewind();
int width = loader.getWidth();
int height = loader.getHeight();
return new ImageImpl(width, height, image);
return new ImageImpl(image, width, height);
}
} catch (Exception e) {
log.log(Level.WARNING, "problems loading image [" + filename + "]", e);
Expand All @@ -192,6 +192,12 @@ public Image loadImage(@Nonnull final String filename) {
return null;
}

@Nullable
@Override
public Image loadImage(@Nonnull final ByteBuffer data, final int w, final int h) {
return new ImageImpl(data, w, h);
}

@Override
public void addImageToTexture(@Nonnull final Image image, final int x, final int y) {
ImageImpl imageImpl = (ImageImpl) image;
Expand All @@ -209,7 +215,7 @@ public void addImageToTexture(@Nonnull final Image image, final int x, final int
image.getHeight(),
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
imageImpl.byteBuffer);
imageImpl.getBuffer());
}

@Override
Expand Down Expand Up @@ -430,25 +436,9 @@ private IntBuffer createIntBuffer(final int size) {
*
* @author void
*/
private static class ImageImpl implements BatchRenderBackend.Image {
private final int width;
private final int height;
private final ByteBuffer byteBuffer;

public ImageImpl(final int width, final int height, final ByteBuffer byteBuffer) {
this.width = width;
this.height = height;
this.byteBuffer = byteBuffer;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
private static class ImageImpl extends ByteBufferedImage implements BatchRenderBackend.Image {
private ImageImpl(ByteBuffer buffer, int width, int height) {
super(buffer, width, height);
}
}

Expand Down
Expand Up @@ -194,7 +194,7 @@ public Image loadImage(@Nonnull final String filename) {
image.rewind();
int width = loader.getWidth();
int height = loader.getHeight();
return new ImageImpl(width, height, image);
return new ImageImpl(image, width, height);
} catch (Exception e) {
log.log(Level.WARNING, "problems loading image [" + filename + "]", e);
} finally {
Expand All @@ -207,6 +207,12 @@ public Image loadImage(@Nonnull final String filename) {
return null;
}

@Nullable
@Override
public Image loadImage(@Nonnull final ByteBuffer data, final int w, final int h) {
return new ByteBufferedImage(data, w, h);
}

@Override
public void addImageToTexture(@Nonnull final Image image, final int x, final int y) {
ImageImpl imageImpl = (ImageImpl) image;
Expand All @@ -224,7 +230,7 @@ public void addImageToTexture(@Nonnull final Image image, final int x, final int
image.getHeight(),
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
imageImpl.byteBuffer);
imageImpl.getBuffer());
}

@Override
Expand Down Expand Up @@ -366,25 +372,9 @@ private void bind() {
*
* @author void
*/
private static class ImageImpl implements BatchRenderBackend.Image {
private final int width;
private final int height;
private final ByteBuffer byteBuffer;

public ImageImpl(final int width, final int height, final ByteBuffer byteBuffer) {
this.width = width;
this.height = height;
this.byteBuffer = byteBuffer;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
private static class ImageImpl extends ByteBufferedImage implements BatchRenderBackend.Image {
private ImageImpl(ByteBuffer buffer, int width, int height) {
super(buffer, width, height);
}
}

Expand Down
Expand Up @@ -225,6 +225,12 @@ public Image loadImage(@Nonnull final String filename) {
}
}

@Nullable
@Override
public Image loadImage(@Nonnull final ByteBuffer data, final int w, final int h) {
return new GdxBatchRenderImage(data, w, h);
}

@Override
public void addImageToTexture(@Nonnull final Image image, final int x, final int y) {
GdxBatchRenderImage gdxImage = (GdxBatchRenderImage) image;
Expand Down
Expand Up @@ -11,7 +11,7 @@
/**
* @author Aaron Mahan &lt;aaron@forerunnergames.com&gt;
*/
public class GdxBatchRenderImage implements BatchRenderBackend.Image {
public class GdxBatchRenderImage extends BatchRenderBackend.ByteBufferedImage implements BatchRenderBackend.Image {
@Nullable
private Pixmap pixmap;

Expand All @@ -23,19 +23,23 @@ public GdxBatchRenderImage(@Nullable final String filename) {
pixmap = convertPixmapToFormat(pixmap, Pixmap.Format.RGBA8888);
}

public GdxBatchRenderImage(final ByteBuffer buffer, final int width, final int height) {
super(buffer, width, height);
}

@Override
public int getWidth() {
return pixmap != null ? pixmap.getWidth() : 0;
return pixmap != null ? pixmap.getWidth() : super.getWidth();
}

@Override
public int getHeight() {
return pixmap != null ? pixmap.getHeight() : 0;
return pixmap != null ? pixmap.getHeight() : super.getHeight();
}

@Nullable
public ByteBuffer asByteBuffer() {
return pixmap != null ? pixmap.getPixels() : null;
return pixmap != null ? pixmap.getPixels() : super.getBuffer();
}

@Nullable
Expand Down

0 comments on commit 472dbdf

Please sign in to comment.