Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.quew8.testapp;
- import java.awt.Graphics2D;
- import java.awt.color.ColorSpace;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.ComponentColorModel;
- import java.awt.image.DataBuffer;
- import java.awt.image.DataBufferByte;
- import java.awt.image.Raster;
- import java.awt.image.WritableRaster;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.ByteBuffer;
- import javax.imageio.ImageIO;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import static org.lwjgl.opengl.GL11.*;
- import org.lwjgl.util.glu.GLU;
- /**
- *
- * @author Quew8
- */
- public class TestApplication {
- private static String[] text;
- private static int charSheetTextureId;
- private static final ColorModel glColourModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
- new int[] {8,8,8,8},
- true,
- false,
- ComponentColorModel.TRANSLUCENT,
- DataBuffer.TYPE_BYTE);
- public static void init() throws LWJGLException, IOException {
- Display.setDisplayMode(new DisplayMode(800, 600));
- Display.setTitle("Test App");
- Display.create();
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_TEXTURE_2D);
- glClearColor(1, 0, 1, 0);
- charSheetTextureId = loadTexture(TestApplication.class.getClassLoader().getResourceAsStream("resources/normal.png"));
- }
- public static void loop() {
- int error;
- while(!Display.isCloseRequested()) {
- glViewport(0, 0, 800, 600);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 200, 0, 200, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- for(int i = 0; i < text.length; i++) {
- drawLineOfText(text[i], 5, 180 - (i * 15), 9, 10, 1);
- }
- if( (error = glGetError()) != GL_NO_ERROR) {
- throw new RuntimeException("Something Bad Has Happened: " + GLU.gluGetString(error));
- }
- Display.update();
- Display.sync(60);
- }
- }
- public static void drawLineOfText(String line, float xPos, float yPos, float charWidth, float charHeight, float xGap) {
- glBindTexture(GL_TEXTURE_2D, charSheetTextureId);
- glBegin(GL_QUADS);
- glColor3f(1, 1, 1);
- for(int i = 0; i < line.length(); i++) {
- int code = (int) line.charAt(i);
- int gridX = code % 16;
- int gridY = ( code - gridX ) / 16;
- float texX = (float) gridX / 16;
- float texY = (float) gridY / 16;
- glTexCoord2f(texX, texY);
- glVertex2f( ( ( xGap + charWidth ) * i ) + xPos, yPos);
- glTexCoord2f(texX, texY + (1f / 16f) );
- glVertex2f( ( ( xGap + charWidth ) * i ) + xPos, yPos - charHeight);
- glTexCoord2f(texX + (1f / 16f), texY + (1f / 16f));
- glVertex2f( ( ( xGap + charWidth ) * i ) + xPos + charWidth, yPos - charHeight);
- glTexCoord2f(texX + (1f / 16f), texY);
- glVertex2f( ( ( xGap + charWidth ) * i ) + xPos + charWidth, yPos);
- }
- glEnd();
- }
- public static int loadTexture(InputStream is) throws IOException {
- int texId = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, texId);
- BufferedImage imgIn = ImageIO.read(is);
- int texWidth = imgIn.getWidth(); //Assumes POT textures.
- int texHeight = imgIn.getHeight(); //Assumes POT textures.
- WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
- BufferedImage texImage = new BufferedImage(glColourModel, raster, false, null);
- Graphics2D g = texImage.createGraphics();
- g.setColor(new java.awt.Color(0f, 0f, 0f, 0f));
- g.fillRect(0, 0, texWidth, texHeight);
- g.drawImage(imgIn, 0, 0, null);
- byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
- ByteBuffer bb = BufferUtils.createByteBuffer(data.length);
- bb.put(data);
- bb.flip();
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D,
- 0,
- GL_RGBA,
- texWidth,
- texHeight,
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- bb);
- glBindTexture(GL_TEXTURE_2D, 0);
- return texId;
- }
- public static void deinit() {
- glDeleteTextures(charSheetTextureId);
- Display.destroy();
- }
- public static void main(String[] args) throws LWJGLException, IOException {
- text =
- args.length > 0 ?
- args :
- new String[]{
- "Some Default Text",
- "Some More Text",
- "Something about",
- "A Lazy Fox. I think",
- "it jumped about or",
- "not I don't know."
- };
- TestApplication.init();
- TestApplication.loop();
- TestApplication.deinit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement