Jypeli 10
The simple game programming library
FontStashSharpMGclasses.cs
Siirry tämän tiedoston dokumentaatioon.
1/*
2 * Original files by Roman Shapiro: https://github.com/rds1983/FontStashSharp/tree/7ec7dbff73eb0826fb1a830fcb2c5bb671095c08/samples/FontStashSharp.Samples.MonoGameBackend
3 * Renderer.cs, Texture2DManager.cs & Utility.cs
4 * Copied here with minor modifications
5 */
6
7using FontStashSharp.Interfaces;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Graphics;
10using System;
11using System.Drawing;
12using System.Numerics;
13using Color = System.Drawing.Color;
14using Rectangle = System.Drawing.Rectangle;
15using Vector2 = System.Numerics.Vector2;
16
18{
19 class Renderer : IFontStashRenderer
20 {
21 SpriteBatch _batch;
22
23 public Renderer(SpriteBatch batch)
24 {
25 if (batch == null)
26 {
27 throw new ArgumentNullException(nameof(batch));
28 }
29
30 _batch = batch;
31 }
32
33 public void Draw(object texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, float depth)
34 {
35 var textureWrapper = (Texture2D)texture;
36
37 _batch.Draw(textureWrapper,
38 position.ToXNA(),
39 sourceRectangle == null ? default(Microsoft.Xna.Framework.Rectangle?) : sourceRectangle.Value.ToXNA(),
40 color.ToXNA(),
41 rotation,
42 origin.ToXNA(),
43 scale.ToXNA(),
44 SpriteEffects.None,
45 depth);
46 }
47 }
48 class Texture2DManager : ITexture2DManager
49 {
50 readonly GraphicsDevice _device;
51
52 public Texture2DManager(GraphicsDevice device)
53 {
54 if (device == null)
55 {
56 throw new ArgumentNullException(nameof(device));
57 }
58
59 _device = device;
60 }
61
62 public object CreateTexture(int width, int height)
63 {
64 return new Texture2D(_device, width, height);
65 }
66
67 public void SetTextureData(object texture, Rectangle bounds, byte[] data)
68 {
69 var mgTexture = (Texture2D)texture;
70 mgTexture.SetData(0, 0, new Microsoft.Xna.Framework.Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height),
71 data, 0, bounds.Width * bounds.Height * 4);
72 }
73 }
74
75 static class Utility
76 {
77 public static Microsoft.Xna.Framework.Vector2 ToXNA(this System.Numerics.Vector2 r)
78 {
79 return new Microsoft.Xna.Framework.Vector2(r.X, r.Y);
80 }
81
82 public static System.Numerics.Vector2 ToSystemNumerics(this Microsoft.Xna.Framework.Vector2 r)
83 {
84 return new System.Numerics.Vector2(r.X, r.Y);
85 }
86
87 public static Microsoft.Xna.Framework.Rectangle ToXNA(this System.Drawing.Rectangle r)
88 {
89 return new Microsoft.Xna.Framework.Rectangle(r.Left, r.Top, r.Width, r.Height);
90 }
91
92 public static System.Drawing.Rectangle ToSystemDrawing(this Microsoft.Xna.Framework.Rectangle r)
93 {
94 return new System.Drawing.Rectangle(r.Left, r.Top, r.Width, r.Height);
95 }
96
97
98 public static Microsoft.Xna.Framework.Color ToXNA(this System.Drawing.Color c)
99 {
100 return new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A);
101 }
102
103 public static System.Drawing.Color ToSystemDrawing(this Microsoft.Xna.Framework.Color c)
104 {
105 return System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B);
106 }
107 }
108}
System.Numerics.Vector2 Vector2
System.Drawing.Color Color
System.Drawing.Rectangle Rectangle
void Draw(object texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, float depth)
void SetTextureData(object texture, Rectangle bounds, byte[] data)
object CreateTexture(int width, int height)
static Microsoft.Xna.Framework.Vector2 ToXNA(this System.Numerics.Vector2 r)
static Microsoft.Xna.Framework.Rectangle ToXNA(this System.Drawing.Rectangle r)
static System.Drawing.Color ToSystemDrawing(this Microsoft.Xna.Framework.Color c)
static System.Drawing.Rectangle ToSystemDrawing(this Microsoft.Xna.Framework.Rectangle r)
static System.Numerics.Vector2 ToSystemNumerics(this Microsoft.Xna.Framework.Vector2 r)
static Microsoft.Xna.Framework.Color ToXNA(this System.Drawing.Color c)