DPF OpenGL examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.0KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // ------------------------------------------------------
  17. // DGL Stuff
  18. #include "App.hpp"
  19. #include "Window.hpp"
  20. #include "Widget.hpp"
  21. #include <cmath>
  22. // ------------------------------------------------------
  23. // use namespace
  24. using namespace DGL;
  25. // taken from http://slabode.exofire.net/circle_draw.shtml
  26. template<typename T>
  27. void drawCircle(const Point<T>& fPos, const T& fSize, const int fNumSegments, const bool isOutline)
  28. {
  29. // precalculate the sine and cosine
  30. const float fTheta = 2.0f * 3.1415926f / float(fNumSegments);
  31. const float fCos = std::cos(fTheta);
  32. const float fSin = std::sin(fTheta);
  33. // paint
  34. float t, x = fSize, y = 0;
  35. glBegin(isOutline ? GL_LINE_LOOP : GL_POLYGON);
  36. for (int i=0; i<fNumSegments; ++i)
  37. {
  38. // output vertex
  39. glVertex2f(x + fPos.getX(), y + fPos.getY());
  40. // apply the rotation matrix
  41. t = x;
  42. x = fCos * x - fSin * y;
  43. y = fSin * t + fCos * y;
  44. }
  45. glEnd();
  46. }
  47. // ------------------------------------------------------
  48. // our widget
  49. class DummyWidget : public Widget
  50. {
  51. public:
  52. DummyWidget(Window& win)
  53. : Widget(win)
  54. {
  55. }
  56. protected:
  57. void onDisplay() override
  58. {
  59. #if 0
  60. glEnable(GL_MULTISAMPLE);
  61. glEnable(GL_LINE_SMOOTH);
  62. glEnable(GL_SRC_ALPHA);
  63. glEnable(GL_ONE_MINUS_SRC_ALPHA);
  64. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  65. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  66. #endif
  67. glLineWidth(1.0f);
  68. glColor3f(0.302f, 0.337f, 0.361f);
  69. bgFull.draw();
  70. glColor3f(0.235f, 0.271f, 0.294f);
  71. bgSmall.draw();
  72. glColor3f(0.176f, 0.212f, 0.235f);
  73. bgSmall.drawOutline();
  74. glColor3f(0.302f*2, 0.337f*2, 0.361f*2);
  75. tri.draw();
  76. glLineWidth(3.0f);
  77. glColor3f(0.302f/2.0f, 0.337f/2.0f, 0.361f/2.0f);
  78. tri.drawOutline();
  79. glColor3f(0.235f, 0.271f, 0.294f);
  80. drawCircle<int>(Point<int>(150, 200), 50, 300, false);
  81. glLineWidth(2.0f);
  82. glColor3f(0.176f/4, 0.212f/4, 0.235f/4);
  83. drawCircle<int>(Point<int>(150, 200), 50, 300, true);
  84. }
  85. void onReshape(int width, int height) override
  86. {
  87. // full bg
  88. bgFull = DGL::Rectangle<int>(0, 0, width, height);
  89. // small bg
  90. bgSmall = DGL::Rectangle<int>(20, 10, width-40, height-20);
  91. // center triangle
  92. tri = Triangle<int>(width*0.5, height*0.1, width*0.1, height*0.9, width*0.9, height*0.9);
  93. // make widget same size as window
  94. setSize(width, height);
  95. // default reshape implementation
  96. Widget::onReshape(width, height);
  97. }
  98. private:
  99. Rectangle<int> bgFull, bgSmall;
  100. Triangle<int> tri;
  101. };
  102. // ------------------------------------------------------
  103. // Our Demo Window
  104. class DemoWindow : public Window
  105. {
  106. public:
  107. DemoWindow(App& app)
  108. : Window(app),
  109. w1(*this)
  110. {
  111. setSize(300, 300);
  112. setTitle("DGL Demo");
  113. }
  114. private:
  115. DummyWidget w1;
  116. };
  117. // ------------------------------------------------------
  118. // main entry point
  119. int main()
  120. {
  121. App app;
  122. DemoWindow win(app);
  123. win.show();
  124. app.exec();
  125. return 0;
  126. }
  127. // ------------------------------------------------------