Ejemplo n.º 1
0
 /// <summary>
 /// No documentation.
 /// </summary>
 /// <param name="version">No documentation.</param>
 /// <param name="factoryOut">No documentation.</param>
 /// <returns>No documentation.</returns>
 /// <include file='.\..\..\Documentation\CodeComments.xml' path="/comments/comment[@id='FW1CreateFactory']/*"/>
 /// <unmanaged>HRESULT FW1CreateFactory([In] unsigned int Version,[Out] IFW1Factory** ppFactory)</unmanaged>
 /// <unmanaged-short>FW1CreateFactory</unmanaged-short>
 public static void FW1CreateFactory(int version, out SharpFontWrapper.Factory factoryOut)
 {
     unsafe {
         IntPtr         factoryOut_ = IntPtr.Zero;
         SharpDX.Result __result__;
         __result__ =
             FW1CreateFactory_(version, &factoryOut_);
         factoryOut = (factoryOut_ == IntPtr.Zero)?null:new SharpFontWrapper.Factory(factoryOut_);
         __result__.CheckError();
     }
 }
Ejemplo n.º 2
0
        private static void Main()
        {
            RenderForm renderForm = new RenderForm("SharpFontWrapper - Simple Text");

            renderForm.Width  = 1024;
            renderForm.Height = 768;

            viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f);

            device        = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
            deviceContext = device.ImmediateContext;

            using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1())
            {
                SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                {
                    BufferCount       = 2,
                    Flags             = SharpDX.DXGI.SwapChainFlags.None,
                    IsWindowed        = true,
                    ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                    OutputHandle      = renderForm.Handle,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0),
                    SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                    Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput
                };

                swapChain = new SwapChain(dxgiFactory, device, swapChainDesc);

                using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0))
                {
                    renderView = new RenderTargetView(device, backBuffer);
                }
            }


            fontFactory = new SharpFontWrapper.Factory();

            fontWrapper = fontFactory.CreateFontWrapper(device, "Arial");

            colorStyle = fontFactory.CreateColor(SharpDX.Color.Green);

            textFormat = new SharpDX.DirectWrite.TextFormat(fontWrapper.DWriteFactory, "Consolas", 32.0f);
            textFormat.TextAlignment      = SharpDX.DirectWrite.TextAlignment.Center;
            textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near;



            textLayout = new SharpDX.DirectWrite.TextLayout(fontWrapper.DWriteFactory,
                                                            "DirectWrite Text Layout with formatting and color", textFormat, 1000.0f, 1000.0f);

            textLayout.SetFontStyle(SharpDX.DirectWrite.FontStyle.Italic, new SharpDX.DirectWrite.TextRange(0, 11));

            textLayout.SetFontFamilyName("Arial", new SharpDX.DirectWrite.TextRange(24, 4));
            textLayout.SetFontSize(72.0f, new SharpDX.DirectWrite.TextRange(29, 10));

            //Stylistic typeography
            SharpDX.DirectWrite.Typography typoGraphy = new SharpDX.DirectWrite.Typography(fontWrapper.DWriteFactory);
            typoGraphy.AddFontFeature(new SharpDX.DirectWrite.FontFeature(SharpDX.DirectWrite.FontFeatureTag.StylisticSet7, 1));
            textLayout.SetFontFamilyName("Gabriola", new SharpDX.DirectWrite.TextRange(12, 4));
            textLayout.SetTypography(typoGraphy, new SharpDX.DirectWrite.TextRange(12, 4));

            //Note : to pass color, make sure to use native pointer, as in that case we do not need sharpdx/.net to build com wrapper, colorStyle is already one
            textLayout.SetDrawingEffect(colorStyle.NativePointer, new SharpDX.DirectWrite.TextRange(44, 5));

            RenderLoop.Run(renderForm, () =>
            {
                float t = (float)watch.Elapsed.TotalMilliseconds;
                float x = ((float)Math.Sin(t * 0.002f) * 100.0f) + (renderForm.Width * 0.5f);

                deviceContext.OutputMerger.SetRenderTargets(renderView);
                deviceContext.Rasterizer.SetViewport(viewPort);
                deviceContext.ClearRenderTargetView(renderView, Color.Black);

                SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping
                                                   | SharpFontWrapper.TextFlags.VerticalCenter
                                                   | SharpFontWrapper.TextFlags.Center;

                fontWrapper.DrawString(deviceContext, "Hello SharpFontWrapper", 64.0f, new Vector2(x, renderForm.Height * 0.25f), Color.Yellow, flags);

                fontWrapper.DrawString(deviceContext, "This is another text", 64.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.25f + 100.0f), Color.White, flags);

                fontWrapper.DrawTextLayout(deviceContext, textLayout, new Vector2(0, renderForm.Height * 0.25f + 200.0f), Color.White, flags);

                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });

            typoGraphy.Dispose();

            colorStyle.Dispose();
            textLayout.Dispose();
            textFormat.Dispose();

            fontWrapper.Dispose();
            fontFactory.Dispose();

            deviceContext.ClearState();
            deviceContext.Flush();

            renderView.Dispose();
            swapChain.Dispose();
            deviceContext.Dispose();
            device.Dispose();
        }
Ejemplo n.º 3
0
        private static void Main()
        {
            RenderForm renderForm = new RenderForm("SharpFontWrapper - Simple Text");

            renderForm.Width  = 1024;
            renderForm.Height = 768;

            viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f);

            device        = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
            deviceContext = new DeviceContext2(device.ImmediateContext.NativePointer);

            using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1())
            {
                SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                {
                    BufferCount       = 2,
                    Flags             = SharpDX.DXGI.SwapChainFlags.None,
                    IsWindowed        = true,
                    ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                    OutputHandle      = renderForm.Handle,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0),
                    SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                    Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput
                };

                swapChain = new SwapChain(dxgiFactory, device, swapChainDesc);

                using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0))
                {
                    renderView = new RenderTargetView(device, backBuffer);
                }
            }

            //Pixel shader and custom cbuffer
            using (ShaderBytecode byteCode = ShaderBytecode.CompileFromFile("PixelShader.hlsl", "PS", "ps_5_0", ShaderFlags.OptimizationLevel3))
            {
                customPixelShader = new PixelShader(device, byteCode);
            }

            BufferDescription cbufferDesc = new BufferDescription()
            {
                BindFlags           = BindFlags.ConstantBuffer,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.None,
                SizeInBytes         = 16, //Need to be 16 aligned
                StructureByteStride = 0,
                Usage = ResourceUsage.Dynamic
            };

            cBuffer = new Buffer(device, cbufferDesc);


            fontFactory = new SharpFontWrapper.Factory();
            fontWrapper = fontFactory.CreateFontWrapper(device, "Arial");

            renderStates = fontWrapper.RenderStates;

            RenderLoop.Run(renderForm, () =>
            {
                float t = (float)watch.Elapsed.TotalSeconds;

                deviceContext.ClearRenderTargetView(renderView, Color.Black);
                deviceContext.OutputMerger.SetRenderTargets(renderView);
                deviceContext.Rasterizer.SetViewport(viewPort);

                SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping
                                                   | SharpFontWrapper.TextFlags.VerticalCenter
                                                   | SharpFontWrapper.TextFlags.Center
                                                   | SharpFontWrapper.TextFlags.StatePrepared; //Make sure this is on,otherwise pixelshader will be reverted to default

                //Custom cbuffer is not overriden, we can set it anywhere
                DataStream ds;
                deviceContext.MapSubresource(cBuffer, MapMode.WriteDiscard, MapFlags.None, out ds);
                ds.Write <float>(-t * 5.0f);
                deviceContext.UnmapSubresource(cBuffer, 0);


                deviceContext.PixelShader.SetConstantBuffer(1, cBuffer);

                //Set default render states
                renderStates.SetStates(deviceContext, 0);

                deviceContext.PixelShader.Set(customPixelShader);

                fontWrapper.DrawString(deviceContext, "SharpFontWrapper", 96.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.5f), Color.White, flags);

                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });



            renderStates.Dispose();
            fontWrapper.Dispose();
            fontFactory.Dispose();

            cBuffer.Dispose();
            customPixelShader.Dispose();
            deviceContext.ClearState();
            deviceContext.Flush();

            renderView.Dispose();
            swapChain.Dispose();
            deviceContext.Dispose();
            device.Dispose();
        }
Ejemplo n.º 4
0
        private static void Main()
        {
            RenderForm renderForm = new RenderForm("SharpFontWrapper - Transformed Text");

            renderForm.Width  = 1024;
            renderForm.Height = 768;

            viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f);

            device        = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
            deviceContext = device.ImmediateContext;

            using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1())
            {
                SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                {
                    BufferCount       = 2,
                    Flags             = SharpDX.DXGI.SwapChainFlags.None,
                    IsWindowed        = true,
                    ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                    OutputHandle      = renderForm.Handle,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0),
                    SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                    Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput
                };

                swapChain = new SwapChain(dxgiFactory, device, swapChainDesc);

                using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0))
                {
                    renderView = new RenderTargetView(device, backBuffer);
                }
            }


            fontFactory = new SharpFontWrapper.Factory();

            fontWrapper = fontFactory.CreateFontWrapper(device, "Arial");

            Matrix view       = Matrix.Translation(0.0f, 0.0f, 2.0f);
            Matrix projection = Matrix.PerspectiveFovLH(1.57f, 1024.0f / 768.0f, 0.01f, 100.0f);


            int objectCount = 128;

            Matrix[] mats = new Matrix[objectCount];

            Random r = new Random();

            for (int i = 0; i < objectCount; i++)
            {
                mats[i] = Matrix.Translation(r.NextVector3(new Vector3(-5.0f), new Vector3(5.0f)));
            }

            RenderLoop.Run(renderForm, () =>
            {
                float t = (float)watch.Elapsed.TotalMilliseconds;

                deviceContext.ClearRenderTargetView(renderView, Color.Black);
                deviceContext.OutputMerger.SetRenderTargets(renderView);
                deviceContext.Rasterizer.SetViewport(viewPort);

                SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping
                                                   | SharpFontWrapper.TextFlags.VerticalCenter
                                                   | SharpFontWrapper.TextFlags.Center;

                Matrix rotation = Matrix.RotationY(t * 0.0001f);

                for (int i = 0; i < objectCount; i++)
                {
                    //Please note that font wrapper defaults to inverted Y
                    Matrix world = Matrix.Scaling(0.002f, -0.002f, 0.002f);
                    world        = world * mats[i] * rotation;

                    Matrix twvp = world * view * projection;

                    fontWrapper.DrawString(deviceContext, "SharpFontWrapper", "Arial", 64.0f, twvp, null, Color.White, flags);
                }



                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });


            fontWrapper.Dispose();
            fontFactory.Dispose();

            deviceContext.ClearState();
            deviceContext.Flush();

            renderView.Dispose();
            swapChain.Dispose();
            deviceContext.Dispose();
            device.Dispose();
        }
Ejemplo n.º 5
0
        private static void Main()
        {
            RenderForm renderForm = new RenderForm("SharpFontWrapper - View glyph sheets");

            renderForm.Width  = 1024;
            renderForm.Height = 768;

            viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f);

            device        = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
            deviceContext = new DeviceContext2(device.ImmediateContext.NativePointer);

            using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1())
            {
                SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                {
                    BufferCount       = 2,
                    Flags             = SharpDX.DXGI.SwapChainFlags.None,
                    IsWindowed        = true,
                    ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                    OutputHandle      = renderForm.Handle,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0),
                    SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                    Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput
                };

                swapChain = new SwapChain(dxgiFactory, device, swapChainDesc);

                using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0))
                {
                    renderView = new RenderTargetView(device, backBuffer);
                }
            }

            //Vertex/Pixel shaders
            using (ShaderBytecode byteCode = ShaderBytecode.CompileFromFile("GlyphSheetView.hlsl", "VS", "vs_5_0", ShaderFlags.OptimizationLevel3))
            {
                vsGlyphView = new VertexShader(device, byteCode);
            }
            using (ShaderBytecode byteCode = ShaderBytecode.CompileFromFile("GlyphSheetView.hlsl", "PS", "ps_5_0", ShaderFlags.OptimizationLevel3))
            {
                psGlyphView = new PixelShader(device, byteCode);
            }

            fontFactory = new SharpFontWrapper.Factory();
            fontWrapper = fontFactory.CreateFontWrapper(device, "Arial");

            renderStates = fontWrapper.RenderStates;

            bool showGlyphMode = false;

            renderForm.KeyDown += (sender, args) =>
            {
                if (args.KeyCode == System.Windows.Forms.Keys.Space)
                {
                    showGlyphMode = !showGlyphMode;
                }
            };

            RenderLoop.Run(renderForm, () =>
            {
                renderForm.Text = string.Format("SharpFontWrapper - Glyph Sheets view - {0} sheets built", fontWrapper.GlyphAtlas.SheetCount);

                float t = (float)watch.Elapsed.TotalSeconds;

                deviceContext.ClearRenderTargetView(renderView, Color.Black);
                deviceContext.OutputMerger.SetRenderTargets(renderView);
                deviceContext.Rasterizer.SetViewport(viewPort);

                SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping
                                                   | SharpFontWrapper.TextFlags.VerticalCenter
                                                   | SharpFontWrapper.TextFlags.Center;


                if (showGlyphMode)
                {
                    deviceContext.VertexShader.Set(vsGlyphView);
                    deviceContext.GeometryShader.Set(null); //Text uses GS, so it might still be bound, ensure to remove it
                    deviceContext.PixelShader.Set(psGlyphView);

                    deviceContext.PixelShader.SetShaderResource(0, fontWrapper.GlyphAtlas.GetSheet(0).SheetTexture);


                    deviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
                    deviceContext.Draw(3, 0);
                }
                else
                {
                    fontWrapper.DrawString(deviceContext, "SharpFontWrapper, space to toggle glyph view", 32.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.5f), Color.White, flags);
                }
                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });



            renderStates.Dispose();
            fontWrapper.Dispose();
            fontFactory.Dispose();

            vsGlyphView.Dispose();
            psGlyphView.Dispose();
            deviceContext.ClearState();
            deviceContext.Flush();

            renderView.Dispose();
            swapChain.Dispose();
            deviceContext.Dispose();
            device.Dispose();
        }
Ejemplo n.º 6
0
        private static void Main()
        {
            RenderForm renderForm = new RenderForm("SharpFontWrapper - Simple Text");

            renderForm.Width  = 1024;
            renderForm.Height = 768;

            viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f);

            device        = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug);
            deviceContext = new DeviceContext2(device.ImmediateContext.NativePointer);

            using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1())
            {
                SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                {
                    BufferCount       = 2,
                    Flags             = SharpDX.DXGI.SwapChainFlags.None,
                    IsWindowed        = true,
                    ModeDescription   = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                    OutputHandle      = renderForm.Handle,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0),
                    SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                    Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput
                };

                swapChain = new SwapChain(dxgiFactory, device, swapChainDesc);

                using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0))
                {
                    renderView = new RenderTargetView(device, backBuffer);
                }
            }


            fontFactory = new SharpFontWrapper.Factory();
            fontWrapper = fontFactory.CreateFontWrapper(device, "Arial");

            RenderLoop.Run(renderForm, () =>
            {
                float t = (float)watch.Elapsed.TotalMilliseconds;

                deviceContext.ClearRenderTargetView(renderView, Color.Black);
                deviceContext.OutputMerger.SetRenderTargets(renderView);
                deviceContext.Rasterizer.SetViewport(viewPort);


                SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping
                                                   | SharpFontWrapper.TextFlags.VerticalCenter
                                                   | SharpFontWrapper.TextFlags.Center;

                RectangleF layoutRect = new RectangleF(renderForm.Width * 0.5f, renderForm.Height * 0.5f, 0, 0);

                var rect = fontWrapper.MeasureString("Hello SharpFontWrapper", "Arial", 64.0f, layoutRect, flags);

                SharpDX.Rectangle r = new Rectangle((int)rect.Left, (int)rect.Top, (int)rect.Right - (int)rect.Left, (int)rect.Bottom - (int)rect.Top);

                deviceContext.ClearView(renderView, Color.Blue, new SharpDX.Mathematics.Interop.RawRectangle[] { r }, 1);


                fontWrapper.DrawString(deviceContext, "Hello SharpFontWrapper", 64.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.5f), Color.White, flags);

                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });



            fontWrapper.Dispose();
            fontFactory.Dispose();

            deviceContext.ClearState();
            deviceContext.Flush();

            renderView.Dispose();
            swapChain.Dispose();
            deviceContext.Dispose();
            device.Dispose();
        }