/**
 * @author Richard Eric M. Lope (Relminator)
 * http://rel.phatcode.net
 *
 */

package net.phatcode.rel.utils;

import javax.microedition.khronos.opengles.GL10;

public class SpriteFont
{
	private ImageAtlas fontAtlas;
	private SpriteBatcher fonts;
	int charWidth = 16;
	int charHeight = 16;
	
	public SpriteFont()
	{
		fonts = new SpriteBatcher();
	}
	
	public SpriteFont( ImageAtlas imageAtlas )
	{
		fontAtlas = imageAtlas;
		charWidth = fontAtlas.getSprite(0).width;
		charHeight = fontAtlas.getSprite(0).height;
		
		fonts = new SpriteBatcher();

	}

	public void loadAtlas( ImageAtlas imageAtlas )
	{
		fontAtlas = imageAtlas;
		charWidth = fontAtlas.getSprite(0).width;
		charHeight = fontAtlas.getSprite(0).height;

	}

	public void print( int x, int y, String text )
	{
		int len = text.length(); 
		for(int i = 0; i< len; i++ )
		{
			int index = text.charAt(i) - 32;
			fonts.sprite( x, y, SpriteGL.FLIP_NONE, fontAtlas.getSprite(index) );
			x += charWidth; 
		}
		
	}

	public void printCenter( int x, int y, int screenWidth, String text )
	{
		int len = text.length(); 
		x = (screenWidth - (len * charWidth))/2;
		for(int i = 0; i< len; i++ )
		{
			int index = text.charAt(i) - 32;
			fonts.sprite( x, y, SpriteGL.FLIP_NONE, fontAtlas.getSprite(index) );
			x += charWidth; 
		}
		
	}

	public void render( GL10 gl )
	{
		fonts.render( gl, fontAtlas.getTextureID() );
	}
	
	public void shutDown( GL10 gl )
	{
		fontAtlas.shutDown( gl );
	}
	
	public void setCharWidth( int charWidth )
	{
		this.charWidth = charWidth;
	}
	
	public void setCharHeight( int charHeight )
	{
		this.charHeight = charHeight;
	}
	
	public int getCharWidth()
	{
		return charWidth;
	}

	public int getCharHeight()
	{
		return charHeight;
	}
	
	public int getNumSprites()
	{
		return fonts.getNumSprites();
	}

}
