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(); Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(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); DDDView ddd = new DDDView(commandExecutor);

View File

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

View File

@@ -72,5 +72,6 @@ public class Renderer implements Closeable {
public void close() throws IOException { public void close() throws IOException {
this.boardShader.close(); this.boardShader.close();
this.pieceShader.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 ImInt detailLevel = new ImInt(10);
private ImBoolean pixelatedFrame = new ImBoolean(true); private ImBoolean pixelatedFrame = new ImBoolean(true);
private boolean shouldBeClosed = false;
public Window(Renderer renderer, World world, Camera camera) { public Window(Renderer renderer, World world, Camera camera) {
this.renderer = new Renderer(); this.renderer = new Renderer();
this.cam = camera; this.cam = camera;
@@ -280,7 +282,7 @@ 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 (!shouldBeClosed && !glfwWindowShouldClose(window)) {
newFrame(); newFrame();
@@ -304,8 +306,13 @@ public class Window implements Closeable {
} }
} }
public void stop() {
shouldBeClosed = true;
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
shouldBeClosed = true;
this.renderer.close(); 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.glTexImage2D;
import static org.lwjgl.opengl.GL11.glViewport; import static org.lwjgl.opengl.GL11.glViewport;
import java.io.Closeable;
import java.io.IOException;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
public class FrameBuffer { public class FrameBuffer implements Closeable {
private int fbo; private int fbo;
private int renderTexture; private int renderTexture;
@@ -103,4 +106,11 @@ public class FrameBuffer {
return renderTexture; return renderTexture;
} }
@Override
public void close() throws IOException {
GL30.glDeleteFramebuffers(this.fbo);
GL30.glDeleteRenderbuffers(this.depthBuffer);
GL30.glDeleteTextures(this.renderTexture);
}
} }