Ejemplo n.º 1
0
        /// <summary>
        /// Function to load true type fonts to generate from.
        /// </summary>
        /// <param name="window">The window containing the loading message.</param>
        /// <returns>The font families to use when building the bitmap fonts.</returns>
        private static IReadOnlyList <Drawing.FontFamily> LoadTrueTypeFonts(FormMain window)
        {
            // Load in a bunch of true type fonts.
            DirectoryInfo dirInfo = GorgonExample.GetResourcePath("Fonts");

            FileInfo[] files = dirInfo.GetFiles("*.ttf", SearchOption.TopDirectoryOnly);

            var fontFamilies = new List <Drawing.FontFamily>();

            // Load all external true type fonts for this example.
            // This takes a while...
            foreach (FileInfo file in files)
            {
                window.UpdateStatus($"Loading Font: {file.FullName}".Ellipses(50));
                Drawing.FontFamily externFont = _fontFactory.LoadTrueTypeFontFamily(file.FullName);
                _fontFamilies.Insert(0, externFont.Name);
                fontFamilies.Add(externFont);
            }

            window.UpdateStatus(null);

            fontFamilies.AddRange(Drawing.FontFamily.Families);

            return(fontFamilies);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to initialize the application.
        /// </summary>
        private static async Task InitializeAsync(FormMain window)
        {
            try
            {
                GorgonExample.ResourceBaseDirectory   = new DirectoryInfo(Settings.Default.ResourceLocation);
                GorgonExample.PlugInLocationDirectory = new DirectoryInfo(Settings.Default.PlugInLocation);

                // Load our packed file system plug in.
                window.UpdateStatus("Loading plugins...");

                IGorgonPlugInService plugIns = await Task.Run(() =>
                {
                    _assemblyCache = new GorgonMefPlugInCache(GorgonApplication.Log);
                    _assemblyCache.LoadPlugInAssemblies(GorgonExample.GetPlugInPath().FullName, "Gorgon.FileSystem.GorPack.dll");
                    return(new GorgonMefPlugInService(_assemblyCache));
                });

                window.UpdateStatus("Initializing graphics...");

                // Retrieve the list of video adapters. We can do this on a background thread because there's no interaction between other threads and the
                // underlying D3D backend yet.
                IReadOnlyList <IGorgonVideoAdapterInfo> videoDevices = await Task.Run(() => GorgonGraphics.EnumerateAdapters(log: GorgonApplication.Log));

                if (videoDevices.Count == 0)
                {
                    throw new GorgonException(GorgonResult.CannotCreate,
                                              "Gorgon requires at least a Direct3D 11.4 capable video device.\nThere is no suitable device installed on the system.");
                }

                // Find the best video device.
                _graphics = new GorgonGraphics(videoDevices.OrderByDescending(item => item.FeatureSet).First());

                _screen = new GorgonSwapChain(_graphics,
                                              window,
                                              new GorgonSwapChainInfo("Gorgon2D Space Scene Example")
                {
                    Width  = Settings.Default.Resolution.Width,
                    Height = Settings.Default.Resolution.Height,
                    Format = BufferFormat.R8G8B8A8_UNorm
                });

                // Create a secondary render target for our scene. We use 16 bit floating point for the effect fidelity.
                // We'll lock our resolution to 1920x1080 (pretty common resolution for most people).
                _mainRtv = GorgonRenderTarget2DView.CreateRenderTarget(_graphics, new GorgonTexture2DInfo("Main RTV")
                {
                    Width   = (int)_baseResolution.X,
                    Height  = (int)_baseResolution.Y,
                    Format  = BufferFormat.R16G16B16A16_Float,
                    Binding = TextureBinding.ShaderResource
                });
                _mainSrv       = _mainRtv.GetShaderResourceView();
                _mainRtvAspect = _mainRtv.Width < _mainRtv.Height ? new DX.Vector2(1, (float)_mainRtv.Height / _mainRtv.Width) : new DX.Vector2((float)_mainRtv.Width / _mainRtv.Height, 1);

                // Initialize the renderer so that we are able to draw stuff.
                _renderer = new Gorgon2D(_graphics);

                // Set up our raw input.
                _input           = new GorgonRawInput(window, GorgonApplication.Log);
                _keyboard        = new GorgonRawKeyboard();
                _keyboard.KeyUp += Keyboard_KeyUp;
                _input.RegisterDevice(_keyboard);

                GorgonExample.LoadResources(_graphics);

                // Now for the fun stuff, load our asset resources. We can load this data by mounting a directory (which I did while developing), or use a packed file.
                //
                // The resource manager will hold all the data we need for the scene. Including 3D meshes, post processing effects, etc...
                _resources = new ResourceManagement(_renderer, plugIns);
                _resources.Load(Path.Combine(GorgonExample.GetResourcePath(@"FileSystems").FullName, "SpaceScene.gorPack"));

                window.UpdateStatus("Loading resources...");
                await _resources.LoadResourcesAsync();

                SetupScene();

                // Build up a font to use for rendering any GUI text.
                _helpFont = GorgonExample.Fonts.GetFont(new GorgonFontInfo("Segoe UI", 10.0f, FontHeightMode.Points, "Segoe UI 10pt")
                {
                    OutlineSize      = 2,
                    Characters       = (Resources.Instructions + "S:1234567890x").Distinct().ToArray(),
                    FontStyle        = FontStyle.Bold,
                    AntiAliasingMode = FontAntiAliasMode.AntiAlias,
                    OutlineColor1    = GorgonColor.Black,
                    OutlineColor2    = GorgonColor.Black
                });

                _textSprite = new GorgonTextSprite(_helpFont)
                {
                    Position = new DX.Vector2(0, 64),
                    DrawMode = TextDrawMode.OutlinedGlyphs,
                    Color    = GorgonColor.YellowPure
                };

                GorgonExample.ShowStatistics = true;

                // Set the idle here. We don't want to try and render until we're done loading.
                GorgonApplication.IdleMethod = Idle;
            }
            finally
            {
                GorgonExample.EndInit();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Function to generate the Gorgon bitmap fonts.
        /// </summary>
        /// <param name="fontFamilies">The list of TrueType font families to use.</param>
        /// <param name="window">The window that contains the loading message.</param>
        private static void GenerateGorgonFonts(IReadOnlyList <Drawing.FontFamily> fontFamilies, FormMain window)
        {
            // Pick a font to use with outlines.
            int fontWithOutlineIndex = GorgonRandom.RandomInt32(1, 5);

            _glowIndex = GorgonRandom.RandomInt32(fontWithOutlineIndex + 1, fontWithOutlineIndex + 5);
            int fontWithGradient = GorgonRandom.RandomInt32(_glowIndex + 1, _glowIndex + 5);
            int fontWithTexture  = GorgonRandom.RandomInt32(fontWithGradient + 1, fontWithGradient + 5).Min(_fontFamilies.Count - 1);

            var pngCodec = new GorgonCodecPng();

            using (IGorgonImage texture = pngCodec.LoadFromFile(Path.Combine(GorgonExample.GetResourcePath(@"Textures\Fonts\").FullName, "Gradient.png")))
            {
                for (int i = 0; i < _fontFamilies.Count; ++i)
                {
                    string fontFamily = _fontFamilies[i];

                    // Use this to determine if the font is avaiable.
                    if (fontFamilies.All(item => !string.Equals(item.Name, fontFamily, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        // Can't locate this one, move on...
                        continue;
                    }

                    bool isExternal =
                        Drawing.FontFamily.Families.All(item => !string.Equals(item.Name, fontFamily, StringComparison.InvariantCultureIgnoreCase));
                    string           fontName;
                    int              outlineSize   = 0;
                    GorgonColor      outlineColor1 = GorgonColor.BlackTransparent;
                    GorgonColor      outlineColor2 = GorgonColor.BlackTransparent;
                    GorgonGlyphBrush brush         = null;

                    if (i == fontWithOutlineIndex)
                    {
                        fontName      = $"{fontFamily} 32px Outlined{(isExternal ? " External TTF" : string.Empty)}";
                        outlineColor1 = GorgonColor.Black;
                        outlineColor2 = GorgonColor.Black;
                        outlineSize   = 3;
                    }
                    else if (i == _glowIndex)
                    {
                        fontName      = $"{fontFamily} 32px Outline as Glow{(isExternal ? " External TTF" : string.Empty)}";
                        outlineColor1 = new GorgonColor(GorgonColor.YellowPure, 1.0f);
                        outlineColor2 = new GorgonColor(GorgonColor.DarkRed, 0.0f);
                        outlineSize   = 16;
                    }
                    else if (i == fontWithGradient)
                    {
                        fontName = $"{fontFamily} 32px Gradient{(isExternal ? " External TTF" : string.Empty)}";
                        brush    = new GorgonGlyphLinearGradientBrush
                        {
                            StartColor = GorgonColor.White,
                            EndColor   = GorgonColor.Black,
                            Angle      = 45.0f
                        };
                    }
                    else if (i == fontWithTexture)
                    {
                        fontName = $"{fontFamily} 32px Textured{(isExternal ? " External TTF" : string.Empty)}";
                        brush    = new GorgonGlyphTextureBrush(texture);
                    }
                    else
                    {
                        fontName = $"{fontFamily} 32px{(isExternal ? " External TTF" : string.Empty)}";
                    }

                    window.UpdateStatus($"Generating Font: {fontFamily}".Ellipses(50));

                    var fontInfo = new GorgonFontInfo(fontFamily,
                                                      30.25f,
                                                      name:
                                                      fontName)
                    {
                        AntiAliasingMode         = FontAntiAliasMode.AntiAlias,
                        OutlineSize              = outlineSize,
                        OutlineColor1            = outlineColor1,
                        OutlineColor2            = outlineColor2,
                        UsePremultipliedTextures = false,
                        Brush = brush
                    };

                    _font.Add(_fontFactory.GetFont(fontInfo));

                    // Texture brushes have to be disposed when we're done with them.
                    var disposableBrush = brush as IDisposable;
                    disposableBrush?.Dispose();
                }
            }
        }