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.

150 lines
3.4KB

  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. // Single color widget
  26. class ColorWidget : public Widget,
  27. public IdleCallback
  28. {
  29. public:
  30. ColorWidget(Window& parent)
  31. : Widget(parent),
  32. cur('r'),
  33. reverse(false),
  34. r(0), g(0), b(0)
  35. {
  36. parent.addIdleCallback(this);
  37. }
  38. private:
  39. void idleCallback() override
  40. {
  41. switch (cur)
  42. {
  43. case 'r':
  44. if (reverse)
  45. {
  46. if (--r == 0)
  47. cur = 'g';
  48. }
  49. else
  50. {
  51. if (++r == 100)
  52. cur = 'g';
  53. }
  54. break;
  55. case 'g':
  56. if (reverse)
  57. {
  58. if (--g == 0)
  59. cur = 'b';
  60. }
  61. else
  62. {
  63. if (++g == 100)
  64. cur = 'b';
  65. }
  66. break;
  67. case 'b':
  68. if (reverse)
  69. {
  70. if (--b == 0)
  71. {
  72. cur = 'r';
  73. reverse = false;
  74. }
  75. }
  76. else
  77. {
  78. if (++b == 100)
  79. {
  80. cur = 'r';
  81. reverse = true;
  82. }
  83. }
  84. break;
  85. }
  86. repaint();
  87. }
  88. void onDisplay() override
  89. {
  90. // paint bg color (in full size)
  91. glColor3b(r, g, b);
  92. bgFull.draw();
  93. // paint inverted color (in 2/3 size)
  94. glColor3b(100-r, 100-g, 100-b);
  95. bgSmall.draw();
  96. }
  97. void onReshape(int width, int height) override
  98. {
  99. // full bg
  100. bgFull = DGL::Rectangle<int>(0, 0, width, height);
  101. // small bg, centered 2/3 size
  102. bgSmall = DGL::Rectangle<int>(width/6, height/6, width*2/3, height*2/3);
  103. // make widget same size as window
  104. setSize(width, height);
  105. // default reshape implementation
  106. Widget::onReshape(width, height);
  107. }
  108. char cur;
  109. bool reverse;
  110. int r, g, b;
  111. DGL::Rectangle<int> bgFull, bgSmall;
  112. };
  113. // ------------------------------------------------------
  114. // main entry point
  115. int main()
  116. {
  117. App app;
  118. Window win(app);
  119. ColorWidget color(win);
  120. win.setSize(300, 300);
  121. win.setTitle("Color");
  122. win.show();
  123. app.exec();
  124. return 0;
  125. }
  126. // ------------------------------------------------------