/// <summary> /// Draw a string of text onscreen /// </summary> /// <param name="text">The text to draw</param> /// <param name="fontname">Name of font to use</param> /// <param name="size">Pt size of font</param> /// <param name="x">Local screen-space x-coordinate to draw at</param> /// <param name="y">Local screen-space y-coordinate to draw at</param> /// <param name="brush"></param> public static void DrawText(string text, string fontname, int size, int x, int y, Brush brush) { StringBuilder sb = new StringBuilder(text); StringBuilder fn = new StringBuilder(fontname); D3DInterop.RtDrawText(sb, text.Length, fn, size, x, y, brush.location); }
/// <summary> /// Deletes releases unmanaged memory for bitmap /// </summary> public void Dispose() { if (!disposed) { D3DInterop.DisposeBitmap(location); disposed = true; } }
/// <summary> /// free as many managed resources as possible and deconstruct the renderer /// </summary> static void DisposeOfEverything() { for (int i = 0; i < objects.Count; i++) { DestroyObject(objects[i]); } mainwindow.Dispose(); D3DInterop.DeInit(); }
/// <summary> /// Loads a bitmap from a file. PNG file format is the only tested type of image /// </summary> /// <param name="filepath">Path to image file</param> /// <returns>New Bitmap object</returns> public static Bitmap fromFile(string filepath) { var str = new StringBuilder(filepath); var intptr = D3DInterop.LoadBitmapFile(str); var bitmap = new Bitmap(); bitmap.location = intptr; str.Clear(); return(bitmap); }
/// <summary> /// Converts from a System.Drawing.Bitmap /// </summary> /// <param name="bitmap">System.Drawing.Bitmap object to convert from</param> /// <returns>New Bitmap object</returns> public static Bitmap FromGDI(System.Drawing.Bitmap bitmap) { //from https://github.com/sharpdx/SharpDX-Samples/blob/master/Desktop/Direct2D1/BitmapApp/Program.cs { var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height); var size = new Size(bitmap.Width, bitmap.Height); // Transform pixels from BGRA to RGBA int stride = bitmap.Width * sizeof(int); using (var tempStream = new MemoryStream(bitmap.Height * stride)) { // Lock System.Drawing.Bitmap var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb); // Convert all pixels for (int y = 0; y < bitmap.Height; y++) { int offset = bitmapData.Stride * y; for (int x = 0; x < bitmap.Width; x++) { byte R = Marshal.ReadByte(bitmapData.Scan0, offset++); byte G = Marshal.ReadByte(bitmapData.Scan0, offset++); byte B = Marshal.ReadByte(bitmapData.Scan0, offset++); byte A = Marshal.ReadByte(bitmapData.Scan0, offset++); int rgba = R | (G << 8) | (B << 16) | (A << 24); byte[] bytes = BitConverter.GetBytes(rgba); for (int i = 0; i < bytes.Length; i++) { tempStream.WriteByte(bytes[i]); } } } bitmap.UnlockBits(bitmapData); tempStream.Position = 0; Bitmap result = new Bitmap(); result.location = D3DInterop.BitmapFromGDI(size.Width, size.Height, tempStream.GetBuffer(), stride); return(result); } } }
/// <summary> /// Initializes the renderer /// </summary> /// <param name="handle">Window handle that the renderer will draw to</param> /// <param name="x">X resolution</param> /// <param name="y">Y resolution</param> /// <param name="fps">Target fps</param> public static void Init(IntPtr handle, int x, int y, int fps) { D3DInterop.Init(handle, x, y, fps); started = true; }
/// <summary> /// stops the sound if if is currently playing /// </summary> public void Stop() { D3DInterop.StopSourceVoice(location); }
/// <summary> /// play the sound, repeats at the previously specified time /// </summary> public void Play() { D3DInterop.PlaySourceVoice(location, xaudiobufloc); }
/// <summary> /// releases unmanaged memory for SourceVoice /// </summary> public void Dispose() { D3DInterop.DisposeSourceVoice(location, bufferloc, xaudiobufloc); }
/// <summary> /// Creates a new SourceVoice form the specified Wav File /// </summary> /// <param name="filename">path to WAV file</param> /// <param name="repeatAt">Seconds at which to loop the audio file</param> public SourceVoice(string filename, float repeatAt) { location = D3DInterop.LoadWAVFile(new StringBuilder(filename), repeatAt, ref bufferloc, ref xaudiobufloc); }
/// <summary> /// Prepares renderer for presenting the drawn image /// </summary> public static void EndDraw() { D3DInterop.EndDraw(Light.active); }
/// <summary> /// Resizes the current game window /// </summary> /// <param name="x">New x resolution</param> /// <param name="y">New y resolution</param> /// <param name="fps">New target fps</param> /// <param name="fullscreen">Set fullscreen property</param> public static void Resize(int x, int y, int fps, bool fullscreen) { D3DInterop.Resize(x, y, fps, fullscreen); }
/// <summary> /// Shows the drawn frame (Call after EndDraw()) /// </summary> public static void Present() { D3DInterop.Present(); }
/// <summary> /// Reset frame /// </summary> public static void Clear() { D3DInterop.Clear(); }
/// <summary> /// Prepares the renderer for drawing instructions /// </summary> public static void BeginDraw() { D3DInterop.BeginDraw(); }
/// <summary> /// Executes drawing instructions for all objects, background, and any expansions /// </summary> static private void Render() { long start = timer.ElapsedMilliseconds; while (paused) { Thread.Sleep(100); return; } SpinWait.SpinUntil(() => render); render = false; camera.CalculateCoords(); if (resize) { D3DInterop.Init(mainwindow.Handle, xres, yres, (int)targetfps); resize = false; return; } RenderTarget.BeginDraw(); //draw background RenderTarget.Clear(); if (bgPtr != null && bgPtr != null) { Point bottomleft = camera.pos - new Point(xres / 2, yres / 2); bottomleft = new Point((int)(bottomleft.x / bgW) * bgW, (int)(bottomleft.y / bgH) * bgH); for (int j = (int)bottomleft.x - 2 * (int)bgW; j < bottomleft.x + xres + 2 * (int)bgW; j += (int)bgW) { for (int k = (int)bottomleft.y - 2 * (int)bgH; k < bottomleft.y + yres + 2 * (int)bgH; k += (int)bgH) { var pos = new Point(j, k); var rectangle = camera.Global2LocalCoords(pos);//.Global2LocalRectangle(pos, (int)bg.Size.Width, (int)bg.Size.Height); RenderTarget.DrawBitmap(bgPtr, (int)rectangle.x, (int)rectangle.y, 0); } } } //render sprites for (int j = 0; j < 5; j++) { lock (objects) for (int i = 0; i < objects.Count(); i++) { var objecti = objects[i]; if (objecti == null || !objecti.active) { continue; } var rectangle = camera.Global2LocalCoords(objecti.pos); bool offscreen = (rectangle.x <-xres || rectangle.x> xres * 2 || rectangle.y <-yres || rectangle.y> yres * 2); if (!offscreen && objecti.layer == j && objecti != null && objecti.sprite != null) { var sprite = objecti.sprite; var rotation = objecti.θ; RenderTarget.DrawBitmap(sprite, (int)rectangle.x, (int)rectangle.y, (int)objecti.θ); var expansions = objecti.GetAllExpansions(); //run all expansions render, based on priority for (int h = 4; h >= 0; h--) { for (int k = 0; k < expansions.Count; k++) { if (expansions[k] != null && expansions[k].Priority == h) { expansions[k].onRender(); } } } } } } //render cursor if (mouse.cursorIcon != null) { RenderTarget.DrawBitmap(mouse.cursorIcon, (int)mouse.location.x, (int)mouse.location.y, 0); } RenderTarget.EndDraw(); RenderTarget.Present(); doLogic = true; //time control favg.Dequeue(); var delta = (timer.ElapsedMilliseconds - lastrender); if (delta > 0) { favg.Enqueue(1000 / delta); } else { favg.Enqueue((float)targetfps); } lastrender = timer.ElapsedMilliseconds; var sleeps = (1000 / targetfps) - ((timer.ElapsedMilliseconds - start)); }
/// <summary> /// creates a solid brush with the specified color /// </summary> /// <param name="r">Red</param> /// <param name="g">Green</param> /// <param name="b">Blue</param> public Brush(float r, float g, float b) { location = D3DInterop.CreateBrush(r, g, b); }
/// <summary> /// releases unmanaged memory for brush /// </summary> public void Dispose() { D3DInterop.DisposeBitmap(location); }
/// <summary> /// Draw the specified bitmap on screen at the local coordinates /// </summary> /// <param name="bitmap">Bitmap to draw</param> /// <param name="x">Local screen-space x-coordinate to draw at</param> /// <param name="y">Local screen-space y-coordinate to draw at</param> /// <param name="rot">Rotation of bitmap</param> public static void DrawBitmap(Bitmap bitmap, int x, int y, int rot) { D3DInterop.DrawBitmap(bitmap.location, x, y, rot); }