Exemplo n.º 1
0
        /// <summary>
        /// Create the GFX device.  Also assigns the global GFX singleton (and asserts if that singleton has 
        /// already been assigned).
        /// </summary>
        public GFXDevice()
        {
            Assert.Fatal(GFXDevice._instance == null, "GFXDevice Constructor - Error: GFXDevice singleton already exists!");

            GFXDevice._instance = this;
            _volatileBufferStorage = new GFXVolatileVertexBufferSourceManager<GFXVertexFormat.PCTTBN>(1024);
        }
 protected override void _SetupProjection(GFXDevice gfx, float aspectRatio)
 {
     // the far distance needs to be greater than the total number of objects on screen at once
     // since each object is assigned a depth value based on it's sort order in the scene
     _srs.Projection = gfx.SetOrtho(false, -0.5f * _t2dCamera.Extent.X, 0.5f * _t2dCamera.Extent.X, 0.5f * _t2dCamera.Extent.Y, -0.5f * _t2dCamera.Extent.Y, 0.1f, _camera.FarDistance);
 }
 protected virtual void _SetupWorld(GFXDevice gfx)
 {
     _srs.World.LoadIdentity();
 }
        /// <summary>
        /// Sets up the engine.  
        /// </summary>
        void _SetupEngine()
        {
            // if we have a d3d device, init video now
            if (_graphicsManager.GraphicsDevice != null)
            {
                // create gfx device
                _gfxDevice = new GFXDevice();

                // override shader profile...
                _gfxDevice.ForceUserShaderProfile = _settings.ForceUserShaderProfile;
                _gfxDevice.UserShaderProfile = _settings.UserShaderProfile;

                // initialize the device
                _gfxDevice.Init(_graphicsManager);

                _ApplyVideoProfile();
            }

            // create audio device
            if (_settings.EnableAudio && _sfxDevice == null && _settings.AudioGlobalSettingsFile != string.Empty)
            {
                TorqueConsole.Echo("Initializing audio...");

                try
                {
                    _sfxDevice = new AudioEngine(_settings.AudioGlobalSettingsFile);
                }
                catch (ArgumentException e) // catch this one so that we can continue running without needing update all of our xact files.
                {
                    _sfxDevice = null;
                    TorqueConsole.Error("TorqueEngineComponent::_SetupEngine - Failed to initialize XACT: " + e.ToString());
                }
            }

            // create gui canvas
            _canvas = new GUICanvas();
            TorqueObjectDatabase.Instance.Register(_canvas);

            TorqueConsole.Echo("Initializing GUICanvas...");

            // put a default gui on the canvas
            GUISceneview defaultContentControl = new GUISceneview();
            defaultContentControl.Name = "DefaultSceneView";
            defaultContentControl.Style = new GUIStyle();
            _canvas.SetContentControl(defaultContentControl);

            // configure time handling on game object.
            if (_settings.UseFixedTimeStep)
            {
                ProcessList.Instance.TickMS = _settings.TickMS; // use for a constant tick (with interpolation)

                if (_settings.UseInterpolation)
                {
                    // We want to interpolate between ticks, have xna give use updates whenever it has time
                    ProcessList.Instance.UseInterpolation = true;
                    Game.IsFixedTimeStep = false;
                }
                else
                {
                    // we only want to render on tick boundaries (no interpolation) so tell xna
                    // to only give us tick divisible updates.
                    ProcessList.Instance.UseInterpolation = false;
                    Game.IsFixedTimeStep = true;
                    Game.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, _settings.TickMS);
                }
            }
            else
            {
                // Variable ticks and no interpolation so tell xna to give us updates whenever it has time
                ProcessList.Instance.TickMS = 0; // use for a variable tick (no interpolation)
                ProcessList.Instance.UseInterpolation = false; // not needed, but for clarity
                Game.IsFixedTimeStep = false;
            }

            TorqueConsole.Echo("Initializing event manager...");

            TorqueEventManager.ListenEvents(_timeEvent, _UpdateSim);
            TorqueEventManager.ListenEvents(PumpEvent, InputManager.Instance.Pump);

            // set up inputs
            Torque.Platform.XGamePadDevice.EnumerateGamepads();
            #if !XBOX
            Torque.Platform.XKeyboardDevice.EnumerateKeyboards();
            Torque.Platform.XMouseDevice.EnumerateMouses();
            Microsoft.Xna.Framework.Input.Mouse.WindowHandle = Game.Window.Handle;
            #endif

            TorqueConsole.Echo("Initializing content manager...");
            // Create global content manager
            ContentManager cm = new ContentManager(this.Game.Services);
            ResourceManager.Instance.GlobalContentManager = cm;

            // make it the current CM
            ResourceManager.Instance.PushContentManager(cm);

            #if DEBUG
            // setup journaling
            if (_settings.JournalMode != TorqueJournal.JournalMode.None)
            {
                TorqueConsole.Echo("Initializing journaling...");
                bool ok;
                string err;
                TorqueJournal journal = new TorqueJournal();

                if (Settings.JournalMode == TorqueJournal.JournalMode.Play)
                {
                    err = " for reading.";
                    ok = journal.OpenForRead(_settings.JournalFile);
                }
                else
                {
                    err = " for writing.";
                    ok = journal.OpenForWrite(_settings.JournalFile);
                }

                Assert.Fatal(ok, "Not able to open journal file " + _settings.JournalFile + err);

                if (ok)
                    TorqueEventManager.Instance.Journal = journal;
            }
            #endif
        }
 protected virtual void _SetupView(GFXDevice gfx)
 {
     _srs.CameraTransform = _camera.Transform;
     _srs._worldViewIndex = _camera.WorldViewIndex;
     _srs.View = Matrix.Invert(_camera.Transform);
 }
 protected virtual void _SetupSceneState(GFXDevice gfx)
 {
     _srs.Gfx = gfx;
     _srs.SceneGraph = this;
 }
 protected abstract void _SetupProjection(GFXDevice gfx, float aspectRatio);
        /// <summary>
        /// Render the objects which have object types compatible with the render mask but not
        /// the noRenderMask.
        /// </summary>
        /// <param name="gfx">Graphics device with which to render</param>
        /// <param name="renderMask">Render objects of these types.</param>
        /// <param name="noRenderMask">Do not render objects of these types.</param>
        public void Render(GFXDevice gfx, TorqueObjectType renderMask, TorqueObjectType noRenderMask)
        {
            #if DEBUG
            Profiler.Instance.StartBlock("SceneGraph.Render");
            #endif

            SceneRenderer.RenderManager.Sort(_srs);
            SceneRenderer.RenderManager.PreRender(_srs);
            SceneRenderer.RenderManager.Render(_srs);
            SceneRenderer.RenderManager.Clear();

            #if DEBUG
            Profiler.Instance.EndBlock("SceneGraph.Render");
            #endif
        }
        /// <summary>
        /// Prepare to render objects which have object types compatible with the render mask but 
        /// not the noRenderMask.
        /// </summary>
        /// <param name="gfx">Graphics device with which to render</param>
        /// <param name="renderMask">Render objects of these types.</param>
        /// <param name="noRenderMask">Do not render objects of these types.</param>
        public void PreRender(GFXDevice gfx, TorqueObjectType renderMask, TorqueObjectType noRenderMask, float aspectRatio)
        {
            #if DEBUG
            Profiler.Instance.StartBlock("SceneGraph.PreRender");
            #endif

            Assert.Fatal(gfx != null, "SceneGraph.PreRender - Must have a GFX device in order to render");
            Assert.Fatal(_camera != null, "SceneGraph.PreRender - Must have a camera in order to render");

            UpdateFrameTime();

            // setup state
            _SetupSceneState(gfx);
            _SetupView(gfx);
            _SetupWorld(gfx);
            _SetupProjection(gfx, aspectRatio);

            if (_preloadMaterialsDelegate != null)
                _preloadMaterialsDelegate(_srs);

            _preloadMaterialsDelegate = null;

            // similar to the preload delegate, provide an event for first render
            // cafTODO: should preload delegate be moved into this?
            if (!_hasRendered)
            {
                TorqueEventManager.TriggerEvent(_firstRenderEvent, 0);
                _hasRendered = true;
            }

            // render
            _RenderObjects(renderMask - noRenderMask, aspectRatio);

            #if DEBUG
            Profiler.Instance.EndBlock("SceneGraph.PreRender");
            #endif
        }