feat: add imgui
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -7,3 +7,6 @@ build
|
||||
app/bin
|
||||
|
||||
.vscode
|
||||
|
||||
*.wav
|
||||
imgui.ini
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
@@ -31,6 +36,9 @@ 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
|
||||
|
||||
BIN
app/src/main/resources/fonts/comic.ttf
Normal file
BIN
app/src/main/resources/fonts/comic.ttf
Normal file
Binary file not shown.
Reference in New Issue
Block a user