Example #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //turn this on for help with leaky stuff
            //Configuration.EnableObjectTracking	=true;

            GraphicsDevice gd = new GraphicsDevice("Collada Conversion Tool",
                                                   FeatureLevel.Level_9_3, 0.1f, 3000f);

            //save renderform position
            gd.RendForm.DataBindings.Add(new System.Windows.Forms.Binding("Location",
                                                                          Settings.Default,
                                                                          "MainWindowPos", true,
                                                                          System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

            gd.RendForm.Location = Settings.Default.MainWindowPos;

            SharedForms.ShaderCompileHelper.mTitle = "Compiling Shaders...";

            StuffKeeper sk = new StuffKeeper();

            sk.eCompileNeeded += SharedForms.ShaderCompileHelper.CompileNeededHandler;
            sk.eCompileDone   += SharedForms.ShaderCompileHelper.CompileDoneHandler;

            sk.Init(gd, ".");

            MatLib matLib = new MatLib(gd, sk);

            matLib.InitCelShading(1);
            matLib.GenerateCelTexturePreset(gd.GD,
                                            gd.GD.FeatureLevel == FeatureLevel.Level_9_3, false, 0);
            matLib.SetCelTexture(0);

            PlayerSteering pSteering    = SetUpSteering();
            Input          inp          = SetUpInput();
            Random         rand         = new Random();
            CommonPrims    comPrims     = new CommonPrims(gd, sk);
            bool           bMouseLookOn = false;

            //set up post processing module
            PostProcess post = new PostProcess(gd, matLib, "Post.fx");

            EventHandler actHandler = new EventHandler(
                delegate(object s, EventArgs ea)
                { inp.ClearInputs(); });

            EventHandler <EventArgs> deActHandler = new EventHandler <EventArgs>(
                delegate(object s, EventArgs ea)
            {
                gd.SetCapture(false);
                bMouseLookOn = false;
            });

            gd.RendForm.Activated      += actHandler;
            gd.RendForm.AppDeactivated += deActHandler;

            int resx = gd.RendForm.ClientRectangle.Width;
            int resy = gd.RendForm.ClientRectangle.Height;

            AnimForm ss = SetUpForms(gd.GD, matLib, sk, comPrims);

            Vector3 pos      = Vector3.One * 5f;
            Vector3 lightDir = -Vector3.UnitY;

            UpdateTimer time = new UpdateTimer(true, false);

            time.SetFixedTimeStepSeconds(1f / 60f);             //60fps update rate
            time.SetMaxDeltaSeconds(MaxTimeDelta);

            List <Input.InputAction> acts = new List <Input.InputAction>();

            RenderLoop.Run(gd.RendForm, () =>
            {
                if (!gd.RendForm.Focused)
                {
                    Thread.Sleep(33);
                }

                gd.CheckResize();

                if (bMouseLookOn && gd.RendForm.Focused)
                {
                    gd.ResetCursorPos();
                }

                //Clear views
                gd.ClearViews();

                time.Stamp();
                while (time.GetUpdateDeltaSeconds() > 0f)
                {
                    acts = UpdateInput(inp, gd,
                                       time.GetUpdateDeltaSeconds(), ref bMouseLookOn);
                    if (!gd.RendForm.Focused)
                    {
                        acts.Clear();
                        bMouseLookOn = false;
                        gd.SetCapture(false);
                        inp.UnMapAxisAction(Input.MoveAxis.MouseYAxis);
                        inp.UnMapAxisAction(Input.MoveAxis.MouseXAxis);
                    }

                    Vector3 deltaMove = pSteering.Update(pos,
                                                         gd.GCam.Forward, gd.GCam.Left, gd.GCam.Up, acts);

                    deltaMove *= 200f;
                    pos       -= deltaMove;

                    ChangeLight(acts, ref lightDir);

                    time.UpdateDone();
                }

                //light direction is backwards now for some strange reason
                matLib.SetParameterForAll("mLightDirection", -lightDir);

                gd.GCam.Update(pos, pSteering.Pitch, pSteering.Yaw, pSteering.Roll);

                matLib.UpdateWVP(Matrix.Identity, gd.GCam.View, gd.GCam.Projection, gd.GCam.Position);

                comPrims.Update(gd.GCam, lightDir);

                ss.RenderUpdate(time.GetRenderUpdateDeltaSeconds());

                post.SetTargets(gd, "BackColor", "BackDepth");

                post.ClearTarget(gd, "BackColor", Color.CornflowerBlue);
                post.ClearDepth(gd, "BackDepth");

                ss.Render(gd.DC);

                if (ss.GetDrawAxis())
                {
                    comPrims.DrawAxis(gd.DC);
                }

                if (ss.GetDrawBox())
                {
                    comPrims.DrawBox(gd.DC, Matrix.Identity);
                }

                if (ss.GetDrawSphere())
                {
                    comPrims.DrawSphere(gd.DC, Matrix.Identity);
                }

                gd.Present();

                acts.Clear();
            }, true);                   //true here is slow but needed for winforms events

            Settings.Default.Save();

            gd.RendForm.Activated      -= actHandler;
            gd.RendForm.AppDeactivated -= deActHandler;

            comPrims.FreeAll();
            inp.FreeAll();
            post.FreeAll(gd);
            matLib.FreeAll();

            sk.eCompileDone   -= SharedForms.ShaderCompileHelper.CompileDoneHandler;
            sk.eCompileNeeded -= SharedForms.ShaderCompileHelper.CompileNeededHandler;

            sk.FreeAll();

            //Release all resources
            gd.ReleaseAll();
        }