GLWidgetBase.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. private void OnExposed(object o, ExposeEventArgs args)
  72. {
  73. if(!MakeCurrent()) {
  74. LogManager.Log(LogLevel.Warning, "Make Current - OnExposed failed");
  75. return;
  76. }
  77. GlUtil.ContextValid = true;
  78. gl.MatrixMode(gl.MODELVIEW);
  79. gl.LoadIdentity();
  80. gl.Scalef(Zoom, Zoom, 1f);
  81. gl.Translatef(Translation.X, Translation.Y, 0f);
  82. DrawGl();
  83. GlUtil.Assert("After Drawing");
  84. SwapBuffers();
  85. GlUtil.ContextValid = false;
  86. }
  87. private void OnConfigure(object o, ConfigureEventArgs args)
  88. {
  89. if(!MakeCurrent()) {
  90. LogManager.Log(LogLevel.Warning, "MakeCurrent() - OnConfigure failed");
  91. return;
  92. }
  93. GlUtil.ContextValid = true;
  94. // setup opengl state and transform
  95. gl.Disable(gl.DEPTH_TEST);
  96. gl.Disable(gl.CULL_FACE);
  97. gl.Enable(gl.TEXTURE_2D);
  98. gl.Enable(gl.BLEND);
  99. gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  100. gl.ClearColor(0f, 0f, 0f, 0f);
  101. gl.Viewport(0, 0, Allocation.Width, Allocation.Height);
  102. gl.MatrixMode(gl.PROJECTION);
  103. gl.LoadIdentity();
  104. gl.Ortho(0, Allocation.Width, Allocation.Height, 0,
  105. -1.0f, 1.0f);
  106. gl.MatrixMode(gl.MODELVIEW);
  107. gl.LoadIdentity();
  108. GlUtil.Assert("After setting opengl transforms");
  109. GlUtil.ContextValid = false;
  110. }
  111. protected abstract void DrawGl();
  112. }
  113. /* EOF */