import com.cult3d.Cult;
import com.cult3d.world.Texture;
import com.cult3d.world.NameNotFoundException;

// To change background in Cult3D you must have a polygon from your
// modelling application which contains the texture/textures you 
// want to change the background to.
// 
public class BGTest implements com.cult3d.Cult3DScript, Runnable
{
	private Texture background;
	private boolean textureFound;
	private boolean finishedLoading = false;

	public BGTest()
	{
		// Make a new thread to load resources in
		// Otherwise there might be moment 22, (everything are waiting for everything)
		new Thread(this).start();
	}

	public void run()
	{
		finishedLoading = false;

		try
		{
			background = new Texture("OnePixelSquare");
			textureFound = true;
		}
		catch (NameNotFoundException e)
		{
			textureFound = false;
			e.printStackTrace();
		}

		finishedLoading = true;
	}

	// Put on the background, tiled
	public void tile(String s)
	{
		if (loadingError())
		{
			return;
		}

		Cult.setBackground(background, 0);
	}

	// Put on the backgound, stretched
	public void stretch(String s)
	{
		if (loadingError())
		{
			return;
		}

		Cult.setBackground(background, 1);
	}

	private boolean loadingError()
	{
		if (background == null)
		{
			System.out.println("Warning");

			if (!finishedLoading)
			{
				System.out.println("The background texture has not yet been fully loaded");
			}
			else if (!textureFound)
			{
				System.out.println("The texture was not found");
			}

			System.out.println("");

			return true;
		}

		return false;
	}

	public void cult3dDestroy()
	{
	}
}



