Now BitmapedText is working whit a costum mirrored font image
This commit is contained in:
parent
39d8eb9247
commit
1c2b9629f0
3 changed files with 49 additions and 265 deletions
|
|
@ -15,10 +15,13 @@ import ei.engine.texture.TextureLoader;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class BitmapText extends Entity{
|
public class BitmapText extends Entity{
|
||||||
private static final int CHAR_SIZE = 10;
|
|
||||||
private Texture texture;
|
private Texture texture;
|
||||||
|
|
||||||
|
// The Text to display
|
||||||
private String text;
|
private String text;
|
||||||
|
// Base Display List For The Font Set
|
||||||
|
private static int base = -1;
|
||||||
|
private boolean bold = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Text object
|
* Create a Text object
|
||||||
|
|
@ -27,7 +30,9 @@ public class BitmapText extends Entity{
|
||||||
*/
|
*/
|
||||||
public BitmapText(String name) {
|
public BitmapText(String name) {
|
||||||
super(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;
|
text = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBold(boolean b){
|
||||||
|
bold = b;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the text
|
* Render the text
|
||||||
*/
|
*/
|
||||||
|
|
@ -56,35 +65,49 @@ public class BitmapText extends Entity{
|
||||||
|
|
||||||
texture.bindGL();
|
texture.bindGL();
|
||||||
|
|
||||||
for(int i=0; i<text.length() ;i++) {
|
int baseOffset = base - 32; // Choose The Font Set (0 or 1)
|
||||||
int c = text.charAt(i);
|
if(bold) baseOffset+= 128;
|
||||||
float y = c/16;
|
for(int i=0;i<text.length();i++) {
|
||||||
float x = c-(16*y);
|
GL11.glCallList(baseOffset + text.charAt(i));
|
||||||
y /= texture.getImageHeight();
|
GL11.glTranslatef(3.0f, 0.0f, 0.0f);
|
||||||
x /= texture.getImageWidth();
|
|
||||||
float size = (float)CHAR_SIZE/texture.getImageWidth();
|
|
||||||
|
|
||||||
System.out.println("lol: "+x+" - "+y+" - "+size);
|
|
||||||
|
|
||||||
GL11.glBegin(GL11.GL_QUADS);
|
|
||||||
GL11.glTexCoord2f(x, y);
|
|
||||||
GL11.glVertex3f(-0.0450f, 0.0450f, 0.0f);
|
|
||||||
GL11.glTexCoord2f((x + size), y);
|
|
||||||
GL11.glVertex3f(0.0450f, 0.0450f, 0.0f);
|
|
||||||
GL11.glTexCoord2f((x + size), y - size);
|
|
||||||
GL11.glVertex3f(0.0450f, -0.0450f, 0.0f);
|
|
||||||
GL11.glTexCoord2f(x, y - size);
|
|
||||||
GL11.glVertex3f(-0.0450f, -0.0450f, 0.0f);
|
|
||||||
GL11.glEnd();
|
|
||||||
|
|
||||||
GL11.glTranslatef(size, 0.0f, 0.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// restore the model view matrix to prevent contamination
|
// restore the model view matrix to prevent contamination
|
||||||
GL11.glPopMatrix();
|
GL11.glPopMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* From NeHe
|
||||||
|
* @author Mark Bernard
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void buildFont() { // Build Our Font Display List
|
||||||
|
float cx; // Holds Our X Character Coord
|
||||||
|
float cy; // Holds Our Y Character Coord
|
||||||
|
|
||||||
|
base = GL11.glGenLists(256); // Creating 256 Display Lists
|
||||||
|
texture.bindGL(); // Select Our Font Texture
|
||||||
|
for (int i=0;i<256;i++) { // Loop Through All 256 Lists
|
||||||
|
cx = ((float)(i % 16)) / 16.0f; // X Position Of Current Character
|
||||||
|
cy = ((float)(i / 16)) / 16.0f; // Y Position Of Current Character
|
||||||
|
|
||||||
|
GL11.glNewList(base + i, GL11.GL_COMPILE); // Start Building A List
|
||||||
|
GL11.glBegin(GL11.GL_QUADS); // Use A Quad For Each Character
|
||||||
|
GL11.glTexCoord2f(cx, 1 - cy - 0.0625f); // Texture Coord (Bottom Left)
|
||||||
|
GL11.glVertex2i(0, 0); // Vertex Coord (Bottom Left)
|
||||||
|
GL11.glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f); // Texture Coord (Bottom Right)
|
||||||
|
GL11.glVertex2i(16,0); // Vertex Coord (Bottom Right)
|
||||||
|
GL11.glTexCoord2f(cx + 0.0625f, 1 - cy); // Texture Coord (Top Right)
|
||||||
|
GL11.glVertex2i(16,16); // Vertex Coord (Top Right)
|
||||||
|
GL11.glTexCoord2f(cx, 1 - cy); // Texture Coord (Top Left)
|
||||||
|
GL11.glVertex2i(0, 16); // Vertex Coord (Top Left)
|
||||||
|
GL11.glEnd(); // Done Building Our Quad (Character)
|
||||||
|
GL11.glTranslatef(10.0f, 0.0f, 0.0f); // Move To The Right Of The Character
|
||||||
|
GL11.glEndList(); // Done Building The Display List
|
||||||
|
} // Loop Until All 256 Are Built
|
||||||
|
}
|
||||||
|
|
||||||
public Rectangle getBound() {
|
public Rectangle getBound() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -1,239 +0,0 @@
|
||||||
package ei.engine.effects;
|
|
||||||
|
|
||||||
import java.awt.Color;
|
|
||||||
import java.awt.Font;
|
|
||||||
import java.awt.FontMetrics;
|
|
||||||
import java.awt.Graphics2D;
|
|
||||||
import java.awt.Rectangle;
|
|
||||||
import java.awt.geom.AffineTransform;
|
|
||||||
import java.awt.image.AffineTransformOp;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.nio.IntBuffer;
|
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL11;
|
|
||||||
|
|
||||||
import ei.engine.scene.Entity;
|
|
||||||
|
|
||||||
public class Text extends Entity{
|
|
||||||
//build colours for font with alpha transparency
|
|
||||||
private static final Color OPAQUE_WHITE = new Color(0xFFFFFFFF, true);
|
|
||||||
private static final Color TRANSPARENT_BLACK = new Color(0x00000000, true);
|
|
||||||
|
|
||||||
// Base Display List For The Font Set
|
|
||||||
private int base;
|
|
||||||
private int texture;
|
|
||||||
|
|
||||||
// Name of the font to use
|
|
||||||
private String fontName = "Courier New";
|
|
||||||
private int fontSize = 24;
|
|
||||||
private String text;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a Text object
|
|
||||||
*
|
|
||||||
* @param name The name of this entity
|
|
||||||
*/
|
|
||||||
public Text(String name) {
|
|
||||||
super(name);
|
|
||||||
buildFont();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the text to be displayed
|
|
||||||
*
|
|
||||||
* @param msg The text
|
|
||||||
*/
|
|
||||||
public void setText(String msg){
|
|
||||||
text = msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render the text
|
|
||||||
*/
|
|
||||||
public void render(){
|
|
||||||
// store the current model matrix
|
|
||||||
GL11.glPushMatrix();
|
|
||||||
|
|
||||||
//Sets the location
|
|
||||||
super.setTranslationGL();
|
|
||||||
// No rotation enabled
|
|
||||||
//super.setRotationGL(texture.getImageWidth()/2,texture.getImageHeight()/2);
|
|
||||||
//Sets the scale of the sprite
|
|
||||||
super.setScaleGL();
|
|
||||||
|
|
||||||
GL11.glColor3f(0.5f, 0.5f, 0.5f);
|
|
||||||
if(text != null) {
|
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
|
||||||
for(int i=0; i<text.length() ;i++) {
|
|
||||||
GL11.glCallList(base + text.charAt(i));
|
|
||||||
GL11.glTranslatef(0.05f, 0.0f, 0.0f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// restore the model view matrix to prevent contamination
|
|
||||||
GL11.glPopMatrix();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds a image out of the font and puts it in opengl
|
|
||||||
* @author Mark Bernard
|
|
||||||
*/
|
|
||||||
private void buildFont() { // Build Our Bitmap Font
|
|
||||||
Font font; // Font object
|
|
||||||
|
|
||||||
/* Note that I have set the font to Courier New. This font is not guraunteed to be on all
|
|
||||||
* systems. However it is very common so it is likely to be there. You can replace this name
|
|
||||||
* with any named font on your system or use the Java default names that are guraunteed to be there.
|
|
||||||
* Also note that this will work well with monospace fonts, but does not look as good with
|
|
||||||
* proportional fonts.
|
|
||||||
*/
|
|
||||||
BufferedImage fontImage; // image for creating the bitmap
|
|
||||||
int bitmapSize = 512; // set the size for the bitmap texture
|
|
||||||
boolean sizeFound = false;
|
|
||||||
boolean directionSet = false;
|
|
||||||
int delta = 0;
|
|
||||||
|
|
||||||
/* To find out how much space a Font takes, you need to use a the FontMetrics class.
|
|
||||||
* To get the FontMetrics, you need to get it from a Graphics context. A Graphics context is
|
|
||||||
* only available from a displayable surface, ie any class that subclasses Component or any Image.
|
|
||||||
* First the font is set on a Graphics object. Then get the FontMetrics and find out the width
|
|
||||||
* and height of the widest character (W). Then take the largest of the 2 values and find the
|
|
||||||
* maximum size font that will fit in the size allocated.
|
|
||||||
*/
|
|
||||||
while(!sizeFound) {
|
|
||||||
font = new Font(fontName, Font.PLAIN, 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);
|
|
||||||
FontMetrics fm = g.getFontMetrics();
|
|
||||||
int width = fm.stringWidth("W");
|
|
||||||
int height = fm.getHeight();
|
|
||||||
int lineWidth = (width > 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -18,14 +18,14 @@ public class TextTest extends LWJGLGameWindow{
|
||||||
|
|
||||||
protected void init(){
|
protected void init(){
|
||||||
text = new BitmapText("Test text");
|
text = new BitmapText("Test text");
|
||||||
text.setText("LOL THIS IS A TEST!!");
|
text.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
||||||
pos = 0;
|
pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void update() {
|
protected void update() {
|
||||||
super.update();
|
super.update();
|
||||||
pos += 0.2f;
|
pos += 0.2f;
|
||||||
text.setLocation(new Vector2f(200,200));
|
text.setLocation(new Vector2f(200+pos,200+pos));
|
||||||
text.update();
|
text.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue