feat: add imgui
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -6,4 +6,7 @@ build
|
|||||||
|
|
||||||
app/bin
|
app/bin
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
|
*.wav
|
||||||
|
imgui.ini
|
||||||
@@ -16,8 +16,10 @@ repositories {
|
|||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def os = "linux";
|
||||||
def lwjgl_version = "3.3.6"
|
def lwjgl_version = "3.3.6"
|
||||||
def lwjgl_natives = "natives-linux"
|
def lwjgl_natives = "natives-$os"
|
||||||
|
def imgui_version = "1.87.0"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
@@ -33,6 +35,10 @@ dependencies {
|
|||||||
implementation "org.lwjgl:lwjgl-opengl::$lwjgl_natives"
|
implementation "org.lwjgl:lwjgl-opengl::$lwjgl_natives"
|
||||||
implementation "org.lwjgl:lwjgl-glfw::$lwjgl_natives"
|
implementation "org.lwjgl:lwjgl-glfw::$lwjgl_natives"
|
||||||
implementation "org.lwjgl:lwjgl-assimp::$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 {
|
application {
|
||||||
|
|||||||
@@ -7,9 +7,14 @@ import org.lwjgl.opengl.*;
|
|||||||
import org.lwjgl.system.*;
|
import org.lwjgl.system.*;
|
||||||
|
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
import chess.view.AssetManager;
|
||||||
import chess.view.DDDrender.world.Entity;
|
import chess.view.DDDrender.world.Entity;
|
||||||
import chess.view.DDDrender.world.World;
|
import chess.view.DDDrender.world.World;
|
||||||
import common.Signal1;
|
import common.Signal1;
|
||||||
|
import imgui.ImFontConfig;
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.gl3.ImGuiImplGl3;
|
||||||
|
import imgui.glfw.ImGuiImplGlfw;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
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.MemoryStack.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
public class Window implements Closeable{
|
public class Window implements Closeable {
|
||||||
|
|
||||||
// The window handle
|
// The window handle
|
||||||
private long window;
|
private long window;
|
||||||
|
|
||||||
|
private final ImGuiImplGl3 implGl3 = new ImGuiImplGl3();
|
||||||
|
private final ImGuiImplGlfw implGlfw = new ImGuiImplGlfw();
|
||||||
|
|
||||||
private Renderer renderer;
|
private Renderer renderer;
|
||||||
private final Camera cam;
|
private final Camera cam;
|
||||||
private final World world;
|
private final World world;
|
||||||
@@ -77,6 +85,37 @@ public class Window implements Closeable{
|
|||||||
// Terminate GLFW and free the error callback
|
// Terminate GLFW and free the error callback
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
glfwSetErrorCallback(null).free();
|
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() {
|
private void init() {
|
||||||
@@ -114,10 +153,21 @@ public class Window implements Closeable{
|
|||||||
window,
|
window,
|
||||||
(vidmode.width() - pWidth.get(0)) / 2,
|
(vidmode.width() - pWidth.get(0)) / 2,
|
||||||
(vidmode.height() - pHeight.get(0)) / 2);
|
(vidmode.height() - pHeight.get(0)) / 2);
|
||||||
|
|
||||||
|
glfwSetWindowSize(window, vidmode.width(), vidmode.height());
|
||||||
} // the stack frame is popped automatically
|
} // the stack frame is popped automatically
|
||||||
|
|
||||||
// Make the OpenGL context current
|
// Make the OpenGL context current
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
|
GL.createCapabilities();
|
||||||
|
|
||||||
|
setCallbacks();
|
||||||
|
|
||||||
|
initImGui();
|
||||||
|
|
||||||
|
renderer.Init();
|
||||||
|
|
||||||
// Enable v-sync
|
// Enable v-sync
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
@@ -152,37 +202,31 @@ public class Window implements Closeable{
|
|||||||
double x[] = new double[1];
|
double x[] = new double[1];
|
||||||
double y[] = new double[1];
|
double y[] = new double[1];
|
||||||
glfwGetCursorPos(this.window, x, y);
|
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);
|
Coordinate selectedCell = DDDPlacement.vectorToCoordinates(cursorPos);
|
||||||
if (this.lastCell == null) {
|
if (this.lastCell == null) {
|
||||||
this.lastCell = selectedCell;
|
this.lastCell = selectedCell;
|
||||||
if (selectedCell.isValid())
|
if (selectedCell.isValid())
|
||||||
this.OnCellEnter.emit(selectedCell);
|
this.OnCellEnter.emit(selectedCell);
|
||||||
}
|
} else if (!this.lastCell.equals(selectedCell)) {
|
||||||
else if (!this.lastCell.equals(selectedCell)) {
|
|
||||||
if (this.lastCell.isValid())
|
if (this.lastCell.isValid())
|
||||||
this.OnCellExit.emit(this.lastCell);
|
this.OnCellExit.emit(this.lastCell);
|
||||||
if (selectedCell.isValid())
|
if (selectedCell.isValid())
|
||||||
this.OnCellEnter.emit(selectedCell);
|
this.OnCellEnter.emit(selectedCell);
|
||||||
this.lastCell = selectedCell;
|
this.lastCell = selectedCell;
|
||||||
}
|
}
|
||||||
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
|
}
|
||||||
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
|
|
||||||
if (this.lastCell.isValid())
|
private void newFrame() {
|
||||||
this.OnCellClick.emit(this.lastCell);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
||||||
}
|
|
||||||
});
|
implGlfw.newFrame();
|
||||||
|
implGl3.newFrame();
|
||||||
|
ImGui.newFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loop() {
|
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
|
// Set the clear color
|
||||||
glClearColor(0.4f, 0.4f, 0.6f, 1.0f);
|
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
|
// Run the rendering loop until the user has attempted to close
|
||||||
// the window or has pressed the ESCAPE key.
|
// the window or has pressed the ESCAPE key.
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
|
||||||
|
newFrame();
|
||||||
|
|
||||||
double currentTime = glfwGetTime();
|
double currentTime = glfwGetTime();
|
||||||
float deltaTime = (float) (currentTime - lastTime);
|
float deltaTime = (float) (currentTime - lastTime);
|
||||||
render(deltaTime, (float) width[0] / (float) height[0]);
|
render(deltaTime, (float) width[0] / (float) height[0]);
|
||||||
|
|
||||||
|
// ImGui.showDemoWindow();
|
||||||
|
ImGui.render();
|
||||||
|
implGl3.renderDrawData(ImGui.getDrawData());
|
||||||
|
|
||||||
lastTime = glfwGetTime();
|
lastTime = glfwGetTime();
|
||||||
|
|
||||||
glfwSwapBuffers(window); // swap the color buffers
|
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