Codi Alumnes 1 Java 3 - Funcio addTRtoAtlas

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

	private static final int maxTR=50;
	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();
		
		TextureRegion tTRegion;
		
		int total_width=0;
		int x=0;
		int yMax=0;
		int tY=0;
		int tX=0;
				
		//Recorremos el texture atlas para saber cual es la posible posición de la siguiente imagen.
		for (int idx=0;idx<imagesOnAtlas.length;idx++)
		{
			if (imagesOnAtyMaxlas[idx]==null) break;
			else
			{
				if(total_width + imagesOnAtlas[idx].getWidth() < 1024){
					total_width+=imagesOnAtlas[idx].getWidth();
					if (yMax<=imagesOnAtlas[idx].getHeight()){
						yMax=imagesOnAtlas[idx].getHeight();
					}					
				}
				else
				{
					total_width=0;
					tY+=;
					yMax=0;
				}
			}
		}
				
		tX=total_width;
				
		//Mostramos datos
		Log.d("---IMAGEN-----","Imagen: "+indexTR);
		Log.d("----Ancho-----","Ancho: " + current_Width);
		Log.d("----Alto------","Alto: " + current_Height);
		Log.d("----Pos X----","Pos X: " + tX);
		Log.d("----Pos Y----","Pos Y: " + tY);
		Log.d(" ", " ");
		
		
		//Si al añadir la nueva imagen a lo que ya hay nos pasamos, ponemos a 0 el ancho y nos quedamos con el alto del objeto mayor.
		if (total_width+current_Width>1024){
			tX=0;
			tY+=yMax;
		}
		
		//Añadirmos la imagen al TextureAtlas
		tTRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(tBTAtlas, myActivity,tPath, tX, tY);
		imagesOnAtlas[indexTR]=tTRegion;
		indexTR++;
		
		
		return tTRegion;
	}
  
}