Provides baseline functionality for rendering a user interface.
Inheritance: IDisposable
Example #1
0
        private void HandleResize(object sender, EventArgs e)
        {
            if (_form.WindowState == FormWindowState.Minimized)
            {
                return;
            }

            OnResourceUnload();

            arcBall.SetWindow(_form.ClientSize.Width, _form.ClientSize.Height);
            _configuration.WindowWidth  = _form.ClientSize.Width;
            _configuration.WindowHeight = _form.ClientSize.Height;

            if (Context != null)
            {
                userInterfaceRenderer.Dispose();

                Context.PresentParameters.BackBufferWidth  = _configuration.WindowWidth;
                Context.PresentParameters.BackBufferHeight = _configuration.WindowHeight;
                Context.Device.Reset(Context.PresentParameters);

                userInterfaceRenderer = new UserInterfaceRenderer9(Context.Device, _form.ClientSize.Width, _form.ClientSize.Height);
            }

            OnResourceLoad();
        }
Example #2
0
        /// <summary>
        /// Handles a key up event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void HandleKeyUp(object sender, KeyEventArgs e)
        {
            if (EnableCamera)
            {
                switch (e.KeyCode)
                {
                case Keys.W:
                case Keys.S:
                    cameraForwardDirection = 0;
                    break;

                case Keys.A:
                case Keys.D:
                    cameraStrafeDirection = 0;
                    break;

                case Keys.PageDown:
                case Keys.PageUp:
                    cameraRaiseDirection = 0;
                    break;
                }
            }

            if (e.Alt && e.KeyCode == Keys.Enter)
            {
                OnResourceUnload();

                isFullScreen = !isFullScreen;

                if (Context != null)
                {
                    userInterfaceRenderer.Dispose();

                    Context.PresentParameters.BackBufferWidth  = _configuration.WindowWidth;
                    Context.PresentParameters.BackBufferHeight = _configuration.WindowHeight;
                    Context.PresentParameters.Windowed         = !isFullScreen;

                    if (!isFullScreen)
                    {
                        _form.MaximizeBox = true;
                    }

                    Context.Device.Reset(Context.PresentParameters);

                    arcBall.SetWindow(_form.ClientSize.Width, _form.ClientSize.Height);
                    userInterfaceRenderer = new UserInterfaceRenderer9(Context.Device, _form.ClientSize.Width, _form.ClientSize.Height);
                }

                OnResourceLoad();
            }
            if (e.KeyCode == Keys.PrintScreen)
            {
                RenderScreenshot();
            }

            OnKeyUp(sender, e);
        }
Example #3
0
 /// <summary>
 /// Renders the element using the specified renderer.
 /// </summary>
 /// <param name="renderer">The renderer.</param>
 /// <param name="element">The element.</param>
 /// <param name="x">The X coordinate (in pixels) of the upper left corner of the region the element should be rendered to.</param>
 /// <param name="y">The Y coordinate (in pixels) of the upper left corner of the region the element should be rendered to.</param>
 /// <param name="width">The width (in pixels) of the region the element should be rendered to.</param>
 /// <param name="height">The height (in pixels) of the region the element should be rendered to.</param>
 public virtual void Render(UserInterfaceRenderer renderer, Element element, int x, int y, int width, int height)
 {
     //Color4 color = new Color4(1.0f, 1.0f, 1.0f);
     //renderer.RenderRectangle(x, y, width, height, color);
     renderer.RenderString(element.Label, x, y, new Color4(1.0f, 0.0f, 0.0f));
 }
Example #4
0
 /// <summary>
 /// Measures the element, returning the size (in pixels) it would occupy if
 /// rendered with the specified renderer.
 /// </summary>
 /// <param name="renderer">The renderer.</param>
 /// <param name="element">The element.</param>
 /// <returns>The size of the element (in pixels).</returns>
 public virtual Vector2 Measure(UserInterfaceRenderer renderer, Element element)
 {
     return(renderer.MeasureString(element.Label));
 }
Example #5
0
 /// <summary>
 /// Renders the element using the specified renderer.
 /// </summary>
 /// <param name="renderer">The renderer.</param>
 /// <param name="element">The element.</param>
 /// <param name="x">The X coordinate (in pixels) of the upper left corner of the region the element should be rendered to.</param>
 /// <param name="y">The Y coordinate (in pixels) of the upper left corner of the region the element should be rendered to.</param>
 /// <param name="width">The width (in pixels) of the region the element should be rendered to.</param>
 /// <param name="height">The height (in pixels) of the region the element should be rendered to.</param>
 public virtual void Render(UserInterfaceRenderer renderer, Element element, int x, int y, int width, int height)
 {
     //Color4 color = new Color4(1.0f, 1.0f, 1.0f);
     //renderer.RenderRectangle(x, y, width, height, color);
     renderer.RenderString(element.Label, x, y, new Color4(1.0f, 0.0f, 0.0f));
 }
Example #6
0
 /// <summary>
 /// Measures the element, returning the size (in pixels) it would occupy if
 /// rendered with the specified renderer.
 /// </summary>
 /// <param name="renderer">The renderer.</param>
 /// <param name="element">The element.</param>
 /// <returns>The size of the element (in pixels).</returns>
 public virtual Vector2 Measure(UserInterfaceRenderer renderer, Element element)
 {
     return renderer.MeasureString(element.Label);
 }
Example #7
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();
                }
            }
        }
Example #8
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(); }
            }
        }
Example #9
0
        private void HandleResize(object sender, EventArgs e)
        {
            if (_form.WindowState == FormWindowState.Minimized)
            {
                return;
            }

            OnResourceUnload();

            arcBall.SetWindow(_form.ClientSize.Width, _form.ClientSize.Height);
            _configuration.WindowWidth = _form.ClientSize.Width;
            _configuration.WindowHeight = _form.ClientSize.Height;

            if (Context != null)
            {
                userInterfaceRenderer.Dispose();

                Context.PresentParameters.BackBufferWidth = _configuration.WindowWidth;
                Context.PresentParameters.BackBufferHeight = _configuration.WindowHeight;
                Context.Device.Reset(Context.PresentParameters);

                userInterfaceRenderer = new UserInterfaceRenderer9(Context.Device, _form.ClientSize.Width, _form.ClientSize.Height);
            }

            OnResourceLoad();
        }
Example #10
0
        /// <summary>
        /// Handles a key up event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void HandleKeyUp(object sender, KeyEventArgs e)
        {
            if (EnableCamera)
            {
                switch (e.KeyCode)
                {
                    case Keys.W:
                    case Keys.S:
                        cameraForwardDirection = 0;
                        break;
                    case Keys.A:
                    case Keys.D:
                        cameraStrafeDirection = 0;
                        break;
                    case Keys.PageDown:
                    case Keys.PageUp:
                        cameraRaiseDirection = 0;
                        break;
                }
            }

            if (e.Alt && e.KeyCode == Keys.Enter)
            {
                OnResourceUnload();

                isFullScreen = !isFullScreen;

                if (Context != null)
                {
                    userInterfaceRenderer.Dispose();

                    Context.PresentParameters.BackBufferWidth = _configuration.WindowWidth;
                    Context.PresentParameters.BackBufferHeight = _configuration.WindowHeight;
                    Context.PresentParameters.Windowed = !isFullScreen;

                    if (!isFullScreen)
                        _form.MaximizeBox = true;

                    Context.Device.Reset(Context.PresentParameters);

                    arcBall.SetWindow(_form.ClientSize.Width, _form.ClientSize.Height);
                    userInterfaceRenderer = new UserInterfaceRenderer9(Context.Device, _form.ClientSize.Width, _form.ClientSize.Height);
                }

                OnResourceLoad();
            }
            if (e.KeyCode == Keys.PrintScreen)
            {
                RenderScreenshot();
            }

            OnKeyUp(sender, e);
        }
Example #11
0
        /// <summary>
        /// Initializes a <see cref="DeviceContext9">Direct3D9 device context</see> according to the specified settings.
        /// The base class retains ownership of the context and will dispose of it when appropriate.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <returns>The initialized device context.</returns>
        protected void InitializeDevice(DeviceSettings9 settings)
        {
            var result = new DeviceContext9(_form.Handle, settings);
            userInterfaceRenderer = new UserInterfaceRenderer9(result.Device, settings.Width, settings.Height);
            apiContext = result;
            Context = result;

            this.globalSettings.Device = Context.Device;
            this.globalSettings.Init();
        }