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:
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 });
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!";
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();
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;
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.