feat: add imgui

This commit is contained in:
2025-05-16 10:41:33 +02:00
parent 39c7ebefe6
commit dba8e8fb1e
4 changed files with 80 additions and 21 deletions

View File

@@ -16,8 +16,10 @@ repositories {
mavenCentral()
}
def os = "linux";
def lwjgl_version = "3.3.6"
def lwjgl_natives = "natives-linux"
def lwjgl_natives = "natives-$os"
def imgui_version = "1.87.0"
dependencies {
// Use JUnit Jupiter for testing.
@@ -33,6 +35,10 @@ dependencies {
implementation "org.lwjgl:lwjgl-opengl::$lwjgl_natives"
implementation "org.lwjgl:lwjgl-glfw::$lwjgl_natives"
implementation "org.lwjgl:lwjgl-assimp::$lwjgl_natives"
implementation "io.github.spair:imgui-java-binding:$imgui_version"
implementation "io.github.spair:imgui-java-natives-$os:$imgui_version"
implementation "io.github.spair:imgui-java-app:$imgui_version"
}
application {

View File

@@ -7,9 +7,14 @@ import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import chess.model.Coordinate;
import chess.view.AssetManager;
import chess.view.DDDrender.world.Entity;
import chess.view.DDDrender.world.World;
import common.Signal1;
import imgui.ImFontConfig;
import imgui.ImGui;
import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw;
import java.io.Closeable;
import java.io.IOException;
@@ -26,11 +31,14 @@ import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Window implements Closeable{
public class Window implements Closeable {
// The window handle
private long window;
private final ImGuiImplGl3 implGl3 = new ImGuiImplGl3();
private final ImGuiImplGlfw implGlfw = new ImGuiImplGlfw();
private Renderer renderer;
private final Camera cam;
private final World world;
@@ -77,6 +85,37 @@ public class Window implements Closeable{
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
implGl3.shutdown();
implGlfw.shutdown();
ImGui.destroyContext();
}
private void setCallbacks() {
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
if (this.lastCell.isValid())
this.OnCellClick.emit(this.lastCell);
}
});
}
private void initImGui() {
ImGui.setCurrentContext(ImGui.createContext());
implGl3.init("#version 330");
implGlfw.init(window, true);
ImFontConfig config = new ImFontConfig();
config.setFontDataOwnedByAtlas(false);
try {
ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("fonts/comic.ttf").readAllBytes(),
50.0f, config);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void init() {
@@ -114,10 +153,21 @@ public class Window implements Closeable{
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2);
glfwSetWindowSize(window, vidmode.width(), vidmode.height());
} // the stack frame is popped automatically
// Make the OpenGL context current
glfwMakeContextCurrent(window);
GL.createCapabilities();
setCallbacks();
initImGui();
renderer.Init();
// Enable v-sync
glfwSwapInterval(1);
@@ -152,37 +202,31 @@ public class Window implements Closeable{
double x[] = new double[1];
double y[] = new double[1];
glfwGetCursorPos(this.window, x, y);
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f((float) x[0], (float) y[0]), windowWidth, windowHeight);
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f((float) x[0], (float) y[0]), windowWidth,
windowHeight);
Coordinate selectedCell = DDDPlacement.vectorToCoordinates(cursorPos);
if (this.lastCell == null) {
this.lastCell = selectedCell;
if (selectedCell.isValid())
this.OnCellEnter.emit(selectedCell);
}
else if (!this.lastCell.equals(selectedCell)) {
} else if (!this.lastCell.equals(selectedCell)) {
if (this.lastCell.isValid())
this.OnCellExit.emit(this.lastCell);
if (selectedCell.isValid())
this.OnCellEnter.emit(selectedCell);
this.lastCell = selectedCell;
}
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
if (this.lastCell.isValid())
this.OnCellClick.emit(this.lastCell);
}
});
}
private void newFrame() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
implGlfw.newFrame();
implGl3.newFrame();
ImGui.newFrame();
}
private void loop() {
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();
renderer.Init();
// Set the clear color
glClearColor(0.4f, 0.4f, 0.6f, 1.0f);
@@ -204,11 +248,17 @@ public class Window implements Closeable{
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
newFrame();
double currentTime = glfwGetTime();
float deltaTime = (float) (currentTime - lastTime);
render(deltaTime, (float) width[0] / (float) height[0]);
// ImGui.showDemoWindow();
ImGui.render();
implGl3.renderDrawData(ImGui.getDrawData());
lastTime = glfwGetTime();
glfwSwapBuffers(window); // swap the color buffers

Binary file not shown.