我的 LWJGL 程序存在严重问题。如果我设置我的文本颜色之一,背景颜色也会改变。
我想我知道为什么,我只是把所有绘图的东西都放在一个无限循环中,也许这就是原因。
你能帮我解决这个问题吗?我对问题的原因是否正确?如果是,我该如何解决?如果不是,你怎么看,有什么问题?这是我的代码:
主类:
<!-- language: lang-java -->
public class DoomMain {
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
public static final int SCALE = 1;
private static State state;
public DoomMain() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH*SCALE,HEIGHT*SCALE));
Display.setTitle("Tomco - Doom");
Display.create();
} catch(LWJGLException e) {
e.printStackTrace();
}
while(!Display.isCloseRequested()) {
state = State.MENU;
switch(state) {
case INTRO:
new Intro();
break;
case MENU:
new MainMenu();
break;
case CREDITS:
break;
case GAME:
break;
default:
break;
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String[] args) {
new DoomMain();
}
}
MainMenu 类:
<!-- language: lang-java -->
public class MainMenu {
private Texture background;
private TrueTypeFont doomfont;
private boolean ana = false;
public MainMenu() {
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL11.GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,DoomMain.WIDTH,DoomMain.HEIGHT);
glMatrixMode(GL11.GL_MODELVIEW);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(0,DoomMain.WIDTH,DoomMain.HEIGHT,0,1,-1);
glMatrixMode(GL11.GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
drawBackground();
drawText();
glDisable(GL_TEXTURE_2D);
}
private void drawBackground() {
try {
background = TextureLoader.getTexture("PNG",new FileInputStream(new File("res/textures/mainmenu/doom_00340461.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
}
glBegin(GL_QUADS);
{
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(background.getTextureWidth(),0);
glTexCoord2f(1,1);
glVertex2f(background.getTextureWidth(),background.getTextureHeight());
glTexCoord2f(0,1);
glVertex2f(0,background.getTextureHeight());
}
glEnd();
}
private void drawText() {
Color.white.bind();
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("res/fonts/DooM.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT,inputStream);
awtFont = awtFont.deriveFont(24f);
doomfont = new TrueTypeFont(awtFont,ana);
} catch(Exception e) {
e.printStackTrace();
}
doomfont.drawString(200,100,"Start new game",Color.red);
}
}