Compare commits

...

5 Commits

Author SHA1 Message Date
fd9aabb6a1 refactor dddview
All checks were successful
Linux arm64 / Build (push) Successful in 46s
2025-05-18 11:45:23 +02:00
1b31643f0b spawn popup at center 2025-05-18 11:45:17 +02:00
fefb826b38 free frame buffer 2025-05-18 11:37:57 +02:00
56f2aa3c56 close window on game end 2025-05-18 11:36:14 +02:00
0e8ee9eacd blocking popups 2025-05-18 11:25:10 +02:00
5 changed files with 50 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ public class OpenGLMain {
Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game);
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, "games/CastlingTest.pgn");
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, "games/FoolCheckmate.pgn");
DDDView ddd = new DDDView(commandExecutor);

View File

@@ -7,7 +7,9 @@ import java.util.function.Consumer;
import chess.controller.commands.*;
import imgui.ImGui;
import imgui.type.ImBoolean;
import imgui.ImVec2;
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiWindowFlags;
import org.joml.Vector2f;
import org.joml.Vector3f;
@@ -44,7 +46,6 @@ public class DDDView extends GameAdapter implements CommandSender {
private float moveProgress = 0.0f;
private String waitingPopup = null;
private final ImBoolean popupOpened = new ImBoolean(false);
public DDDView(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
@@ -148,7 +149,7 @@ public class DDDView extends GameAdapter implements CommandSender {
private void onCellExit(Coordinate coordinate) {
if (this.click == null) {
this.boardEntity.resetCellColor(coordinate);
Piece p = pieceAt(coordinate);
Piece p = getPieceAt(coordinate);
if (p == null)
return;
@@ -168,7 +169,7 @@ public class DDDView extends GameAdapter implements CommandSender {
private void cancelPreview(Coordinate coordinate) {
this.boardEntity.resetCellColor(coordinate);
Piece p = pieceAt(coordinate);
Piece p = getPieceAt(coordinate);
if (p == null)
return;
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
@@ -180,12 +181,6 @@ public class DDDView extends GameAdapter implements CommandSender {
}
}
private Piece pieceAt(Coordinate pos) {
GetPieceAtCommand cmd = new GetPieceAtCommand(pos);
this.commandExecutor.executeCommand(cmd);
return cmd.getPiece();
}
@Override
public void onGameStart() {
this.window.scheduleTask(() -> {
@@ -251,7 +246,7 @@ public class DDDView extends GameAdapter implements CommandSender {
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate pos = new Coordinate(i, j);
Piece piece = pieceAt(pos);
Piece piece = getPieceAt(pos);
if (piece == null)
continue;
@@ -377,8 +372,16 @@ public class DDDView extends GameAdapter implements CommandSender {
}
private void renderPopup(String title, String text) {
if (ImGui.beginPopupModal(title, popupOpened)) {
ImVec2 center = ImGui.getMainViewport().getCenter();
ImGui.setNextWindowPos(center, ImGuiCond.Appearing, new ImVec2(0.5f, 0.5f));
if (ImGui.beginPopupModal(title, null, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoMove)) {
ImGui.text(text);
if (ImGui.button("Close")) {
ImGui.closeCurrentPopup();
synchronized (this) {
notifyAll();
}
}
ImGui.endPopup();
}
}
@@ -397,8 +400,15 @@ public class DDDView extends GameAdapter implements CommandSender {
}
private void openPopup(String title) {
this.popupOpened.set(true);
this.waitingPopup = title;
// block the current thread until the popup is closed
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
@@ -419,6 +429,12 @@ public class DDDView extends GameAdapter implements CommandSender {
@Override
public void onGameEnd() {
openPopup("End");
this.window.stop();
}
@Override
public void onWin(Color color) {
openPopup(color == Color.White ? "Black victory" : "White victory");
}
@Override
@@ -429,7 +445,6 @@ public class DDDView extends GameAdapter implements CommandSender {
@Override
public void onSurrender(Color color) {
openPopup(color == Color.White ? "White surrender" : "Black surrender");
openPopup(color == Color.White ? "Black victory" : "White victory");
}
@Override

View File

@@ -72,5 +72,6 @@ public class Renderer implements Closeable {
public void close() throws IOException {
this.boardShader.close();
this.pieceShader.close();
this.frameBuffer.close();
}
}

View File

@@ -64,6 +64,8 @@ public class Window implements Closeable {
private ImInt detailLevel = new ImInt(10);
private ImBoolean pixelatedFrame = new ImBoolean(true);
private boolean shouldBeClosed = false;
public Window(Renderer renderer, World world, Camera camera) {
this.renderer = new Renderer();
this.cam = camera;
@@ -280,7 +282,7 @@ 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)) {
while (!shouldBeClosed && !glfwWindowShouldClose(window)) {
newFrame();
@@ -304,8 +306,13 @@ public class Window implements Closeable {
}
}
public void stop() {
shouldBeClosed = true;
}
@Override
public void close() throws IOException {
shouldBeClosed = true;
this.renderer.close();
}

View File

@@ -11,9 +11,12 @@ import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glViewport;
import java.io.Closeable;
import java.io.IOException;
import org.lwjgl.opengl.GL30;
public class FrameBuffer {
public class FrameBuffer implements Closeable {
private int fbo;
private int renderTexture;
@@ -103,4 +106,11 @@ public class FrameBuffer {
return renderTexture;
}
@Override
public void close() throws IOException {
GL30.glDeleteFramebuffers(this.fbo);
GL30.glDeleteRenderbuffers(this.depthBuffer);
GL30.glDeleteTextures(this.renderTexture);
}
}