
using System;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;


using System.Drawing;
using System.Drawing.Imaging;

namespace PlanetExpress.Tools {	
		
	public class TexClass {
		
		protected int ID;
		protected string Name;
		public TexClass(int id, string name) {
			ID = id;
			Name = name;
		}
		
		~TexClass() {
			if (Release != null)
				Release(this, new TexDestroyEventArgs(ID, Name));
		}
		
		public void Load(string fileName) {
			Load(new Bitmap(fileName));	
		}
		
		Bitmap bitmap;
		public void Load(Bitmap bmp) {
			bitmap = bmp;
			FWidth = bitmap.Width;
			FHeight = bitmap.Height;
			
			GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            
            GL.GenTextures(1, out ID);
            GL.BindTexture(TextureTarget.Texture2D, ID);

            BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D,
			              0,
			              PixelInternalFormat.Rgba,
			              data.Width,
			              data.Height,
			              0,
			              OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
			              PixelType.UnsignedByte,
			              data.Scan0);
            
            bitmap.UnlockBits(data);

            GL.TexParameter(TextureTarget.Texture2D,
			                TextureParameterName.TextureMinFilter,
			                (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D,
			                TextureParameterName.TextureMagFilter,
			                (int)TextureMagFilter.Linear);	
		}		
		public event EventHandler<TexDestroyEventArgs> Release;
		
		public int FWidth;
		public int Width {
			get { return FWidth; }
		}
		
		private int FHeight;
		public int Height {
			get { return FHeight; }
		}
		
		public void Bind() {
			GL.BindTexture(TextureTarget.Texture2D, ID);	
		}
		
		public void Render(double x,
		                   double y) {
			GL.BindTexture(TextureTarget.Texture2D, ID);
            GL.Begin(BeginMode.Quads); {
	            GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(x, y);
	            GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(x + Width, y);
	            GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(x + Width, y + Height);
	            GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(x, y + Height);
			} GL.End();
		}
		
		public void Render(double x,
		                   double y,
		                   RectangleF r) {
			
			double tx = 1.0/Width * r.Left;
			double ty = 1.0/Height * r.Top;
			double tw = 1.0/Width * r.Width;
			double th = 1.0/Height * r.Height;
			
			GL.BindTexture(TextureTarget.Texture2D, ID);
            GL.Begin(BeginMode.Quads); {
	            GL.TexCoord2(tx, ty); GL.Vertex2(x, y);
	            GL.TexCoord2(tx + tw, ty); GL.Vertex2(x + r.Width, y);
	            GL.TexCoord2(tx + tw, ty + th); GL.Vertex2(x + r.Width, y + r.Height);
	            GL.TexCoord2(tx, ty + th); GL.Vertex2(x, y + r.Height);
			} GL.End();
		}
	}
}
