i failed you

This commit is contained in:
2025-04-16 19:03:55 +02:00
parent 78d48fafc6
commit 3ac7b4ad65
3 changed files with 165 additions and 10 deletions

View File

@@ -0,0 +1,33 @@
package chess;
import chess.ai.DumbAI;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.controller.event.GameAdaptator;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Game;
import chess.pgn.PgnExport;
import chess.view.render.Window;
public class OpenGLMain {
public static void main(String[] args) {
Game game = new Game(new ChessBoard());
CommandExecutor commandExecutor = new CommandExecutor(game);
Window window = new Window(commandExecutor);
commandExecutor.addListener(window);
// DumbAI ai = new DumbAI(commandExecutor, Color.Black);
// commandExecutor.addListener(ai);
// DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
// commandExecutor.addListener(ai2);
commandExecutor.executeCommand(new NewGameCommand());
window.run();
}
}

View File

@@ -3,6 +3,7 @@ package chess.view.render;
import org.joml.Vector3f;
import org.lwjgl.opengl.*;
import chess.model.Coordinate;
import chess.view.render.shader.BoardShader;
import static org.lwjgl.opengl.GL30.*;
@@ -59,16 +60,42 @@ public class Renderer {
return positions;
}
private Coordinate GetCellFromColor(Vector3f color) {
int offset = 1;
if (color.x > 0.5) {
color = new Vector3f(1.0f, 1.0f, 1.0f).sub(color);
offset = 0;
}
int r = (int) (color.x * 255.0f);
int g = (int) (color.y * 255.0f);
int b = (int) (color.z * 255.0f);
int index = (r + g + b) * 2 + offset;
return Coordinate.fromIndex(index);
}
private Vector3f GetCellColor(int x, int y) {
float index = (y * BOARD_WIDTH + x) / 2.0f;
float r = (int) (index / 3) / 255.0f;
float g = (int) ((index + 1) / 3) / 255.0f;
float b = (int) ((index + 2) / 3) / 255.0f;
if ((x + y) % 2 != 0) {
System.out.println(GetCellFromColor(new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f)));
return new Vector3f(1.0f - r - 1.0f / 255.0f, 1.0f - g - 1.0f / 255.0f, 1.0f - b - 1.0f / 255.0f);
} else {
System.out.println(GetCellFromColor(new Vector3f(r, g, b)));
return new Vector3f(r, g, b);
}
}
private float[] GetBoardColors() {
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
for (int i = 0; i < BOARD_WIDTH; i++) {
for (int j = 0; j < BOARD_HEIGHT; j++) {
Vector3f color;
if ((i + j) % 2 != 0) {
color = new Vector3f(1.0f, 1.0f, 1.0f);
} else {
color = new Vector3f(0.0f, 0.0f, 0.0f);
}
for (int j = 0; j < BOARD_HEIGHT; j++) {
for (int i = 0; i < BOARD_WIDTH; i++) {
Vector3f color = GetCellColor(i, j);
int squareIndex = i * BOARD_WIDTH + j;
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
@@ -109,6 +136,12 @@ public class Renderer {
this.vao.Unbind();
}
public Coordinate GetSelectedCell() {
float pixels[] = new float[3];
GL30.glReadPixels(500, 500, 1, 1, GL_RGB, GL_FLOAT, pixels);
return GetCellFromColor(new Vector3f(pixels[0], pixels[1], pixels[2]));
}
public void Render(Camera cam) {
this.shader.Start();
this.shader.SetCamMatrix(cam.getMatrix());

View File

@@ -5,6 +5,12 @@ import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import chess.controller.CommandExecutor;
import chess.controller.event.GameListener;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;
@@ -13,17 +19,20 @@ import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Window {
public class Window implements GameListener{
// The window handle
private long window;
private final CommandExecutor commandExecutor;
private Renderer renderer;
private Camera cam;
public Window() {
public Window(CommandExecutor commandExecutor) {
this.renderer = new Renderer();
this.cam = new Camera();
this.commandExecutor = new CommandExecutor();
}
public void run() {
@@ -114,6 +123,8 @@ public class Window {
render();
System.out.println(this.renderer.GetSelectedCell());
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
@@ -132,4 +143,82 @@ public class Window {
}
}
@Override
public void onBoardUpdate() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onBoardUpdate'");
}
@Override
public void onDraw() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onDraw'");
}
@Override
public void onGameEnd() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onGameEnd'");
}
@Override
public void onGameStart() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onGameStart'");
}
@Override
public void onKingInCheck() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onKingInCheck'");
}
@Override
public void onKingInMat() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onKingInMat'");
}
@Override
public void onMove(Move move) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onMove'");
}
@Override
public void onMoveNotAllowed(Move move) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onMoveNotAllowed'");
}
@Override
public void onPatSituation() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onPatSituation'");
}
@Override
public void onPlayerTurn(Color color) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onPlayerTurn'");
}
@Override
public void onPromotePawn(Coordinate pieceCoords) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onPromotePawn'");
}
@Override
public void onSurrender(Color coward) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onSurrender'");
}
@Override
public void onWin(Color winner) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'onWin'");
}
}