/// <summary> /// Initializes a new instance of the <see cref="Graphics2D"/> class. /// </summary> /// <param name="device">The hardware device which is used for rendering.</param> /// <param name="renderTarget">The render target which is used by this graphics object.</param> /// <param name="screenSize">The size of the screen in device independent pixels.</param> internal Graphics2D(EngineDevice device, D2D.ID2D1RenderTarget renderTarget, SizeF screenSize) { _transformSettings = Graphics2DTransformSettings.Default; this.TransformStack = new Matrix3x2Stack(); this.Device = device; _renderTarget = renderTarget; this.ScreenPixelSize = screenSize; _deviceContext = (D2D.ID2D1DeviceContext)_renderTarget; this.Internals = new Graphics2DInternals(this); }
/// <summary> /// Sets current transform settings on this graphics object. /// (be careful, the state is changed on device level!) /// </summary> /// <param name="transformSettings">The settings to be set.</param> internal void PushTransformSettings(Graphics2DTransformSettings transformSettings) { _transformSettings = transformSettings; switch (transformSettings.TransformMode) { // Overtake given scaling matrix case Graphics2DTransformMode.Custom: this.TransformStack.Push(transformSettings.CustomTransform); break; // Calculate scaling matrix here case Graphics2DTransformMode.AutoScaleToVirtualScreen: var virtualWidth = _transformSettings.VirtualScreenSize.Width; var virtualHeight = _transformSettings.VirtualScreenSize.Height; if (EngineMath.EqualsWithTolerance(virtualWidth, 0f)) { virtualWidth = this.ScreenPixelSize.Width; } if (EngineMath.EqualsWithTolerance(virtualHeight, 0f)) { virtualHeight = this.ScreenPixelSize.Height; } var scaleFactorX = this.ScreenPixelSize.Width / virtualWidth; var scaleFactorY = this.ScreenPixelSize.Height / virtualHeight; var combinedScaleFactor = Math.Min(scaleFactorX, scaleFactorY); var truePixelWidth = virtualWidth * combinedScaleFactor; var truePixelHeight = virtualHeight * combinedScaleFactor; this.TransformStack.Push(); this.TransformStack.TransformLocal( Matrix3x2.CreateScale(combinedScaleFactor) * Matrix3x2.CreateTranslation(this.ScreenPixelSize.Width / 2f - truePixelWidth / 2f, this.ScreenPixelSize.Height / 2f - truePixelHeight / 2f)); break; default: throw new SeeingSharpGraphicsException($"Unable to handle transform mode {transformSettings.TransformMode}"); } // Apply current transform this.ApplyTransformStack(); }