/// <summary> /// Renders your user contents to the screen. /// Before running this, make sure that <see cref="Direct2DRenderMethod"/> is assigned. /// </summary> public void DirectXRender() { lock (RenderLock) { // If the window/surface we are rendering on is correctly set up and ready. if (Direct2DSetupComplete) { // Begin Drawing! Direct2DWindowTarget.BeginDraw(); // Clear everything drawn previously. ClearScreen(0, 0, 0, 0); // Call our own rendering methods assigned to the delegate. Direct2DRenderMethod?.Invoke(Direct2DWindowTarget); // Run any of our own assigned code to be run after rendering occurs... safely try { Direct2DOnframeDelegate?.Invoke(); } catch (Exception ex) { Bindings.PrintWarning?.Invoke("[libReloaded] Exception thrown in user code ran on Window " + "overlay frame render, let the mod/application developer know he screwed up | " + ex.Message); } // End Drawing Direct2DWindowTarget.EndDraw(); } } }
/// <summary> /// Resizes the Direct2D window to match the client area size of a specified window handle. /// </summary> /// <param name="targetWindowHandle">The handle of the window of whose client area size should be matched.</param> public void ResizeWindow(IntPtr targetWindowHandle) { // Wait for any draw operation to finish. lock (RenderLock) { // Retrieve window size of target window. Point windowSize = WindowProperties.GetClientAreaSize2(targetWindowHandle); // Resize the D2D WindowRenderTarget Direct2DWindowTarget.Resize(new Size2(windowSize.X, windowSize.Y)); } }
/// <summary> /// Initializes a Direct2D device used to draw to the screen. /// For drawing, you MUST add your methods to the Direct2D Rendering Delegate (direct2DRenderMethod). /// </summary> public void InitializeDirectX(IntPtr targetWindowHandle) { try { // Wait for any draw operation to finish. lock (RenderLock) { // Mark D2D Setup as incomplete, disallow Rendering. Direct2DSetupComplete = false; // Dispose Render Target if Necessary Direct2DWindowTarget?.Dispose(); // Create the D2D Factory which aids with the creation of a WindowRenderTarget object. Factory direct2DFactory = new Factory(FactoryType.SingleThreaded); // Retrieve window size of target window. Point windowSize = WindowProperties.GetClientAreaSize2(targetWindowHandle); // Set the render properties! HwndRenderTargetProperties direct2DRenderTargetProperties = new HwndRenderTargetProperties { Hwnd = targetWindowHandle, PixelSize = new Size2(windowSize.X, windowSize.Y), PresentOptions = PresentOptions.None }; // Assign the Window Render Target Direct2DWindowTarget = new WindowRenderTarget ( direct2DFactory, new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)), direct2DRenderTargetProperties ); // Clear the screen of the Window Render Target. DirectXClearScreen(); // Mark D2D Setup as Complete, allow Rendering. Direct2DSetupComplete = true; } } catch (Exception ex) { Bindings.PrintError?.Invoke("[libReloaded] Failed to initialize DirectX rendering to window | " + ex.Message); } }
/// <summary> /// Draws over the entire form/window with RGBA(0,0,0,0). /// This clears the entire screen of any previously drawn graphics. /// </summary> public void DirectXClearScreen() { lock (RenderLock) { // If the window/surface we are rendering on is correctly set up and ready. if (Direct2DSetupComplete) { // Begin Drawing! Direct2DWindowTarget.BeginDraw(); // Clears everything drawn previously. ClearScreen(0, 0, 0, 0); // End Drawing! Direct2DWindowTarget.EndDraw(); } } }
/// <summary> /// Clears the drawing area with the specified RGBA Colours. Use this to wipe the screen. /// </summary> private void ClearScreen(float r, float g, float b, float a) { Direct2DWindowTarget.Clear(new RawColor4(r, g, b, a)); }