free opengl resources

This commit is contained in:
2025-04-28 18:23:29 +02:00
parent 0fb24263e0
commit ec98b05d61
12 changed files with 113 additions and 33 deletions

View File

@@ -1,10 +1,12 @@
package chess.view.DDDrender;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import chess.view.DDDrender.opengl.VertexArray;
public class DDDModel {
public class DDDModel implements Closeable {
private final List<VertexArray> vaos;
public DDDModel(List<VertexArray> vaos) {
@@ -15,4 +17,11 @@ public class DDDModel {
return vaos;
}
@Override
public void close() throws IOException {
for (VertexArray vertexArray : vaos) {
vertexArray.close();
}
}
}

View File

@@ -23,15 +23,13 @@ public class DDDView extends GameAdaptator {
private final CommandExecutor commandExecutor;
private final Window window;
private final Renderer renderer;
private final World world;
private BoardEntity boardEntity;
public DDDView(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
this.renderer = new Renderer();
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
@@ -114,6 +112,14 @@ public class DDDView extends GameAdaptator {
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
// });
this.window.run();
// free OpenGL resources
try {
this.window.close();
this.world.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -2,6 +2,9 @@ package chess.view.DDDrender;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
import java.io.Closeable;
import java.io.IOException;
import org.joml.Matrix4f;
import org.joml.Vector3f;
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.ShaderProgram;
public class Renderer {
public class Renderer implements Closeable{
private BoardShader boardShader;
private PieceShader pieceShader;
@@ -56,4 +59,10 @@ public class Renderer {
public BoardShader getBoardShader() {
return boardShader;
}
@Override
public void close() throws IOException {
this.boardShader.close();
this.pieceShader.close();
}
}

View File

@@ -11,6 +11,8 @@ import chess.view.DDDrender.world.Entity;
import chess.view.DDDrender.world.World;
import common.Signal1;
import java.io.Closeable;
import java.io.IOException;
import java.nio.*;
import java.util.ArrayList;
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.MemoryUtil.*;
public class Window {
public class Window implements Closeable{
// The window handle
private long window;
@@ -225,4 +227,9 @@ public class Window {
}
}
@Override
public void close() throws IOException {
this.renderer.close();
}
}

View File

@@ -1,10 +1,13 @@
package chess.view.DDDrender.opengl;
import java.io.Closeable;
import java.io.IOException;
import org.lwjgl.opengl.GL30;
public class ElementBuffer {
private int id;
private int indiciesCount;
public class ElementBuffer implements Closeable {
private final int id;
private final int indiciesCount;
public ElementBuffer(int[] indicies) {
this.indiciesCount = indicies.length;
@@ -15,10 +18,6 @@ public class ElementBuffer {
Unbind();
}
public void Destroy() {
GL30.glDeleteBuffers(this.id);
}
public void Bind() {
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.id);
}
@@ -30,4 +29,9 @@ public class ElementBuffer {
public int GetIndiciesCount() {
return this.indiciesCount;
}
@Override
public void close() throws IOException {
GL30.glDeleteBuffers(this.id);
}
}

View File

@@ -1,14 +1,17 @@
package chess.view.DDDrender.opengl;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.opengl.GL30;
public class VertexArray {
private int id;
private ElementBuffer elementBuffer;
private List<VertexBuffer> vertexBuffers;
public class VertexArray implements Closeable {
private final int id;
private final ElementBuffer elementBuffer;
private final List<VertexBuffer> vertexBuffers;
public VertexArray(ElementBuffer elementBuffer) {
this.id = GL30.glGenVertexArrays();
@@ -19,10 +22,6 @@ public class VertexArray {
Unbind();
}
public void Destroy() {
GL30.glDeleteBuffers(this.id);
}
public int GetVertexCount() {
return this.elementBuffer.GetIndiciesCount();
}
@@ -48,4 +47,13 @@ public class VertexArray {
public List<VertexBuffer> getVertexBuffers() {
return vertexBuffers;
}
@Override
public void close() throws IOException {
GL30.glDeleteBuffers(this.id);
for (VertexBuffer vertexBuffer : vertexBuffers) {
vertexBuffer.close();
}
elementBuffer.close();
}
}

View File

@@ -2,15 +2,17 @@ package chess.view.DDDrender.opengl;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.opengl.GL30;
public class VertexBuffer {
private int id;
private int dataStride;
List<VertexAttribPointer> vertexAttribs;
public class VertexBuffer implements Closeable {
private final int id;
private final int dataStride;
private final List<VertexAttribPointer> vertexAttribs;
public VertexBuffer(float[] data, int stride) {
this.id = GL30.glGenBuffers();
@@ -28,10 +30,6 @@ public class VertexBuffer {
Unbind();
}
public void Destroy() {
GL30.glDeleteBuffers(id);
}
public void Bind() {
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.id);
}
@@ -52,4 +50,9 @@ public class VertexBuffer {
this.dataStride * 4, vertexAttribPointer.offset());
}
}
@Override
public void close() throws IOException {
GL30.glDeleteBuffers(id);
}
}

View File

@@ -1,5 +1,7 @@
package chess.view.DDDrender.shader;
import java.io.Closeable;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
@@ -9,7 +11,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryStack;
public abstract class ShaderProgram {
public abstract class ShaderProgram implements Closeable {
private int programId;
private int vertexShaderId;
private int fragmentShaderId;
@@ -91,4 +93,11 @@ public abstract class ShaderProgram {
GL30.glUniformMatrix4fv(location, false, buffer);
}
}
@Override
public void close() throws IOException {
GL30.glDeleteShader(vertexShaderId);
GL30.glDeleteShader(fragmentShaderId);
GL30.glDeleteProgram(programId);
}
}

View File

@@ -1,5 +1,7 @@
package chess.view.DDDrender.world;
import java.io.IOException;
import org.joml.Vector3f;
import chess.model.Coordinate;
@@ -38,4 +40,9 @@ public class BoardEntity extends Entity {
renderer.RenderVao(renderer.getBoardShader(), vao);
}
@Override
public void close() throws IOException {
vao.close();
}
}

View File

@@ -1,8 +1,10 @@
package chess.view.DDDrender.world;
import java.io.Closeable;
import chess.view.DDDrender.Renderer;
public abstract class Entity {
public abstract class Entity implements Closeable{
public abstract void render(Renderer renderer);

View File

@@ -1,5 +1,7 @@
package chess.view.DDDrender.world;
import java.io.IOException;
import org.joml.Vector3f;
import chess.view.DDDrender.DDDModel;
@@ -36,4 +38,9 @@ public class ModelEntity extends Entity {
this.rotation = rotation;
}
@Override
public void close() throws IOException {
this.model.close();
}
}

View File

@@ -1,5 +1,7 @@
package chess.view.DDDrender.world;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -7,7 +9,7 @@ import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
public class World {
public class World implements Closeable{
/** Renderable entity list */
private final List<Entity> entites;
@@ -55,4 +57,11 @@ public class World {
return entites;
}
@Override
public void close() throws IOException {
for (Entity entity : entites) {
entity.close();
}
}
}