예제 #1
0
        /// <summary>
        /// Renders the sample.
        /// </summary>
        private void Render(bool isScreenshot)
        {
            if (deviceLost)
            {
                if (Context.Device.TestCooperativeLevel() == SlimDX.Direct3D9.ResultCode.DeviceNotReset)
                {
                    Context.Device.Reset(Context.PresentParameters);
                    deviceLost            = false;
                    userInterfaceRenderer = new UserInterfaceRenderer9(Context.Device, WindowWidth, WindowHeight);
                    OnResourceLoad();
                }
                else
                {
                    Thread.Sleep(100);
                    return;
                }
            }

            frameAccumulator += FrameDelta;
            ++frameCount;
            if (frameAccumulator >= 1.0f)
            {
                framesPerSecond.Value = frameCount / frameAccumulator;

                frameAccumulator = 0.0f;
                frameCount       = 0;
            }

            SlimDX.Direct3D9.Surface renderTarget = null;
            try
            {
                if (isScreenshot)
                {
                    renderTarget = Context.Device.GetRenderTarget(0);
                    if (!Context.Direct3D.CheckDepthStencilMatch(0, DeviceType.Hardware, SlimDX.Direct3D9.Format.X8R8G8B8, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Format.D24X8))
                    {
                        throw new InvalidOperationException("DepthStencil mismatch");
                    }
                    screenshotSurface = SlimDX.Direct3D9.Surface.CreateRenderTarget(Context.Device, WindowWidth, WindowHeight, SlimDX.Direct3D9.Format.A8R8G8B8, Context.MultisampleType, Context.MultisampleQuality, false);
                    Context.Device.SetRenderTarget(0, screenshotSurface);
                }
                OnRenderBegin(isScreenshot);
                OnRender();
                if (userInterfaceRenderer != null && isScreenshot == false)
                {
                    userInterfaceRenderer.Render(userInterface);
                }
                OnRenderEnd(isScreenshot);
                if (isScreenshot)
                {
                    string myPicturesPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
                    string fileName       = string.Format("ss{0}.png", System.DateTime.Now.ToFileTime());
                    string filePath       = System.IO.Path.Combine(myPicturesPath, fileName);
                    SlimDX.Direct3D9.Surface.ToFile(screenshotSurface, filePath, ImageFileFormat.Png);
                    screenshotSurface.Dispose();
                    screenshotSurface = null;
                    Context.Device.SetRenderTarget(0, renderTarget);
                }
            }
            catch (SlimDX.Direct3D9.Direct3D9Exception e)
            {
                if (screenshotSurface != null)
                {
                    screenshotSurface.Dispose();
                }

                if (e.ResultCode == SlimDX.Direct3D9.ResultCode.DeviceLost)
                {
                    OnResourceUnload();
                    userInterfaceRenderer.Dispose();
                    deviceLost = true;
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (renderTarget != null)
                {
                    renderTarget.Dispose();
                }
            }
        }
예제 #2
0
 public override void Dispose()
 {
     mRenderTarget.Dispose();
 }