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.

204 lines
5.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. #ifndef EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "Image.hpp"
  21. #include "Widget.hpp"
  22. #include "Window.hpp"
  23. // ------------------------------------------------------
  24. // Images
  25. #include "../images_res/CatPics.hpp"
  26. // ------------------------------------------------------
  27. // our widget
  28. class ExampleImagesWidget : public Widget,
  29. public IdleCallback
  30. {
  31. public:
  32. static const int kImg1y = 0;
  33. static const int kImg2y = 500/2-CatPics::cat2Height/2;
  34. static const int kImg3x = 400/3-CatPics::cat3Width/3;
  35. static const int kImg1max = 500-CatPics::cat1Width;
  36. static const int kImg2max = 500-CatPics::cat2Width;
  37. static const int kImg3max = 400-CatPics::cat3Height;
  38. ExampleImagesWidget(Window& parent, const bool setParentSize = false)
  39. : Widget(parent),
  40. fImgTop1st(1),
  41. fImgTop2nd(2),
  42. fImgTop3rd(3),
  43. fImg1x(0),
  44. fImg2x(kImg2max),
  45. fImg3y(kImg3max),
  46. fImg1rev(false),
  47. fImg2rev(true),
  48. fImg3rev(true),
  49. fImg1(CatPics::cat1Data, CatPics::cat1Width, CatPics::cat1Height, GL_BGR),
  50. fImg2(CatPics::cat2Data, CatPics::cat2Width, CatPics::cat2Height, GL_BGR),
  51. fImg3(CatPics::cat3Data, CatPics::cat3Width, CatPics::cat3Height, GL_BGR)
  52. {
  53. setSize(500, 400);
  54. parent.addIdleCallback(this);
  55. if (setParentSize)
  56. {
  57. parent.setSize(500, 400);
  58. parent.setResizable(false);
  59. }
  60. }
  61. private:
  62. void idleCallback() noexcept override
  63. {
  64. if (fImg1rev)
  65. {
  66. fImg1x -= 2;
  67. if (fImg1x <= -50)
  68. {
  69. fImg1rev = false;
  70. setNewTopImg(1);
  71. }
  72. }
  73. else
  74. {
  75. fImg1x += 2;
  76. if (fImg1x >= kImg1max+50)
  77. {
  78. fImg1rev = true;
  79. setNewTopImg(1);
  80. }
  81. }
  82. if (fImg2rev)
  83. {
  84. fImg2x -= 1;
  85. if (fImg2x <= -50)
  86. {
  87. fImg2rev = false;
  88. setNewTopImg(2);
  89. }
  90. }
  91. else
  92. {
  93. fImg2x += 4;
  94. if (fImg2x >= kImg2max+50)
  95. {
  96. fImg2rev = true;
  97. setNewTopImg(2);
  98. }
  99. }
  100. if (fImg3rev)
  101. {
  102. fImg3y -= 3;
  103. if (fImg3y <= -50)
  104. {
  105. fImg3rev = false;
  106. setNewTopImg(3);
  107. }
  108. }
  109. else
  110. {
  111. fImg3y += 3;
  112. if (fImg3y >= kImg3max+50)
  113. {
  114. fImg3rev = true;
  115. setNewTopImg(3);
  116. }
  117. }
  118. repaint();
  119. }
  120. void onDisplay() override
  121. {
  122. switch (fImgTop3rd)
  123. {
  124. case 1:
  125. fImg1.drawAt(fImg1x, kImg1y);
  126. break;
  127. case 2:
  128. fImg2.drawAt(fImg2x, kImg2y);
  129. break;
  130. case 3:
  131. fImg3.drawAt(kImg3x, fImg3y);
  132. break;
  133. };
  134. switch (fImgTop2nd)
  135. {
  136. case 1:
  137. fImg1.drawAt(fImg1x, kImg1y);
  138. break;
  139. case 2:
  140. fImg2.drawAt(fImg2x, kImg2y);
  141. break;
  142. case 3:
  143. fImg3.drawAt(kImg3x, fImg3y);
  144. break;
  145. };
  146. switch (fImgTop1st)
  147. {
  148. case 1:
  149. fImg1.drawAt(fImg1x, kImg1y);
  150. break;
  151. case 2:
  152. fImg2.drawAt(fImg2x, kImg2y);
  153. break;
  154. case 3:
  155. fImg3.drawAt(kImg3x, fImg3y);
  156. break;
  157. };
  158. }
  159. void setNewTopImg(const int imgId) noexcept
  160. {
  161. if (fImgTop1st == imgId)
  162. return;
  163. if (fImgTop2nd == imgId)
  164. {
  165. fImgTop2nd = fImgTop1st;
  166. fImgTop1st = imgId;
  167. return;
  168. }
  169. fImgTop3rd = fImgTop2nd;
  170. fImgTop2nd = fImgTop1st;
  171. fImgTop1st = imgId;
  172. }
  173. int fImgTop1st, fImgTop2nd, fImgTop3rd;
  174. int fImg1x, fImg2x, fImg3y;
  175. bool fImg1rev, fImg2rev, fImg3rev;
  176. Image fImg1, fImg2, fImg3;
  177. };
  178. // ------------------------------------------------------
  179. #endif // EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED