Codi simple Java 3 - Funcio addTRtoAtlas

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

text/plain sample2.txt — 2.2 KB

Continguts del fitxer

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;
			
			// Calculate current max width (TRY CATCH???)
			int total_width=0;
			for (int idx=0;idx<imagesOnAtlas.length;idx++)
			{
				if (imagesOnAtlas[idx]==null) break;
				else
				{
					total_width+=imagesOnAtlas[idx].getWidth();
				}
			}
			
			
			// Maybe can allocate here.
			if ((1024-total_width)>current_Width)
			{
				// Variables a calcular en funcion del width i height del textureRegion
				int tX=total_width;
				int tY=0;
				
				tTRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(tBTAtlas, myActivity,tPath, tX, tY);
				
				// Add to array
				imagesOnAtlas[indexTR]=tTRegion;
				indexTR++;
				Log.d("------- MARK -------->","Ponlo en:" + total_width);
			}
			else
			{
				int Ymax=getMaxHeightOnAtlas(imagesOnAtlas);
				
				
				//if not
				tTRegion=null;
			}
		
			
		//-----------------------------
		
		return tTRegion;
	}
	
	//Find maximum (largest) value in array using loop  
	private static int getMaxHeightOnAtlas(TextureRegion[] sampleTR){  
	    int maxValue =  sampleTR[0].getHeight();  
	    for(int i=1;i<sampleTR.length;i++){ 
	    	if (sampleTR[i]!=null){
		        if(sampleTR[i].getHeight() > maxValue){  
		            maxValue = sampleTR[i].getHeight();  
		        }
	    	}
	    	else break;
	        
	    }  
	    return maxValue;  
	}  
	
}