/*
 *   EZ GL2D Fonts Example
 *   Source is provided "as is" without express or implied warranty of any kind.
 * 
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

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

namespace EZGL2DexampleFont
{
    public class GL2Dexample : GameWindow  // inherit the GameWindow class
    {
        const int SCREEN_WIDTH = 800;       // screen dimension
        const int SCREEN_HEIGHT = 600;

        int cubeDisplayList;                // out display list 
        
        int frame;

        FontGL fontProto = new FontGL();
        FontGL fontBig = new FontGL();
        FontGL fontDigi = new FontGL();

        GL2D.SpriteGL texture;  // texture for our cube

        // Init screen
        public GL2Dexample()
            : base(SCREEN_WIDTH, SCREEN_HEIGHT)
        {

        }

        // this is where you would initialize everything
        // like loading textures, setting up OpenGL screen, etc.
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            VSync = VSyncMode.On;                           // force vertical synching
            Title = "EZGL2D is too easy to use it hurts!";  // set the title
            //WindowBorder = WindowBorder.Hidden;             // hide the window

            initCubeDisplayList();      // initialize our display list

            // Load our fonts
            fontProto.loadFont("data/EZGL2DexampleFont/protofont.png", 32, 32, (int)TextureMinFilter.Nearest);
            fontBig.loadFont("data/EZGL2DexampleFont/bigfont.png", 64, 64, (int)TextureMinFilter.Linear);
            fontDigi.loadFont("data/EZGL2DexampleFont/digifont.png", 16, 16, (int)TextureMinFilter.Nearest);

            // Loar our texture
            texture = GL2D.LoadSprite("data/EZGL2DexampleFont/cubetexture.bmp", (int)TextureMinFilter.Linear);

        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL2D.Screen2D(Width, Height);  // reset the screen every resize
        }

        // this is where all your game logic
        // should be done. (Input, Entity movement, collisions, etc)
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            
            frame++;

            // get input
            if (Keyboard[OpenTK.Input.Key.Escape])
            {
                GL.DeleteLists(cubeDisplayList, 1);
                this.Exit();
                return;
            }
        }

        // This is where all rendering/drawing should be done
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            
            GL2D.ClearScreen();   // clear the screen

            // Draw the single moving textured cube
            GL.Color4(Color.White);
            GL.Enable(EnableCap.DepthTest);  // unless you're doing translucent effects enable this
            GL.BindTexture(TextureTarget.Texture2D, texture.textureID); // yes, we can use out sprite as a texture :)
            GL.PushMatrix();
                GL.Translate(Math.Sin(frame * 0.02), Math.Cos(frame * 0.07), Math.Cos(frame* 0.002) + Math.Sin(frame * 0.01));
                GL.Rotate(frame * 2, 1, 0, 0);
                GL.Rotate(frame * 2, 0, 1, 0);
                GL.Rotate(frame * 2, 0, 0, 1);
                DrawCube();
            GL.PopMatrix();
 
            GL2D.Begin2D();         // start 2D drawing mode
                fontBig.printCenter(10, 100, "EZ GL2D", Width);
                fontProto.printCenterSine(10, 230, "http://rel.phatcode.net", Width, 20, 2, frame*3);   // Wavy font
                GL.Color4(Color.Blue);
                fontProto.printCenterSine(10, 270, "http://rel.phatcode.net", Width, 20, 2, frame * 3); // wavy font in blue
                GL.Color4(Color.Red);
                fontBig.printCenterSine(10, 400, "--2D + 3D--", Width, 30, 1, frame * -13);             // red big fonts

                // moving a text dynamically
                double dx = Math.Sin(frame * 0.005) + Math.Cos(frame * 0.005) + Math.Sin(frame * 0.032);
                double dy = Math.Cos(frame * 0.005) + Math.Sin(frame * 0.005) + Math.Cos(frame * 0.032);
                String me = " RELMINATOR ";
                GL.Color4(Color.Azure);
                fontDigi.print(Width / 2 + (int)(dx * 200) - me.Length, Height / 2 + (int)(dy * 100), " RELMINATOR ");
            GL2D.End2D();

            this.SwapBuffers();     // copy what we draw to screen

            Thread.Sleep(1);        // sleep so that we don't hog CPU

        }

        // Draws a single cube
        private void DrawCube()
        {
            GL.CallList(cubeDisplayList);
        }

        // initializes our display list
        private void initCubeDisplayList()
        {
            cubeDisplayList = GL.GenLists(1);
            GL.NewList(cubeDisplayList, ListMode.Compile);

            GL.Begin(PrimitiveType.Quads);

            // Front Face
            GL.TexCoord2(0, 0); GL.Vertex3(-1, -1, 1);
            GL.TexCoord2(1, 0); GL.Vertex3(1, -1, 1);
            GL.TexCoord2(1, 1); GL.Vertex3(1, 1, 1);
            GL.TexCoord2(0, 1); GL.Vertex3(-1, 1, 1);
            // Back Face
            GL.TexCoord2(1, 0); GL.Vertex3(-1, -1, -1);
            GL.TexCoord2(1, 1); GL.Vertex3(-1, 1, -1);
            GL.TexCoord2(0, 1); GL.Vertex3(1, 1, -1);
            GL.TexCoord2(0, 0); GL.Vertex3(1, -1, -1);
            // Top Face
            GL.TexCoord2(0, 1); GL.Vertex3(-1, 1, -1);
            GL.TexCoord2(0, 0); GL.Vertex3(-1, 1, 1);
            GL.TexCoord2(1, 0); GL.Vertex3(1, 1, 1);
            GL.TexCoord2(1, 1); GL.Vertex3(1, 1, -1);
            // Bottom Face
            GL.TexCoord2(1, 1); GL.Vertex3(-1, -1, -1);
            GL.TexCoord2(0, 1); GL.Vertex3(1, -1, -1);
            GL.TexCoord2(0, 0); GL.Vertex3(1, -1, 1);
            GL.TexCoord2(1, 0); GL.Vertex3(-1, -1, 1);
            // Right face
            GL.TexCoord2(1, 0); GL.Vertex3(1, -1, -1);
            GL.TexCoord2(1, 1); GL.Vertex3(1, 1, -1);
            GL.TexCoord2(0, 1); GL.Vertex3(1, 1, 1);
            GL.TexCoord2(0, 0); GL.Vertex3(1, -1, 1);
            // Left Face
            GL.TexCoord2(0, 0); GL.Vertex3(-1, -1, -1);
            GL.TexCoord2(1, 0); GL.Vertex3(-1, -1, 1);
            GL.TexCoord2(1, 1); GL.Vertex3(-1, 1, 1);
            GL.TexCoord2(0, 1); GL.Vertex3(-1, 1, -1);
            
            GL.End();

            GL.EndList();

        }

        [STAThread]
        public static void Main()
        {
            using (GL2Dexample example = new GL2Dexample())
            {
                example.Run(60);    // run the game at 60 frames per second
            }
        }

    }
}
