using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace D3DObjekter
{
    public partial class Form1 : Form
    {
        Device device;
        Timer timer = new Timer();
        Building currentBuilding;

        public Form1()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
        public void Initialize()
        {
            PresentParameters presentationParameters = new PresentParameters();
            presentationParameters.DeviceWindow = this;
            presentationParameters.AutoDepthStencilFormat = DepthFormat.D16;
            presentationParameters.EnableAutoDepthStencil = true;
            presentationParameters.SwapEffect = SwapEffect.Discard;
            presentationParameters.Windowed = true;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentationParameters);
            
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 9.5f, 17.0f), new Vector3(), new Vector3(0, 1, 0));
            
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Direction = new Vector3(1, -1, -1);
            device.Lights[0].Enabled = true;
            device.Lights[1].Type = LightType.Directional;
            device.Lights[1].Diffuse = Color.White;
            device.Lights[1].Direction = new Vector3(-1, 1, -1);
            device.Lights[1].Enabled = true;

            currentBuilding = new Building(device, 1);
           
            timer.Interval = 20;
            timer.Tick += Render;
            timer.Enabled = true;
        }
        void Render(object sender, EventArgs e)
        {
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1, 0);
            device.BeginScene();

            currentBuilding.Render();

            device.EndScene();
            device.Present();
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    currentBuilding.Matrix.M41 += 0.1f;
                    break;
                case Keys.Right:
                    currentBuilding.Matrix.M41 -= 0.1f;
                    break;
                case Keys.Up:
                    currentBuilding.Matrix.M42 += 0.1f;
                    break;
                case Keys.Down:
                    currentBuilding.Matrix.M42 -= 0.1f;
                    break;
            }
        }
    }
}