GLWidgetBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Gtk;
  18. using Gdk;
  19. using OpenGl;
  20. using DataStructures;
  21. using Drawing;
  22. public abstract class GLWidgetBase : GLArea
  23. {
  24. public static GLWidgetBase ShareArea = null;
  25. private float _Zoom = 1.0f;
  26. protected float Zoom
  27. {
  28. get
  29. {
  30. return _Zoom;
  31. }
  32. set
  33. {
  34. _Zoom = value;
  35. QueueDraw();
  36. }
  37. }
  38. private Vector _Translation;
  39. protected Vector Translation
  40. {
  41. get
  42. {
  43. return _Translation;
  44. }
  45. set
  46. {
  47. _Translation = value;
  48. QueueDraw();
  49. }
  50. }
  51. private static int[] attrlist = {
  52. GLContextAttributes.Rgba,
  53. GLContextAttributes.RedSize, 1,
  54. GLContextAttributes.GreenSize, 1,
  55. GLContextAttributes.BlueSize, 1,
  56. // not really needed but some opengl drivers are buggy and need this
  57. GLContextAttributes.DepthSize, 1,
  58. GLContextAttributes.DoubleBuffer,
  59. GLContextAttributes.None
  60. };
  61. public GLWidgetBase()
  62. : base(attrlist, ShareArea)
  63. {
  64. GlUtil.ContextValid = false;
  65. ExposeEvent += OnExposed;
  66. ConfigureEvent += OnConfigure;
  67. if(ShareArea == null) {
  68. ShareArea = this;
  69. }
  70. }
  71. public GLWidgetBase(IntPtr windowHandle)
  72. : base(attrlist, ShareArea)
  73. {
  74. GlUtil.ContextValid = false;
  75. ExposeEvent += OnExposed;
  76. ConfigureEvent += OnConfigure;
  77. if(ShareArea == null) {
  78. ShareArea = this;
  79. ( (GLArea)(ShareArea) ).windowRef = IntPtr.Zero;
  80. }
  81. }
  82. private void OnExposed(object o, ExposeEventArgs args)
  83. {
  84. if(!MakeCurrent()) {
  85. LogManager.Log(LogLevel.Warning, "Make Current - OnExposed failed");
  86. return;
  87. }
  88. GlUtil.ContextValid = true;
  89. gl.MatrixMode(gl.MODELVIEW);
  90. gl.LoadIdentity();
  91. gl.Scalef(Zoom, Zoom, 1f);
  92. gl.Translatef(Translation.X, Translation.Y, 0f);
  93. DrawGl();
  94. GlUtil.Assert("After Drawing");
  95. SwapBuffers();
  96. GlUtil.ContextValid = false;
  97. }
  98. private void OnConfigure(object o, ConfigureEventArgs args)
  99. {
  100. if(!MakeCurrent()) {
  101. LogManager.Log(LogLevel.Warning, "MakeCurrent() - OnConfigure failed");
  102. return;
  103. }
  104. GlUtil.ContextValid = true;
  105. // setup opengl state and transform
  106. gl.Disable(gl.DEPTH_TEST);
  107. gl.Disable(gl.CULL_FACE);
  108. gl.Enable(gl.TEXTURE_2D);
  109. gl.Enable(gl.BLEND);
  110. gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  111. gl.ClearColor(0f, 0f, 0f, 0f);
  112. gl.Viewport(0, 0, Allocation.Width, Allocation.Height);
  113. gl.MatrixMode(gl.PROJECTION);
  114. gl.LoadIdentity();
  115. gl.Ortho(0, Allocation.Width, Allocation.Height, 0,
  116. -1.0f, 1.0f);
  117. gl.MatrixMode(gl.MODELVIEW);
  118. gl.LoadIdentity();
  119. GlUtil.Assert("After setting opengl transforms");
  120. GlUtil.ContextValid = false;
  121. }
  122. protected abstract void DrawGl();
  123. }
  124. /* EOF */