Esempio n. 1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            GraphicsDevice gd = new GraphicsDevice("Audio Test Program",
                                                   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;

            //used to have a hard coded path here for #debug
            //but now can just use launch.json to provide it
            string gameRootDir = ".";

            Audio aud = new Audio();

            aud.LoadAllSounds(gameRootDir + "/Audio/SoundFX");
            aud.LoadAllSounds(gameRootDir + "/Audio/Music");

            List <string> sounds = aud.GetSoundList();

            int curSound = 0;

            Emitter emitter = Audio.MakeEmitter(Vector3.Zero);

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

            StuffKeeper sk = new StuffKeeper();

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

            sk.Init(gd, gameRootDir);

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

            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;

            MatLib fontMats = new MatLib(gd, sk);

            fontMats.CreateMaterial("Text");
            fontMats.SetMaterialEffect("Text", "2D.fx");
            fontMats.SetMaterialTechnique("Text", "Text");

            List <string> fonts = sk.GetFontList();

            ScreenText st = new ScreenText(gd.GD, fontMats, fonts[0], 1000);

            Matrix textProj = Matrix.OrthoOffCenterLH(0, resx, resy, 0, 0.1f, 5f);

            Vector4 color = Vector4.UnitX + (Vector4.UnitW * 0.95f);

            //string indicators for various statusy things
            st.AddString(fonts[0], "P - Play2D   L - Play at Emitter   [] - Prev/Next Sound  E - Set Emitter Pos to Camera Pos",
                         "Instructions", color, Vector2.UnitX * 20f + Vector2.UnitY * 520f, Vector2.One);
            st.AddString(fonts[0], "Stuffs", "CurrentSound",
                         color, Vector2.UnitX * 20f + Vector2.UnitY * 540f, Vector2.One);
            st.AddString(fonts[0], "Stuffs", "EmitterPosition",
                         color, Vector2.UnitX * 20f + Vector2.UnitY * 560f, Vector2.One);
            st.AddString(fonts[0], "Stuffs", "PosStatus",
                         color, Vector2.UnitX * 20f + Vector2.UnitY * 580f, Vector2.One);

            Vector3     pos      = Vector3.One * 5f;
            Vector3     lightDir = -Vector3.UnitY;
            UpdateTimer time     = new UpdateTimer(false, 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();
                }

                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 moveDelta = pSteering.Update(pos, gd.GCam.Forward,
                                                         gd.GCam.Left, gd.GCam.Up, acts);

                    moveDelta *= 200f;

                    pos -= moveDelta;

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

                    CheckInputKeys(acts, aud, ref curSound, sounds, emitter, gd.GCam.Position);

                    //update status text
                    st.ModifyStringText(fonts[0], "Current Sound: " + sounds[curSound], "CurrentSound");
                    st.ModifyStringText(fonts[0], "Emitter Pos: " + emitter.Position.X + ", " + emitter.Position.Y + ", " + emitter.Position.Z, "EmitterPosition");
                    st.ModifyStringText(fonts[0], "Cam Pos: " + gd.GCam.Position +
                                        ", Sounds Playing: " + aud.GetNumInstances(), "PosStatus");
                    time.UpdateDone();
                }


                st.Update(gd.DC);

                comPrims.Update(gd.GCam, lightDir);

                aud.Update(gd.GCam);

                //Clear views
                gd.ClearViews();

                comPrims.DrawAxis(gd.DC);

                st.Draw(gd.DC, Matrix.Identity, textProj);

                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;

            //Release all resources
            st.FreeAll();
            fontMats.FreeAll();
            comPrims.FreeAll();
            inp.FreeAll();

            sk.eCompileDone   -= SharedForms.ShaderCompileHelper.CompileDoneHandler;
            sk.eCompileNeeded -= SharedForms.ShaderCompileHelper.CompileNeededHandler;
            sk.FreeAll();

            aud.FreeAll();
            gd.ReleaseAll();
        }