DISTRHO Plugin Framework
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.

230 lines
6.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. #include "tests.hpp"
  17. #include "dgl/NanoVG.hpp"
  18. START_NAMESPACE_DGL
  19. // --------------------------------------------------------------------------------------------------------------------
  20. // Images
  21. #include "images_res/CatPics.cpp"
  22. // --------------------------------------------------------------------------------------------------------------------
  23. class NanoImageExample : public NanoStandaloneWindow,
  24. public IdleCallback
  25. {
  26. static const int kImg1y = 0;
  27. static const int kImg2y = 500/2-CatPics::cat2Height/2;
  28. static const int kImg3x = 400/3-CatPics::cat3Width/3;
  29. static const int kImg1max = 500-CatPics::cat1Width;
  30. static const int kImg2max = 500-CatPics::cat2Width;
  31. static const int kImg3max = 400-CatPics::cat3Height;
  32. static const int kImgFlags = IMAGE_GENERATE_MIPMAPS | IMAGE_REPEAT_X;
  33. int imgTop1st, imgTop2nd, imgTop3rd;
  34. int img1x, img2x, img3y;
  35. bool img1rev, img2rev, img3rev;
  36. NanoImage img1, img2, img3;
  37. public:
  38. NanoImageExample(Application& app)
  39. : NanoStandaloneWindow(app),
  40. imgTop1st(1),
  41. imgTop2nd(2),
  42. imgTop3rd(3),
  43. img1x(0),
  44. img2x(kImg2max),
  45. img3y(kImg3max),
  46. img1rev(false),
  47. img2rev(true),
  48. img3rev(true),
  49. img1(createImageFromRawMemory(CatPics::cat1Width, CatPics::cat1Height, (uchar*)CatPics::cat1Data, kImgFlags, kImageFormatBGR)),
  50. img2(createImageFromRawMemory(CatPics::cat2Width, CatPics::cat2Height, (uchar*)CatPics::cat2Data, kImgFlags, kImageFormatBGR)),
  51. img3(createImageFromRawMemory(CatPics::cat3Width, CatPics::cat3Height, (uchar*)CatPics::cat3Data, kImgFlags, kImageFormatBGR))
  52. {
  53. DISTRHO_SAFE_ASSERT(img1.isValid());
  54. DISTRHO_SAFE_ASSERT(img2.isValid());
  55. DISTRHO_SAFE_ASSERT(img3.isValid());
  56. DISTRHO_SAFE_ASSERT_UINT2(img1.getSize().getWidth() == CatPics::cat1Width,
  57. img1.getSize().getWidth(), CatPics::cat1Width);
  58. DISTRHO_SAFE_ASSERT_UINT2(img1.getSize().getHeight() == CatPics::cat1Height,
  59. img1.getSize().getHeight(), CatPics::cat1Height);
  60. DISTRHO_SAFE_ASSERT_UINT2(img2.getSize().getWidth() == CatPics::cat2Width,
  61. img2.getSize().getWidth(), CatPics::cat2Width);
  62. DISTRHO_SAFE_ASSERT_UINT2(img2.getSize().getHeight() == CatPics::cat2Height,
  63. img2.getSize().getHeight(), CatPics::cat2Height);
  64. DISTRHO_SAFE_ASSERT_UINT2(img3.getSize().getWidth() == CatPics::cat3Width,
  65. img3.getSize().getWidth(), CatPics::cat3Width);
  66. DISTRHO_SAFE_ASSERT_UINT2(img3.getSize().getHeight() == CatPics::cat3Height,
  67. img3.getSize().getHeight(), CatPics::cat3Height);
  68. setResizable(true);
  69. setSize(500, 500);
  70. setGeometryConstraints(500, 500, false, true);
  71. setTitle("NanoImage");
  72. done();
  73. addIdleCallback(this);
  74. }
  75. protected:
  76. void onNanoDisplay() override
  77. {
  78. // bottom image
  79. beginPath();
  80. fillPaint(setupImagePaint(imgTop3rd));
  81. fill();
  82. // middle image
  83. beginPath();
  84. fillPaint(setupImagePaint(imgTop2nd));
  85. fill();
  86. // top image
  87. beginPath();
  88. fillPaint(setupImagePaint(imgTop1st));
  89. fill();
  90. }
  91. void idleCallback() noexcept override
  92. {
  93. if (img1rev)
  94. {
  95. img1x -= 2;
  96. if (img1x <= -50)
  97. {
  98. img1rev = false;
  99. setNewTopImg(1);
  100. }
  101. }
  102. else
  103. {
  104. img1x += 2;
  105. if (img1x >= kImg1max+50)
  106. {
  107. img1rev = true;
  108. setNewTopImg(1);
  109. }
  110. }
  111. if (img2rev)
  112. {
  113. img2x -= 1;
  114. if (img2x <= -50)
  115. {
  116. img2rev = false;
  117. setNewTopImg(2);
  118. }
  119. }
  120. else
  121. {
  122. img2x += 4;
  123. if (img2x >= kImg2max+50)
  124. {
  125. img2rev = true;
  126. setNewTopImg(2);
  127. }
  128. }
  129. if (img3rev)
  130. {
  131. img3y -= 3;
  132. if (img3y <= -50)
  133. {
  134. img3rev = false;
  135. setNewTopImg(3);
  136. }
  137. }
  138. else
  139. {
  140. img3y += 3;
  141. if (img3y >= kImg3max+50)
  142. {
  143. img3rev = true;
  144. setNewTopImg(3);
  145. }
  146. }
  147. repaint();
  148. }
  149. private:
  150. Paint setupImagePaint(const int imgId) noexcept
  151. {
  152. switch (imgId)
  153. {
  154. case 1:
  155. rect(img1x, kImg1y, CatPics::cat1Width, CatPics::cat1Height);
  156. return imagePattern(img1x, kImg1y, CatPics::cat1Width, CatPics::cat1Height, 0, img1, 1.0f);
  157. case 2:
  158. rect(img2x, kImg2y, CatPics::cat2Width, CatPics::cat2Height);
  159. return imagePattern(img2x, kImg2y, CatPics::cat2Width, CatPics::cat2Height, 0, img2, 1.0f);
  160. case 3:
  161. rect(kImg3x, img3y, CatPics::cat3Width, CatPics::cat3Height);
  162. return imagePattern(kImg3x, img3y, CatPics::cat3Width, CatPics::cat3Height, 0, img3, 1.0f);
  163. };
  164. return Paint();
  165. }
  166. void setNewTopImg(const int imgId) noexcept
  167. {
  168. if (imgTop1st == imgId)
  169. return;
  170. if (imgTop2nd == imgId)
  171. {
  172. imgTop2nd = imgTop1st;
  173. imgTop1st = imgId;
  174. return;
  175. }
  176. imgTop3rd = imgTop2nd;
  177. imgTop2nd = imgTop1st;
  178. imgTop1st = imgId;
  179. }
  180. };
  181. // --------------------------------------------------------------------------------------------------------------------
  182. END_NAMESPACE_DGL
  183. int main()
  184. {
  185. USE_NAMESPACE_DGL;
  186. Application app(true);
  187. NanoImageExample win(app);
  188. win.show();
  189. app.exec();
  190. return 0;
  191. }
  192. // --------------------------------------------------------------------------------------------------------------------