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.

213 lines
5.2KB

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