Java OpenGL(LWJGL+;Slick Util)-文本不';不能正确显示
我正试图通过LWJGL和Slick Util使用OpenGL开发一个游戏(仍然不能完全确定它们之间的关系)。我已经知道如何让TrueTypeFonts正常工作了。问题是,一旦我使用了字体,我的游戏的其他方面(加载栏和地图)现在就根本不显示了。知道问题出在哪里吗?这是我的游戏课Java OpenGL(LWJGL+;Slick Util)-文本不';不能正确显示,java,opengl,lwjgl,slick2d,Java,Opengl,Lwjgl,Slick2d,我正试图通过LWJGL和Slick Util使用OpenGL开发一个游戏(仍然不能完全确定它们之间的关系)。我已经知道如何让TrueTypeFonts正常工作了。问题是,一旦我使用了字体,我的游戏的其他方面(加载栏和地图)现在就根本不显示了。知道问题出在哪里吗?这是我的游戏课 package manager; import java.awt.Font; import java.io.InputStream; import org.lwjgl.LWJGLException; import or
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();
}
}
包管理器;
导入java.awt.Font;
导入java.io.InputStream;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.input.Keyboard;
导入org.lwjgl.input.Mouse;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入org.lwjgl.opengl.GL11;
导入org.newdawn.slick.Color;
导入org.newdawn.slick.TrueTypeFont;
导入org.newdawn.slick.UnicodeFont;
导入org.newdawn.slick.util.ResourceLoader;
公开课游戏{
//场景状态0
公共静态最终int惊人加载_场景=0;
公共静态最终int主菜单\场景=1;
公共静态最终整数游戏场景=2;
int游戏场景;
int加载条宽度;
int宽度、高度;
int mouseXonMap,mouseYonMap;
突出显示字符串平铺;
布尔鼠标映射;
布尔wKeyDown、aKeyDown、sKeyDown、dKeyDown;
游戏地图;
int mapOffsetX,mapOffsetY;
int瓦宽;
TrueTypeFont-commonGameFont;
TrueTypeFont备份字体;
/**
*运行初始化组件
*/
公开作废开始()
{
宽度=640;
高度=480;
initGL(宽度、高度);
init();
游戏场景=0;
游戏地图=新游戏地图(10,10);
mapOffsetX=50;
mapOffsetY=50;
波浪宽度=25;
while(true){
GL11.glClear(GL11.GL\u颜色\u缓冲\u位);
checkForInput();
更新对象();
render();
Display.update();
显示同步(100);
if(Display.isCloseRequested())
{
Display.destroy();
系统出口(0);
}
}
}
/**
*初始化显示屏幕
*@param width-显示器的宽度
*@param height-显示器的高度
*/
公共void initGL(整数宽度、整数高度)
{
试一试{
Display.setDisplayMode(新的显示模式(宽度、高度));
Display.create();
Display.setVSyncEnabled(true);
}
捕获(LWJGLEXE)
{
e、 printStackTrace();
系统出口(0);
}
//从教程开始
GL11.glEnable(GL11.GL_纹理_2D);
GL11.glShadeModel(GL11.GLU平滑);
GL11.glDisable(GL11.GLU深度测试);
GL11.glDisable(GL11.GLU照明);
GL11.glClearColor(0.0f,0.0f,0.0f,0.0f);
GL11.gl清除深度(1);
GL11.glEnable(GL11.GL_BLEND);//这条线很重要,但我不知道为什么。
GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_减去SRC_ALPHA);
GL11.glViewport(0,0,宽度,高度);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GLU投影);
GL11.glLoadIdentity();
GL11.格洛托(0,宽度,高度,0,1,-1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
//教程的结尾部分
}
/**
*初始化资源
*/
公共void init()
{
//初始化资源
试一试{
InputStream InputStream=ResourceLoader.getResourceAsStream(“res/8-Bit-Madness.ttf”);//ResourceLoader.resourceExists()方法表示.ttf存在。
if(ResourceLoader.resourceExists(“res/8-Bit-Madness.ttf”))
{
System.out.println(“文本存在”);
}
其他的
{
System.out.println(“文本加载错误”);
}
Font awtFont=Font.createFont(Font.TRUETYPE\u Font,inputStream);
awtFont=awtFont.deriveFont(36f);//设置字体大小
commonGameFont=新的TrueTypeFont(awtFont,false);
}捕获(例外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\u W){
System.out.println(“按W键”);
wKeyDown=true;
}
if(Keyboard.getEventKey()==Keyboard.KEY_A){
System.out.println(“按下的键”);
aKeyDown=true;
}
if(Keyboard.getEventKey()==Keyboard.KEY\S){
System.out.println(“按下S键”);
sKeyDown=true;
}
if(Keyboard.getEventKey()==Keyboard.KEY\u D){
System.out.println(“按下D键”);
dKeyDown=true;
}
}
其他的
{
if(Keyboard.getEventKey()==Keyboard.KEY\u W){
System.out.println(“W键已释放”);
wKeyDown=false;
}
if(Keyboard.getEventKey()==Keyboard.KEY_A){
System.out.println(“密钥释放”);
aKeyDown=false;
}
if(Keyboard.getEventKey()==Keyboard.KEY\S){
System.out.println(“S键已释放”);
sKeyDown=假;
}
if(Keyboard.getEventKey()==Keyboard.KEY\u D){
System.out.println(“D键已释放”);
dKeyDown=false;
}
}
}
如果(游戏场景==游戏场景)
{
如果(mouseXonScreen>mapOffsetX&&
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);
}