Skip to content

Commit fe14b95

Browse files
committed
Cleanup Texture class
Move texture cleanup into Texture class Rename height -> length
1 parent 2f0a21a commit fe14b95

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/engine/Mesh.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ public Mesh(float[] posArray, int[] indexArray, float[] coordArray, float[] norm
103103
public void prepare(Mesh lastMesh) {
104104
if (this == lastMesh)
105105
return;
106-
if (this.isTextured()) {
107-
glActiveTexture(GL_TEXTURE0);
108-
glBindTexture(GL_TEXTURE_2D, this.texture.getId());
109-
}
106+
if (this.isTextured())
107+
this.texture.prepare();
110108
glBindVertexArray(this.vaoId);
111109
}
112110

@@ -126,7 +124,8 @@ public void restore(Mesh nextMesh) {
126124
protected void deleteVbos() {
127125
// Delete the VBO
128126
glBindBuffer(GL_ARRAY_BUFFER, 0);
129-
this.vboIdList.stream().forEach(id -> glDeleteBuffers(id));
127+
for (int id : this.vboIdList)
128+
glDeleteBuffers(id);
130129
}
131130

132131
protected void disableVao() {
@@ -143,10 +142,8 @@ protected void deleteVao() {
143142
public void cleanup(boolean cleanupTexture) {
144143
this.disableVao();
145144
this.deleteVbos();
146-
if (cleanupTexture && this.isTextured()) {
145+
if (cleanupTexture && this.isTextured())
147146
this.texture.cleanup();
148-
this.texture = null;
149-
}
150147
this.deleteVao();
151148
}
152149
}

src/engine/TextItem.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private Mesh buildMesh(Texture texture) {
4747
List<Integer> indexList = new ArrayList<>();
4848

4949
float width = (float) texture.getWidth() / this.fontCols;
50-
float height = (float) texture.getHeight() / this.fontRows;
50+
float length = (float) texture.getLength() / this.fontRows;
5151

5252
int currentCol = 0;
5353
int currentRow = 0;
@@ -66,31 +66,31 @@ private Mesh buildMesh(Texture texture) {
6666

6767
// Left Top vertex
6868
posList.add(currentCol*width); // x
69-
posList.add(currentRow*height); // y
69+
posList.add(currentRow*length); // y
7070
posList.add(ZPOS); // z
7171
coordList.add((float) fontCol / this.fontCols);
7272
coordList.add((float) fontRow / this.fontRows);
7373
indexList.add(currentIndex*VERTICES_PER_QUAD + 0);
7474

7575
// Left Bottom vertex
7676
posList.add(currentCol*width); // x
77-
posList.add(currentRow*height + height); // y
77+
posList.add(currentRow*length + length); // y
7878
posList.add(ZPOS); // z
7979
coordList.add((float) fontCol / this.fontCols);
8080
coordList.add((float) (fontRow + 1) / this.fontRows);
8181
indexList.add(currentIndex*VERTICES_PER_QUAD + 1);
8282

8383
// Right Bottom vertex
8484
posList.add(currentCol*width + width); // x
85-
posList.add(currentRow*height + height); // y
85+
posList.add(currentRow*length + length); // y
8686
posList.add(ZPOS); // z
8787
coordList.add((float) (fontCol + 1) / this.fontCols);
8888
coordList.add((float) (fontRow + 1) / this.fontRows);
8989
indexList.add(currentIndex*VERTICES_PER_QUAD + 2);
9090

9191
// Right Top vertex
9292
posList.add(currentCol*width + width); // x
93-
posList.add(currentRow*height); // y
93+
posList.add(currentRow*length); // y
9494
posList.add(ZPOS); // z
9595
coordList.add((float) (fontCol + 1) / this.fontCols);
9696
coordList.add((float) fontRow / this.fontRows);

src/engine/Texture.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,42 @@
88
import java.nio.*;
99
import org.lwjgl.system.*;
1010

11-
import static org.lwjgl.opengl.GL11.*;
12-
import static org.lwjgl.opengl.GL30.glGenerateMipmap;
11+
import static org.lwjgl.opengl.GL30.*;
1312
import static org.lwjgl.system.MemoryUtil.*;
1413
import static org.lwjgl.stb.STBImage.*;
1514

1615
public class Texture {
1716

1817
private int id;
1918
private int width;
20-
private int height;
19+
private int length;
2120

2221
public Texture(String fileName) throws Exception {
2322
this.loadTexture(fileName);
2423
}
25-
public Texture(int id, int width, int height) throws Exception {
24+
public Texture(int id, int width, int length) throws Exception {
2625
this.id = id;
2726
this.width = width;
28-
this.height = height;
27+
this.length = length;
2928
}
3029

3130
public int getId() { return this.id; }
3231
public int getWidth() { return this.width; }
33-
public int getHeight() { return this.height; }
32+
public int getLength() { return this.length; }
3433

3534
public void bind() {
3635
glBindTexture(GL_TEXTURE_2D, this.id);
3736
}
37+
public void prepare() {
38+
glActiveTexture(GL_TEXTURE0);
39+
this.bind();
40+
}
3841
public void cleanup() {
3942
glDeleteTextures(this.id);
4043
}
4144

4245
private void loadTexture(String fileName) throws Exception {
43-
ByteBuffer image = Utils.loadImage(fileName, (width, height) -> { this.width = width; this.height = height; });
46+
ByteBuffer image = Utils.loadImage(fileName, (w, l) -> { this.width = w; this.length = l; });
4447

4548
// Create a new OpenGL texture
4649
int textureId = glGenTextures();
@@ -55,7 +58,7 @@ private void loadTexture(String fileName) throws Exception {
5558
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
5659

5760
// Upload the texture data
58-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
61+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.length, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
5962
// Generate Mip Map
6063
glGenerateMipmap(GL_TEXTURE_2D);
6164

0 commit comments

Comments
 (0)