| | 1 | | using System; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | |
|
| | 4 | | using static SDL2.SDL_blendmode; |
| | 5 | | using static SDL2.SDL_error; |
| | 6 | | using static SDL2.SDL_surface; |
| | 7 | | using static Imagini.ErrorHandler; |
| | 8 | | using System.Drawing; |
| | 9 | | using Imagini; |
| | 10 | | using static SDL2.SDL_rect; |
| | 11 | | using System.Diagnostics.CodeAnalysis; |
| | 12 | | using Imagini.Core.Internal; |
| | 13 | |
|
| | 14 | | namespace Imagini.Drawing |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Defines surface blend modes. |
| | 18 | | /// </summary> |
| | 19 | | /// <remarks> |
| | 20 | | /// None: |
| | 21 | | /// <code> |
| | 22 | | /// dstRGBA = srcRGBA |
| | 23 | | /// </code> |
| | 24 | | /// AlphaBlend: |
| | 25 | | /// <code> |
| | 26 | | /// dstRGB = (srcRGB * srcA) + (dstRGB * (1 - srcA)) |
| | 27 | | /// dstA = srcA + (dstA * (1 - srcA)) |
| | 28 | | /// </code> |
| | 29 | | /// Add: |
| | 30 | | /// <code> |
| | 31 | | /// dstRGB = (srcRGB * srcA) + dstRGB |
| | 32 | | /// dstA = dstA |
| | 33 | | /// </code> |
| | 34 | | /// Modulate: |
| | 35 | | /// <code> |
| | 36 | | /// dstRGB = srcRGB * dstRGB |
| | 37 | | /// dstA = dstA |
| | 38 | | /// </code> |
| | 39 | | /// </remarks> |
| | 40 | | public enum BlendMode |
| | 41 | | { |
| | 42 | | None = SDL_BlendMode.SDL_BLENDMODE_NONE, |
| | 43 | | AlphaBlend = SDL_BlendMode.SDL_BLENDMODE_BLEND, |
| | 44 | | Add = SDL_BlendMode.SDL_BLENDMODE_ADD, |
| | 45 | | Modulate = SDL_BlendMode.SDL_BLENDMODE_MOD |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Defines a surface which stores it's pixel data in |
| | 50 | | /// the RAM. |
| | 51 | | /// </summary> |
| | 52 | | public sealed class Surface : Resource, IDisposable |
| | 53 | | { |
| | 54 | | internal readonly IntPtr Handle; |
| | 55 | | private readonly IntPtr _pixels; |
| 2 | 56 | | internal IntPtr PixelsHandle => _pixels; |
| | 57 | | private readonly bool _shouldFreePixels; |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Returns the surface's pixel format. |
| | 61 | | /// </summary> |
| | 62 | | /// <returns></returns> |
| 95 | 63 | | public PixelFormatInfo PixelInfo { get; private set; } |
| | 64 | | /// <summary> |
| | 65 | | /// Returns the surface width in pixels. |
| | 66 | | /// </summary> |
| 170 | 67 | | public int Width { get; private set; } |
| | 68 | | /// <summary> |
| | 69 | | /// Returns the surface height in pixels. |
| | 70 | | /// </summary> |
| 161 | 71 | | public int Height { get; private set; } |
| | 72 | | /// <summary> |
| | 73 | | /// Returns the size of this surface in pixels. |
| | 74 | | /// </summary> |
| 4 | 75 | | public Size Size => new Size(Width, Height); |
| | 76 | | /// <summary> |
| | 77 | | /// Returns the pixel count of the surface (width * height). |
| | 78 | | /// </summary> |
| 26 | 79 | | public int PixelCount => Width * Height; |
| | 80 | | /// <summary> |
| | 81 | | /// Returns the surface stride (aka pitch, or length of a pixel row) in bytes. |
| | 82 | | /// </summary> |
| | 83 | | /// <returns></returns> |
| 73 | 84 | | public int Stride { get; private set; } |
| | 85 | | /// <summary> |
| | 86 | | /// Returns the size of the pixel data array in bytes (stride * height). |
| | 87 | | /// </summary> |
| 14 | 88 | | public int SizeInBytes => Stride * Height; |
| | 89 | | /// <summary> |
| | 90 | | /// Indicates if this surface should be locked in order to access |
| | 91 | | /// the pixel data. |
| | 92 | | /// </summary> |
| | 93 | | /// <returns></returns> |
| 73 | 94 | | public bool MustBeLocked { get; private set; } |
| | 95 | | /// <summary> |
| | 96 | | /// Indicates if this surface is currently locked or not. |
| | 97 | | /// </summary> |
| 47 | 98 | | public bool Locked { get; private set; } |
| | 99 | | /// <summary> |
| | 100 | | /// Indicates if this surface has RLE acceleration enabled. |
| | 101 | | /// </summary> |
| 5 | 102 | | public bool RLEEnabled { get; private set; } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Gets or sets the clipping rectangle. |
| | 106 | | /// </summary> |
| | 107 | | public Rectangle? ClipRect |
| | 108 | | { |
| | 109 | | get |
| | 110 | | { |
| | 111 | | unsafe |
| | 112 | | { |
| 3 | 113 | | var rect = new SDL_Rect(); |
| 3 | 114 | | var p = ▭ |
| 3 | 115 | | SDL_GetClipRect(Handle, &rect); |
| 3 | 116 | | if (rect.x == 0 && rect.y == 0 && rect.w == Width && rect.h == Height) |
| 2 | 117 | | return null; |
| 1 | 118 | | return rect.ToRectangle(); |
| | 119 | | } |
| | 120 | | } |
| | 121 | | set |
| | 122 | | { |
| | 123 | | unsafe |
| | 124 | | { |
| 0 | 125 | | var rect = value.HasValue ? value.Value.ToSDL() : |
| 0 | 126 | | new SDL_Rect() { x = 0, y = 0, w = Width, h = Height }; |
| 2 | 127 | | var p = ▭ |
| 4 | 128 | | Try(() => SDL_SetClipRect(Handle, p), "SDL_SetClipRect"); |
| | 129 | | } |
| 2 | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Gets or sets the additional color value multiplied into blit operations. |
| | 135 | | /// Only R, G and B channels are used. |
| | 136 | | /// </summary> |
| | 137 | | /// <remarks> |
| | 138 | | /// When this surface is blitted, during the blit operation each source color channel is modulated by the approp |
| | 139 | | /// srcC = srcC * (color / 255) |
| | 140 | | /// </remarks> |
| | 141 | | public Color ColorMod |
| | 142 | | { |
| | 143 | | get |
| | 144 | | { |
| 6 | 145 | | byte r = 0; byte g = 0; byte b = 0; |
| 2 | 146 | | Try(() => |
| 2 | 147 | | SDL_GetSurfaceColorMod(Handle, ref r, ref g, ref b), |
| 2 | 148 | | "SDL_GetSurfaceColorMod"); |
| 2 | 149 | | return Color.FromArgb(r, g, b); |
| | 150 | | } |
| | 151 | | set |
| | 152 | | { |
| 0 | 153 | | Try(() => |
| 1 | 154 | | SDL_SetSurfaceColorMod(Handle, value.R, value.G, value.B), |
| 0 | 155 | | "SDL_SetSurfaceColorMod"); |
| 1 | 156 | | } |
| | 157 | | } |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// Gets or sets an additional alpha value used in blit operations. |
| | 161 | | /// </summary> |
| | 162 | | /// <remarks> |
| | 163 | | /// When this surface is blitted, during the blit operation the source alpha value is modulated by this alpha va |
| | 164 | | /// srcA = srcA * (alpha / 255) |
| | 165 | | /// </remarks> |
| | 166 | | public byte AlphaMod |
| | 167 | | { |
| | 168 | | get |
| | 169 | | { |
| 2 | 170 | | byte a = 0; |
| 2 | 171 | | Try(() => SDL_GetSurfaceAlphaMod(Handle, ref a), |
| 2 | 172 | | "SDL_GetSurfaceAlphaMod"); |
| 2 | 173 | | return a; |
| | 174 | | } |
| | 175 | | set |
| | 176 | | { |
| 1 | 177 | | Try(() => SDL_SetSurfaceAlphaMod(Handle, value), |
| 0 | 178 | | "SDL_SetSurfaceAlphaMod"); |
| 1 | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Gets or sets the <see cref="ColorMod" /> and <see cref="AlphaMod" /> values. |
| | 184 | | /// </summary> |
| | 185 | | public Color Tint |
| | 186 | | { |
| | 187 | | get { |
| 0 | 188 | | var color = ColorMod; |
| 0 | 189 | | var alpha = AlphaMod; |
| 0 | 190 | | return Color.FromArgb(alpha, color); |
| | 191 | | } |
| | 192 | | set { |
| 0 | 193 | | ColorMod = value; |
| 0 | 194 | | AlphaMod = value.A; |
| 0 | 195 | | } |
| | 196 | | } |
| | 197 | |
|
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Gets or sets the blend mode used for blit operations. |
| | 201 | | /// </summary> |
| | 202 | | public BlendMode BlendMode |
| | 203 | | { |
| | 204 | | get |
| | 205 | | { |
| 2 | 206 | | var blendMode = SDL_BlendMode.SDL_BLENDMODE_NONE; |
| 2 | 207 | | Try(() => SDL_GetSurfaceBlendMode(Handle, ref blendMode), |
| 2 | 208 | | "SDL_GetSurfaceBlendMode"); |
| 2 | 209 | | return (BlendMode)blendMode; |
| | 210 | | } |
| | 211 | | set |
| | 212 | | { |
| 1 | 213 | | var blendMode = (SDL_BlendMode)value; |
| 1 | 214 | | Try(() => SDL_SetSurfaceBlendMode(Handle, blendMode), |
| 0 | 215 | | "SDL_SetSurfaceBlendMode"); |
| 1 | 216 | | } |
| | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Gets or sets the color key (transparent pixel). |
| | 221 | | /// </summary> |
| | 222 | | public Color? ColorKey |
| | 223 | | { |
| | 224 | | get |
| | 225 | | { |
| 3 | 226 | | var key = 0u; |
| 3 | 227 | | var result = SDL_GetColorKey(Handle, ref key); |
| 4 | 228 | | if (result == 0) return ColorExtensions.FromUint(key, PixelInfo); |
| 4 | 229 | | if (result == -1) return null; |
| 0 | 230 | | throw new ImaginiException($"Could not obtain color key: {SDL_GetError()}"); |
| | 231 | | } |
| | 232 | | set |
| | 233 | | { |
| 2 | 234 | | var val = value?.AsUint(PixelInfo) ?? 0u; |
| 2 | 235 | | Try(() => SDL_SetColorKey(Handle, value != null ? 1 : 0, val), |
| 0 | 236 | | "SDL_SetColorKey"); |
| 2 | 237 | | } |
| | 238 | | } |
| | 239 | |
|
| 40 | 240 | | internal Surface(IntPtr handle) |
| | 241 | | { |
| 40 | 242 | | Handle = handle; |
| 40 | 243 | | var data = Marshal.PtrToStructure<SDL_Surface>(handle); |
| 40 | 244 | | PixelInfo = new PixelFormatInfo(data.format); |
| 40 | 245 | | Width = data.w; |
| 40 | 246 | | Height = data.h; |
| 40 | 247 | | Stride = data.pitch; |
| 40 | 248 | | MustBeLocked = SDL_MUSTLOCK(data); |
| 40 | 249 | | _pixels = data.pixels; |
| 40 | 250 | | _shouldFreePixels = false; |
| 40 | 251 | | Locked = data.locked > 0; |
| 40 | 252 | | } |
| | 253 | |
|
| | 254 | | internal Surface(IntPtr handle, IntPtr pixels) |
| 11 | 255 | | : this(handle) => |
| 11 | 256 | | (this._pixels, this._shouldFreePixels) = (pixels, true); |
| | 257 | |
|
| | 258 | | /// <summary> |
| | 259 | | /// Locks the surface |
| | 260 | | /// </summary> |
| | 261 | | public void Lock() |
| | 262 | | { |
| 1 | 263 | | if (Locked) return; |
| 1 | 264 | | Try(() => SDL_LockSurface(Handle), |
| 0 | 265 | | "SDL_LockSurface"); |
| 1 | 266 | | Locked = true; |
| 1 | 267 | | } |
| | 268 | |
|
| | 269 | | /// <summary> |
| | 270 | | /// Unlocks the surface. |
| | 271 | | /// </summary> |
| | 272 | | public void Unlock() |
| | 273 | | { |
| 1 | 274 | | if (!Locked) return; |
| 1 | 275 | | SDL_UnlockSurface(Handle); |
| 1 | 276 | | Locked = false; |
| 1 | 277 | | } |
| | 278 | |
|
| | 279 | | /// <summary> |
| | 280 | | /// Enabled or disables the RLE acceleration. |
| | 281 | | /// </summary> |
| | 282 | | public void SetRLEAcceleration(bool enable) |
| | 283 | | { |
| 4 | 284 | | Try(() => SDL_SetSurfaceRLE(Handle, enable ? 1 : 0), "SDL_SetSurfaceRLE"); |
| 2 | 285 | | RLEEnabled = enable; |
| 2 | 286 | | var data = Marshal.PtrToStructure<SDL_Surface>(Handle); |
| 2 | 287 | | MustBeLocked = SDL_MUSTLOCK(data); |
| 2 | 288 | | } |
| | 289 | |
|
| | 290 | | /// <summary> |
| | 291 | | /// Creates a new surface with the specified format. |
| | 292 | | /// </summary> |
| | 293 | | /// <param name="width">Surface width</param> |
| | 294 | | /// <param name="height">Surface height</param> |
| | 295 | | /// <param name="format">Surface format</param> |
| | 296 | | public static Surface Create(int width, int height, PixelFormat format = PixelFormat.Format_ARGB8888) |
| | 297 | | { |
| 22 | 298 | | var fmt = new PixelFormatInfo(format); |
| 22 | 299 | | var result = Create(width, height, fmt.BitsPerPixel, |
| 22 | 300 | | fmt.MaskR, fmt.MaskG, fmt.MaskB, fmt.MaskA); |
| 22 | 301 | | fmt.Dispose(); |
| 22 | 302 | | return result; |
| | 303 | | } |
| | 304 | |
|
| | 305 | | /// <summary> |
| | 306 | | /// Creates a new surface. |
| | 307 | | /// </summary> |
| | 308 | | /// <param name="width">Surface width</param> |
| | 309 | | /// <param name="height">Surface height</param> |
| | 310 | | /// <param name="depth">Surface depth in bits (defaults to 32)</param> |
| | 311 | | /// <remarks> |
| | 312 | | /// The mask parameters are the bitmasks used to extract that |
| | 313 | | /// color from a pixel. For instance, Rmask being FF000000 means |
| | 314 | | /// the red data is stored in the most significant byte. Uzing zeros for |
| | 315 | | /// the RGB masks sets a default value, based on the depth. However, |
| | 316 | | /// using zero for the Amask results in an Amask of 0. |
| | 317 | | /// </remarks> |
| | 318 | | public static Surface Create(int width, int height, int depth, |
| | 319 | | int Rmask = 0, int Gmask = 0, int Bmask = 0, int Amask = 0) |
| | 320 | | { |
| | 321 | | unchecked |
| | 322 | | { |
| 0 | 323 | | var handle = SDL_CreateRGBSurface(0, width, height, depth, |
| 0 | 324 | | (uint)Rmask, (uint)Gmask, (uint)Bmask, (uint)Amask); |
| 24 | 325 | | if (handle == IntPtr.Zero) |
| 1 | 326 | | throw new ImaginiException($"Could not create surface: {SDL_GetError()}"); |
| 23 | 327 | | return new Surface(handle); |
| | 328 | | } |
| | 329 | | } |
| | 330 | |
|
| | 331 | | /// <summary> |
| | 332 | | /// Creates a surface with the specified format from the specified pixel data. |
| | 333 | | /// </summary> |
| | 334 | | /// <param name="data">The pixel data to create surface from</param> |
| | 335 | | /// <param name="width">Surface width</param> |
| | 336 | | /// <param name="height">Surface height</param> |
| | 337 | | /// <param name="format">Surface format</param> |
| | 338 | | public static Surface CreateFrom(byte[] data, int width, int height, PixelFormat format) |
| | 339 | | { |
| 11 | 340 | | var fmt = new PixelFormatInfo(format); |
| 11 | 341 | | var result = CreateFrom(data, width, height, |
| 11 | 342 | | fmt.BytesPerPixel * width, fmt.BitsPerPixel, |
| 0 | 343 | | fmt.MaskR, fmt.MaskG, fmt.MaskB, fmt.MaskA); |
| 11 | 344 | | fmt.Dispose(); |
| 11 | 345 | | return result; |
| | 346 | | } |
| | 347 | |
|
| | 348 | | /// <summary> |
| | 349 | | /// Creates a new surface from existing data. Data is copied. |
| | 350 | | /// </summary> |
| | 351 | | /// <param name="data">The pixel data to create surface from</param> |
| | 352 | | /// <param name="width">Surface width</param> |
| | 353 | | /// <param name="height">Surface height</param> |
| | 354 | | /// <param name="stride">Length of pixel row in bytes. RGBA = 4 * width</param> |
| | 355 | | /// <param name="depth">Surface depth in bits (defaults to 32)</param> |
| | 356 | | /// <remarks> |
| | 357 | | /// The mask parameters are the bitmasks used to extract that |
| | 358 | | /// color from a pixel. For instance, Rmask being FF000000 means |
| | 359 | | /// the red data is stored in the most significant byte. Uzing zeros for |
| | 360 | | /// the RGB masks sets a default value, based on the depth. However, |
| | 361 | | /// using zero for the Amask results in an Amask of 0. |
| | 362 | | /// </remarks> |
| | 363 | | public static Surface CreateFrom(byte[] data, int width, int height, int stride, int depth, |
| | 364 | | int Rmask, int Gmask, int Bmask, int Amask) |
| | 365 | | { |
| 11 | 366 | | var allocated = Marshal.AllocHGlobal(data.Length); |
| 11 | 367 | | Marshal.Copy(data, 0, allocated, data.Length); |
| 11 | 368 | | var handle = SDL_CreateRGBSurfaceFrom(allocated, width, height, depth, stride, |
| 11 | 369 | | (uint)Rmask, (uint)Gmask, (uint)Bmask, (uint)Amask); |
| 11 | 370 | | if (handle == IntPtr.Zero) |
| 0 | 371 | | throw new ImaginiException($"Could not create surface: {SDL_GetError()}"); |
| 11 | 372 | | return new Surface(handle, allocated); |
| | 373 | | } |
| | 374 | |
|
| | 375 | | /// <summary> |
| | 376 | | /// Copies the surface into a new one that is optimized for blitting to |
| | 377 | | /// a surface of the specified pixel format. |
| | 378 | | /// </summary> |
| | 379 | | public Surface OptimizeFor(PixelFormat format) |
| | 380 | | { |
| 1 | 381 | | var fmt = new PixelFormatInfo(format); |
| 1 | 382 | | return OptimizeFor(fmt); |
| | 383 | | } |
| | 384 | |
|
| | 385 | | /// <summary> |
| | 386 | | /// Copies the surface into a new one that is optimized for blitting to |
| | 387 | | /// a surface of the specified pixel format. |
| | 388 | | /// </summary> |
| | 389 | | public Surface OptimizeFor(PixelFormatInfo format) |
| | 390 | | { |
| 1 | 391 | | var handle = SDL_ConvertSurface(Handle, format.Handle, 0); |
| 1 | 392 | | if (handle == IntPtr.Zero) |
| 0 | 393 | | throw new ImaginiException($"Could not create surface: {SDL_GetError()}"); |
| 1 | 394 | | return new Surface(handle); |
| | 395 | | } |
| | 396 | |
|
| | 397 | | /// <summary> |
| | 398 | | /// Copies the surface into a new one that has the specified pixel format. |
| | 399 | | /// </summary> |
| | 400 | | public Surface ConvertTo(PixelFormat format) |
| | 401 | | { |
| 3 | 402 | | var handle = SDL_ConvertSurfaceFormat(Handle, (uint)format, 0); |
| 3 | 403 | | if (handle == IntPtr.Zero) |
| 0 | 404 | | throw new ImaginiException($"Could not create surface: {SDL_GetError()}"); |
| 3 | 405 | | return new Surface(handle); |
| | 406 | | } |
| | 407 | |
|
| | 408 | | [ExcludeFromCodeCoverage] |
| | 409 | | /// <summary> |
| | 410 | | /// Copies the surface into a new one that has the specified pixel format. |
| | 411 | | /// </summary> |
| | 412 | | public Surface ConvertTo(PixelFormatInfo format) => ConvertTo(format.Format); |
| | 413 | |
|
| | 414 | | /// <summary> |
| | 415 | | /// Reads the pixel data to the specified pixel buffer, making automatic |
| | 416 | | /// conversion if necessary. |
| | 417 | | /// </summary> |
| | 418 | | public void ReadPixels<T>(ref T[] target) |
| | 419 | | where T : struct, IColor |
| | 420 | | { |
| 24 | 421 | | if (MustBeLocked && !Locked) |
| 0 | 422 | | throw new ImaginiException("Surface must be locked before accessing"); |
| 24 | 423 | | var p = _pixels; |
| 24 | 424 | | var shouldFree = false; |
| 24 | 425 | | var targetFormat = target[0].Format; |
| 24 | 426 | | var targetStride = targetFormat.GetBytesPerPixel() * Width; |
| 24 | 427 | | var sizeInBytes = Util.SizeOf<T>() * (Width * Height); |
| 24 | 428 | | if (PixelInfo.Format != targetFormat) |
| | 429 | | { |
| 12 | 430 | | var p2 = Marshal.AllocHGlobal(sizeInBytes); |
| 0 | 431 | | Try(() => SDL_ConvertPixels(Width, Height, |
| 0 | 432 | | (uint)PixelInfo.Format, p, Stride, |
| 0 | 433 | | (uint)targetFormat, p2, targetStride), |
| 0 | 434 | | "SDL_ConvertPixels"); |
| 12 | 435 | | p = p2; |
| 12 | 436 | | shouldFree = true; |
| | 437 | | } |
| 24 | 438 | | Util.CopyTo(target, from: p, count: Width * Height); |
| 24 | 439 | | if (shouldFree) |
| 12 | 440 | | Marshal.FreeHGlobal(p); |
| 24 | 441 | | } |
| | 442 | |
|
| | 443 | | /// <summary> |
| | 444 | | /// Copies the pixel data in the specified byte array. |
| | 445 | | /// </summary> |
| | 446 | | public void ReadPixels(ref byte[] target) |
| | 447 | | { |
| 3 | 448 | | if (MustBeLocked && !Locked) |
| 0 | 449 | | throw new ImaginiException("Surface must be locked before accessing"); |
| 3 | 450 | | Marshal.Copy(_pixels, target, 0, Stride * Height); |
| 3 | 451 | | } |
| | 452 | |
|
| | 453 | | /// <summary> |
| | 454 | | /// Copies the pixel data from the specified pixel array, making a |
| | 455 | | /// conversion if necessary. |
| | 456 | | /// </summary> |
| | 457 | | public void SetPixelData<T>(ref T[] source) |
| | 458 | | where T : struct, IColor |
| | 459 | | { |
| 3 | 460 | | if (MustBeLocked && !Locked) |
| 0 | 461 | | throw new ImaginiException("Surface must be locked before accessing"); |
| 3 | 462 | | var shouldFree = false; |
| 3 | 463 | | var sourceFormat = source[0].Format; |
| 3 | 464 | | var sourceStride = sourceFormat.GetBytesPerPixel() * Width; |
| 3 | 465 | | var result = new T[Width * Height]; |
| 3 | 466 | | var srcHandle = GCHandle.Alloc(source, GCHandleType.Pinned); |
| 3 | 467 | | var p = srcHandle.AddrOfPinnedObject(); |
| | 468 | | try |
| | 469 | | { |
| 3 | 470 | | if (PixelInfo.Format != sourceFormat) |
| | 471 | | { |
| 1 | 472 | | var p2 = Marshal.AllocHGlobal(SizeInBytes); |
| 1 | 473 | | shouldFree = true; |
| 1 | 474 | | Try(() => SDL_ConvertPixels(Width, Height, |
| 0 | 475 | | (uint)sourceFormat, p, sourceStride, |
| 1 | 476 | | (uint)PixelInfo.Format, p2, Stride), |
| 1 | 477 | | "SDL_ConvertPixels"); |
| 1 | 478 | | p = p2; |
| | 479 | | } |
| | 480 | | unsafe |
| | 481 | | { |
| 3 | 482 | | Buffer.MemoryCopy((void*)p, (void*)_pixels, SizeInBytes, SizeInBytes); |
| | 483 | | } |
| 3 | 484 | | } |
| | 485 | | finally |
| | 486 | | { |
| 3 | 487 | | srcHandle.Free(); |
| 3 | 488 | | if (shouldFree) |
| 1 | 489 | | Marshal.FreeHGlobal(p); |
| 3 | 490 | | } |
| 3 | 491 | | } |
| | 492 | |
|
| | 493 | | /// <summary> |
| | 494 | | /// Copies the pixel data from the specified byte array to the surface. |
| | 495 | | /// </summary> |
| | 496 | | /// <param name="source"></param> |
| | 497 | | public void SetPixelData(ref byte[] source) |
| | 498 | | { |
| 1 | 499 | | if (MustBeLocked && !Locked) |
| 0 | 500 | | throw new ImaginiException("Surface must be locked before accessing"); |
| 1 | 501 | | Marshal.Copy(source, 0, _pixels, Stride * Height); |
| 1 | 502 | | } |
| | 503 | |
|
| | 504 | | /// <summary> |
| | 505 | | /// Performs a fast fill of a rectangle with the specified color. No |
| | 506 | | /// alpha blending is performed if alpha channel data is present. |
| | 507 | | /// </summary> |
| | 508 | | /// <param name="rectangle">Rectangle to fill, or NULL to fill entire surface</param> |
| | 509 | | /// <param name="color">Color to fill with</param> |
| | 510 | | public void Fill(Color color, Rectangle? rectangle = null) |
| | 511 | | { |
| 3 | 512 | | SDL_Rect? rect = rectangle?.ToSDL(); |
| 3 | 513 | | var rectHandle = GCHandle.Alloc(rect, GCHandleType.Pinned); |
| | 514 | | try |
| | 515 | | { |
| | 516 | | unsafe |
| | 517 | | { |
| 3 | 518 | | Try(() => SDL_FillRect(Handle, |
| 3 | 519 | | (SDL_Rect*)rectHandle.AddrOfPinnedObject(), color.AsUint(PixelInfo)), |
| 3 | 520 | | "SDL_FillRect"); |
| | 521 | | } |
| 3 | 522 | | } |
| | 523 | | finally |
| | 524 | | { |
| 3 | 525 | | rectHandle.Free(); |
| 3 | 526 | | } |
| 3 | 527 | | } |
| | 528 | |
|
| | 529 | | /// <summary> |
| | 530 | | /// Performs a fast fill of rectangles with the specified color. No |
| | 531 | | /// alpha blending is performed if alpha channel data is present. |
| | 532 | | /// </summary> |
| | 533 | | /// <param name="rectangle">Rectangles to fill</param> |
| | 534 | | /// <param name="color">Color to fill with</param> |
| | 535 | | public void Fill(Color color, params Rectangle[] rectangles) |
| | 536 | | { |
| 3 | 537 | | var rects = new SDL_Rect[rectangles.Length]; |
| 14 | 538 | | for (int i = 0; i < rects.Length; i++) |
| 4 | 539 | | rects[i] = rectangles[i].ToSDL(); |
| 3 | 540 | | var rectsHandle = GCHandle.Alloc(rects, GCHandleType.Pinned); |
| | 541 | | try |
| | 542 | | { |
| | 543 | | unsafe |
| | 544 | | { |
| 3 | 545 | | Try(() => SDL_FillRects(Handle, |
| 0 | 546 | | rectsHandle.AddrOfPinnedObject(), |
| 3 | 547 | | rects.Length, color.AsUint(PixelInfo)), |
| 3 | 548 | | "SDL_FillRects"); |
| | 549 | | } |
| 3 | 550 | | } |
| | 551 | | finally |
| | 552 | | { |
| 3 | 553 | | rectsHandle.Free(); |
| 3 | 554 | | } |
| 3 | 555 | | } |
| | 556 | |
|
| | 557 | | /// <summary> |
| | 558 | | /// Performs a scaled surface copy to a destination surface. |
| | 559 | | /// </summary> |
| | 560 | | /// <param name="srcRect">Rectangle to be copied, or null to copy entire surface</param> |
| | 561 | | /// <param name="destination">Blit target</param> |
| | 562 | | /// <param name="dstRect">Rectangle that is copied into, or null to copy into the entire surface</param> |
| | 563 | | public void BlitTo(Surface destination, Rectangle? srcRect = null, Rectangle? dstRect = null) |
| | 564 | | { |
| 2 | 565 | | SDL_Rect? src = srcRect?.ToSDL(); |
| 2 | 566 | | SDL_Rect? dst = dstRect?.ToSDL(); |
| 2 | 567 | | var srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned); |
| 2 | 568 | | var dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned); |
| | 569 | | try |
| | 570 | | { |
| | 571 | | unsafe |
| | 572 | | { |
| 2 | 573 | | Try(() => |
| 0 | 574 | | SDL_BlitScaled(Handle, |
| 2 | 575 | | (SDL_Rect*)srcHandle.AddrOfPinnedObject(), |
| 2 | 576 | | destination.Handle, |
| 0 | 577 | | (SDL_Rect*)dstHandle.AddrOfPinnedObject()), |
| 0 | 578 | | "SDL_BlitScaled"); |
| | 579 | | } |
| 2 | 580 | | } |
| | 581 | | finally |
| | 582 | | { |
| 2 | 583 | | srcHandle.Free(); |
| 2 | 584 | | dstHandle.Free(); |
| 2 | 585 | | } |
| 2 | 586 | | } |
| | 587 | |
|
| | 588 | | internal override void Destroy() |
| | 589 | | { |
| 34 | 590 | | if (IsDisposed) return; |
| 34 | 591 | | SDL_FreeSurface(Handle); |
| 34 | 592 | | if (_pixels != IntPtr.Zero && _shouldFreePixels) |
| 9 | 593 | | Marshal.FreeHGlobal(_pixels); |
| 34 | 594 | | } |
| | 595 | |
|
| | 596 | | /// <summary> |
| | 597 | | /// Disposes the surface. |
| | 598 | | /// </summary> |
| 34 | 599 | | public void Dispose() => Destroy(); |
| | 600 | |
|
| | 601 | |
|
| 1 | 602 | | static Surface() => Lifecycle.TryInitialize(); |
| | 603 | | } |
| | 604 | | } |