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.

169 lines
3.8KB

  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. // ------------------------------------------------------
  22. // use namespace
  23. using namespace DGL;
  24. // ------------------------------------------------------
  25. // Geometry
  26. class RectanglesWidget : public Widget
  27. {
  28. public:
  29. RectanglesWidget(Window& parent)
  30. : Widget(parent)
  31. {
  32. for (int i=0; i<9; ++i)
  33. fClicked[i] = false;
  34. }
  35. protected:
  36. void onDisplay() override
  37. {
  38. const int width = getWidth();
  39. const int height = getHeight();
  40. DGL::Rectangle<int> r;
  41. r.setWidth(width/3 - 6);
  42. r.setHeight(height/3 - 6);
  43. // draw a 3x3 grid
  44. for (int i=0; i<3; ++i)
  45. {
  46. r.setX(3 + i*width/3);
  47. // 1st
  48. r.setY(3);
  49. if (fClicked[0+i])
  50. glColor3f(0.8f, 0.5f, 0.3f);
  51. else
  52. glColor3f(0.3f, 0.5f, 0.8f);
  53. r.draw();
  54. // 2nd
  55. r.setY(3 + height/3);
  56. if (fClicked[3+i])
  57. glColor3f(0.8f, 0.5f, 0.3f);
  58. else
  59. glColor3f(0.3f, 0.5f, 0.8f);
  60. r.draw();
  61. // 3rd
  62. r.setY(3 + height*2/3);
  63. if (fClicked[6+i])
  64. glColor3f(0.8f, 0.5f, 0.3f);
  65. else
  66. glColor3f(0.3f, 0.5f, 0.8f);
  67. r.draw();
  68. }
  69. }
  70. void onReshape(int width, int height) override
  71. {
  72. // make this widget same size as window
  73. setSize(width, height);
  74. Widget::onReshape(width, height);
  75. }
  76. bool onMouse(int button, bool press, int x, int y) override
  77. {
  78. if (button != 1 || ! press)
  79. return false;
  80. const Point<int> pos(x, y);
  81. const int width = getWidth();
  82. const int height = getHeight();
  83. DGL::Rectangle<int> r;
  84. r.setWidth(width/3 - 6);
  85. r.setHeight(height/3 - 6);
  86. // draw a 3x3 grid
  87. for (int i=0; i<3; ++i)
  88. {
  89. r.setX(3 + i*width/3);
  90. r.setY(3);
  91. if (r.contains(pos))
  92. {
  93. fClicked[0+i] = !fClicked[0+i];
  94. repaint();
  95. break;
  96. }
  97. r.setY(3 + height/3);
  98. if (r.contains(pos))
  99. {
  100. fClicked[3+i] = !fClicked[3+i];
  101. repaint();
  102. break;
  103. }
  104. r.setY(3 + height*2/3);
  105. if (r.contains(pos))
  106. {
  107. fClicked[6+i] = !fClicked[6+i];
  108. repaint();
  109. break;
  110. }
  111. }
  112. return true;
  113. }
  114. private:
  115. bool fClicked[9];
  116. };
  117. // ------------------------------------------------------
  118. // main entry point
  119. int main()
  120. {
  121. App app;
  122. Window win(app);
  123. RectanglesWidget rects(win);
  124. win.setSize(300, 300);
  125. win.setTitle("Rectangles");
  126. win.show();
  127. app.exec();
  128. return 0;
  129. }
  130. // ------------------------------------------------------