我试图通过LWJGL和Slick-Util使用OpenGL开发游戏(仍然不完全确定它们彼此之间的关系)。我已经弄清楚如何让TrueTypeFonts工作。问题是,只要我的字体工作,我的游戏的其他方面(加载栏和地图)现在根本不显示。知道问题是什么吗?这是我游戏的课程。
package manager;
import java.awt.Font;
import java.io.InputStream;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.util.ResourceLoader;
public class Game {
//Scene States0
public static final int STARTLOADING_SCENE = 0;
public static final int MAINMENU_SCENE = 1;
public static final int GAME_SCENE = 2;
int gameScene;
int loadingBarWidth;
int width, height;
int mouseXonMap,mouseYonMap;
String tileTypeHighlighted;
boolean mouseOnMap;
boolean wKeyDown, aKeyDown, sKeyDown, dKeyDown;
GameMap gameMap;
int mapOffsetX, mapOffsetY;
int tileWidth;
TrueTypeFont commonGameFont;
TrueTypeFont backupFont;
/**
* Runs initialization components
*/
public void start()
{
width = 640;
height = 480;
initGL(width,height);
init();
gameScene = 0;
gameMap = new GameMap(10,10);
mapOffsetX = 50;
mapOffsetY = 50;
tileWidth = 25;
while(true) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
checkForInput();
updateObjects();
render();
Display.update();
Display.sync(100);
if(Display.isCloseRequested())
{
Display.destroy();
System.exit(0);
}
}
}
/**
* Initializes the display screen
* @param width - Width of the display
* @param height - Height of the display
*/
public void initGL(int width, int height)
{
try{
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
}
catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
//Begin stuff from tutorial
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);
GL11.glEnable(GL11.GL_BLEND); //This line is important, but I don't know why.
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
//End Stuff from tutorial
}
/**
* Initializes resources
*/
public void init()
{
//Initialize Resources
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("res/8-Bit-Madness.ttf"); //The ResourceLoader.resourceExists() method says this .ttf exists.
if(ResourceLoader.resourceExists("res/8-Bit-Madness.ttf"))
{
System.out.println("Text Exists");
}
else
{
System.out.println("Text load error");
}
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(36f); // set font size
commonGameFont = new TrueTypeFont(awtFont,false);
} catch (Exception e) {
e.printStackTrace();
}
}
public void checkForInput()
{
int mouseXonScreen = Mouse.getX();
int mouseYonScreen = Mouse.getY();
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_W) {
System.out.println("W Key Pressed");
wKeyDown = true;
}
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Pressed");
aKeyDown = true;
}
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
System.out.println("S Key Pressed");
sKeyDown = true;
}
if (Keyboard.getEventKey() == Keyboard.KEY_D) {
System.out.println("D Key Pressed");
dKeyDown = true;
}
}
else
{
if (Keyboard.getEventKey() == Keyboard.KEY_W) {
System.out.println("W Key Released");
wKeyDown = false;
}
if (Keyboard.getEventKey() == Keyboard.KEY_A) {
System.out.println("A Key Released");
aKeyDown = false;
}
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
System.out.println("S Key Released");
sKeyDown = false;
}
if (Keyboard.getEventKey() == Keyboard.KEY_D) {
System.out.println("D Key Released");
dKeyDown = false;
}
}
}
if(gameScene == GAME_SCENE)
{
if(mouseXonScreen>mapOffsetX && mouseXonScreen<mapOffsetX+(tileWidth*gameMap.getWidth()))
{
if(mouseYonScreen>mapOffsetY && mouseYonScreen<mapOffsetY+(tileWidth*gameMap.getHeight()))
{
mouseXonMap = (mouseXonScreen-mapOffsetX)/tileWidth;
mouseYonMap = (mouseYonScreen-mapOffsetY)/tileWidth;
tileTypeHighlighted = gameMap.getTileAt(mouseXonMap, mouseYonMap).getTileType();
mouseOnMap = true;
}
else
{
mouseOnMap = false;
}
}
else
{
mouseOnMap = false;
}
}
}
public void updateObjects()
{
if (gameScene == 0)
{
if (loadingBarWidth <= (width/2))
{
loadingBarWidth++;
}
else
{
gameScene = 2;
}
}
}
public void render()
{
if (gameScene == 0)
{
//These quads load properly, unless the blend line is in the program.
GL11.glColor3f(0.5f,0.5f,0.5f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f((width/4),(height/4));
GL11.glVertex2f((width/4),(height/4)+25);
GL11.glVertex2f((width/4)*3,(height/4)+25);
GL11.glVertex2f((width/4)*3,(height/4));
GL11.glEnd();
GL11.glColor3d(255d,255d,0d);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f((width/4),(height/4));
GL11.glVertex2f((width/4),(height/4)+25);
GL11.glVertex2f((width/4)+loadingBarWidth,(height/4)+25);
GL11.glVertex2f((width/4)+loadingBarWidth,(height/4));
GL11.glEnd();
}
else if (gameScene == 2)
{
for(int x = 0; x<gameMap.getWidth(); x++)
{
for(int y = 0; y<gameMap.getHeight();y++)
{
//These quads load correctly, unless that blend line above is active.
GL11.glColor3d(gameMap.getTileAt(x,y).getRColor(), gameMap.getTileAt(x,y).getGColor(), gameMap.getTileAt(x,y).getBColor());
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(mapOffsetX+(x*tileWidth), mapOffsetY+(y*tileWidth)+tileWidth);
GL11.glVertex2f(mapOffsetX+(x*tileWidth), mapOffsetY+(y*tileWidth));
GL11.glVertex2f(mapOffsetX+(x*tileWidth)+tileWidth, mapOffsetY+(y*tileWidth));
GL11.glVertex2f(mapOffsetX+(x*tileWidth)+tileWidth, mapOffsetY+(y*tileWidth)+tileWidth);
GL11.glEnd();
}
}
if(mouseOnMap)
{
commonGameFont.drawString(10,45, tileTypeHighlighted+ " Tile at X: "+ mouseXonMap +" Y: "+ mouseYonMap, Color.darkGray);
commonGameFont.drawString(10,20, "This is a long space to test sizing ... ... ... ... ... ... ...",Color.white);
}
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
答案 0 :(得分:2)
不在glEnable(GL_BLEND)
中调用initGL
,但在绘制文本时调用glEnable(GL_BLEND)
应该可以使其正常工作。使用代码的示例:
if(mouseOnMap)
{
glEnable(GL_BLEND);
commonGameFont.drawString(10,20, "This is a long space to test sizing ... ... ... ... ... ... ...",Color.white);
commonGameFont.drawString(10,45, tileTypeHighlighted+ " Tile at X: "+ mouseXonMap +" Y: "+ mouseYonMap, Color.darkGray);
glDisable(GL_BLEND);
}