This commit is contained in:
parent
257d7e9db5
commit
98c96ed24d
12 changed files with 782 additions and 22 deletions
92
src/ei/engine/effects/BitmapText.java
Normal file
92
src/ei/engine/effects/BitmapText.java
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
package ei.engine.effects;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import ei.engine.scene.Entity;
|
||||
import ei.engine.texture.Texture;
|
||||
import ei.engine.texture.TextureLoader;
|
||||
|
||||
/**
|
||||
* Displays text on the screen by a bitmap
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
|
||||
public class BitmapText extends Entity{
|
||||
private static final int CHAR_SIZE = 10;
|
||||
private Texture texture;
|
||||
|
||||
private String text;
|
||||
|
||||
/**
|
||||
* Create a Text object
|
||||
*
|
||||
* @param name The name of this entity
|
||||
*/
|
||||
public BitmapText(String name) {
|
||||
super(name);
|
||||
texture = TextureLoader.getTextureLoaderInstance().getTexture("data/font.png");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the text to be displayed
|
||||
*
|
||||
* @param msg The text
|
||||
*/
|
||||
public void setText(String msg){
|
||||
text = msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the text
|
||||
*/
|
||||
public void render(){
|
||||
if(text != null && !text.isEmpty()){
|
||||
// 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();
|
||||
|
||||
texture.bindGL();
|
||||
|
||||
for(int i=0; i<text.length() ;i++) {
|
||||
int c = text.charAt(i);
|
||||
float y = c/16;
|
||||
float x = c-(16*y);
|
||||
y /= texture.getImageHeight();
|
||||
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
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
public Rectangle getBound() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,5 @@
|
|||
package ei.engine.effects;
|
||||
|
||||
/*
|
||||
* This Code Was Created By Jeff Molofee and GB Schmick 2000
|
||||
* A HUGE Thanks To Fredric Echols For Cleaning Up
|
||||
* And Optimizing The Base Code, Making It More Flexible!
|
||||
* If You've Found This Code Useful, Please Let Me Know.
|
||||
* Visit Our Sites At www.tiptup.com and nehe.gamedev.net
|
||||
*/
|
||||
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
|
@ -26,10 +18,6 @@ import ei.engine.texture.TextureLoader;
|
|||
* Title: Particle Engine Using Triangle Strips
|
||||
* Uses version 0.9alpha of LWJGL http://www.lwjgl.org/
|
||||
*
|
||||
* Be sure that the LWJGL libraries are in your classpath
|
||||
*
|
||||
* Ported directly from the C++ version
|
||||
*
|
||||
*/
|
||||
public class Particles extends Entity{
|
||||
public boolean regenerate = true;
|
||||
|
|
|
|||
239
src/ei/engine/effects/Text.java
Normal file
239
src/ei/engine/effects/Text.java
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue