using System;
using AgkSharp;
using AGKCore;

namespace Benchmark
{

    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Core.CreateWin32Window("Benchmark", 1024, 768, false);
            if (!Core.InitAGK())
                return;

            Agk.SetVirtualResolution(320, 480);
            Agk.SetSyncRate(0, 0);

            // arrays for sprites
            float[] speed = new float[500];
            float[] move = new float[500];

            // display a background
            Agk.CreateSprite(Agk.LoadImage("media/background1.jpg"));

            // load an image for the particles and sprites
            Agk.LoadImage(1, "media/shrapnel3.png");
            Agk.LoadImage(2, "media/ball1.png");


            // create particles off screen
            Agk.CreateParticles(1, -100, -100);

            if (Agk.GetImageExists(2) != 0)
            {
                // create a batch of 500 sprites
                for (uint i = 1; i <= 500; i++)
                {
                    Agk.CreateSprite(i, 2);
                    Agk.SetSpritePosition(i, Agk.Random(320, 2000), Agk.Random(60, 480));
                    Agk.SetSpriteColor(i, (int)Agk.Random(0, 255), (int)Agk.Random(0, 255), (int)Agk.Random(0, 255), (int)Agk.Random(50, 255));

                    speed[i - 1] = (float)Agk.Random(10, 40) / 10.0f;
                    move[i - 1] = 0.0f;
                }
            }

            int fire = 1;
            while (Core.LoopAGK())
            {
                // show statistics
                Agk.Print("Drawing set up time      = " + Agk.GetDrawingSetupTime().ToString());
                Agk.Print("Drawing time             = " + Agk.GetDrawingTime().ToString());
                Agk.Print("Sprites drawn            = " + Agk.GetManagedSpriteDrawnCount().ToString());
                Agk.Print("Particles drawn          = " + Agk.GetParticleDrawnQuadCount().ToString());
                Agk.Print("Frame rate               = " + Agk.ScreenFPS().ToString());

                // build 107 additions
                Agk.Print("Get pixels drawn         = " + Agk.GetPixelsDrawn().ToString());
                Agk.Print("Get managed sprite calls = " + Agk.GetManagedSpriteDrawCalls().ToString());

                Agk.Print(Agk.GetRawLastKey().ToString());

                // move all sprites
                for (uint i = 1; i <= 500; i++)
                {
                    float x = Agk.GetSpriteX(i);
                    float y = Agk.GetSpriteY(i);

                    y = y + Agk.Cos(move[i - 1]);
                    move[i - 1] = move[i - 1] + speed[i - 1];

                    Agk.SetSpritePosition(i, x - speed[i - 1], y);
                    Agk.SetSpriteAngle(i, Agk.GetSpriteAngle(i) + speed[i - 1]);

                    if (x < -100)
                        Agk.SetSpritePosition(i, Agk.Random(320, 2000), Agk.Random(60, 480));
                }

                // fire particles when ready
                if (fire == 1)
                {
                    // set up particles
                    Agk.SetParticlesPosition(1, Agk.Random(100, 200), Agk.Random(100, 300));

                    Agk.ResetParticleCount(1);
                    Agk.SetParticlesFrequency(1, 250);
                    Agk.SetParticlesLife(1, 3.0f);
                    Agk.SetParticlesSize(1, 64);
                    Agk.SetParticlesStartZone(1, -10, 0, 10, 0);
                    Agk.SetParticlesImage(1, 1);
                    Agk.SetParticlesDirection(1, 30, 30);
                    Agk.SetParticlesAngle(1, 360);
                    Agk.SetParticlesVelocityRange(1, 0.8f, 2.5f);
                    Agk.SetParticlesMax(1, 500);

                    Agk.AddParticlesColorKeyFrame(1, 0.0f, 0, 0, 0, 0);
                    Agk.AddParticlesColorKeyFrame(1, 0.5f, 255, 255, 0, 255);
                    Agk.AddParticlesColorKeyFrame(1, 2.8f, 255, 0, 0, 0);

                    Agk.AddParticlesForce(1, 2.0f, 2.8f, 25, -25);

                    // reset fire value
                    fire = 0;
                }

                // when the explosion has finished it is safe to fire once more
                if (Agk.GetParticlesMaxReached(1) == 1)
                    fire = 1;

                // update the screen
                Agk.Sync();
            }
            Core.CleanUp();
        }
    }
}
