SpriteTexture.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// Texture interface that encapsulates underlying texture which allows us to create a sprite texture atlas
  9. /// (for example multiple sprite textures referencing different parts of a single texture).
  10. /// </summary>
  11. public sealed class SpriteTexture : Resource
  12. {
  13. /// <summary>
  14. /// Constructor for internal use by the runtime.
  15. /// </summary>
  16. private SpriteTexture()
  17. { }
  18. /// <summary>
  19. /// Creates a new sprite texture that references the entire area of the provided texture.
  20. /// </summary>
  21. /// <param name="texture">Texture to wrap by the sprite texture.</param>
  22. public SpriteTexture(Texture2D texture)
  23. {
  24. Vector2 offset = Vector2.Zero;
  25. Vector2 scale = Vector2.One;
  26. Internal_CreateInstance(this, texture, ref offset, ref scale);
  27. }
  28. /// <summary>
  29. /// Creates a new sprite texture that references a sub-area of the provided texture.
  30. /// </summary>
  31. /// <param name="texture">Texture to wrap by the sprite texture.</param>
  32. /// <param name="uvOffset">Top-left position of the area used by the sprite texture, in normalized coordinates.
  33. /// </param>
  34. /// <param name="uvScale">Size of the area used by the sprite texture, in normalized coordinates.</param>
  35. public SpriteTexture(Texture2D texture, Vector2 uvOffset, Vector2 uvScale)
  36. {
  37. Internal_CreateInstance(this, texture, ref uvOffset, ref uvScale);
  38. }
  39. /// <summary>
  40. /// Texture that the sprite texture references.
  41. /// </summary>
  42. public Texture2D Texture
  43. {
  44. get { return Internal_GetTexture(mCachedPtr); }
  45. set
  46. {
  47. IntPtr texturePtr = IntPtr.Zero;
  48. if (value != null)
  49. texturePtr = value.GetCachedPtr();
  50. Internal_SetTexture(mCachedPtr, texturePtr);
  51. }
  52. }
  53. /// <summary>
  54. /// Offset into the referenced texture where the sprite starts. In UV coordinates, range [0, 1].
  55. /// </summary>
  56. public Vector2 Offset
  57. {
  58. get { Vector2 value; Internal_GetOffset(mCachedPtr, out value); return value; }
  59. set { Internal_SetOffset(mCachedPtr, ref value); }
  60. }
  61. /// <summary>
  62. /// Size of the sprite in the referenced texture. In UV coordinates, range [0, 1].
  63. /// </summary>
  64. public Vector2 Scale
  65. {
  66. get { Vector2 value; Internal_GetScale(mCachedPtr, out value); return value; }
  67. set { Internal_SetScale(mCachedPtr, ref value); }
  68. }
  69. /// <summary>
  70. /// Returns width of the sprite texture in pixels.
  71. /// </summary>
  72. public int Width
  73. {
  74. get { return Internal_GetWidth(mCachedPtr); }
  75. }
  76. /// <summary>
  77. /// Returns height of the sprite texture in pixels.
  78. /// </summary>
  79. public int Height
  80. {
  81. get { return Internal_GetHeight(mCachedPtr); }
  82. }
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. private static extern void Internal_CreateInstance(SpriteTexture instance,
  85. Texture2D texture, ref Vector2 offset, ref Vector2 scale);
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. private static extern Texture2D Internal_GetTexture(IntPtr thisPtr);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. private static extern void Internal_SetTexture(IntPtr thisPtr, IntPtr value);
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern void Internal_GetOffset(IntPtr thisPtr, out Vector2 value);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_SetOffset(IntPtr thisPtr, ref Vector2 value);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern void Internal_GetScale(IntPtr thisPtr, out Vector2 value);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_SetScale(IntPtr thisPtr, ref Vector2 value);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern int Internal_GetWidth(IntPtr thisPtr);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern int Internal_GetHeight(IntPtr thisPtr);
  102. }
  103. }