From 1c2b9629f0c5375c06de34ff73393a2d656d987a Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 7 Apr 2007 18:14:19 +0000 Subject: [PATCH] Now BitmapedText is working whit a costum mirrored font image --- src/ei/engine/effects/BitmapText.java | 71 +++++--- src/ei/engine/effects/Text.java | 239 -------------------------- src/ei/engine/test/TextTest.java | 4 +- 3 files changed, 49 insertions(+), 265 deletions(-) delete mode 100644 src/ei/engine/effects/Text.java diff --git a/src/ei/engine/effects/BitmapText.java b/src/ei/engine/effects/BitmapText.java index a51675b..6c52c9c 100644 --- a/src/ei/engine/effects/BitmapText.java +++ b/src/ei/engine/effects/BitmapText.java @@ -15,10 +15,13 @@ import ei.engine.texture.TextureLoader; */ public class BitmapText extends Entity{ - private static final int CHAR_SIZE = 10; private Texture texture; + // The Text to display private String text; + // Base Display List For The Font Set + private static int base = -1; + private boolean bold = false; /** * Create a Text object @@ -27,7 +30,9 @@ public class BitmapText extends Entity{ */ public BitmapText(String name) { super(name); - texture = TextureLoader.getTextureLoaderInstance().getTexture("data/font.png"); + texture = TextureLoader.getTextureLoaderInstance().getTexture("data/font.bmp"); + if(base < 0) + buildFont(); } /** @@ -39,6 +44,10 @@ public class BitmapText extends Entity{ text = msg; } + public void setBold(boolean b){ + bold = b; + } + /** * Render the text */ @@ -56,35 +65,49 @@ public class BitmapText extends Entity{ texture.bindGL(); - for(int i=0; i height) ? width * 16 : height * 16; - if(!directionSet) { - if(lineWidth > bitmapSize) { - delta = -2; - } - else { - delta = 2; - } - directionSet = true; - } - if(delta > 0) { - if(lineWidth < bitmapSize) { - fontSize += delta; - } - else { - sizeFound = true; - fontSize -= delta; - } - } - else if(delta < 0) { - if(lineWidth > bitmapSize) { - fontSize += delta; - } - else { - sizeFound = true; - fontSize -= delta; - } - } - } - - /* Now that a font size has been determined, create the final image, set the font and draw the - * standard/extended ASCII character set for that font. - */ - font = new Font(fontName, Font.BOLD, fontSize); // Font Name - // use BufferedImage.TYPE_4BYTE_ABGR to allow alpha blending - fontImage = new BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR); - Graphics2D g = (Graphics2D)fontImage.getGraphics(); - g.setFont(font); - g.setColor(OPAQUE_WHITE); - g.setBackground(TRANSPARENT_BLACK); - FontMetrics fm = g.getFontMetrics(); - for(int i=0;i<256;i++) { - int x = i % 16; - int y = i / 16; - char ch[] = {(char)i}; - String temp = new String(ch); - g.drawString(temp, (x * 32) + 1, (y * 32) + fm.getAscent()); - } - - /* The following code is taken directly for the LWJGL example code. - * It takes a Java Image and converts it into an OpenGL texture. - * This is a very powerful feature as you can use this to generate textures on the fly out - * of anything. - */ - // Flip Image - AffineTransform tx = AffineTransform.getScaleInstance(1, -1); - tx.translate(0, -fontImage.getHeight(null)); - AffineTransformOp op = - new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); - fontImage = op.filter(fontImage, null); - - // Put Image In Memory - ByteBuffer scratch = - ByteBuffer.allocateDirect( - 4 * fontImage.getWidth() * fontImage.getHeight()); - - byte data[] = - (byte[])fontImage.getRaster().getDataElements( - 0, - 0, - fontImage.getWidth(), - fontImage.getHeight(), - null); - scratch.clear(); - scratch.put(data); - scratch.rewind(); - - // Create A IntBuffer For Image Address In Memory - IntBuffer buf = - ByteBuffer - .allocateDirect(4) - .order(ByteOrder.nativeOrder()) - .asIntBuffer(); - GL11.glGenTextures(buf); // Create Texture In OpenGL - - GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0)); - // Typical Texture Generation Using Data From The Image - - // Linear Filtering - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); - // Linear Filtering - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); - // Generate The Texture - GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, fontImage.getWidth(), fontImage.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch); - - - texture = buf.get(0); // Return Image Address In Memory - - base = GL11.glGenLists(256); // Storage For 256 Characters - - /* Generate the display lists. One for each character in the standard/extended ASCII chart. - */ - float textureDelta = 1.0f / 16.0f; - for(int i=0;i<256;i++) { - float u = ((float)(i % 16)) / 16.0f; - float v = 1.f - (((float)(i / 16)) / 16.0f); - GL11.glNewList(base + i, GL11.GL_COMPILE); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture); - GL11.glBegin(GL11.GL_QUADS); - GL11.glTexCoord2f(u, v); - GL11.glVertex3f(-0.0450f, 0.0450f, 0.0f); - GL11.glTexCoord2f((u + textureDelta), v); - GL11.glVertex3f(0.0450f, 0.0450f, 0.0f); - GL11.glTexCoord2f((u + textureDelta), v - textureDelta); - GL11.glVertex3f(0.0450f, -0.0450f, 0.0f); - GL11.glTexCoord2f(u, v - textureDelta); - GL11.glVertex3f(-0.0450f, -0.0450f, 0.0f); - GL11.glEnd(); - GL11.glEndList(); - } - } - - public Rectangle getBound() { - return null; - } -} diff --git a/src/ei/engine/test/TextTest.java b/src/ei/engine/test/TextTest.java index 1e75a0e..d0e91f3 100644 --- a/src/ei/engine/test/TextTest.java +++ b/src/ei/engine/test/TextTest.java @@ -18,14 +18,14 @@ public class TextTest extends LWJGLGameWindow{ protected void init(){ text = new BitmapText("Test text"); - text.setText("LOL THIS IS A TEST!!"); + text.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); pos = 0; } protected void update() { super.update(); pos += 0.2f; - text.setLocation(new Vector2f(200,200)); + text.setLocation(new Vector2f(200+pos,200+pos)); text.update(); }