internal static void Initialize(Device device) { _debugTexts = new List<string>(); _debugLog = new List<debugTexts>(); _device = device; // Initialize the Font FontDescription fontDescription = new FontDescription() { Height = _textHeight, Italic = false, CharacterSet = FontCharacterSet.Ansi, FaceName = "Console", MipLevels = 0, OutputPrecision = FontPrecision.TrueType, PitchAndFamily = FontPitchAndFamily.Default, Quality = FontQuality.Antialiased, Weight = FontWeight.SemiBold }; _debugTextFont = new Font(device, fontDescription); }
static void Main() { var worldSize = new Size2(1024, 768); var renderSize = new Size2(1920, 1080); const bool windowed = true; const int numParticles = 1000000; const int numEmitters = 5; const int budget = numParticles / numEmitters; var form = new RenderForm("Mercury Particle Engine - SharpDX.Direct3D9 Sample") { Size = new System.Drawing.Size(renderSize.Width, renderSize.Height) }; var direct3d = new Direct3D(); var device = new Device(direct3d, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(renderSize.Width, renderSize.Height) { PresentationInterval = PresentInterval.Immediate, Windowed = windowed }); var view = new Matrix( 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); var proj = Matrix.OrthoOffCenterLH(worldSize.Width * -0.5f, worldSize.Width * 0.5f, worldSize.Height * 0.5f, worldSize.Height * -0.5f, 0f, 1f); var wvp = Matrix.Identity * view * proj; var emitters = new Emitter[numEmitters]; for (int i = 0; i < numEmitters; i++) { emitters[i] = new Emitter(budget, TimeSpan.FromSeconds(600), Profile.BoxFill(worldSize.Width, worldSize.Height)) { Parameters = new ReleaseParameters { Colour = new Colour(220f, 0.7f, 0.1f), Opacity = 1f, Quantity = budget, Speed = 0f, Scale = 1f, Rotation = 0f, Mass = new RangeF(8f, 12f) }, BlendMode = BlendMode.Add, ReclaimInterval = 600f }; emitters[i].Modifiers.Add(new DragModifier { DragCoefficient = .47f, Density = .15f }, 15f); emitters[i].Modifiers.Add(new VortexModifier { Position = Coordinate.Origin, Mass = 200f, MaxSpeed = 1000f }, 30f); emitters[i].Modifiers.Add(new VelocityHueModifier { StationaryHue = 220f, VelocityHue = 300f, VelocityThreshold = 800f }, 15f); emitters[i].Modifiers.Add(new ContainerModifier { RestitutionCoefficient = 0.75f, Position = Coordinate.Origin, Width = worldSize.Width, Height = worldSize.Height }, 30f); emitters[i].Modifiers.Add(new MoveModifier(), 60f); }; var renderer = new PointSpriteRenderer(device, budget) { // EnableFastFade = true }; var texture = Texture.FromFile(device, "Pixel.dds"); var fontDescription = new FontDescription { Height = 16, FaceName = "Consolas", PitchAndFamily = FontPitchAndFamily.Mono, Quality = FontQuality.Draft }; var font = new Font(device, fontDescription); var totalTimer = Stopwatch.StartNew(); var updateTimer = new Stopwatch(); var renderTimer = new Stopwatch(); var totalTime = 0f; foreach (var emitter in emitters) { emitter.Trigger(Coordinate.Origin); } float updateTime = 0f; RenderLoop.Run(form, () => { // ReSharper disable AccessToDisposedClosure var frameTime = ((float)totalTimer.Elapsed.TotalSeconds) - totalTime; totalTime = (float)totalTimer.Elapsed.TotalSeconds; var mousePosition = form.PointToClient(RenderForm.MousePosition); Task.WaitAll( Task.Factory.StartNew(() => { var mouseVector = new Vector3(mousePosition.X, mousePosition.Y, 0f); var unprojected = Vector3.Unproject(mouseVector, 0, 0, renderSize.Width, renderSize.Height, 0f, 1f, wvp); Parallel.ForEach(emitters, emitter => ((VortexModifier)emitter.Modifiers.ElementAt(1)).Position = new Coordinate(unprojected.X, unprojected.Y)); updateTimer.Restart(); Parallel.ForEach(emitters, emitter => emitter.Update(frameTime)); updateTimer.Stop(); updateTime = (float)updateTimer.Elapsed.TotalSeconds; _updateTimes.Add(updateTime); }), Task.Factory.StartNew(() => { device.Clear(ClearFlags.Target, Color.Black, 1f, 0); device.BeginScene(); renderTimer.Restart(); for (int i = 0; i < numEmitters; i++) { renderer.Render(emitters[i], wvp, texture); } renderTimer.Stop(); var renderTime = (float)renderTimer.Elapsed.TotalSeconds; var totalUpdateTime = 0f; // foreach (var time in _updateTimes) // { // totalUpdateTime += time; // } // totalUpdateTime /= _updateTimes.Count; // // if(_updateTimes.Count > 100) // _updateTimes.RemoveAt(0); font.DrawText(null, String.Format("Time: {0}", totalTimer.Elapsed), 0, 0, Color.White); font.DrawText(null, String.Format("Particles: {0:n0}", emitters[0].ActiveParticles * numEmitters), 0, 16, Color.White); font.DrawText(null, String.Format("Update: {0:n4} ({1,8:P2})", updateTime, updateTime / 0.01666666f), 0, 32, Color.White); font.DrawText(null, String.Format("Render: {0:n4} ({1,8:P2})", renderTime, renderTime / 0.01666666f), 0, 48, Color.White); device.EndScene(); device.Present(); }) ); if (Keyboard.IsKeyDown(Key.Escape)) Environment.Exit(0); // ReSharper restore AccessToDisposedClosure }); form.Dispose(); font.Dispose(); device.Dispose(); direct3d.Dispose(); }
/// <summary> /// Initializes a new instance of the <see cref="Font"/> class. /// </summary> /// <param name="device">The device.</param> /// <param name="fontDescription">The font description.</param> public Font(Device device, FontDescription fontDescription) : base(IntPtr.Zero) { D3DX9.CreateFontIndirect(device, ref fontDescription, this); }
static void Main() { var form = new RenderForm("SharpDX - Direct3D9 Font Sample"); int width = form.ClientSize.Width; int height = form.ClientSize.Height; var device = new 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 Rectangle(0, 0, width, height), FontDrawFlags.Center | FontDrawFlags.VerticalCenter); int xDir = 1; int yDir = 1; RenderLoop.Run(form, () => { device.Clear(ClearFlags.Target, Color.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, Color.White); device.EndScene(); device.Present(); }); }
void aquireResources() { if (videoWidth == 0 || videoHeight == 0) { throw new VideoPlayerException("Cannot instantiate D3D surface with a width or height of 0 pixels"); } D3D.Format pixelFormat = makeFourCC('Y', 'V', '1', '2'); offscreen = D3D.Surface.CreateOffscreenPlain(device, videoWidth, videoHeight, pixelFormat, D3D.Pool.Default); screenShot = D3D.Surface.CreateOffscreenPlain(device, videoWidth, videoHeight, D3D.Format.A8R8G8B8, D3D.Pool.Default); backBuffer = device.GetBackBuffer(0, 0); FontDescription fontDescription = new FontDescription(); fontDescription.FaceName = "TimesNewRoman"; fontDescription.Height = 15; infoFont = new D3D.Font(device, fontDescription); fontDescription = new FontDescription(); fontDescription.FaceName = "Arial"; videoDestRect = getVideoDestRect(backBuffer); fontDescription.Height = videoDestRect.Height / 14; fontDescription.Quality = FontQuality.Antialiased; subtitleShadowOffset = fontDescription.Height / 18; subtitleBottomMargin = videoDestRect.Height / 12; subtitleFont = new D3D.Font(device, fontDescription); }
public static void Initialize_Menu() { var fontDescription = new FontDescription() { Height = 40, Italic = false, CharacterSet = FontCharacterSet.Ansi, FaceName = "Arial", MipLevels = 0, OutputPrecision = FontPrecision.TrueType, PitchAndFamily = FontPitchAndFamily.Default, Quality = FontQuality.ClearType, Weight = FontWeight.Bold }; font = new SharpDX.Direct3D9.Font(Program.device, fontDescription); _menuTabMain = new EltInfo[] { new EltInfo(Vector2.Zero, new Vector2(Program.resolution[0], Program.resolution[1]), @"Ressources\Game\Images\bg.jpg", EltType.Default, String.Empty), new EltInfo(new Vector2(0, 3*Program.resolution[1]/5), new Vector2(Program.resolution[0], 2*Program.resolution[1]/5), @"Ressources\Menu\smoke2.png", EltType.Default, String.Empty), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Play"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Settings"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Exit"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Resolution"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "-"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Info, Program.resolution[0] + " x " + Program.resolution[1]), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "+"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 300), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Volume"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 300), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "- "), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 300), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Info, Get_Volume()), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 300), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "+ "), new EltInfo(new Vector2(Program.resolution[0]/2 - 140, Program.resolution[1] + 400), new Vector2(120, 50), @"Ressources\HUD\button.png", EltType.Button, "Back") }; _menuTabSettings = new EltInfo[] { new EltInfo(Vector2.Zero, new Vector2(Program.resolution[0], Program.resolution[1]), @"Ressources\Game\Images\bg.jpg", EltType.Default, String.Empty), new EltInfo(new Vector2(0, 3*Program.resolution[1]/5), new Vector2(Program.resolution[0], 2*Program.resolution[1]/5), @"Ressources\Menu\smoke1.png", EltType.Default, String.Empty), new EltInfo(new Vector2(Program.resolution[0] + 200, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Play"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Settings"), new EltInfo(new Vector2(Program.resolution[0] + 200, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Exit"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 ), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Info, "Resolution"), new EltInfo(new Vector2(Program.resolution[0]/2 + 150, Program.resolution[1]/2), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "-"), new EltInfo(new Vector2(Program.resolution[0]/2 + 210, Program.resolution[1] /2), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Info, Program.resolution[0] + " x " + Program.resolution[1]), new EltInfo(new Vector2(Program.resolution[0]/2 + 460, Program.resolution[1] /2), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "+"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Volume"), new EltInfo(new Vector2(Program.resolution[0]/2 + 150, Program.resolution[1]/2 + 100), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "- "), new EltInfo(new Vector2(Program.resolution[0]/2 + 210, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Info, Get_Volume()), new EltInfo(new Vector2(Program.resolution[0]/2 +460, Program.resolution[1]/2 + 100), new Vector2(50, 50), @"Ressources\HUD\button.png", EltType.Button, "+ "), new EltInfo(new Vector2(Program.resolution[0]/2 - 140, Program.resolution[1]/2 + 200), new Vector2(120, 50), @"Ressources\HUD\button.png", EltType.Button, "Back") }; _menuTabExtra = new EltInfo[] { new EltInfo(Vector2.Zero, new Vector2(Program.resolution[0], Program.resolution[1]), @"Ressources\Game\Images\bg.jpg", EltType.Default, String.Empty), new EltInfo(new Vector2(0, 3*Program.resolution[1]/5), new Vector2(Program.resolution[0], 2*Program.resolution[1]/5), @"Ressources\Menu\smoke1.png", EltType.Default, String.Empty), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Play"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Settings"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Exit"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Resolution"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "-"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, Program.resolution[0] + " x " + Program.resolution[1]), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1] + 200), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "+"), new EltInfo(new Vector2(Program.resolution[0]/2 - 140, Program.resolution[1] + 200), new Vector2(120, 50), @"Ressources\HUD\button.png", EltType.Button, "Back") }; _pauseTab = new EltInfo[] { new EltInfo(new Vector2(0, 3*Program.resolution[1]/5), new Vector2(Program.resolution[0], 2*Program.resolution[1]/5), @"Ressources\Menu\smoke2.png", EltType.Default, String.Empty), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Resume"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Exit") }; _pauseTab2 = new EltInfo[] { new EltInfo(new Vector2(0, 3*Program.resolution[1]/5), new Vector2(Program.resolution[0], 2*Program.resolution[1]/5), @"Ressources\Menu\smoke2.png", EltType.Default, String.Empty), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 - 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Play again"), new EltInfo(new Vector2(Program.resolution[0]/2 - 120, Program.resolution[1]/2 + 100), new Vector2(240, 50), @"Ressources\HUD\button.png", EltType.Button, "Exit") }; IsInMenu = true; _switchTo = _tabTmp = new EltInfo[_menuTabMain.Length]; Array.Copy(_menuTabMain, _switchTo, _menuTabMain.Length); Array.Copy(_menuTabMain, _tabTmp, _menuTabMain.Length); }