using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectXEksempel01
{
    public partial class Form1 : Form
    {
        // Global variables for this project
        private Device device = null; // Rendering device
        CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[3];

        public Form1() // Constructor
        {
            // Set the initial size of the form
            this.ClientSize = new System.Drawing.Size(800, 600);
            // And its caption
            this.Text = "DirectX Eksempel 1";
        }

        public void CreateVertices()
        {
            int i = 0;
            vertices[i++] = new CustomVertex.TransformedColored(400f, 100f, 0, 1f, System.Drawing.Color.Red.ToArgb());
            vertices[i++] = new CustomVertex.TransformedColored(700f, 500f, 0, 1f, System.Drawing.Color.Green.ToArgb());
            vertices[i++] = new CustomVertex.TransformedColored(100f, 500f, 0, 1f, System.Drawing.Color.Yellow.ToArgb());
        }

        public void Render()
        {
            if (device == null)
                return;

            //Clear the backbuffer to a blue color 
            device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            device.VertexFormat = CustomVertex.TransformedColored.Format;
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
            //End the scene
            device.EndScene();
            device.Present();
        }

        public void InitializeGraphics()
        {
            // Definering af device til DirectX Rendering
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this,
                CreateFlags.SoftwareVertexProcessing, presentParams);
            //device.RenderState.CullMode = Cull.None;
            // Opbygning af vertex tabel
            CreateVertices();
        }

        static void Main() // Entry point
        {
            using (Form1 engine = new Form1())
            {
                engine.InitializeGraphics();
                engine.Show();

                // While the form is still valid, render and process messages
                while (engine.Created)
                {
                    engine.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}