流程

  1. 创建或打开项目
    打开 IntelliJ IDEA 并创建一个新的 Gradle 项目,或者打开现有的 Gradle 项目。
  2. 配置 build.gradle 文件
    在项目的 build.gradle 文件中添加 LWJGL 的依赖项。以下是一个示例配置(涵盖了大多库):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
plugins {
id 'java'
}

project.ext.lwjglVersion = "3.3.4"
project.ext.joml_primitivesVersion = "1.10.0"
project.ext.jomlVersion = "1.10.7"
project.ext.lwjgl3_awtVersion = "0.1.8"
project.ext.steamworks4j_serverVersion = "1.9.0"
project.ext.steamworks4jVersion = "1.9.0"
project.ext.lwjglNatives = "natives-windows"

group = 'cn.codnot.trails'
version = '1.0.0'

repositories {
mavenCentral()
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
// lwjgl
implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")
implementation "org.lwjgl:lwjgl"
implementation "org.lwjgl:lwjgl-assimp"
implementation "org.lwjgl:lwjgl-bgfx"
implementation "org.lwjgl:lwjgl-cuda"
implementation "org.lwjgl:lwjgl-egl"
implementation "org.lwjgl:lwjgl-fmod"
implementation "org.lwjgl:lwjgl-freetype"
implementation "org.lwjgl:lwjgl-glfw"
implementation "org.lwjgl:lwjgl-harfbuzz"
implementation "org.lwjgl:lwjgl-hwloc"
implementation "org.lwjgl:lwjgl-jawt"
implementation "org.lwjgl:lwjgl-jemalloc"
implementation "org.lwjgl:lwjgl-ktx"
implementation "org.lwjgl:lwjgl-libdivide"
implementation "org.lwjgl:lwjgl-llvm"
implementation "org.lwjgl:lwjgl-lmdb"
implementation "org.lwjgl:lwjgl-lz4"
implementation "org.lwjgl:lwjgl-meow"
implementation "org.lwjgl:lwjgl-meshoptimizer"
implementation "org.lwjgl:lwjgl-msdfgen"
implementation "org.lwjgl:lwjgl-nanovg"
implementation "org.lwjgl:lwjgl-nfd"
implementation "org.lwjgl:lwjgl-nuklear"
implementation "org.lwjgl:lwjgl-odbc"
implementation "org.lwjgl:lwjgl-openal"
implementation "org.lwjgl:lwjgl-opencl"
implementation "org.lwjgl:lwjgl-opengl"
implementation "org.lwjgl:lwjgl-opengles"
implementation "org.lwjgl:lwjgl-openvr"
implementation "org.lwjgl:lwjgl-openxr"
implementation "org.lwjgl:lwjgl-opus"
implementation "org.lwjgl:lwjgl-ovr"
implementation "org.lwjgl:lwjgl-par"
implementation "org.lwjgl:lwjgl-remotery"
implementation "org.lwjgl:lwjgl-rpmalloc"
implementation "org.lwjgl:lwjgl-shaderc"
implementation "org.lwjgl:lwjgl-spvc"
implementation "org.lwjgl:lwjgl-sse"
implementation "org.lwjgl:lwjgl-stb"
implementation "org.lwjgl:lwjgl-tinyexr"
implementation "org.lwjgl:lwjgl-tinyfd"
implementation "org.lwjgl:lwjgl-tootle"
implementation "org.lwjgl:lwjgl-vma"
implementation "org.lwjgl:lwjgl-vulkan"
implementation "org.lwjgl:lwjgl-xxhash"
implementation "org.lwjgl:lwjgl-yoga"
implementation "org.lwjgl:lwjgl-zstd"
runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-assimp::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-bgfx::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-freetype::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-harfbuzz::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-hwloc::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-jemalloc::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-ktx::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-libdivide::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-llvm::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-lmdb::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-lz4::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-meow::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-meshoptimizer::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-msdfgen::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-nanovg::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-nfd::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-nuklear::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-opengl::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-opengles::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-openvr::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-openxr::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-opus::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-ovr::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-par::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-remotery::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-rpmalloc::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-shaderc::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-spvc::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-sse::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-stb::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-tinyexr::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-tinyfd::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-tootle::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-vma::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-xxhash::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-yoga::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-zstd::$lwjglNatives"
implementation "org.joml:joml-primitives:${joml_primitivesVersion}"
implementation "org.joml:joml:${jomlVersion}"
implementation "org.lwjglx:lwjgl3-awt:${lwjgl3_awtVersion}"
implementation "com.code-disaster.steamworks4j:steamworks4j-server:${steamworks4j_serverVersion}"
implementation "com.code-disaster.steamworks4j:steamworks4j:${steamworks4jVersion}"
}

test {
useJUnitPlatform()
}
  1. 同步 Gradle 项目
    在 IntelliJ IDEA 中,点击右上角的 “Sync Now” 按钮,同步 Gradle 项目以下载所有依赖项。
  2. 编写测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;
import java.util.Objects;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {
private long window;
public void run() {
System.out.println("Hello LWJGL " + Version.getVersion() + "!");

init();
loop();

//释放窗口回调并销毁窗口
glfwFreeCallbacks(window);
glfwDestroyWindow(window);

// 终止 GLFW 并释放错误回调
glfwTerminate();
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}
private void init() {
// 设置错误回调。默认实现
// 将在 System.err 中打印错误消息。
GLFWErrorCallback.createPrint(System.err).set();

// 初始化 GLFW。在执行此操作之前,大多数 GLFW 函数将不起作用。
if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");

// Configure GLFW
glfwDefaultWindowHints(); //optional,则当前窗口提示已经是默认的
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); // t该窗口在创建后将保持隐藏状态
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // 窗口将可调整大小

// 创建窗口
window = glfwCreateWindow(800, 600, "Demo!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");

// 设置按键回调。每次按下、重复或释放一个键时,都会调用它。
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, true); // 我们将在渲染循环中检测这一点
});

// 获取线程堆栈并推送新帧
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*

//获取传递给 glfwCreateWindow 的窗口大小
glfwGetWindowSize(window, pWidth, pHeight);

// 获取主监视器的分辨率
GLFWVidMode glfwVidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());

// Center the window
if (glfwVidMode != null) {
glfwSetWindowPos(
window,
(glfwVidMode.width() - pWidth.get(0)) / 2,
(glfwVidMode.height() - pHeight.get(0)) / 2
);
}
} // 堆栈帧会自动弹出

// 将 OpenGL 上下文设为当前上下文
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);

// 展示窗口
glfwShowWindow(window);
}
private void loop() {
// 这条线路对于 LWJGL 与 GLFW 的
// OpenGL 上下文,或外部管理的任何上下文。
// LWJGL 检测当前线程中的当前上下文,
// 创建 GLCapabilities 实例并将 OpenGL
// 可供使用的绑定。
GL.createCapabilities();

// 设置清除颜色
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

// 运行渲染循环,直到用户尝试关闭
// 窗口或已按 ESCAPE 键。
while ( !glfwWindowShouldClose(window) ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除 framebuffer

glfwSwapBuffers(window); //交换颜色缓冲区

// 轮询窗口事件。上面的 key callback 只会是
//在此调用期间调用。
glfwPollEvents();
}
}
public static void main(String[] args) {
new Main().run();
}
}
  1. 测试

运行项目发现已经能够展示一个窗口了!