Sou a: Inici / Usuaris / jordifarrero / 2012-13 :: Entorns desenvolupament :: DAW2 Tarda / Codis exemple / Codi simple Java 3 - Funcio addTRtoAtlas

Codi simple Java 3 - Funcio addTRtoAtlas

Plain Text icon sample2.txt — Plain Text, 2 kB (2255 bytes)

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;  
	}  
	
}