Skip to content

Commit

Permalink
GLFrameBuffer: move the responsibility of disposing the texture in li…
Browse files Browse the repository at this point in the history
…ne with creating the texture

Fixes #3324
  • Loading branch information
xoppa committed Aug 5, 2015
1 parent 803de04 commit b38a1db
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -3,6 +3,7 @@
- Made possible to use any actor for tooltips.
- Improved cross-platform reflection api for annotations.
- NinePatch#scale now also scales middle patch size.
- GLFrameBuffer is now abstract, renamed setupTexture to createColorTexture, added disposeColorTexture

[1.6.4]
- TextField cursor and selection size changed. https://github.com/libgdx/libgdx/commit/2a830dea348948d2a37bd8f6338af2023fec9b09
Expand Down
21 changes: 13 additions & 8 deletions gdx/src/com/badlogic/gdx/graphics/glutils/FloatFrameBuffer.java
Expand Up @@ -16,9 +16,8 @@

package com.badlogic.gdx.graphics.glutils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
Expand All @@ -37,15 +36,21 @@ public FloatFrameBuffer (int width, int height, boolean hasDepth) {
super(null, width, height, hasDepth);
}

/** Override this method in a derived class to set up the backing texture as you like. */
protected void setupTexture () {
@Override
protected Texture createColorTexture () {
FloatTextureData data = new FloatTextureData(width, height);
colorTexture = new Texture(data);
Texture result = new Texture(data);
if (Gdx.app.getType() == ApplicationType.Desktop || Gdx.app.getType() == ApplicationType.Applet)
colorTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
else
// no filtering for float textures in OpenGL ES
colorTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
colorTexture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
result.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
return result;
}

@Override
protected void disposeColorTexture (Texture colorTexture) {
colorTexture.dispose();
}
}
15 changes: 10 additions & 5 deletions gdx/src/com/badlogic/gdx/graphics/glutils/FrameBuffer.java
Expand Up @@ -63,12 +63,17 @@ public FrameBuffer (Pixmap.Format format, int width, int height, boolean hasDept
super(format, width, height, hasDepth, hasStencil);
}

/** Override this method in a derived class to set up the backing texture as you like. */
@Override
protected void setupTexture () {
colorTexture = new Texture(width, height, format);
colorTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
colorTexture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
protected Texture createColorTexture () {
Texture result = new Texture(width, height, format);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
return result;
}

@Override
protected void disposeColorTexture (Texture colorTexture) {
colorTexture.dispose();
}

/** See {@link GLFrameBuffer#unbind()} */
Expand Down
15 changes: 10 additions & 5 deletions gdx/src/com/badlogic/gdx/graphics/glutils/FrameBufferCubemap.java
Expand Up @@ -87,12 +87,17 @@ public FrameBufferCubemap (Pixmap.Format format, int width, int height, boolean
super(format, width, height, hasDepth, hasStencil);
}

/** Override this method in a derived class to set up the backing texture as you like. */
@Override
protected void setupTexture () {
colorTexture = new Cubemap(width, height, width, format);
colorTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
colorTexture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
protected Cubemap createColorTexture () {
Cubemap result = new Cubemap(width, height, width, format);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
return result;
}

@Override
protected void disposeColorTexture (Cubemap colorTexture) {
colorTexture.dispose();
}

/** Makes the frame buffer current so everything gets drawn to it, must be followed by call to either {@link #nextSide()} or
Expand Down
17 changes: 9 additions & 8 deletions gdx/src/com/badlogic/gdx/graphics/glutils/GLFrameBuffer.java
Expand Up @@ -30,7 +30,6 @@
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;

/** <p>
* Encapsulates OpenGL ES 2.0 frame buffer objects. This is a simple helper class which should cover most FBO uses. It will
Expand All @@ -48,7 +47,7 @@
* </p>
*
* @author mzechner, realitix */
public class GLFrameBuffer<T extends GLTexture> implements Disposable {
public abstract class GLFrameBuffer<T extends GLTexture> implements Disposable {
/** the frame buffers **/
private final static Map<Application, Array<GLFrameBuffer>> buffers = new HashMap<Application, Array<GLFrameBuffer>>();

Expand Down Expand Up @@ -114,9 +113,10 @@ public GLFrameBuffer (Pixmap.Format format, int width, int height, boolean hasDe
}

/** Override this method in a derived class to set up the backing texture as you like. */
protected void setupTexture () {
throw new GdxRuntimeException("Texture must be setup");
}
protected abstract T createColorTexture ();

/** Override this method in a derived class to dispose the backing texture as you like. */
protected abstract void disposeColorTexture (T colorTexture);

private void build () {
GL20 gl = Gdx.gl20;
Expand All @@ -133,7 +133,7 @@ private void build () {
}
}

setupTexture();
colorTexture = createColorTexture();

framebufferHandle = gl.glGenFramebuffer();

Expand Down Expand Up @@ -176,7 +176,7 @@ private void build () {
int result = gl.glCheckFramebufferStatus(GL20.GL_FRAMEBUFFER);

if (result != GL20.GL_FRAMEBUFFER_COMPLETE) {
colorTexture.dispose();
disposeColorTexture(colorTexture);

if (hasDepth) gl.glDeleteRenderbuffer(depthbufferHandle);

Expand All @@ -201,7 +201,8 @@ private void build () {
public void dispose () {
GL20 gl = Gdx.gl20;

colorTexture.dispose();
disposeColorTexture(colorTexture);

if (hasDepth) gl.glDeleteRenderbuffer(depthbufferHandle);

if (hasStencil) gl.glDeleteRenderbuffer(stencilbufferHandle);
Expand Down

0 comments on commit b38a1db

Please sign in to comment.