GLBoolean.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. namespace Veldrid.OpenGLBinding
  3. {
  4. /// <summary>
  5. /// A boolean value stored in an unsigned byte.
  6. /// </summary>
  7. public struct GLboolean : IEquatable<GLboolean>
  8. {
  9. /// <summary>
  10. /// The raw value of the <see cref="GLboolean"/>. A value of 0 represents "false", all other values represent "true".
  11. /// </summary>
  12. public byte Value;
  13. /// <summary>
  14. /// Constructs a new <see cref="GLboolean"/> with the given raw value.
  15. /// </summary>
  16. /// <param name="value"></param>
  17. public GLboolean(byte value)
  18. {
  19. Value = value;
  20. }
  21. /// <summary>
  22. /// Represents the boolean "true" value. Has a raw value of 1.
  23. /// </summary>
  24. public static readonly GLboolean True = new GLboolean(1);
  25. /// <summary>
  26. /// Represents the boolean "true" value. Has a raw value of 0.
  27. /// </summary>
  28. public static readonly GLboolean False = new GLboolean(0);
  29. /// <summary>
  30. /// Returns whether another <see cref="GLboolean"/> value is considered equal to this one.
  31. /// Two <see cref="GLboolean"/>s are considered equal when their raw values are equal.
  32. /// </summary>
  33. /// <param name="other">The value to compare to.</param>
  34. /// <returns>True if the other value's underlying raw value is equal to this instance's. False otherwise.</returns>
  35. public bool Equals(GLboolean other)
  36. {
  37. return Value.Equals(other.Value);
  38. }
  39. public override bool Equals(object obj)
  40. {
  41. return obj is GLboolean b && Equals(b);
  42. }
  43. public override int GetHashCode()
  44. {
  45. return Value.GetHashCode();
  46. }
  47. public override string ToString()
  48. {
  49. return $"{(this ? "True" : "False")} ({Value})";
  50. }
  51. public static implicit operator bool(GLboolean b) => b.Value != 0;
  52. public static implicit operator uint(GLboolean b) => b.Value;
  53. public static implicit operator GLboolean(bool b) => b ? True : False;
  54. public static implicit operator GLboolean(byte value) => new GLboolean(value);
  55. public static bool operator ==(GLboolean left, GLboolean right) => left.Value == right.Value;
  56. public static bool operator !=(GLboolean left, GLboolean right) => left.Value != right.Value;
  57. }
  58. }