コード例 #1
0
ファイル: Program.cs プロジェクト: hammerforgegames/Gorgon
        /// <summary>
        /// Function called when the application goes into an idle state.
        /// </summary>
        /// <returns><b>true</b> to continue executing, <b>false</b> to stop.</returns>
        private static bool Idle()
        {
            if (_lightBrightDir)
            {
                _lightValue += 1.0f * GorgonTiming.Delta;
            }
            else
            {
                _lightValue -= 1.0f * GorgonTiming.Delta;
            }

            if (_torchFrameTime.Milliseconds > 250)
            {
                _torchSprite.TextureRegion =
                    _torchTexture.Texture.ToTexel(new DX.Rectangle(_torchSprite.TextureRegion.Left == 0 ? 56 : 0, 0, 55, _torchTexture.Height));
                _torchFrameTime.Reset();

                _lightValue = _torchSprite.TextureRegion.Left == 0 ? _lightMinMax.Maximum : _lightMinMax.Minimum;
            }

            if (_lightValue < _lightMinMax.Minimum)
            {
                _lightValue     = _lightMinMax.Minimum;
                _lightBrightDir = !_lightBrightDir;
            }

            if (_lightValue > _lightMinMax.Maximum)
            {
                _lightValue     = _lightMinMax.Maximum;
                _lightBrightDir = !_lightBrightDir;
            }

            _lightEffect.Lights[0].Color         = new GorgonColor((_lightValue * 253.0f) / 255.0f, (_lightValue * 248.0f) / 255.0f, (_lightValue * 230.0f) / 255.0f);
            _lightEffect.Lights[0].SpecularPower = (1.0f - (_lightValue / 1.2f)) * 15;

            _finalTarget.Clear(GorgonColor.BlackTransparent);
            _screen.RenderTargetView.Clear(GorgonColor.Black);

            // Render the lit sprite.
            _lightEffect.Render(DrawLitScene, _finalTarget);

            // Blit our final texture to the main screen.
            _graphics.SetRenderTarget(_screen.RenderTargetView);
            _renderer.Begin();
            _renderer.DrawFilledRectangle(new DX.RectangleF(0, 0, _screen.Width, _screen.Height),
                                          GorgonColor.White,
                                          _finalTexture,
                                          new DX.RectangleF(0, 0, 1, 1));

            _renderer.DrawSprite(_torchSprite);

            _renderer.DrawString($"Specular Power: {_lightEffect.Lights[0].SpecularPower:0.0#####}\nLight [c #{GorgonColor.CornFlowerBlue.ToHex()}]Z[/c]: {_lightEffect.Lights[0].Position.Z:0.0}",
                                 new DX.Vector2(0, 64));
            _renderer.End();

            GorgonExample.DrawStatsAndLogo(_renderer);

            _screen.Present(1);
            return(true);
        }
コード例 #2
0
ファイル: RenderTargetFactory.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to rent a render target from the factory.
        /// </summary>
        /// <param name="targetInfo">The information about the render target to retrieve.</param>
        /// <param name="name">A unique user defined name for a new render target.</param>
        /// <param name="clearOnRetrieve">[Optional] <b>true</b> to clear the render target when retrieved, or <b>false</b> to leave the contents as-is.</param>
        /// <returns>The requested render target.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="targetInfo"/>, or the <paramref name="name"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> parameter is empty.</exception>
        /// <remarks>
        /// <para>
        /// All calls to this method should be paired with a call to the <see cref="Return"/> method.  Failure to do so may result in a leak.
        /// </para>
        /// <para>
        /// The optional <paramref name="clearOnRetrieve"/> parameter, if set to <b>true</b>,, will clear the contents of a render target that is being reused
        /// prior to returning it.  In some cases this is not ideal, so setting it to <b>false</b> will preserve the contents.  New render targets will always
        /// be cleared.
        /// </para>
        /// <note type="caution">
        /// <para>
        /// For performance reasons, any exceptions thrown from this method will only be thrown when Gorgon is compiled as DEBUG.
        /// </para>
        /// </note>
        /// </remarks>
        public GorgonRenderTarget2DView Rent(IGorgonTexture2DInfo targetInfo, string name, bool clearOnRetrieve = true)
        {
            name.ValidateString(nameof(name));
            targetInfo.ValidateObject(nameof(targetInfo));

            ExpireTargets();

            // Ensure the information is valid.
            GorgonTexture2DInfo newInfo = _textureInfoAllocator.Allocate();

            newInfo.Copy(name, targetInfo);
            newInfo.Binding = targetInfo.Binding | TextureBinding.RenderTarget | TextureBinding.ShaderResource;
            newInfo.Usage   = ResourceUsage.Default;

            for (int i = 0; i < _renderTargets.Count; ++i)
            {
                GorgonRenderTarget2DView rtv    = _renderTargets[i];
                GorgonTexture2D          target = _renderTargets[i].Texture;

                if ((!_rented.Contains(rtv)) && (target.Width == newInfo.Width) && (target.Height == newInfo.Height) && (target.MipLevels == newInfo.MipLevels) &&
                    (target.ArrayCount == newInfo.ArrayCount) && (target.Format == newInfo.Format) && (target.Binding == newInfo.Binding) &&
                    (target.MultisampleInfo.Equals(newInfo.MultisampleInfo)) && (newInfo.IsCubeMap == target.IsCubeMap) &&
                    (string.Equals(newInfo.Name, rtv.Texture.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    if (clearOnRetrieve)
                    {
                        rtv.Clear(GorgonColor.BlackTransparent);
                    }

                    _renderTargets.Remove(rtv);
                    _expiryTime.Remove(rtv);
                    _rented.Add(rtv);
                    return(rtv);
                }
            }

            if (_renderTargets.Count == 0)
            {
                _expiryTimer.Reset();
            }

            var newRtv = GorgonRenderTarget2DView.CreateRenderTarget(_graphics, newInfo);

            // Cache a default shader resource view (the texture holds the cache, we hold a separate one so we can clean it up later).
            _srvs.Add(newRtv.GetShaderResourceView());
            newRtv.OwnerFactory = this;
            _rented.Add(newRtv);
            newRtv.Clear(GorgonColor.BlackTransparent);
            return(newRtv);
        }
コード例 #3
0
ファイル: Context.cs プロジェクト: hammerforgegames/Gorgon
        /// <summary>
        /// Function to begin the execution of the application context.
        /// </summary>
        public void RunMe()
        {
            string annoyUser = "******";
            int    counter   = 0;

            try
            {
                _timer.Reset();
                _splashScreen.Show();
                _splashScreen.UpdateText("This is the splash screen.");

                // Fade in the splash screen about 10% every 7 milliseconds.
                while (_splashScreen.Opacity < 1)
                {
                    if (!(_timer.Milliseconds > 7))
                    {
                        continue;
                    }

                    _timer.Reset();
                    _splashScreen.Opacity += 0.01;
                }

                // Annoy the user.  They're asking for it.
                while (counter < 5)
                {
                    while (_timer.Seconds > 1)
                    {
                        if (annoyUser.Length < 50)
                        {
                            annoyUser += ".";
                        }
                        else
                        {
                            annoyUser = "******";
                        }

                        _splashScreen.UpdateText(annoyUser);
                        _timer.Reset();
                        counter++;
                    }
                }

                // Fade it out.
                while (_splashScreen.Opacity > 0.02)
                {
                    if (!(_timer.Milliseconds > 5))
                    {
                        continue;
                    }

                    _timer.Reset();
                    _splashScreen.Opacity -= 0.01;
                }

                // Resize the main form to 640 x 480.
                MainForm.KeyDown    += MainForm_KeyDown;
                MainForm.Deactivate += (sender, args) => GorgonApplication.Log.Print("Application is deactivated. Loops will pause.", LoggingLevel.All);
                MainForm.Activated  += (sender, args) => GorgonApplication.Log.Print("Application is activated. Loops will run.", LoggingLevel.All);
                MainForm.ClientSize  = new Size(640, 480);
                MainForm.Show();
            }
            catch (Exception ex)
            {
                // If we get an error, then leave the application.
                GorgonDialogs.ErrorBox(MainForm, ex);

                MainForm?.Dispose();
                MainForm = null;
            }
            finally
            {
                // We don't need this any more.
                _splashScreen?.Dispose();
                _splashScreen = null;
            }
        }