v0.4.2: decoupled texture creation from opengl binding

This commit is contained in:
Evan Debenham 2016-08-24 23:40:39 -04:00 committed by Evan Debenham
parent 36aa02de2b
commit c137a465c5
3 changed files with 44 additions and 25 deletions

View File

@ -42,11 +42,10 @@ public class SmartTexture extends Texture {
public Atlas atlas; public Atlas atlas;
protected SmartTexture( ) { protected SmartTexture( ) {
super();
//useful for subclasses which want to manage their own texture data //useful for subclasses which want to manage their own texture data
// in cases where android.graphics.bitmap isn't fast enough. // in cases where android.graphics.bitmap isn't fast enough.
//subclasses which use this MUST also override reload() //subclasses which use this MUST also override some mix of reload/generate/bind
} }
public SmartTexture( Bitmap bitmap ) { public SmartTexture( Bitmap bitmap ) {
@ -55,21 +54,36 @@ public class SmartTexture extends Texture {
public SmartTexture( Bitmap bitmap, int filtering, int wrapping, boolean premultiplied ) { public SmartTexture( Bitmap bitmap, int filtering, int wrapping, boolean premultiplied ) {
super(); this.bitmap = bitmap;
width = bitmap.getWidth();
bitmap( bitmap, premultiplied ); height = bitmap.getHeight();
filter( filtering, filtering ); this.fModeMin = this.fModeMax = filtering;
wrap( wrapping, wrapping ); this.wModeH = this.wModeV = wrapping;
this.premultiplied = premultiplied;
} }
@Override
protected void generate() {
super.generate();
bitmap( bitmap, premultiplied );
filter( fModeMin, fModeMax );
wrap( wModeH, wModeV );
}
@Override @Override
public void filter(int minMode, int maxMode) { public void filter(int minMode, int maxMode) {
fModeMin = minMode;
fModeMax = maxMode;
if (id != -1)
super.filter( fModeMin = minMode, fModeMax = maxMode); super.filter( fModeMin = minMode, fModeMax = maxMode);
} }
@Override @Override
public void wrap( int s, int t ) { public void wrap( int s, int t ) {
wModeH = s;
wModeV = t;
if (id != -1)
super.wrap( wModeH = s, wModeV = t ); super.wrap( wModeH = s, wModeV = t );
} }
@ -91,9 +105,8 @@ public class SmartTexture extends Texture {
} }
public void reload() { public void reload() {
id = new SmartTexture( bitmap, NEAREST, CLAMP, premultiplied ).id; id = -1;
filter( fModeMin, fModeMax ); generate();
wrap( wModeH, wModeV );
} }
@Override @Override

View File

@ -38,24 +38,25 @@ public class Texture {
public static final int MIRROR = GLES20.GL_MIRRORED_REPEAT; public static final int MIRROR = GLES20.GL_MIRRORED_REPEAT;
public static final int CLAMP = GLES20.GL_CLAMP_TO_EDGE; public static final int CLAMP = GLES20.GL_CLAMP_TO_EDGE;
public int id; public int id = -1;
private static int bound_id = 0; //id of the currently bound texture private static int bound_id = 0; //id of the currently bound texture
public boolean premultiplied = false; public boolean premultiplied = false;
public Texture() { protected void generate(){
int[] ids = new int[1]; int[] ids = new int[1];
GLES20.glGenTextures( 1, ids, 0 ); GLES20.glGenTextures( 1, ids, 0 );
id = ids[0]; id = ids[0];
bind();
} }
public static void activate( int index ) { public static void activ1ate( int index ) {
GLES20.glActiveTexture( GLES20.GL_TEXTURE0 + index ); GLES20.glActiveTexture( GLES20.GL_TEXTURE0 + index );
} }
public void bind() { public void bind() {
if (id == -1){
generate();
}
if (id != bound_id) { if (id != bound_id) {
GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, id ); GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, id );
bound_id = id; bound_id = id;

View File

@ -135,21 +135,25 @@ public class FogOfWar extends Image {
order( ByteOrder.nativeOrder() ). order( ByteOrder.nativeOrder() ).
asIntBuffer(); asIntBuffer();
filter( Texture.LINEAR, Texture.LINEAR );
TextureCache.add( FogOfWar.class, this ); TextureCache.add( FogOfWar.class, this );
} }
@Override @Override
public void reload() { protected void generate() {
int[] ids = new int[1]; int[] ids = new int[1];
GLES20.glGenTextures( 1, ids, 0 ); GLES20.glGenTextures( 1, ids, 0 );
id = ids[0]; id = ids[0];
filter( Texture.LINEAR, Texture.LINEAR ); }
@Override
public void reload() {
generate();
update(); update();
} }
public void update(){ public void update(){
bind(); bind();
filter( Texture.LINEAR, Texture.LINEAR );
pixels.position(0); pixels.position(0);
GLES20.glTexImage2D( GLES20.glTexImage2D(
GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_2D,
@ -166,6 +170,7 @@ public class FogOfWar extends Image {
//allows partially updating the texture //allows partially updating the texture
public void update(int top, int bottom){ public void update(int top, int bottom){
bind(); bind();
filter( Texture.LINEAR, Texture.LINEAR );
pixels.position(top*width); pixels.position(top*width);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D,
0, 0,