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 EZGL2DexampleParticles
{
    public class GL2Dexample : GameWindow    // inherit the GameWindow class
    {
        const int SCREEN_WIDTH = 800;        // screen dimensions
        const int SCREEN_HEIGHT = 600;

        GL2D.SpriteGL bulletSprite;          // our bullet texture

        ParticleSystem particles = new ParticleSystem(2047);    // Particle system

        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
            
            GL2D.Screen2D(Width, Height);     // init our OpenGL screen

            bulletSprite = GL2D.LoadSprite("data/EZGL2DexampleParticles/bullet.png", (int)TextureMinFilter.Linear);    // load the texture

        }

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

            // move/kill our particles
            particles.update(Width, Height);

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

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL2D.ClearScreen();   // clear the screen

            GL2D.Begin2D();         // start 2D drawing mode

            // make the background greenish-blue
            GL.Color4(0.1f, 0.8f, 0.4f, 1.0f);
            // fill the screen with the color by drawing a filled box.
            GL2D.BoxFilled(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

            // draw our particles in Black for kicks
            GL.Color4(0.0f, 0.0f, 0.0f, 1.0f);
            particles.draw(bulletSprite);
            
            GL2D.End2D();           // end 2D drawing mode

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

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


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

    }
}
