Wil*_*ind 2 java malloc lwjgl glfw
我是第一次学习 LWJGL,并探索LWJGL3 站点上作为示例给出的简单代码块。我之前从未使用过 OpenGL 或 GLFW。
我不明白这一小块代码。如果我删除它,主要代码仍然有效。这整件事只是为了在创建时将窗口居中吗?
mallocInt (1)应该是什么意思?奇怪的方法调用的整个想法stackPush()已经被混淆了。我使用过 SWT 和 awt,但从未见过类似的东西。
// Get the thread stack and push a new frame
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center the window
glfwSetWindowPos(window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2);
}
// the stack frame is popped automatically
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢。
LWJGL 允许堆栈上的内存分配速度比纯 Java 通过类允许的速度快得多MemoryStack。为了获取堆栈,您可以调用stackPush(). 通过将其放入try子句中,就像您所做的那样,它使得堆栈分配是线程本地的,并且一旦语句try完成,就会弹出堆栈并释放缓冲区。
MemoryStack.mallocXX(count)(其中 xx 是类型,count 是缓冲区的大小)是从堆栈进行分配的方式。 stack.mallocInt(1)从堆栈而不是堆返回大小为 1 的整数缓冲区。
一般来说,当您做制服或任何需要缓冲区的事情时,请使用MemoryStack.stackPush(). LWJGL 有一篇非常好的文章(此处)介绍了 LWJGL3 中内存管理的不同方法,我绝对建议您花时间坐下来学习这些新的内存技术。