Surface.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using OpenGl;
  18. using DataStructures;
  19. namespace Drawing
  20. {
  21. /// <summary>
  22. /// Surface class. This class basically holds a reference to an opengl texture
  23. /// along with texture coordinates that specify a rectangle on that texture.
  24. /// Several surface may share a single texture (but can still have different
  25. /// texture coordinates)
  26. /// </summary>
  27. public sealed class Surface : IDisposable, ICloneable
  28. {
  29. private float width;
  30. /// <summary>get surface width in pixels</summary>
  31. public float Width
  32. {
  33. get
  34. {
  35. return width;
  36. }
  37. }
  38. private float height;
  39. /// <summary>get surface height in pixels</summary>
  40. public float Height
  41. {
  42. get
  43. {
  44. return height;
  45. }
  46. }
  47. private ImageTexture texture;
  48. /// <summary>Get OpenGL Texture</summary>
  49. public Texture Texture
  50. {
  51. get
  52. {
  53. return texture;
  54. }
  55. }
  56. public float Left;
  57. public float Right;
  58. public float Top;
  59. public float Bottom;
  60. /// <summary>Construct a new Surface from the given image resource</summary>
  61. public Surface(string Resourcepath)
  62. {
  63. texture = TextureManager.Get(Resourcepath);
  64. texture.Ref();
  65. width = texture.ImageWidth;
  66. height = texture.ImageHeight;
  67. Left = 0;
  68. Top = 0;
  69. Right = texture.UVRight;
  70. Bottom = texture.UVBottom;
  71. }
  72. public Surface(string Resourcepath, float x, float y, float w, float h)
  73. {
  74. texture = TextureManager.Get(Resourcepath);
  75. texture.Ref();
  76. width = w;
  77. height = h;
  78. Left = x / texture.Width;
  79. Top = y / texture.Height;
  80. Right = (x + w) / texture.Width;
  81. Bottom = (y + h) / texture.Height;
  82. }
  83. public Surface(Surface other)
  84. {
  85. texture = other.texture;
  86. texture.Ref();
  87. width = other.width;
  88. height = other.height;
  89. Left = other.Left;
  90. Top = other.Top;
  91. Right = other.Right;
  92. Bottom = other.Bottom;
  93. }
  94. public object Clone()
  95. {
  96. return new Surface(this);
  97. }
  98. public void Dispose()
  99. {
  100. texture.UnRef();
  101. texture = null;
  102. }
  103. public void Draw(Vector pos)
  104. {
  105. gl.BindTexture(gl.TEXTURE_2D, texture.Handle);
  106. gl.Begin(gl.QUADS);
  107. gl.TexCoord2f(Left, Top);
  108. gl.Vertex2f(pos.X, pos.Y);
  109. gl.TexCoord2f(Right, Top);
  110. gl.Vertex2f(pos.X + Width, pos.Y);
  111. gl.TexCoord2f(Right, Bottom);
  112. gl.Vertex2f(pos.X + Width, pos.Y + Height);
  113. gl.TexCoord2f(Left, Bottom);
  114. gl.Vertex2f(pos.X, pos.Y + Height);
  115. gl.End();
  116. }
  117. }
  118. }
  119. /* EOF */