Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to draw boxes, triangles and text using SharpDx. The documentation seems very vague for me, and through several examples I came up with a code that I cannot understand why it does not work.

What I have tried:

My code so far is:


C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX.Direct3D9;
using SharpDX.Mathematics.Interop;
using SharpDX.Windows;
using Device = SharpDX.Direct2D1.Device;
using Font = SharpDX.Direct3D9.Font;

namespace SharpDxDrawing
{
    class Program
    {
        static void Main(string[] args)
        {
            var form = new RenderForm("SharpDX - Direct3D9 Font Sample");
            int width = form.ClientSize.Width;
            int height = form.ClientSize.Height;
            var device = new SharpDX.Direct3D9.Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(width, height) { PresentationInterval = PresentInterval.One });

            // Initialize the Font
            FontDescription fontDescription = new FontDescription()
            {
                Height = 72,
                Italic = false,
                CharacterSet = FontCharacterSet.Ansi,
                FaceName = "Arial",
                MipLevels = 0,
                OutputPrecision = FontPrecision.TrueType,
                PitchAndFamily = FontPitchAndFamily.Default,
                Quality = FontQuality.ClearType,
                Weight = FontWeight.Bold
            };



            var font = new Font(device, fontDescription);

            var displayText = "Direct3D9 Text!";

            // Measure the text to display
            var fontDimension = font.MeasureText(null, displayText, new RawRectangle(0, 0, width, height), FontDrawFlags.Center | FontDrawFlags.VerticalCenter);

            int xDir = 1;
            int yDir = 1;

            RenderLoop.Run(form, () =>
            {
                RawColorBGRA red;
                red.R = 255;
                red.G = 0;
                red.B = 0;
                red.A = 1;

                RawColorBGRA black;
                black.R = 1;
                black.G = 1;
                black.B = 1;
                black.A = 1;

                device.Clear(ClearFlags.Target, black, 1.0f, 0);
                device.BeginScene();

                // Make the text boucing on the screen limits
                if ((fontDimension.Right + xDir) > width)
                    xDir = -1;
                else if ((fontDimension.Left + xDir) <= 0)
                    xDir = 1;

                if ((fontDimension.Bottom + yDir) > height)
                    yDir = -1;
                else if ((fontDimension.Top + yDir) <= 0)
                    yDir = 1;

                fontDimension.Left += (int)xDir;
                fontDimension.Top += (int)yDir;
                fontDimension.Bottom += (int)yDir;
                fontDimension.Right += (int)xDir;

                // Draw the text
                font.DrawText(null, displayText, fontDimension, FontDrawFlags.Center | FontDrawFlags.VerticalCenter, red);

                device.EndScene();
                device.Present();
            });
        }
    }
}



The output is just a black window (which is expected), but I do not get my text drawn in there.
Posted
Updated 27-Jul-18 7:45am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900