我正在做一个简单的蛇游戏,因为可以,所以我偶然发现了一个有趣的错误。我正在尝试使用Slick-Util在屏幕上绘制一些文本,但是每当我尝试创建字体对象时,游戏都会冻结。 Here是我的代码。突出显示的部分仅打印一次“ a”。
package tlf.snake.main;
import tlf.snake.main.game.helper.Pos;
import tlf.snake.main.game.input.Input;
import java.awt.Font;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWWindowPosCallback;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GLContext;
import org.newdawn.slick.TrueTypeFont;
import static org.lwjgl.glfw.Callbacks.glfwSetCallback;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
/**
* @author thislooksfun
*/
public class SnakeGame
{
private static final Game game = new Game();
private int WIDTH = game.gameWidth*15;
private int HEIGHT = game.gameHeight*15;
private static final int MINWIDTH = game.gameWidth*10;
private static final int MINHEIGHT = game.gameHeight*10;
private Pos lastPos;
private int size = 10;
private boolean resized = false;
private boolean running = false;
private long window;
private static SnakeGame instance;
public TrueTypeFont font;
public SnakeGame()
{
instance = this;
}
private void init()
{
if (glfwInit() != GL_TRUE) {
System.out.println("Error initializing");
//TODO handle
return;
}
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, "NEAT Snake", NULL, NULL);
if (window == NULL) {
System.out.println("Error creating window");
//TODO handle
return;
}
ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - WIDTH) / 2, (GLFWvidmode.height(vidmode) - HEIGHT) / 2);
initCallbacks();
glfwMakeContextCurrent(window);
glfwShowWindow(window);
GLContext.createFromCurrent();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDepthFunc(GL_NEVER);
glDisable(GL_DEPTH_TEST);
setOrtho();
glDisable(GL_TEXTURE_2D);
initFonts();
running = true;
resized = true;
game.start();
}
private void initCallbacks()
{
glfwSetCallback(window, GLFWWindowSizeCallback(new GLFWWindowSizeCallback.SAM()
{
@Override
public void invoke(long window, int width, int height)
{
resized = true;
WIDTH = width;
HEIGHT = height;
if (width < MINWIDTH && height >= MINHEIGHT) {
glfwSetWindowSize(window, MINWIDTH, height);
glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
} else if (width >= MINWIDTH && height < MINHEIGHT) {
glfwSetWindowSize(window, width, MINHEIGHT);
glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
} else if (width < MINWIDTH && height < MINHEIGHT) {
glfwSetWindowSize(window, MINWIDTH, MINHEIGHT);
glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
} else
lastPos = getWindowPos();
render();
setOrtho();
}
}));
glfwSetCallback(window, GLFWWindowPosCallback(new GLFWWindowPosCallback.SAM()
{
@Override
public void invoke(long window, int xpos, int ypos)
{
lastPos = new Pos(xpos, ypos);
}
}));
glfwSetKeyCallback(window, new Input());
}
private Pos getWindowPos()
{
IntBuffer xbuf = BufferUtils.createIntBuffer(4);
IntBuffer ybuf = BufferUtils.createIntBuffer(4);
glfwGetWindowPos(window, xbuf, ybuf);
return new Pos(xbuf.get(0), ybuf.get(0));
}
private void setOrtho()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
private void initFonts()
{
System.out.println("a");
Font awtFont = new Font("Default", Font.BOLD, 24);
System.out.println("a");
font = new TrueTypeFont(awtFont, true);
System.out.println("a");
}
public void start()
{
init();
while (running)
{
update();
render();
if (glfwWindowShouldClose(window) == GL_TRUE) {
running = false;
}
}
}
private void update()
{
glfwPollEvents();
game.tick();
}
private void render()
{
if (resized) {
setOrtho();
calcTileSize();
resized = false;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
game.render();
glfwSwapBuffers(window);
}
public static SnakeGame instance()
{
return instance;
}
private void calcTileSize()
{
int sh, sw;
sh = (int)Math.floor((1.0f*HEIGHT)/game.gameHeight);
sw = (int)Math.floor((1.0f*WIDTH)/game.gameWidth);
size = sh > sw ? sw : sh;
}
public int tileSize()
{
return size;
}
public void stop()
{
running = false;
}
}
有任何想法吗?
谢谢,
-tlf
最佳答案
您是否尝试过使用其他字体,例如“ Arial”或“ Serif”?我猜“默认”不是Windows中字体的有效名称。
尝试这样的事情:
Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
font = new TrueTypeFont(awtFont, false);
或者,如果您有
.ttf
文件,请尝试如下操作:try {
InputStream inputStream = ResourceLoader.getResourceAsStream("anotherfont.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont2.deriveFont(24f);
font = new TrueTypeFont(awtFont2, false);
} catch (Exception e) {
e.printStackTrace();
}