Compare commits
2 Commits
533e6260d5
...
ec98b05d61
| Author | SHA1 | Date | |
|---|---|---|---|
| ec98b05d61 | |||
| 0fb24263e0 |
@@ -1,10 +1,12 @@
|
|||||||
package chess.view.DDDrender;
|
package chess.view.DDDrender;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import chess.view.DDDrender.opengl.VertexArray;
|
import chess.view.DDDrender.opengl.VertexArray;
|
||||||
|
|
||||||
public class DDDModel {
|
public class DDDModel implements Closeable {
|
||||||
private final List<VertexArray> vaos;
|
private final List<VertexArray> vaos;
|
||||||
|
|
||||||
public DDDModel(List<VertexArray> vaos) {
|
public DDDModel(List<VertexArray> vaos) {
|
||||||
@@ -15,4 +17,11 @@ public class DDDModel {
|
|||||||
return vaos;
|
return vaos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
for (VertexArray vertexArray : vaos) {
|
||||||
|
vertexArray.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ import org.joml.Vector2f;
|
|||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
|
||||||
class DDDPlacement {
|
class DDDPlacement {
|
||||||
public static Vector2f coordinates_to_vector(Coordinate coo) {
|
public static Vector2f coordinatesToVector(Coordinate coo) {
|
||||||
return coordinates_to_vector(coo.getX(), coo.getY());
|
return coordinatesToVector(coo.getX(), coo.getY());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2f coordinates_to_vector(float x, float y) {
|
public static Vector2f coordinatesToVector(float x, float y) {
|
||||||
return new Vector2f(1.0f - 0.125f - x * 0.250f, 1.0f - 0.125f - y * 0.250f);
|
return new Vector2f(1.0f - 0.125f - x * 0.250f, 1.0f - 0.125f - y * 0.250f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Coordinate vector_to_coordinates(Vector2f pos) {
|
public static Coordinate vectorToCoordinates(Vector2f pos) {
|
||||||
int x = (int) ((1.0f - pos.x) * 4.0f);
|
int x = (int) ((1.0f - pos.x) * 4.0f);
|
||||||
int y = (int) ((1.0f - pos.y) * 4.0f);
|
int y = (int) ((1.0f - pos.y) * 4.0f);
|
||||||
return new Coordinate(x, y);
|
return new Coordinate(x, y);
|
||||||
|
|||||||
@@ -23,15 +23,13 @@ public class DDDView extends GameAdaptator {
|
|||||||
|
|
||||||
private final CommandExecutor commandExecutor;
|
private final CommandExecutor commandExecutor;
|
||||||
private final Window window;
|
private final Window window;
|
||||||
private final Renderer renderer;
|
|
||||||
private final World world;
|
private final World world;
|
||||||
private BoardEntity boardEntity;
|
private BoardEntity boardEntity;
|
||||||
|
|
||||||
public DDDView(CommandExecutor commandExecutor) {
|
public DDDView(CommandExecutor commandExecutor) {
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
this.renderer = new Renderer();
|
|
||||||
this.world = new World();
|
this.world = new World();
|
||||||
this.window = new Window(this.renderer, this.world, new Camera());
|
this.window = new Window(new Renderer(), this.world, new Camera());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoked when a cell is clicked
|
// Invoked when a cell is clicked
|
||||||
@@ -87,7 +85,7 @@ public class DDDView extends GameAdaptator {
|
|||||||
if (piece == null)
|
if (piece == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Vector2f pieceBoardPos = DDDPlacement.coordinates_to_vector(pos);
|
Vector2f pieceBoardPos = DDDPlacement.coordinatesToVector(pos);
|
||||||
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
|
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
|
||||||
|
|
||||||
PieceEntity entity = new PieceEntity(piece, pieceWorldPos,
|
PieceEntity entity = new PieceEntity(piece, pieceWorldPos,
|
||||||
@@ -114,6 +112,14 @@ public class DDDView extends GameAdaptator {
|
|||||||
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
|
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
|
||||||
// });
|
// });
|
||||||
this.window.run();
|
this.window.run();
|
||||||
|
|
||||||
|
// free OpenGL resources
|
||||||
|
try {
|
||||||
|
this.window.close();
|
||||||
|
this.world.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package chess.view.DDDrender;
|
|||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
@@ -11,7 +14,7 @@ import chess.view.DDDrender.shader.BoardShader;
|
|||||||
import chess.view.DDDrender.shader.PieceShader;
|
import chess.view.DDDrender.shader.PieceShader;
|
||||||
import chess.view.DDDrender.shader.ShaderProgram;
|
import chess.view.DDDrender.shader.ShaderProgram;
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer implements Closeable{
|
||||||
private BoardShader boardShader;
|
private BoardShader boardShader;
|
||||||
private PieceShader pieceShader;
|
private PieceShader pieceShader;
|
||||||
|
|
||||||
@@ -56,4 +59,10 @@ public class Renderer {
|
|||||||
public BoardShader getBoardShader() {
|
public BoardShader getBoardShader() {
|
||||||
return boardShader;
|
return boardShader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
this.boardShader.close();
|
||||||
|
this.pieceShader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ 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 java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -24,7 +26,7 @@ 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 {
|
public class Window implements Closeable{
|
||||||
|
|
||||||
// The window handle
|
// The window handle
|
||||||
private long window;
|
private long window;
|
||||||
@@ -151,7 +153,7 @@ public class Window {
|
|||||||
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.vector_to_coordinates(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())
|
||||||
@@ -225,4 +227,9 @@ public class Window {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
this.renderer.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,13 @@
|
|||||||
package chess.view.DDDrender.opengl;
|
package chess.view.DDDrender.opengl;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
public class ElementBuffer {
|
public class ElementBuffer implements Closeable {
|
||||||
private int id;
|
private final int id;
|
||||||
private int indiciesCount;
|
private final int indiciesCount;
|
||||||
|
|
||||||
public ElementBuffer(int[] indicies) {
|
public ElementBuffer(int[] indicies) {
|
||||||
this.indiciesCount = indicies.length;
|
this.indiciesCount = indicies.length;
|
||||||
@@ -15,10 +18,6 @@ public class ElementBuffer {
|
|||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Destroy() {
|
|
||||||
GL30.glDeleteBuffers(this.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Bind() {
|
public void Bind() {
|
||||||
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.id);
|
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.id);
|
||||||
}
|
}
|
||||||
@@ -30,4 +29,9 @@ public class ElementBuffer {
|
|||||||
public int GetIndiciesCount() {
|
public int GetIndiciesCount() {
|
||||||
return this.indiciesCount;
|
return this.indiciesCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
GL30.glDeleteBuffers(this.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package chess.view.DDDrender.opengl;
|
package chess.view.DDDrender.opengl;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
public class VertexArray {
|
public class VertexArray implements Closeable {
|
||||||
private int id;
|
|
||||||
private ElementBuffer elementBuffer;
|
private final int id;
|
||||||
private List<VertexBuffer> vertexBuffers;
|
private final ElementBuffer elementBuffer;
|
||||||
|
private final List<VertexBuffer> vertexBuffers;
|
||||||
|
|
||||||
public VertexArray(ElementBuffer elementBuffer) {
|
public VertexArray(ElementBuffer elementBuffer) {
|
||||||
this.id = GL30.glGenVertexArrays();
|
this.id = GL30.glGenVertexArrays();
|
||||||
@@ -19,10 +22,6 @@ public class VertexArray {
|
|||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Destroy() {
|
|
||||||
GL30.glDeleteBuffers(this.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetVertexCount() {
|
public int GetVertexCount() {
|
||||||
return this.elementBuffer.GetIndiciesCount();
|
return this.elementBuffer.GetIndiciesCount();
|
||||||
}
|
}
|
||||||
@@ -48,4 +47,13 @@ public class VertexArray {
|
|||||||
public List<VertexBuffer> getVertexBuffers() {
|
public List<VertexBuffer> getVertexBuffers() {
|
||||||
return vertexBuffers;
|
return vertexBuffers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
GL30.glDeleteBuffers(this.id);
|
||||||
|
for (VertexBuffer vertexBuffer : vertexBuffers) {
|
||||||
|
vertexBuffer.close();
|
||||||
|
}
|
||||||
|
elementBuffer.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,17 @@ package chess.view.DDDrender.opengl;
|
|||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
public class VertexBuffer {
|
public class VertexBuffer implements Closeable {
|
||||||
private int id;
|
private final int id;
|
||||||
private int dataStride;
|
private final int dataStride;
|
||||||
List<VertexAttribPointer> vertexAttribs;
|
private final List<VertexAttribPointer> vertexAttribs;
|
||||||
|
|
||||||
public VertexBuffer(float[] data, int stride) {
|
public VertexBuffer(float[] data, int stride) {
|
||||||
this.id = GL30.glGenBuffers();
|
this.id = GL30.glGenBuffers();
|
||||||
@@ -28,10 +30,6 @@ public class VertexBuffer {
|
|||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Destroy() {
|
|
||||||
GL30.glDeleteBuffers(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Bind() {
|
public void Bind() {
|
||||||
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.id);
|
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.id);
|
||||||
}
|
}
|
||||||
@@ -52,4 +50,9 @@ public class VertexBuffer {
|
|||||||
this.dataStride * 4, vertexAttribPointer.offset());
|
this.dataStride * 4, vertexAttribPointer.offset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
GL30.glDeleteBuffers(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chess.view.DDDrender.shader;
|
package chess.view.DDDrender.shader;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
@@ -9,7 +11,7 @@ import org.lwjgl.BufferUtils;
|
|||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
import org.lwjgl.system.MemoryStack;
|
import org.lwjgl.system.MemoryStack;
|
||||||
|
|
||||||
public abstract class ShaderProgram {
|
public abstract class ShaderProgram implements Closeable {
|
||||||
private int programId;
|
private int programId;
|
||||||
private int vertexShaderId;
|
private int vertexShaderId;
|
||||||
private int fragmentShaderId;
|
private int fragmentShaderId;
|
||||||
@@ -91,4 +93,11 @@ public abstract class ShaderProgram {
|
|||||||
GL30.glUniformMatrix4fv(location, false, buffer);
|
GL30.glUniformMatrix4fv(location, false, buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
GL30.glDeleteShader(vertexShaderId);
|
||||||
|
GL30.glDeleteShader(fragmentShaderId);
|
||||||
|
GL30.glDeleteProgram(programId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chess.view.DDDrender.world;
|
package chess.view.DDDrender.world;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
@@ -38,4 +40,9 @@ public class BoardEntity extends Entity {
|
|||||||
renderer.RenderVao(renderer.getBoardShader(), vao);
|
renderer.RenderVao(renderer.getBoardShader(), vao);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
vao.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package chess.view.DDDrender.world;
|
package chess.view.DDDrender.world;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
|
||||||
import chess.view.DDDrender.Renderer;
|
import chess.view.DDDrender.Renderer;
|
||||||
|
|
||||||
public abstract class Entity {
|
public abstract class Entity implements Closeable{
|
||||||
|
|
||||||
public abstract void render(Renderer renderer);
|
public abstract void render(Renderer renderer);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chess.view.DDDrender.world;
|
package chess.view.DDDrender.world;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
import chess.view.DDDrender.DDDModel;
|
import chess.view.DDDrender.DDDModel;
|
||||||
@@ -36,4 +38,9 @@ public class ModelEntity extends Entity {
|
|||||||
this.rotation = rotation;
|
this.rotation = rotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
this.model.close();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package chess.view.DDDrender.world;
|
package chess.view.DDDrender.world;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -7,7 +9,7 @@ import chess.model.Coordinate;
|
|||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
public class World {
|
public class World implements Closeable{
|
||||||
/** Renderable entity list */
|
/** Renderable entity list */
|
||||||
private final List<Entity> entites;
|
private final List<Entity> entites;
|
||||||
|
|
||||||
@@ -55,4 +57,11 @@ public class World {
|
|||||||
return entites;
|
return entites;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
for (Entity entity : entites) {
|
||||||
|
entity.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user