-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.cs
305 lines (263 loc) · 9.26 KB
/
Graphics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.IO;
using SDL2;
using StbSharp;
namespace Melon
{
public struct Color
{
public byte r;
public byte g;
public byte b;
public byte a;
public static Color white => new Color(255, 255, 255);
public static Color black => new Color(0, 0, 0);
public static Color red => new Color(255, 0, 0);
public static Color green => new Color(0, 255, 0);
public static Color blue => new Color(0, 0, 255);
public static Color yellow => new Color(255, 255, 0);
public static Color cyan => new Color(0, 255, 255);
public static Color magenta => new Color(255, 0, 255);
public static Color clear => new Color(0, 0, 0, 0);
public Color(byte r, byte g, byte b, byte a = 255)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
internal Color(SDL.SDL_Color color)
{
r = color.r;
g = color.g;
b = color.b;
a = color.a;
}
internal SDL.SDL_Color ToSDLColor() => new SDL.SDL_Color { r = r, g = g, b = b, a = a };
}
public struct Rect
{
public float x;
public float y;
public float w;
public float h;
public Rect(float x, float y, float w, float h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
internal SDL_gpu.GPU_Rect ToGPURect() => new SDL_gpu.GPU_Rect { x = x, y = y, w = w, h = h };
}
public class Image
{
internal IntPtr Ptr; // GPU_Image*
public int Width { get; private set; }
public int Height { get; private set; }
private Color _color;
public Color Color
{
get { return _color; }
set
{
_color = value;
SDL_gpu.GPU_SetRGBA(Ptr, _color.r, _color.g, _color.b, _color.a);
}
}
internal Image(int width, int height)
{
this.Width = width;
this.Height = height;
}
~Image()
{
if (Ptr != IntPtr.Zero)
{
SDL_gpu.GPU_FreeImage(Ptr);
Ptr = IntPtr.Zero;
}
}
}
public class RenderTarget
{
internal IntPtr TargetPtr; // GPU_Target*;
internal IntPtr ImagePtr; // GPU_Image*;
public int Width { get; private set; }
public int Height { get; private set; }
internal RenderTarget(int width, int height)
{
this.Width = width;
this.Height = height;
}
~RenderTarget()
{
if (TargetPtr != IntPtr.Zero && ImagePtr != IntPtr.Zero)
{
SDL_gpu.GPU_FreeTarget(TargetPtr);
TargetPtr = IntPtr.Zero;
ImagePtr = IntPtr.Zero;
}
}
}
public static class Graphics
{
public enum DrawMode
{
Line,
Fill
}
internal static IntPtr MainScreen { get; set; }
#region Global state
private static SDL.SDL_Color _backgroundColor;
#endregion
#region Color state
public static void SetBackgroundColor(Color color)
{
SetBackgroundColor(color.r, color.g, color.b);
}
public static void SetBackgroundColor(byte r, byte g, byte b)
{
_backgroundColor.r = r;
_backgroundColor.g = g;
_backgroundColor.b = b;
}
public static Color GetBackgroundColor() => new Color(_backgroundColor);
public static void ClearRenderTarget(RenderTarget target, Color color)
{
SDL_gpu.GPU_ClearRGB(target.TargetPtr, color.r, color.g, color.b);
}
#endregion
#region Shapes
public static void Pixel(float x, float y, Color color, RenderTarget target = null)
{
SDL_gpu.GPU_Pixel(target?.TargetPtr ?? MainScreen, x, y, color.ToSDLColor());
}
public static void Line(float x1, float y1, float x2, float y2, Color color, RenderTarget target = null)
{
SDL_gpu.GPU_Line(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, color.ToSDLColor());
}
public static void Circle(DrawMode mode, float x, float y, float radius, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Circle(target?.TargetPtr ?? MainScreen, x, y, radius, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_CircleFilled(target?.TargetPtr ?? MainScreen, x, y, radius, color.ToSDLColor()); break;
}
}
public static void Arc(DrawMode mode, float x, float y, float radius, float start_angle, float end_angle, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Arc(target?.TargetPtr ?? MainScreen, x, y, radius, start_angle, end_angle, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_ArcFilled(target?.TargetPtr ?? MainScreen, x, y, radius, start_angle, end_angle, color.ToSDLColor()); break;
}
}
public static void Ellipse(DrawMode mode, float x, float y, float rx, float ry, float degrees, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Ellipse(target?.TargetPtr ?? MainScreen, x, y, rx, ry, degrees, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_EllipseFilled(target?.TargetPtr ?? MainScreen, x, y, rx, ry, degrees, color.ToSDLColor()); break;
}
}
public static void Sector(DrawMode mode, float x, float y, float inner_radius, float outer_radius, float start_angle, float end_angle, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Sector(target?.TargetPtr ?? MainScreen, x, y, inner_radius, outer_radius, start_angle, end_angle, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_SectorFilled(target?.TargetPtr ?? MainScreen, x, y, inner_radius, outer_radius, start_angle, end_angle, color.ToSDLColor()); break;
}
}
public static void Triangle(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Tri(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, x3, y3, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_TriFilled(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, x3, y3, color.ToSDLColor()); break;
}
}
public static void Rectangle(DrawMode mode, float x1, float y1, float x2, float y2, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Rectangle(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_RectangleFilled(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, color.ToSDLColor()); break;
}
}
public static void RectangleRounded(DrawMode mode, float x1, float y1, float x2, float y2, float radius, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_RectangleRound(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, radius, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_RectangleRoundFilled(target?.TargetPtr ?? MainScreen, x1, y1, x2, y2, radius, color.ToSDLColor()); break;
}
}
public static void Polygon(DrawMode mode, float[] vertices, Color color, RenderTarget target = null)
{
switch (mode)
{
case DrawMode.Line: SDL_gpu.GPU_Polygon(target?.TargetPtr ?? MainScreen, (uint)vertices.Length / 2, vertices, color.ToSDLColor()); break;
case DrawMode.Fill: SDL_gpu.GPU_PolygonFilled(target?.TargetPtr ?? MainScreen, (uint)vertices.Length / 2, vertices, color.ToSDLColor()); break;
}
}
#endregion
#region Images
public static Image NewImage(string filepath)
{
byte[] fileBytes = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), filepath));
return NewImage(fileBytes);
}
/// <summary>
/// Returns a new Image reference
/// </summary>
/// <param name="bytes">Array of raw bytes from an image file</param>
/// <returns>Image object</returns>
public static Image NewImage(byte[] bytes)
{
var stbImage = StbImage.LoadFromMemory(bytes);
Image image = new Image(stbImage.Width, stbImage.Height);
SDL_gpu.GPU_FormatEnum format = SDL_gpu.GPU_FormatEnum.GPU_FORMAT_RGB;
switch (stbImage.Comp)
{
case 3: format = SDL_gpu.GPU_FormatEnum.GPU_FORMAT_RGB; break;
case 4: format = SDL_gpu.GPU_FormatEnum.GPU_FORMAT_RGBA; break;
default: break;
}
image.Ptr = SDL_gpu.GPU_CreateImage((ushort)image.Width, (ushort)image.Height, format);
var rect = new SDL_gpu.GPU_Rect { x = 0, y = 0, w = image.Width, h = image.Height };
SDL_gpu.GPU_UpdateImageBytes(image.Ptr, ref rect, stbImage.Data, stbImage.Width * stbImage.Comp);
return image;
}
public static void DrawImage(Image image, float x, float y, RenderTarget target = null)
{
var rect = new SDL_gpu.GPU_Rect { x = 0, y = 0, w = image.Width, h = image.Height };
SDL_gpu.GPU_Blit(image.Ptr, ref rect, target?.TargetPtr ?? MainScreen, x, y);
}
public static void DrawImage(Image image, Rect rect, float x, float y, RenderTarget target = null)
{
var gpuRect = rect.ToGPURect();
SDL_gpu.GPU_Blit(image.Ptr, ref gpuRect, target?.TargetPtr ?? MainScreen, x, y);
}
#endregion
#region RenderTarget
public static RenderTarget NewRenderTarget(int width, int height)
{
IntPtr image = SDL_gpu.GPU_CreateImage((ushort)width, (ushort)height, SDL_gpu.GPU_FormatEnum.GPU_FORMAT_RGB);
SDL_gpu.GPU_SetImageFilter(image, SDL_gpu.GPU_FilterEnum.GPU_FILTER_NEAREST);
IntPtr target = SDL_gpu.GPU_LoadTarget(image);
var rt = new RenderTarget(width, height)
{
ImagePtr = image,
TargetPtr = target
};
return rt;
}
public static void DrawRenderTarget(RenderTarget target, int x, int y, int scaleX, int scaleY)
{
var rect = new SDL_gpu.GPU_Rect { x = 0, y = 0, w = target.Width, h = target.Height };
SDL_gpu.GPU_BlitScale(target.ImagePtr, ref rect, MainScreen, x, y, scaleX, scaleY);
}
#endregion
}
}