/// <inheritdoc/> public override void Render(IDrawingContextImpl context) { // TODO: Probably need to introduce some kind of locking mechanism in the case of // WriteableBitmap. context.Transform = Transform; context.DrawImage(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode); }
private void RenderComposite(Scene scene, ref IDrawingContextImpl context) { EnsureDrawingContext(ref context); context.Clear(Colors.Transparent); var clientRect = new Rect(scene.Size); foreach (var layer in scene.Layers) { var bitmap = Layers[layer.LayerRoot].Bitmap; var sourceRect = new Rect(0, 0, bitmap.Item.PixelSize.Width, bitmap.Item.PixelSize.Height); if (layer.GeometryClip != null) { context.PushGeometryClip(layer.GeometryClip); } if (layer.OpacityMask == null) { context.DrawImage(bitmap, layer.Opacity, sourceRect, clientRect); } else { context.DrawImage(bitmap, layer.OpacityMask, layer.OpacityMaskRect, sourceRect); } if (layer.GeometryClip != null) { context.PopGeometryClip(); } } if (_overlay != null) { var sourceRect = new Rect(0, 0, _overlay.Item.PixelSize.Width, _overlay.Item.PixelSize.Height); context.DrawImage(_overlay, 0.5, sourceRect, clientRect); } if (DrawFps) { RenderFps(context, clientRect, scene.Layers.Count); } }
private void RenderComposite(Scene scene, IDrawingContextImpl context) { var clientRect = new Rect(scene.Size); foreach (var layer in scene.Layers) { var bitmap = _layers[layer.LayerRoot].Bitmap; var sourceRect = new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight); if (layer.GeometryClip != null) { context.PushGeometryClip(layer.GeometryClip); } if (layer.OpacityMask == null) { context.DrawImage(bitmap, layer.Opacity, sourceRect, clientRect); } else { context.DrawImage(bitmap, layer.OpacityMask, layer.OpacityMaskRect, sourceRect); } if (layer.GeometryClip != null) { context.PopGeometryClip(); } } if (_overlay != null) { var sourceRect = new Rect(0, 0, _overlay.PixelWidth, _overlay.PixelHeight); context.DrawImage(_overlay, 0.5, sourceRect, clientRect); } if (DrawFps) { RenderFps(context, clientRect, true); } }
public static WriteableBitmap ToWriteableBitmap(Bitmap bmp) { var wb = new WriteableBitmap(bmp.PixelSize, bmp.Dpi, PixelFormat.Bgra8888); using (IRenderTarget rtb = Utils.RenderInterface.CreateRenderTarget(new[] { new WriteableBitmapSurface(wb) })) using (IDrawingContextImpl ctx = rtb.CreateDrawingContext(null)) { var rect = new Rect(bmp.Size); ctx.DrawImage(bmp.PlatformImpl, 1, rect, rect); } bmp.Dispose(); return(wb); }
/// <summary> /// Draws a bitmap image. /// </summary> /// <param name="source">The bitmap image.</param> /// <param name="opacity">The opacity to draw with.</param> /// <param name="sourceRect">The rect in the image to draw.</param> /// <param name="destRect">The rect in the output to draw to.</param> public void DrawImage(IBitmap source, double opacity, Rect sourceRect, Rect destRect) => _impl.DrawImage(source, opacity, sourceRect, destRect);
public void Render(IDrawingContextImpl context, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default) { Read(b => context.DrawImage(_read.PlatformImpl, opacity, sourceRect, destRect, bitmapInterpolationMode)); NotifyRendered(); }
/// <inheritdoc/> public override void Render(IDrawingContextImpl context) { context.Transform = Transform; context.DrawImage(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode); }
public static Bitmap RenderString(string str, StringRenderStyle style) { // Return null for bad strings if (string.IsNullOrWhiteSpace(str)) { return(null); } string path; int charHeight, spaceWidth; switch (style) { case StringRenderStyle.BattleName: path = "BattleName"; charHeight = 11; spaceWidth = 2; break; case StringRenderStyle.BattleLevel: path = "BattleLevel"; charHeight = 10; spaceWidth = 7; break; case StringRenderStyle.BattleHP: path = "BattleHP"; charHeight = 8; spaceWidth = 0; break; default: path = "Default"; charHeight = 15; spaceWidth = 4; break; } int index; string GetCharKey() { string key = $"FONT_{path}_"; if (index + 6 <= str.Length && str.Substring(index, 6) == "[PKMN]") { key += "PKMN"; index += 6; } else if (index + 4 <= str.Length && str.Substring(index, 4) == "[LV]") { key += "LV"; index += 4; } else { key += ((int)str[index]).ToString("X"); index++; } const string questionMark = "FONT_Default_3F"; return(DoesResourceExist($"Kermalis.PokemonBattleEngineClient.FONT.{path}.{key}.png") ? key : questionMark); } // Measure how large the string will end up int stringWidth = 0, stringHeight = charHeight, curLineWidth = 0; index = 0; while (index < str.Length) { if (str[index] == ' ') { index++; curLineWidth += spaceWidth; } else if (str[index] == '\r') { index++; continue; } else if (str[index] == '\n') { index++; stringHeight += charHeight + 1; if (curLineWidth > stringWidth) { stringWidth = curLineWidth; } curLineWidth = 0; } else { string key = GetCharKey(); if (!loadedBitmaps.ContainsKey(key)) { loadedBitmaps.TryAdd(key, UriToBitmap(new Uri($"resm:Kermalis.PokemonBattleEngineClient.FONT.{path}.{key}.png?assembly=PokemonBattleEngineClient"))); } curLineWidth += loadedBitmaps[key].PixelSize.Width; } } if (curLineWidth > stringWidth) { stringWidth = curLineWidth; } // Draw the string var wb = new WriteableBitmap(new PixelSize(stringWidth, stringHeight), new Vector(96, 96), PixelFormat.Bgra8888); using (IRenderTarget rtb = AvaloniaLocator.Current.GetService <IPlatformRenderInterface>().CreateRenderTarget(new[] { new WriteableBitmapSurface(wb) })) using (IDrawingContextImpl ctx = rtb.CreateDrawingContext(null)) { double x = 0, y = 0; index = 0; while (index < str.Length) { if (str[index] == ' ') { index++; x += spaceWidth; } else if (str[index] == '\r') { index++; continue; } else if (str[index] == '\n') { index++; y += charHeight + 1; x = 0; } else { Bitmap bmp = loadedBitmaps[GetCharKey()]; ctx.DrawImage(bmp.PlatformImpl, 1.0, new Rect(0, 0, bmp.PixelSize.Width, charHeight), new Rect(x, y, bmp.PixelSize.Width, charHeight)); x += bmp.PixelSize.Width; } } } // Edit colors using (ILockedFramebuffer l = wb.Lock()) { uint primary = 0xFFFFFFFF, secondary = 0xFF000000, tertiary = 0xFF808080; switch (style) { case StringRenderStyle.MenuBlack: primary = 0xFF5A5252; secondary = 0xFFA5A5AD; break; case StringRenderStyle.BattleWhite: //secondary = 0xF0FFFFFF; break; // Looks horrible because of Avalonia's current issues case StringRenderStyle.MenuWhite: secondary = 0xFF848484; break; case StringRenderStyle.BattleName: case StringRenderStyle.BattleLevel: primary = 0xFFF7F7F7; secondary = 0xFF181818; break; case StringRenderStyle.BattleHP: primary = 0xFFF7F7F7; secondary = 0xFF101010; tertiary = 0xFF9C9CA5; break; } for (int x = 0; x < stringWidth; x++) { for (int y = 0; y < stringHeight; y++) { var address = new IntPtr(l.Address.ToInt64() + (x * sizeof(uint)) + (y * l.RowBytes)); uint pixel = (uint)Marshal.ReadInt32(address); if (pixel == 0xFFFFFFFF) { Marshal.WriteInt32(address, (int)primary); } else if (pixel == 0xFF000000) { Marshal.WriteInt32(address, (int)secondary); } else if (pixel == 0xFF808080) { Marshal.WriteInt32(address, (int)tertiary); } } } } return(wb); }