Java LWJGL-文本呈现
我需要在我的2d游戏上渲染2d文本,但没有一个解决方案适合我。我使用了slick和位图渲染。我认为我的问题的一部分是,我找不到一个更新版本的slick有Unicode字体。我的只有TrueType。给我链接一个slick的下载,或者帮助我完成另一种类型的渲染。目前我正在使用这里找到的垃圾类Java LWJGL-文本呈现,java,opengl,text,2d,lwjgl,Java,Opengl,Text,2d,Lwjgl,我需要在我的2d游戏上渲染2d文本,但没有一个解决方案适合我。我使用了slick和位图渲染。我认为我的问题的一部分是,我找不到一个更新版本的slick有Unicode字体。我的只有TrueType。给我链接一个slick的下载,或者帮助我完成另一种类型的渲染。目前我正在使用这里找到的垃圾类 import java.awt.Font; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.opengl.PNGDecoder;
import static org.lwjgl.opengl.GL11.*;
public class Reign {
private static Player player;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
private static Level level = new Level();
public static int levelNum = 1;
private static TrueTypeFont font;
private static int fontTexture;
public static void main(String[] args) throws Exception {
// Set screen size
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
Display.setTitle("Reign - The Platform Game | Pre Alpha 1.0.1 (DEV 3a)");
//Fonts??
//This is where I load the slick font that wont work
Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
font = new TrueTypeFont(awtFont, false);
// Player instance
player = new Player();
init();
// Mainloop
while (!Display.isCloseRequested()) {
//Here I try to render the slik font that wont work
font.drawString(100, 100, "HELLO");
// Call main render method
setCamera();
// Draw the Background
drawBG();
// update and draw the player
player.update();
// Draw Platforms
for (final Platform platform : level.getPlatforms()) {
platform.Draw();
}
//Draw mobs
for (Mob mob : level.getMobs()) {
mob.update();
}
// Draw Goal
level.getGP().Draw();
// Update screen
Display.update();
// 60fps
Display.sync(60);
if (player.win) {
Thread.sleep(1000);
level = new Level();
player.win = false;
player.reset();
levelNum++;
}
}
Display.destroy();
}
public static void init() throws FileNotFoundException, IOException {
fontTexture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, fontTexture);
final PNGDecoder decoder = new PNGDecoder(new FileInputStream(
"res/font.png"));
final ByteBuffer buffer = BufferUtils.createByteBuffer(4
* decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.ABGR);
buffer.flip();
glTexImage2D(GL_TEXTURE, 0, GL_RGB, decoder.getWidth(),
decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
}
// important Opengl shit
public static void setCamera() {
glClearColor(1f, 1f, 1f, 1.0f);
// Clear
glClear(GL_COLOR_BUFFER_BIT);
// Modify projection matrix - 2d projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 0, 720, -1, 1);
// Modify modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
private static void renderString(String string, int textureObject,
int gridSize, float x, float y, float characterWidth,
float characterHeight) {
glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureObject);
// Enable linear texture filtering for smoothed results.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Enable additive blending. This means that the colours will be added
// to already existing colours in the
// frame buffer. In practice, this makes the black parts of the texture
// become invisible.
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
// Store the current model-view matrix.
glPushMatrix();
// Offset all subsequent (at least up until 'glPopMatrix') vertex
// coordinates.
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
// Iterate over all the characters in the string.
for (int i = 0; i < string.length(); i++) {
// Get the ASCII-code of the character by type-casting to integer.
final int asciiCode = string.charAt(i);
// There are 16 cells in a texture, and a texture coordinate ranges
// from 0.0 to 1.0.
final float cellSize = 1.0f / gridSize;
// The cell's x-coordinate is the greatest integer smaller than
// remainder of the ASCII-code divided by the
// amount of cells on the x-axis, times the cell size.
final float cellX = asciiCode % gridSize * cellSize;
// The cell's y-coordinate is the greatest integer smaller than the
// ASCII-code divided by the amount of
// cells on the y-axis.
final float cellY = asciiCode / gridSize * cellSize;
glTexCoord2f(cellX, cellY + cellSize);
glVertex2f(i * characterWidth / 3, y);
glTexCoord2f(cellX + cellSize, cellY + cellSize);
glVertex2f(i * characterWidth / 3 + characterWidth / 2, y);
glTexCoord2f(cellX + cellSize, cellY);
glVertex2f(i * characterWidth / 3 + characterWidth / 2, y
+ characterHeight);
glTexCoord2f(cellX, cellY);
glVertex2f(i * characterWidth / 3, y + characterHeight);
}
glEnd();
glPopMatrix();
glPopAttrib();
}
public static void drawBG() {
glClear(GL_COLOR_BUFFER_BIT);
renderString("hello", fontTexture, 16, -0.9f, 0, 0.3f, 0.225f);
// static sky
{
glBegin(GL_QUADS);
glColor3d(0.3, 0.4, 0.6);
glVertex2d(0, 0);
glVertex2d(1280, 0);
glColor3d(0.1, 0.2, 0.5);
glVertex2d(1280, 720);
glVertex2d(0, 720);
glEnd();
}
// static ground
{
// mud
{
glBegin(GL_QUADS);
glColor3d(0.6, 0.2, 0.1);
glVertex2d(0, 0);
glVertex2d(1280, 0);
glVertex2d(1280, 32);
glVertex2d(0, 32);
glEnd();
}
// grass
{
glBegin(GL_QUADS);
glColor3d(0.2, 0.75, 0.2);
glVertex2d(0, 32);
glVertex2d(1280, 32);
glVertex2d(1280, 37);
glVertex2d(0, 37);
glEnd();
}
//GUI TOP
{
/*
* For a quad the coords are:
* vertex 1 = 0, 0
* vertex 2 = width, 0
* vertex 3 = width, height
* vertex 4 = 0, height
*/
glBegin(GL_QUADS);
glColor3d(0, 0, 0);
glVertex2d(0, 720);
glVertex2d(1280, 720);
glVertex2d(1280, 640);
glVertex2d(0, 640);
glEnd();
}
//RENDER HINTS
if (levelNum == 1) {
drawString("Watch out for the spikes, ", 200, 700);
drawString("they will kill you if you get too close!", 200, 685);
}
if (levelNum == 2) {
drawString("Those red mobs will kill you just like spikes", 200, 700);
drawString(" but you can win points if you jump on their heads!", 200, 685);
}
if (levelNum == 3) {
drawString("The pink platforms will dissappear", 200, 700);
drawString("after a while so watch out! ", 200, 685);
}
if (levelNum == 5) {
drawString("The blue platforms are TRAMPOLINES!!", 200, 700);
}
}
}
public static ArrayList<Platform> getPlatforms() {
return level.getPlatforms();
}
public static ArrayList<Mob> getMobs() {
return level.getMobs();
}
public static void drawString(String s, int x, int y) {
int startX = x;
GL11.glBegin(GL11.GL_POINTS);
glColor3d(0, 1, 0);
for (char c : s.toLowerCase().toCharArray()) {
if (c == 'a') {
for (int i = 0; i < 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == 'b') {
for (int i = 0; i < 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
GL11.glVertex2f(x + i, y + 4);
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 7, y + 5);
GL11.glVertex2f(x + 7, y + 7);
GL11.glVertex2f(x + 7, y + 6);
GL11.glVertex2f(x + 7, y + 1);
GL11.glVertex2f(x + 7, y + 2);
GL11.glVertex2f(x + 7, y + 3);
x += 8;
} else if (c == 'c') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y);
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 6, y + 2);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 6, y + 7);
x += 8;
} else if (c == 'd') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y);
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 6, y + 2);
GL11.glVertex2f(x + 6, y + 3);
GL11.glVertex2f(x + 6, y + 4);
GL11.glVertex2f(x + 6, y + 5);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 6, y + 7);
x += 8;
} else if (c == 'e') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 0);
GL11.glVertex2f(x + i, y + 8);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == 'f') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == 'g') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y);
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 6, y + 2);
GL11.glVertex2f(x + 6, y + 3);
GL11.glVertex2f(x + 5, y + 3);
GL11.glVertex2f(x + 7, y + 3);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 6, y + 7);
x += 8;
} else if (c == 'h') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == 'i') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 3, y + i);
}
for (int i = 1; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 0);
GL11.glVertex2f(x + i, y + 8);
}
x += 7;
} else if (c == 'j') {
for (int i = 1; i <= 8; i++) {
GL11.glVertex2f(x + 6, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 0);
}
GL11.glVertex2f(x + 1, y + 3);
GL11.glVertex2f(x + 1, y + 2);
GL11.glVertex2f(x + 1, y + 1);
x += 8;
} else if (c == 'k') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
GL11.glVertex2f(x + 6, y + 8);
GL11.glVertex2f(x + 5, y + 7);
GL11.glVertex2f(x + 4, y + 6);
GL11.glVertex2f(x + 3, y + 5);
GL11.glVertex2f(x + 2, y + 4);
GL11.glVertex2f(x + 2, y + 3);
GL11.glVertex2f(x + 3, y + 4);
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 5, y + 2);
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 7, y);
x += 8;
} else if (c == 'l') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
}
x += 7;
} else if (c == 'm') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
GL11.glVertex2f(x + 3, y + 6);
GL11.glVertex2f(x + 2, y + 7);
GL11.glVertex2f(x + 4, y + 5);
GL11.glVertex2f(x + 5, y + 6);
GL11.glVertex2f(x + 6, y + 7);
GL11.glVertex2f(x + 4, y + 5);
x += 8;
} else if (c == 'n') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
GL11.glVertex2f(x + 2, y + 7);
GL11.glVertex2f(x + 2, y + 6);
GL11.glVertex2f(x + 3, y + 5);
GL11.glVertex2f(x + 4, y + 4);
GL11.glVertex2f(x + 5, y + 3);
GL11.glVertex2f(x + 6, y + 2);
GL11.glVertex2f(x + 6, y + 1);
x += 8;
} else if (c == 'o' || c == '0') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 0);
}
x += 8;
} else if (c == 'p') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 4);
}
GL11.glVertex2f(x + 6, y + 7);
GL11.glVertex2f(x + 6, y + 5);
GL11.glVertex2f(x + 6, y + 6);
x += 8;
} else if (c == 'q') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
if (i != 1)
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
if (i != 6)
GL11.glVertex2f(x + i, y + 0);
}
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 5, y + 2);
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 7, y);
x += 8;
} else if (c == 'r') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 4);
}
GL11.glVertex2f(x + 6, y + 7);
GL11.glVertex2f(x + 6, y + 5);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 5, y + 2);
GL11.glVertex2f(x + 6, y + 1);
GL11.glVertex2f(x + 7, y);
x += 8;
} else if (c == 's') {
for (int i = 2; i <= 7; i++) {
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 1, y + 7);
GL11.glVertex2f(x + 1, y + 6);
GL11.glVertex2f(x + 1, y + 5);
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 4);
GL11.glVertex2f(x + i, y);
}
GL11.glVertex2f(x + 7, y + 3);
GL11.glVertex2f(x + 7, y + 2);
GL11.glVertex2f(x + 7, y + 1);
GL11.glVertex2f(x + 1, y + 1);
GL11.glVertex2f(x + 1, y + 2);
x += 8;
} else if (c == 't') {
for (int i = 0; i <= 8; i++) {
GL11.glVertex2f(x + 4, y + i);
}
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + i, y + 8);
}
x += 7;
} else if (c == 'u') {
for (int i = 1; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 0);
}
x += 8;
} else if (c == 'v') {
for (int i = 2; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 6, y + i);
}
GL11.glVertex2f(x + 2, y + 1);
GL11.glVertex2f(x + 5, y + 1);
GL11.glVertex2f(x + 3, y);
GL11.glVertex2f(x + 4, y);
x += 7;
} else if (c == 'w') {
for (int i = 1; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
GL11.glVertex2f(x + 2, y);
GL11.glVertex2f(x + 3, y);
GL11.glVertex2f(x + 5, y);
GL11.glVertex2f(x + 6, y);
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + 4, y + i);
}
x += 8;
} else if (c == 'x') {
for (int i = 1; i <= 7; i++)
GL11.glVertex2f(x + i, y + i);
for (int i = 7; i >= 1; i--)
GL11.glVertex2f(x + i, y + 8 - i);
x += 8;
} else if (c == 'y') {
GL11.glVertex2f(x + 4, y);
GL11.glVertex2f(x + 4, y + 1);
GL11.glVertex2f(x + 4, y + 2);
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 4, y + 4);
GL11.glVertex2f(x + 3, y + 5);
GL11.glVertex2f(x + 2, y + 6);
GL11.glVertex2f(x + 1, y + 7);
GL11.glVertex2f(x + 1, y + 8);
GL11.glVertex2f(x + 5, y + 5);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 7, y + 7);
GL11.glVertex2f(x + 7, y + 8);
x += 8;
} else if (c == 'z') {
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + i);
}
GL11.glVertex2f(x + 6, y + 7);
x += 8;
} else if (c == '1') {
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
}
for (int i = 1; i <= 8; i++) {
GL11.glVertex2f(x + 4, y + i);
}
GL11.glVertex2f(x + 3, y + 7);
x += 8;
} else if (c == '2') {
for (int i = 1; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 1, y + 7);
GL11.glVertex2f(x + 1, y + 6);
GL11.glVertex2f(x + 6, y + 7);
GL11.glVertex2f(x + 6, y + 6);
GL11.glVertex2f(x + 6, y + 5);
GL11.glVertex2f(x + 5, y + 4);
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 3, y + 2);
GL11.glVertex2f(x + 2, y + 1);
x += 8;
} else if (c == '3') {
for (int i = 1; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y);
}
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 6, y + i);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == '4') {
for (int i = 2; i <= 8; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 7; i++) {
GL11.glVertex2f(x + i, y + 1);
}
for (int i = 0; i <= 4; i++) {
GL11.glVertex2f(x + 4, y + i);
}
x += 8;
} else if (c == '5') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + i, y + 8);
}
for (int i = 4; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
}
GL11.glVertex2f(x + 1, y + 1);
GL11.glVertex2f(x + 2, y);
GL11.glVertex2f(x + 3, y);
GL11.glVertex2f(x + 4, y);
GL11.glVertex2f(x + 5, y);
GL11.glVertex2f(x + 6, y);
GL11.glVertex2f(x + 7, y + 1);
GL11.glVertex2f(x + 7, y + 2);
GL11.glVertex2f(x + 7, y + 3);
GL11.glVertex2f(x + 6, y + 4);
GL11.glVertex2f(x + 5, y + 4);
GL11.glVertex2f(x + 4, y + 4);
GL11.glVertex2f(x + 3, y + 4);
GL11.glVertex2f(x + 2, y + 4);
x += 8;
} else if (c == '6') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y);
}
for (int i = 2; i <= 5; i++) {
GL11.glVertex2f(x + i, y + 4);
GL11.glVertex2f(x + i, y + 8);
}
GL11.glVertex2f(x + 7, y + 1);
GL11.glVertex2f(x + 7, y + 2);
GL11.glVertex2f(x + 7, y + 3);
GL11.glVertex2f(x + 6, y + 4);
x += 8;
} else if (c == '7') {
for (int i = 0; i <= 7; i++)
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + 7, y + 7);
GL11.glVertex2f(x + 7, y + 6);
GL11.glVertex2f(x + 6, y + 5);
GL11.glVertex2f(x + 5, y + 4);
GL11.glVertex2f(x + 4, y + 3);
GL11.glVertex2f(x + 3, y + 2);
GL11.glVertex2f(x + 2, y + 1);
GL11.glVertex2f(x + 1, y);
x += 8;
} else if (c == '8') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 0);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 4);
}
x += 8;
} else if (c == '9') {
for (int i = 1; i <= 7; i++) {
GL11.glVertex2f(x + 7, y + i);
}
for (int i = 5; i <= 7; i++) {
GL11.glVertex2f(x + 1, y + i);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 8);
GL11.glVertex2f(x + i, y + 0);
}
for (int i = 2; i <= 6; i++) {
GL11.glVertex2f(x + i, y + 4);
}
GL11.glVertex2f(x + 1, y + 0);
x += 8;
} else if (c == '.') {
GL11.glVertex2f(x + 1, y);
x += 2;
} else if (c == ',') {
GL11.glVertex2f(x + 1, y);
GL11.glVertex2f(x + 1, y + 1);
x += 2;
} else if (c == '\n') {
y -= 10;
x = startX;
} else if (c == ' ') {
x += 8;
}
}
GL11.glEnd();
}
}
编辑:当我在lwjgl wiki上运行演示程序时,它会正确地呈现字体(见此处)
导入java.awt.Font;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.nio.ByteBuffer;
导入java.util.ArrayList;
导入org.lwjgl.BufferUtils;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入org.lwjgl.opengl.GL11;
导入org.newdawn.slick.TrueTypeFont;
导入org.newdawn.slick.opengl.PNGDecoder;
导入静态org.lwjgl.opengl.GL11.*;
公共阶级统治{
私人静态播放器;
公共静态最终整数宽度=1280;
公共静态最终内部高度=720;
私有静态级别=新级别();
公共静态int-levelNum=1;
私有静态TrueTypeFont字体;
私有静态纹理;
公共静态void main(字符串[]args)引发异常{
//设置屏幕大小
Display.setDisplayMode(新的显示模式(宽度、高度));
Display.create();
Display.setTitle(“统治-平台游戏| Alpha 1.0.1之前(dev3a)”);
//字体??
//这就是我加载不起作用的光滑字体的地方
Font awtFont=新字体(“Times new Roman”,Font.BOLD,24);
font=新的TrueTypeFont(awtFont,false);
//玩家实例
player=新玩家();
init();
//主回路
而(!Display.isCloseRequested()){
//在这里,我尝试渲染无法工作的slik字体
字体.抽绳(100100,“你好”);
//调用主渲染方法
设置摄像机();
//画背景
drawBG();
//更新并绘制播放器
player.update();
//画平台
for(最终平台:level.getPlatforms()){
platform.Draw();
}
//招兵买马
for(Mob Mob:level.getMobs()){
mob.update();
}
//平局
level.getGP().Draw();
//更新屏幕
Display.update();
//每秒60帧
显示同步(60);
如果(玩家赢){
睡眠(1000);
级别=新级别();
player.win=false;
player.reset();
levelNum++;
}
}
Display.destroy();
}
public static void init()引发FileNotFoundException,IOException{
fontTexture=glGenTextures();
glBindTexture(GL_TEXTURE_2D,fontTexture);
最终PNGDecoder解码器=新PNGDecoder(新文件输入流(
“res/font.png”);
final ByteBuffer buffer=BufferUtils.createByteBuffer(4
*decoder.getWidth()*decoder.getHeight());
decoder.decode(buffer,decoder.getWidth()*4,PNGDecoder.ABGR);
flip();
glTexImage2D(GL_纹理,0,GL_RGB,解码器.getWidth(),
解码器.getHeight(),0,GL_RGBA,GL_无符号字节,缓冲区);
glBindTexture(GL_TEXTURE_2D,0);
}
//重要的Opengl
公共静态照相机(){
glClearColor(1f、1f、1f、1.0f);
//清楚的
glClear(GLU颜色缓冲位);
//修改投影矩阵-二维投影
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(01280,0720,-1,1);
//修改模型视图矩阵
glMatrixMode(GLU模型视图);
glLoadIdentity();
}
私有静态void renderString(字符串字符串、int-textureObject、,
int gridSize、float x、float y、float characterWidth、,
浮点字符高度){
glPushAttrib(GL_纹理|u位| GL_启用|u位| GL_颜色_缓冲|位);
glEnable(GL_CULL_面);
glEnable(GL_纹理_2D);
glBindTexture(GL_TEXTURE_2D,textureObject);
//为平滑结果启用线性纹理过滤。
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
//启用添加剂混合。这意味着将添加颜色
//对已存在的颜色
//帧缓冲区。实际上,这会使纹理的黑色部分
//变得看不见。
glEnable(GL_混合物);
glBlendFunc(GL_ONE,GL_ONE);
//存储当前模型视图矩阵。
glPushMatrix();
//偏移所有后续(至少直到“GLPOPMARX”)顶点
//坐标。
glTranslatef(x,y,0);
glBegin(GL_QUADS);
//迭代字符串中的所有字符。
对于(int i=0;i