| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Drawing; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using static Imagini.ErrorHandler; |
| | | 8 | | using static SDL2.SDL_blendmode; |
| | | 9 | | using static SDL2.SDL_error; |
| | | 10 | | using static SDL2.SDL_hints; |
| | | 11 | | using static SDL2.SDL_rect; |
| | | 12 | | using static SDL2.SDL_render; |
| | | 13 | | |
| | | 14 | | namespace Imagini.Drawing |
| | | 15 | | { |
| | | 16 | | public sealed class Graphics : Resource |
| | | 17 | | { |
| | | 18 | | internal IntPtr Handle; |
| | 126 | 19 | | public RendererInfo Renderer { get; private set; } |
| | | 20 | | /// <summary> |
| | | 21 | | /// Returns the output size of this renderer in pixels. |
| | | 22 | | /// </summary> |
| | | 23 | | public Size OutputSize |
| | | 24 | | { |
| | | 25 | | get |
| | | 26 | | { |
| | 206 | 27 | | var w = 0; var h = 0; |
| | 103 | 28 | | Try(() => SDL_GetRendererOutputSize(Handle, out w, out h), |
| | 0 | 29 | | "SDL_GetRendererOutputSize"); |
| | 103 | 30 | | return new Size(w, h); |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the output pixel count. |
| | | 36 | | /// </summary> |
| | | 37 | | public int PixelCount |
| | | 38 | | { |
| | | 39 | | get |
| | | 40 | | { |
| | 7 | 41 | | var size = OutputSize; |
| | 7 | 42 | | return size.Width * size.Height; |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | [ExcludeFromCodeCoverage] |
| | | 47 | | internal Graphics(Window owner, RendererInfo rendererInfo) |
| | | 48 | | { |
| | | 49 | | var fallbackRenderer = |
| | | 50 | | RendererInfo.All.FirstOrDefault(r => |
| | 63 | 51 | | r.IsHardwareAccelerated && r.SupportsRenderingToTexture) ?? |
| | | 52 | | RendererInfo.All.FirstOrDefault(r => |
| | 0 | 53 | | r.IsHardwareAccelerated) ?? |
| | | 54 | | RendererInfo.All.First(); |
| | | 55 | | |
| | | 56 | | Renderer = rendererInfo ?? fallbackRenderer; |
| | | 57 | | Handle = SDL_CreateRenderer(owner.Handle, Renderer.Index, 0); |
| | | 58 | | if (Handle == IntPtr.Zero) |
| | | 59 | | throw new ImaginiException($"Could not initialize renderer: {SDL_GetError()}"); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Creates a texture with the specified dimensions, format and access |
| | | 64 | | /// type. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="w">Width in pixels</param> |
| | | 67 | | /// <param name="h">Height in pixels</param> |
| | | 68 | | /// <param name="quality">Filtering quality when texture is scaled</param> |
| | | 69 | | /// <param name="format">Pixel format</param> |
| | | 70 | | /// <param name="access">Texture access type</param> |
| | | 71 | | public Texture CreateTexture(int w, int h, |
| | | 72 | | TextureScalingQuality quality = TextureScalingQuality.Nearest, |
| | | 73 | | PixelFormat format = PixelFormat.Format_ARGB8888, |
| | | 74 | | TextureAccess access = TextureAccess.Static) |
| | | 75 | | { |
| | 0 | 76 | | Try(() => |
| | 37 | 77 | | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality.AsHint()), |
| | 0 | 78 | | "SDL_SetHint"); |
| | 37 | 79 | | var texture = SDL_CreateTexture(Handle, (uint)format, (int)access, w, h); |
| | 37 | 80 | | if (texture == IntPtr.Zero) |
| | 0 | 81 | | throw new ImaginiException($"Could not create texture: {SDL_GetError()}"); |
| | 37 | 82 | | return new Texture(texture, this); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Creates a static texture from an existing surface. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="surface">Surface to create texture from</param> |
| | | 89 | | /// <param name="quality">Filtering quality when texture is scaled</param> |
| | | 90 | | /// <remarks> |
| | | 91 | | /// The surface is not modified or disposed by this function. |
| | | 92 | | /// <see cref="TextureAccess" /> is static. |
| | | 93 | | /// The pixel format of the created texture may be different from the |
| | | 94 | | /// pixel format of the surface. |
| | | 95 | | /// </remarks> |
| | | 96 | | public Texture CreateTextureFromSurface(Surface surface, |
| | | 97 | | TextureScalingQuality quality = TextureScalingQuality.Nearest) |
| | | 98 | | { |
| | 0 | 99 | | Try(() => |
| | 1 | 100 | | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality.AsHint()), |
| | 0 | 101 | | "SDL_SetHint"); |
| | 1 | 102 | | var texture = SDL_CreateTextureFromSurface(Handle, surface.Handle); |
| | 1 | 103 | | if (texture == IntPtr.Zero) |
| | 0 | 104 | | throw new ImaginiException($"Could not create texture: {SDL_GetError()}"); |
| | 1 | 105 | | return new Texture(texture, this); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private Texture _renderTarget = null; |
| | | 109 | | /// <summary> |
| | | 110 | | /// Returns the currently active render target or null if it's not set. |
| | | 111 | | /// </summary> |
| | 3 | 112 | | public Texture GetRenderTarget() => _renderTarget; |
| | | 113 | | /// <summary> |
| | | 114 | | /// Sets the active render target. If null is specified, sets the |
| | | 115 | | /// default render target. |
| | | 116 | | /// </summary> |
| | | 117 | | public void SetRenderTarget(Texture texture) |
| | | 118 | | { |
| | 2 | 119 | | if (texture != null && texture.Access != TextureAccess.Target) |
| | 0 | 120 | | throw new ImaginiException("Texture should have Target access"); |
| | 2 | 121 | | Try(() => SDL_SetRenderTarget(Handle, texture?.Handle ?? IntPtr.Zero), |
| | 2 | 122 | | "SDL_SetRenderTarget"); |
| | 2 | 123 | | _renderTarget = texture; |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Reads the pixel data from current render target to the specified pixel data buffer. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="pixelData"></param> |
| | | 130 | | /// <param name="rect">Rectangle to read data from, or null to read entire texture</param> |
| | | 131 | | /// <remarks>This function is pretty slow and shouldn't be used often.</remarks> |
| | | 132 | | public void ReadPixels(ref ColorRGB888[] pixelData, Rectangle? rectangle = null) |
| | | 133 | | { |
| | 32 | 134 | | if (pixelData.Length < GetPixelBufferSize(rectangle)) |
| | 0 | 135 | | throw new ArgumentOutOfRangeException("Pixel array is too small"); |
| | 32 | 136 | | SDL_Rect? rect = rectangle?.ToSDL(); |
| | 32 | 137 | | var rectHandle = Pin(rect); |
| | 32 | 138 | | var pixelHandle = Pin(pixelData); |
| | 32 | 139 | | var width = rect?.w ?? OutputSize.Width; |
| | | 140 | | try |
| | | 141 | | { |
| | | 142 | | unsafe |
| | | 143 | | { |
| | 32 | 144 | | var pitch = width * 4; |
| | 0 | 145 | | Try(() => SDL_RenderReadPixels(Handle, |
| | 0 | 146 | | rectHandle.AddrOfPinnedObject(), |
| | 0 | 147 | | (uint)PixelFormat.Format_RGB888, |
| | 0 | 148 | | pixelHandle.AddrOfPinnedObject(), |
| | 0 | 149 | | pitch), |
| | 0 | 150 | | "SDL_RenderReadPixels"); |
| | | 151 | | } |
| | 32 | 152 | | } |
| | | 153 | | finally |
| | | 154 | | { |
| | 32 | 155 | | pixelHandle.Free(); |
| | 32 | 156 | | rectHandle.Free(); |
| | 32 | 157 | | } |
| | 32 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Calculates the buffer size needed for pixel writing and reading |
| | | 162 | | /// operations. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="rectangle">Rectangle to read the data from, or null to read entire texture</param> |
| | | 165 | | /// <seealso cref="Lock" /> |
| | | 166 | | public int GetPixelBufferSize(Rectangle? rectangle = null) |
| | | 167 | | { |
| | 49 | 168 | | var size = OutputSize; |
| | 49 | 169 | | return Texture.InternalGetPixelBufferSize(size.Width, size.Height, rectangle); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Sets the current drawing color. |
| | | 174 | | /// </summary> |
| | | 175 | | public void SetDrawingColor(Color color) => |
| | 0 | 176 | | Try(() => |
| | 65 | 177 | | SDL_SetRenderDrawColor(Handle, color.R, color.G, color.B, color.A), |
| | 0 | 178 | | "SDL_SetRenderDrawColor"); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets the current drawing color. |
| | | 182 | | /// </summary> |
| | | 183 | | public Color GetDrawingColor() |
| | | 184 | | { |
| | 132 | 185 | | byte r = 0; byte g = 0; byte b = 0; byte a = 0; |
| | 0 | 186 | | Try(() => |
| | 33 | 187 | | SDL_GetRenderDrawColor(Handle, ref r, ref g, ref b, ref a), |
| | 0 | 188 | | "SDL_GetRenderDrawColor"); |
| | 33 | 189 | | return Color.FromArgb(a, r, g, b); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Sets the blend mode. |
| | | 194 | | /// </summary> |
| | | 195 | | public void SetBlendMode(BlendMode mode) => |
| | 6 | 196 | | Try(() => SDL_SetRenderDrawBlendMode(Handle, (SDL_BlendMode)mode), |
| | 6 | 197 | | "SDL_SetRenderDrawBlendMode"); |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Gets the blend mode. |
| | | 201 | | /// </summary> |
| | | 202 | | public BlendMode GetBlendMode() |
| | | 203 | | { |
| | 6 | 204 | | SDL_BlendMode val = SDL_BlendMode.SDL_BLENDMODE_NONE; |
| | 6 | 205 | | Try(() => SDL_GetRenderDrawBlendMode(Handle, ref val), |
| | 0 | 206 | | "SDL_GetRenderDrawBlendMode"); |
| | 6 | 207 | | return (BlendMode)val; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Sets the clipping rectangle. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="rect">Clipping rectangle. If null, disables clipping</param> |
| | | 214 | | public void SetClipRectangle(Rectangle? rect) |
| | | 215 | | { |
| | 2 | 216 | | var rectHandle = Pin(rect?.ToSDL()); |
| | | 217 | | try |
| | | 218 | | { |
| | | 219 | | unsafe |
| | | 220 | | { |
| | 0 | 221 | | Try(() => SDL_RenderSetClipRect(Handle, |
| | 0 | 222 | | (SDL_Rect*)rectHandle.AddrOfPinnedObject()), |
| | 2 | 223 | | "SDL_RenderSetClipRect"); |
| | | 224 | | } |
| | 2 | 225 | | } |
| | | 226 | | finally |
| | | 227 | | { |
| | 2 | 228 | | rectHandle.Free(); |
| | 2 | 229 | | } |
| | 2 | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets the clip rectangle, or null if clipping is disabled. |
| | | 234 | | /// </summary> |
| | | 235 | | public Rectangle? GetClipRectangle() |
| | | 236 | | { |
| | | 237 | | unsafe |
| | | 238 | | { |
| | 3 | 239 | | var p = stackalloc int[4]; |
| | 3 | 240 | | SDL_RenderGetClipRect(Handle, (SDL_Rect*)p); |
| | 3 | 241 | | var value = new Rectangle( |
| | 0 | 242 | | *p, |
| | 0 | 243 | | *(p + 1), |
| | 0 | 244 | | *(p + 2), |
| | 0 | 245 | | *(p + 3) |
| | 0 | 246 | | ); |
| | 3 | 247 | | if (value.Size == Size.Empty) |
| | 2 | 248 | | return null; |
| | 1 | 249 | | return value; |
| | | 250 | | } |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Sets the viewport. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <param name="rect">Viewport rectangle. If null, whole target is used</param> |
| | | 257 | | public void SetViewport(Rectangle? rect) |
| | | 258 | | { |
| | 2 | 259 | | var rectHandle = Pin(rect?.ToSDL()); |
| | | 260 | | try |
| | | 261 | | { |
| | | 262 | | unsafe |
| | | 263 | | { |
| | 0 | 264 | | Try(() => SDL_RenderSetViewport(Handle, |
| | 0 | 265 | | (SDL_Rect*)rectHandle.AddrOfPinnedObject()), |
| | 0 | 266 | | "SDL_RenderSetViewport"); |
| | | 267 | | } |
| | 2 | 268 | | } |
| | | 269 | | finally |
| | | 270 | | { |
| | 2 | 271 | | rectHandle.Free(); |
| | 2 | 272 | | } |
| | 2 | 273 | | } |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Gets the current viewport bounds. |
| | | 277 | | /// </summary> |
| | | 278 | | public Rectangle GetViewport() |
| | | 279 | | { |
| | | 280 | | unsafe |
| | | 281 | | { |
| | 3 | 282 | | var p = stackalloc int[4]; |
| | 3 | 283 | | SDL_RenderGetViewport(Handle, (SDL_Rect*)p); |
| | 0 | 284 | | var value = new Rectangle( |
| | 0 | 285 | | *p, |
| | 0 | 286 | | *(p + 1), |
| | 0 | 287 | | *(p + 2), |
| | 0 | 288 | | *(p + 3) |
| | 3 | 289 | | ); |
| | 3 | 290 | | return value; |
| | | 291 | | } |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Sets the drawing scale factor for the current target. |
| | | 296 | | /// </summary> |
| | | 297 | | public void SetScale(float scaleX, float scaleY) => |
| | 1 | 298 | | Try(() => SDL_RenderSetScale(Handle, scaleX, scaleY), |
| | 1 | 299 | | "SDL_RenderSetScale"); |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Sets the drawing scale factor for the current target. |
| | | 303 | | /// </summary> |
| | | 304 | | public void SetScale(SizeF scale) => |
| | 1 | 305 | | SetScale(scale.Width, scale.Height); |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Gets the drawing scale factor for the current target. |
| | | 309 | | /// </summary> |
| | | 310 | | public void GetScale(out float scaleX, out float scaleY) |
| | | 311 | | { |
| | 2 | 312 | | var sX = 0.0f; |
| | 2 | 313 | | var sY = 0.0f; |
| | 2 | 314 | | SDL_RenderGetScale(Handle, ref sX, ref sY); |
| | 4 | 315 | | scaleX = sX; scaleY = sY; |
| | 2 | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Gets the drawing scale factor for the current target. |
| | | 320 | | /// </summary> |
| | | 321 | | public SizeF GetScale() |
| | | 322 | | { |
| | 2 | 323 | | GetScale(out float scaleX, out float scaleY); |
| | 2 | 324 | | return new SizeF(scaleX, scaleY); |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | /// <summary> |
| | | 328 | | /// Sets a device independent resolution for rendering. |
| | | 329 | | /// </summary> |
| | | 330 | | public void SetLogicalSize(int width, int height) => |
| | 1 | 331 | | Try(() => SDL_RenderSetLogicalSize(Handle, width, height), |
| | 1 | 332 | | "SDL_RenderSetLogicalSize"); |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Sets a device independent resolution for rendering. |
| | | 336 | | /// </summary> |
| | | 337 | | public void SetLogicalSize(Size size) => |
| | 1 | 338 | | SetLogicalSize(size.Width, size.Height); |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Gets a device independent resolution for rendering. If it was never |
| | | 342 | | /// set, returns zeros. |
| | | 343 | | /// </summary> |
| | | 344 | | public void GetLogicalSize(out int width, out int height) |
| | | 345 | | { |
| | 4 | 346 | | var w = 0; var h = 0; |
| | 2 | 347 | | SDL_RenderGetLogicalSize(Handle, ref w, ref h); |
| | 4 | 348 | | width = w; height = h; |
| | 2 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Gets a device independent resolution for rendering. If it was never |
| | | 353 | | /// set, returns zeros. |
| | | 354 | | /// </summary> |
| | | 355 | | public Size GetLogicalSize() |
| | | 356 | | { |
| | 2 | 357 | | GetLogicalSize(out int w, out int h); |
| | 2 | 358 | | return new Size(w, h); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Clears the current render target with the drawing color. |
| | | 363 | | /// </summary> |
| | | 364 | | public void Clear() => |
| | 54 | 365 | | Try(() => SDL_RenderClear(Handle), "SDL_RenderClear"); |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Clears the current render target with the specified color. |
| | | 369 | | /// </summary> |
| | | 370 | | public void Clear(Color color) |
| | | 371 | | { |
| | 27 | 372 | | var oldColor = GetDrawingColor(); |
| | 27 | 373 | | SetDrawingColor(color); |
| | 27 | 374 | | Clear(); |
| | 27 | 375 | | SetDrawingColor(oldColor); |
| | 27 | 376 | | } |
| | | 377 | | |
| | | 378 | | [ExcludeFromCodeCoverage] |
| | | 379 | | /// <summary> |
| | | 380 | | /// Draws a texture at the specified position. |
| | | 381 | | /// </summary> |
| | | 382 | | public void Draw(Texture texture, Point position) |
| | | 383 | | { |
| | | 384 | | var dstRect = new Rectangle(position, texture.Size); |
| | | 385 | | Draw(texture, null, dstRect); |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | [ExcludeFromCodeCoverage] |
| | | 389 | | /// <summary> |
| | | 390 | | /// Draws a texture at the specified position with applied tint color. |
| | | 391 | | /// </summary> |
| | | 392 | | public void Draw(Texture texture, Point position, Color tint) => |
| | | 393 | | Draw(texture, null, new Rectangle(position, texture.Size), tint, 0); |
| | | 394 | | |
| | | 395 | | [ExcludeFromCodeCoverage] |
| | | 396 | | /// <summary> |
| | | 397 | | /// Draws a texture part at the specified position. |
| | | 398 | | /// </summary> |
| | | 399 | | public void Draw(Texture texture, Point position, Rectangle srcRect) => |
| | | 400 | | Draw(texture, srcRect, new Rectangle(position, texture.Size)); |
| | | 401 | | |
| | | 402 | | [ExcludeFromCodeCoverage] |
| | | 403 | | /// <summary> |
| | | 404 | | /// Draws a texture part at the specified position with applied tint color. |
| | | 405 | | /// </summary> |
| | | 406 | | public void Draw(Texture texture, Point position, Rectangle srcRect, Color tint) => |
| | | 407 | | Draw(texture, srcRect, new Rectangle(position, texture.Size), tint); |
| | | 408 | | |
| | | 409 | | /// <summary> |
| | | 410 | | /// Copies a portion of the texture to the current rendering target. |
| | | 411 | | /// </summary> |
| | | 412 | | /// <param name="texture">Texture to copy</param> |
| | | 413 | | /// <param name="srcRect">Source rectangle (null for copying whole texture)</param> |
| | | 414 | | /// <param name="dstRect">Destination rectangle (null to fill the entire render target)</param> |
| | | 415 | | public void Draw(Texture texture, Rectangle? srcRect = null, |
| | | 416 | | Rectangle? dstRect = null) |
| | | 417 | | { |
| | 88 | 418 | | var srcR = srcRect?.ToSDL(); |
| | 88 | 419 | | var dstR = dstRect?.ToSDL(); |
| | 88 | 420 | | var src = Pin(srcR); |
| | 88 | 421 | | var dst = Pin(dstR); |
| | | 422 | | try |
| | | 423 | | { |
| | | 424 | | unsafe |
| | | 425 | | { |
| | 0 | 426 | | Try(() => SDL_RenderCopy(Handle, texture.Handle, |
| | 0 | 427 | | (SDL_Rect*)src.AddrOfPinnedObject(), |
| | 0 | 428 | | (SDL_Rect*)dst.AddrOfPinnedObject()), |
| | 88 | 429 | | "SDL_RenderCopy"); |
| | | 430 | | } |
| | 88 | 431 | | } |
| | | 432 | | finally |
| | | 433 | | { |
| | 88 | 434 | | src.Free(); |
| | 88 | 435 | | dst.Free(); |
| | 88 | 436 | | } |
| | 88 | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Copies a portion of the texture to the current rendering target. |
| | | 441 | | /// </summary> |
| | | 442 | | /// <param name="texture">Texture to copy</param> |
| | | 443 | | /// <param name="srcRect">Source rectangle (null for copying whole texture)</param> |
| | | 444 | | /// <param name="dstRect">Destination rectangle (null to fill the entire render target)</param> |
| | | 445 | | /// <param name="angle"> |
| | | 446 | | /// Angle in degrees that indicates the rotation that will be applied |
| | | 447 | | /// to dstRect, rotating it in a clockwise direction |
| | | 448 | | /// </param> |
| | | 449 | | /// <param name="center"> |
| | | 450 | | /// Point around which dstRect will be rotated (if null, rotation will be |
| | | 451 | | /// done around dstRect's center) |
| | | 452 | | /// </param> |
| | | 453 | | /// <param name="flip"> |
| | | 454 | | /// Flipping actions that should be performed on the texture |
| | | 455 | | /// </param> |
| | | 456 | | public void Draw(Texture texture, Rectangle? srcRect, |
| | | 457 | | Rectangle? dstRect, double angle = 0.0, |
| | | 458 | | Point? center = null, TextureFlip flip = TextureFlip.None) |
| | | 459 | | { |
| | 4 | 460 | | var srcR = Pin(srcRect?.ToSDL()); |
| | 4 | 461 | | var dstR = Pin(dstRect?.ToSDL()); |
| | 4 | 462 | | var cntr = Pin(center?.ToSDL()); |
| | | 463 | | try |
| | | 464 | | { |
| | | 465 | | unsafe |
| | | 466 | | { |
| | 0 | 467 | | Try(() => SDL_RenderCopyEx(Handle, texture.Handle, |
| | 4 | 468 | | (SDL_Rect*)srcR.AddrOfPinnedObject(), |
| | 4 | 469 | | (SDL_Rect*)dstR.AddrOfPinnedObject(), |
| | 0 | 470 | | angle, |
| | 4 | 471 | | (SDL_Point*)cntr.AddrOfPinnedObject(), |
| | 0 | 472 | | (SDL_RendererFlip)flip), |
| | 0 | 473 | | "SDL_RenderCopyEx"); |
| | | 474 | | } |
| | 4 | 475 | | } |
| | | 476 | | finally |
| | | 477 | | { |
| | 4 | 478 | | srcR.Free(); |
| | 4 | 479 | | dstR.Free(); |
| | 4 | 480 | | cntr.Free(); |
| | 4 | 481 | | } |
| | 4 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Copies a portion of the texture to the current rendering target. |
| | | 486 | | /// </summary> |
| | | 487 | | /// <param name="texture">Texture to copy</param> |
| | | 488 | | /// <param name="srcRect">Source rectangle (null for copying whole texture)</param> |
| | | 489 | | /// <param name="dstRect">Destination rectangle (null to fill the entire render target)</param> |
| | | 490 | | /// <param name="angle"> |
| | | 491 | | /// Angle in degrees that indicates the rotation that will be applied |
| | | 492 | | /// to dstRect, rotating it in a clockwise direction |
| | | 493 | | /// </param> |
| | | 494 | | /// <param name="center"> |
| | | 495 | | /// Point around which dstRect will be rotated (if null, rotation will be |
| | | 496 | | /// done around dstRect's center) |
| | | 497 | | /// </param> |
| | | 498 | | /// <param name="flip"> |
| | | 499 | | /// Flipping actions that should be performed on the texture |
| | | 500 | | /// </param> |
| | | 501 | | /// <param name="tint">Tint color to be applied on the texture</param> |
| | | 502 | | public void Draw(Texture texture, Rectangle? srcRect, |
| | | 503 | | Rectangle? dstRect, Color tint, double angle = 0.0, |
| | | 504 | | Point? center = null, TextureFlip flip = TextureFlip.None) |
| | | 505 | | { |
| | 0 | 506 | | var oldTint = texture.Tint; |
| | 0 | 507 | | texture.Tint = tint; |
| | 0 | 508 | | Draw(texture, srcRect, dstRect, angle, center, flip); |
| | 0 | 509 | | texture.Tint = oldTint; |
| | 0 | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Draws a line between two points with the current color. |
| | | 514 | | /// </summary> |
| | | 515 | | public void DrawLine(int x1, int y1, int x2, int y2) => |
| | 1 | 516 | | Try(() => |
| | 1 | 517 | | SDL_RenderDrawLine(Handle, x1, y1, x2, y2), |
| | 1 | 518 | | "SDL_RenderDrawLine"); |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Draws a line between two points with the current color. |
| | | 522 | | /// </summary> |
| | | 523 | | public void DrawLine(Point from, Point to) => |
| | 1 | 524 | | DrawLine(from.X, from.Y, to.X, to.Y); |
| | | 525 | | |
| | | 526 | | /// <summary> |
| | | 527 | | /// Draws a series of connected lines between the specified points with |
| | | 528 | | /// the current color. |
| | | 529 | | /// </summary> |
| | | 530 | | public void DrawLines(params Point[] points) |
| | | 531 | | { |
| | | 532 | | unsafe |
| | | 533 | | { |
| | 1 | 534 | | var p = Pin(points); |
| | | 535 | | try |
| | | 536 | | { |
| | 0 | 537 | | Try(() => SDL_RenderDrawLines(Handle, |
| | 0 | 538 | | (SDL_Point*)p.AddrOfPinnedObject(), points.Length), |
| | 0 | 539 | | "SDL_RenderDrawLines"); |
| | 1 | 540 | | } |
| | | 541 | | finally |
| | | 542 | | { |
| | 1 | 543 | | p.Free(); |
| | 1 | 544 | | } |
| | | 545 | | } |
| | 1 | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Draws a point at the specified coordinates using current color. |
| | | 550 | | /// </summary> |
| | | 551 | | public void DrawPoint(int x, int y) => |
| | 1 | 552 | | Try(() => SDL_RenderDrawPoint(Handle, x, y), |
| | 0 | 553 | | "SDL_RenderDrawPoint"); |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Draws a point at the specified coordinates using current color. |
| | | 557 | | /// </summary> |
| | | 558 | | public void DrawPoint(Point point) => |
| | 1 | 559 | | DrawPoint(point.X, point.Y); |
| | | 560 | | |
| | | 561 | | /// <summary> |
| | | 562 | | /// Draws points at the specified coordinates using current color. |
| | | 563 | | /// </summary> |
| | | 564 | | public void DrawPoints(params Point[] points) |
| | | 565 | | { |
| | | 566 | | unsafe |
| | | 567 | | { |
| | 1 | 568 | | var p = Pin(points); |
| | | 569 | | try |
| | | 570 | | { |
| | 1 | 571 | | Try(() => SDL_RenderDrawPoints(Handle, |
| | 1 | 572 | | (SDL_Point*)p.AddrOfPinnedObject(), points.Length), |
| | 1 | 573 | | "SDL_RenderDrawPoints"); |
| | 1 | 574 | | } |
| | | 575 | | finally |
| | | 576 | | { |
| | 1 | 577 | | p.Free(); |
| | 1 | 578 | | } |
| | | 579 | | } |
| | 1 | 580 | | } |
| | | 581 | | |
| | | 582 | | /// <summary> |
| | | 583 | | /// Draws a rectangle outline with the current color. |
| | | 584 | | /// </summary> |
| | | 585 | | /// <param name="rectangle">Rectangle to draw outline for. If null, |
| | | 586 | | /// outlines a whole target.</param> |
| | | 587 | | public void DrawRect(Rectangle? rectangle) |
| | | 588 | | { |
| | | 589 | | unsafe |
| | | 590 | | { |
| | 13 | 591 | | var p = Pin(rectangle); |
| | | 592 | | try |
| | | 593 | | { |
| | 0 | 594 | | Try(() => |
| | 13 | 595 | | SDL_RenderDrawRect(Handle, (SDL_Rect*)p.AddrOfPinnedObject()), |
| | 13 | 596 | | "SDL_RenderDrawRect"); |
| | 13 | 597 | | } |
| | | 598 | | finally |
| | | 599 | | { |
| | 13 | 600 | | p.Free(); |
| | 13 | 601 | | } |
| | | 602 | | } |
| | 13 | 603 | | } |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Draws a rectangle outline with the current color. |
| | | 607 | | /// </summary> |
| | | 608 | | /// <param name="rectangles">Rectangles to draw outline for</param> |
| | | 609 | | public void DrawRects(params Rectangle[] rectangles) |
| | | 610 | | { |
| | | 611 | | unsafe |
| | | 612 | | { |
| | 1 | 613 | | var p = Pin(rectangles); |
| | | 614 | | try |
| | | 615 | | { |
| | 1 | 616 | | Try(() => |
| | 1 | 617 | | SDL_RenderDrawRects(Handle, |
| | 1 | 618 | | (SDL_Rect*)p.AddrOfPinnedObject(), rectangles.Length), |
| | 1 | 619 | | "SDL_RenderDrawRects"); |
| | 1 | 620 | | } |
| | | 621 | | finally |
| | | 622 | | { |
| | 1 | 623 | | p.Free(); |
| | 1 | 624 | | } |
| | | 625 | | } |
| | 1 | 626 | | } |
| | | 627 | | |
| | | 628 | | /// <summary> |
| | | 629 | | /// Fills a rectangle outline with the current color. |
| | | 630 | | /// </summary> |
| | | 631 | | /// <param name="rectangle">Rectangle to fill. If null, |
| | | 632 | | /// fills a whole target.</param> |
| | | 633 | | public void FillRect(Rectangle? rectangle) |
| | | 634 | | { |
| | | 635 | | unsafe |
| | | 636 | | { |
| | 1 | 637 | | var p = Pin(rectangle); |
| | | 638 | | try |
| | | 639 | | { |
| | 1 | 640 | | Try(() => |
| | 1 | 641 | | SDL_RenderFillRect(Handle, (SDL_Rect*)p.AddrOfPinnedObject()), |
| | 1 | 642 | | "SDL_RenderFillRect"); |
| | 1 | 643 | | } |
| | | 644 | | finally |
| | | 645 | | { |
| | 1 | 646 | | p.Free(); |
| | 1 | 647 | | } |
| | | 648 | | } |
| | 1 | 649 | | } |
| | | 650 | | |
| | | 651 | | /// <summary> |
| | | 652 | | /// Fills rectangles with the current color. |
| | | 653 | | /// </summary> |
| | | 654 | | /// <param name="rectangles">Rectangles to fill</param> |
| | | 655 | | public void FillRects(params Rectangle[] rectangles) |
| | | 656 | | { |
| | | 657 | | unsafe |
| | | 658 | | { |
| | 1 | 659 | | var p = Pin(rectangles); |
| | | 660 | | try |
| | | 661 | | { |
| | 1 | 662 | | Try(() => |
| | 1 | 663 | | SDL_RenderFillRects(Handle, |
| | 1 | 664 | | (SDL_Rect*)p.AddrOfPinnedObject(), rectangles.Length), |
| | 1 | 665 | | "SDL_RenderFillRects"); |
| | 1 | 666 | | } |
| | | 667 | | finally |
| | | 668 | | { |
| | 1 | 669 | | p.Free(); |
| | 1 | 670 | | } |
| | | 671 | | } |
| | 1 | 672 | | } |
| | | 673 | | |
| | | 674 | | internal override void Destroy() |
| | | 675 | | { |
| | 63 | 676 | | if (IsDisposed) return; |
| | 63 | 677 | | base.Destroy(); |
| | 63 | 678 | | SDL_DestroyRenderer(Handle); |
| | 63 | 679 | | } |
| | | 680 | | |
| | 1 | 681 | | static Graphics() => Lifecycle.TryInitialize(); |
| | | 682 | | |
| | | 683 | | // TODO Explore possibility of using stackalloc instead of pinning |
| | | 684 | | |
| | | 685 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 274 | 686 | | static GCHandle Pin(object obj) => GCHandle.Alloc(obj, GCHandleType.Pinned); |
| | | 687 | | } |
| | | 688 | | } |