/// <summary> /// Function to set up the renderer. /// </summary> /// <param name="renderer">Renderer to use.</param> public void SetupRenderer(Gorgon2D renderer) { if (DesignMode) { return; } try { Gorgon.ApplicationIdleLoopMethod = null; if (_renderer != renderer) { CleanUpRenderer(); } if (renderer == null) { return; } _renderer = renderer; _graphics = renderer.Graphics; if (_swapChain == null) { _swapChain = _graphics.Output.CreateSwapChain("ImageDisplay", new GorgonSwapChainSettings { Window = panelTextureDisplay, Format = BufferFormat.R8G8B8A8_UIntNormal }); } if (_lastState == null) { _lastState = renderer.Begin2D(); } if (_defaultTexture == null) { _defaultTexture = _graphics.Textures.CreateTexture <GorgonTexture2D>("DefaultPattern", Resources.Pattern); } if (_clipper == null) { InitializeClipper(null, RectangleF.Empty); } _region = new RectangleF(0, 0, 1, 1); _renderer.Target = _swapChain; Gorgon.ApplicationIdleLoopMethod = Idle; } finally { ValidateCommands(); } }
/// <summary> /// Function to clean up the renderer objects. /// </summary> private void CleanUpRenderer() { if (_renderer == null) { return; } if (_lastState != null) { _renderer.End2D(_lastState); _lastState = null; } if (_texture != null) { _texture.Dispose(); _texture = null; } if (_defaultTexture != null) { _defaultTexture.Dispose(); _defaultTexture = null; } if (_swapChain != null) { _swapChain.Dispose(); _swapChain = null; } _clipper = null; _graphics = null; }
/// <summary> /// Function to handle idle time for the application. /// </summary> /// <returns>TRUE to continue processing, FALSE to stop.</returns> private static bool Idle() { // Clear to our gray color. _swap.Clear(Color.FromArgb(173, 173, 173), 1.0f); // Animate the ball. UpdateBall(); // Draw our planes. foreach (var plane in _planes) { plane.Draw(); } // Draw the main sphere. _sphere.Draw(); // Draw the sphere shadow first. var spherePosition = _sphere.Position; Graphics.Output.DepthStencilState.States = _noDepth; Graphics.Shaders.PixelShader.Current = _pixelShaderShadow; _sphere.Position = new Vector3(spherePosition.X + 0.25f, spherePosition.Y - 0.125f, spherePosition.Z); _sphere.Draw(); // Reset our sphere position, pixel shader and depth writing state. _sphere.Position = spherePosition; Graphics.Output.DepthStencilState.States = _depth; Graphics.Shaders.PixelShader.Current = _pixelShader; // Draw our text. // Use this to show how incredibly slow and terrible my 3D code is. // Tell the 2D renderer to remember the current state of the 3D scene. _3DState = _2D.Begin2D(); _2D.Drawing.FilledRectangle(new RectangleF(0, 0, _mainForm.ClientSize.Width - 1.0f, 38.0f), Color.FromArgb(128, 0, 0, 0)); _2D.Drawing.DrawRectangle(new RectangleF(0, 0, _mainForm.ClientSize.Width, 38.0f), Color.White); _2D.Drawing.DrawString(Graphics.Fonts.DefaultFont, "FPS: " + GorgonTiming.AverageFPS.ToString("0.0") + "\nDelta: " + (GorgonTiming.AverageDelta * 1000.0f).ToString("0.0##") + " milliseconds", new Vector2(3.0f, 0.0f), GorgonColor.White); // Draw our logo because I'm insecure. _2D.Drawing.Blit(Graphics.Textures.GorgonLogo, new RectangleF(_mainForm.ClientSize.Width - Graphics.Textures.GorgonLogo.Settings.Width, _mainForm.ClientSize.Height - Graphics.Textures.GorgonLogo.Settings.Height, Graphics.Textures.GorgonLogo.Settings.Width, Graphics.Textures.GorgonLogo.Settings.Height)); // Note that we're rendering here but not flipping the buffer (the 'false' parameter). This just delays the page // flipping until later. Technically, we don't need to do this here because it's the last thing we're doing, but // if we had more rendering to do after, we'd have to flip manually. _2D.Flush(); // Restore the 3D scene state. _2D.End2D(_3DState); // Now we flip our buffers. // We need to this or we won't see anything. _swap.Flip(); return(true); }