Codi Alumnes 3 Java 3 - Funcio addTRtoAtlas

per Jordi Farrero darrera modificació 2020-03-25T14:52:34+01:00

public class commons {

private static final int maxTR=10;
private static TextureRegion[] imagesOnAtlas = new TextureRegion[maxTR];
private static int indexTR=0;

public static TextureRegion addToTextureAtlas(

BitmapTextureAtlas tBTAtlas,BaseGameActivity myActivity,String tPath) {

// Get the Widht & Height in internal Sample
BitmapTextureAtlas internalTA = new BitmapTextureAtlas(1024, 1024,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegion tTRegion_sample=BitmapTextureAtlasTextureRegionFactory.createFromAsset(internalTA, myActivity,tPath, 0, 0);
int current_Width= tTRegion_sample.getWidth();
int current_Height= tTRegion_sample.getHeight();

//Log.d("------- MARK -------->","Width es:" + tTRegion_sample.getWidth() + "Height es" +  tTRegion_sample.getHeight());

TextureRegion tTRegion = null;
int empiezaFilaAtlas = 0;
int acabaFilaAtlas = 0;
int total_width=0;
int Ymax = 0;

//    Calculamos la posicion donde toca colocar el siguiente TextureRegion
for (int idx=0;idx<imagesOnAtlas.length;idx++) {

if (imagesOnAtlas[idx]==null) {
empiezaFilaAtlas = acabaFilaAtlas;
acabaFilaAtlas = idx;
break;
}
else {
if (total_width + imagesOnAtlas[idx].getWidth() < 1024) total_width+=imagesOnAtlas[idx].getWidth();
else {
empiezaFilaAtlas = acabaFilaAtlas;
acabaFilaAtlas = idx;
Ymax += getMaxHeightOnAtlas(imagesOnAtlas, empiezaFilaAtlas, acabaFilaAtlas);
total_width = imagesOnAtlas[idx].getWidth();

}
}
}

boolean colocado = false;

while (!colocado) {

if (Ymax + current_Height < 1024) {
// Maybe can allocate here.
if ((total_width + current_Width) < 1024) {
// Variables a calcular en funcion del width i height del textureRegion
int tX=total_width;    //quiza haya que corregir solapamiento, poner +1
int tY=Ymax;

tTRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(tBTAtlas, myActivity,tPath, tX, tY);

// Add to array
imagesOnAtlas[indexTR]=tTRegion;
indexTR++;

colocado = true;

} else {

//if (current_Width >= 1024) break;
Ymax += getMaxHeightOnAtlas(imagesOnAtlas, empiezaFilaAtlas, acabaFilaAtlas);
total_width = 0;

}
} else colocado = true;
}
Log.d("------- MARK -------->","posicion X:" + total_width + "posicion Y" +  Ymax);
return tTRegion;
}

//Find maximum (largest) value in array using loop 
private static int getMaxHeightOnAtlas(TextureRegion[] sampleTR, int empiezaFilaAtlas, int acabaFilaAtlas){ 
int maxValue =  sampleTR[empiezaFilaAtlas].getHeight(); 
for(int i=empiezaFilaAtlas+1;i<acabaFilaAtlas;i++){
if (sampleTR[i]!=null){
if(sampleTR[i].getHeight() > maxValue){ 
maxValue = sampleTR[i].getHeight(); 
}
}
else break;           

return maxValue; 


}