|
17 | 17 | */
|
18 | 18 | package net.raphimc.noteblocktool.frames.visualizer;
|
19 | 19 |
|
20 |
| -import net.lenni0451.commons.logging.impl.SysoutLogger; |
21 | 20 | import net.raphimc.noteblocktool.util.SoundSystemSongPlayer;
|
22 | 21 | import net.raphimc.thingl.ThinGL;
|
23 |
| -import net.raphimc.thingl.framebuffer.impl.TextureFramebuffer; |
24 |
| -import net.raphimc.thingl.framebuffer.impl.WindowFramebuffer; |
25 |
| -import net.raphimc.thingl.implementation.DebugMessageCallback; |
26 |
| -import net.raphimc.thingl.implementation.GLFWWindowInterface; |
27 |
| -import net.raphimc.thingl.wrapper.Blending; |
| 22 | +import net.raphimc.thingl.implementation.application.StandaloneApplicationInterface; |
| 23 | +import net.raphimc.thingl.implementation.application.StandaloneApplicationRunner; |
| 24 | +import net.raphimc.thingl.implementation.window.GLFWWindowInterface; |
28 | 25 | import org.joml.Matrix4fStack;
|
29 | 26 | import org.lwjgl.glfw.GLFW;
|
30 |
| -import org.lwjgl.glfw.GLFWErrorCallback; |
31 |
| -import org.lwjgl.opengl.GL; |
32 |
| -import org.lwjgl.opengl.GL11C; |
33 | 27 |
|
34 |
| -public class VisualizerWindow { |
| 28 | +public class VisualizerWindow extends StandaloneApplicationRunner { |
35 | 29 |
|
36 | 30 | private final DropRenderer dropRenderer;
|
| 31 | + private final Runnable openCallback; |
37 | 32 | private final Runnable closeCallback;
|
38 | 33 | private final Thread renderThread;
|
39 |
| - private long window; |
40 | 34 |
|
41 | 35 | public VisualizerWindow(final SoundSystemSongPlayer songPlayer, final Runnable openCallback, final Runnable closeCallback) {
|
| 36 | + super(new Configuration() |
| 37 | + .setWindowTitle("NoteBlockTool Song Visualizer - " + songPlayer.getSong().getTitleOrFileNameOr("No Title")) |
| 38 | + .setDebugMode(true) |
| 39 | + .setExtendedDebugMode(false)); |
| 40 | + |
42 | 41 | this.dropRenderer = new DropRenderer(songPlayer);
|
| 42 | + this.openCallback = openCallback; |
43 | 43 | this.closeCallback = closeCallback;
|
44 |
| - this.renderThread = new Thread(() -> { |
45 |
| - GLFWErrorCallback.createPrint(System.err).set(); |
46 |
| - |
47 |
| - if (!GLFW.glfwInit()) { |
48 |
| - throw new IllegalStateException("Unable to initialize GLFW"); |
49 |
| - } |
50 |
| - |
51 |
| - GLFW.glfwDefaultWindowHints(); |
52 |
| - GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); |
53 |
| - GLFW.glfwWindowHint(GLFW.GLFW_FOCUS_ON_SHOW, GLFW.GLFW_FALSE); |
54 |
| - GLFW.glfwWindowHint(GLFW.GLFW_CLIENT_API, GLFW.GLFW_OPENGL_API); |
55 |
| - GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_CREATION_API, GLFW.GLFW_NATIVE_CONTEXT_API); |
56 |
| - GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4); |
57 |
| - GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 5); |
58 |
| - GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); |
59 |
| - GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); |
60 |
| - |
61 |
| - this.window = GLFW.glfwCreateWindow(1280, 720, "NoteBlockTool Song Visualizer - " + songPlayer.getSong().getTitleOrFileNameOr("No Title"), 0L, 0L); |
62 |
| - if (this.window == 0L) { |
63 |
| - throw new RuntimeException("Failed to create the GLFW window"); |
64 |
| - } |
65 |
| - openCallback.run(); |
66 |
| - |
67 |
| - GLFW.glfwMakeContextCurrent(this.window); |
68 |
| - GLFW.glfwSwapInterval(1); |
69 |
| - GL.createCapabilities(); |
70 |
| - |
71 |
| - ThinGL.LOGGER = SysoutLogger.builder().name("ThinGL").build(); |
72 |
| - ThinGL.setInstance(new ExtendedThinGL(NoteBlockToolApplicationInterface::new, GLFWWindowInterface::new)); |
73 |
| - DebugMessageCallback.install(false); |
74 |
| - |
75 |
| - this.dropRenderer.init(); |
76 |
| - |
77 |
| - ThinGL.glStateManager().enable(GL11C.GL_BLEND); |
78 |
| - Blending.standardBlending(); |
79 |
| - ThinGL.glStateManager().enable(GL11C.GL_DEPTH_TEST); |
80 |
| - ThinGL.glStateManager().setDepthFunc(GL11C.GL_LEQUAL); |
81 |
| - final TextureFramebuffer mainFramebuffer = new TextureFramebuffer(); |
82 |
| - mainFramebuffer.setClearColor(0.5F, 0.5F, 0.5F, 0.5F); |
83 |
| - final Matrix4fStack positionMatrix = new Matrix4fStack(8); |
84 |
| - |
85 |
| - while (!GLFW.glfwWindowShouldClose(this.window)) { |
86 |
| - ThinGL.get().onStartFrame(); |
87 |
| - mainFramebuffer.bind(true); |
88 |
| - mainFramebuffer.clear(); |
89 |
| - |
90 |
| - positionMatrix.pushMatrix(); |
91 |
| - this.dropRenderer.render(positionMatrix); |
92 |
| - positionMatrix.popMatrix(); |
93 |
| - |
94 |
| - mainFramebuffer.unbind(); |
95 |
| - mainFramebuffer.blitTo(WindowFramebuffer.INSTANCE, true, false, false); |
96 |
| - ThinGL.get().onFinishFrame(); |
97 |
| - GLFW.glfwSwapBuffers(this.window); |
98 |
| - GLFW.glfwPollEvents(); |
99 |
| - ThinGL.get().onEndFrame(); |
100 |
| - } |
101 |
| - |
102 |
| - this.dropRenderer.free(); |
103 |
| - ThinGL.get().free(); |
104 |
| - GLFW.glfwDestroyWindow(this.window); |
105 |
| - GLFW.glfwTerminate(); |
106 |
| - if (this.closeCallback != null) { |
107 |
| - this.closeCallback.run(); |
108 |
| - } |
109 |
| - }, "Visualizer Render Thread"); |
| 44 | + this.renderThread = new Thread(this::launch, "Visualizer Render Thread"); |
110 | 45 | this.renderThread.setDaemon(true);
|
111 | 46 | this.renderThread.start();
|
112 | 47 |
|
@@ -138,4 +73,42 @@ public void close() {
|
138 | 73 | }
|
139 | 74 | }
|
140 | 75 |
|
| 76 | + @Override |
| 77 | + protected void setWindowFlags() { |
| 78 | + super.setWindowFlags(); |
| 79 | + GLFW.glfwWindowHint(GLFW.GLFW_FOCUS_ON_SHOW, GLFW.GLFW_FALSE); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected void createWindow() { |
| 84 | + super.createWindow(); |
| 85 | + this.openCallback.run(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected ThinGL createThinGL() { |
| 90 | + return new ExtendedThinGL(StandaloneApplicationInterface::new, GLFWWindowInterface::new); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void init() { |
| 95 | + super.init(); |
| 96 | + this.dropRenderer.init(); |
| 97 | + this.mainFramebuffer.setClearColor(0.5F, 0.5F, 0.5F, 0.5F); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected void render(final Matrix4fStack positionMatrix) { |
| 102 | + this.dropRenderer.render(positionMatrix); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected void free() { |
| 107 | + this.dropRenderer.free(); |
| 108 | + super.free(); |
| 109 | + if (this.closeCallback != null) { |
| 110 | + this.closeCallback.run(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
141 | 114 | }
|
0 commit comments