Audio plugin host https://kx.studio/carla
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.

222 lines
5.0KB

  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "dgl/Image.hpp"
  18. #include "dgl/StandaloneWindow.hpp"
  19. #include "CarlaUtils.hpp"
  20. #include "NekoArtwork.hpp"
  21. // this is just a test file!
  22. #include <QtCore/QThread>
  23. USE_NAMESPACE_DGL;
  24. class NekoWidget : public Widget,
  25. QThread
  26. {
  27. public:
  28. NekoWidget(StandaloneWindow* win)
  29. : Widget(win->getWindow()),
  30. fPos(0),
  31. fTimer(0),
  32. fTimerSpeed(20),
  33. fCurAction(kActionNone),
  34. fCurImage(&fImages.sit)
  35. {
  36. // load images
  37. {
  38. using namespace NekoArtwork;
  39. fImages.background.loadFromMemory(backgroundData, backgroundWidth, backgroundHeight, GL_BGR);
  40. #define JOIN(a, b) a ## b
  41. #define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));
  42. LOAD_IMAGE(sit)
  43. LOAD_IMAGE(tail)
  44. LOAD_IMAGE(claw1)
  45. LOAD_IMAGE(claw2)
  46. LOAD_IMAGE(scratch1)
  47. LOAD_IMAGE(scratch2)
  48. LOAD_IMAGE(run1)
  49. LOAD_IMAGE(run2)
  50. LOAD_IMAGE(run3)
  51. LOAD_IMAGE(run4)
  52. #undef JOIN
  53. #undef LOAD_IMAGE
  54. }
  55. QThread::start();
  56. }
  57. ~NekoWidget()
  58. {
  59. hide();
  60. QThread::quit();
  61. QThread::wait();
  62. }
  63. protected:
  64. void onDisplay() override
  65. {
  66. int x = fPos+118;
  67. int y = -2;
  68. if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
  69. {
  70. x += 2;
  71. y += 12;
  72. }
  73. fImages.background.draw();
  74. fCurImage->draw(x, y);
  75. }
  76. void idle()
  77. {
  78. if (++fTimer % fTimerSpeed != 0) // target is 25ms
  79. return;
  80. if (fTimer == fTimerSpeed*9)
  81. {
  82. if (fCurAction == kActionNone)
  83. fCurAction = static_cast<Action>(rand() % kActionCount);
  84. else
  85. fCurAction = kActionNone;
  86. fTimer = 0;
  87. }
  88. switch (fCurAction)
  89. {
  90. case kActionNone:
  91. if (fCurImage == &fImages.sit)
  92. fCurImage = &fImages.tail;
  93. else
  94. fCurImage = &fImages.sit;
  95. break;
  96. case kActionClaw:
  97. if (fCurImage == &fImages.claw1)
  98. fCurImage = &fImages.claw2;
  99. else
  100. fCurImage = &fImages.claw1;
  101. break;
  102. case kActionScratch:
  103. if (fCurImage == &fImages.scratch1)
  104. fCurImage = &fImages.scratch2;
  105. else
  106. fCurImage = &fImages.scratch1;
  107. break;
  108. case kActionRunRight:
  109. if (fTimer == 0 && fPos > 20*9)
  110. {
  111. // run the other way
  112. --fTimer;
  113. fCurAction = kActionRunLeft;
  114. idle();
  115. break;
  116. }
  117. fPos += 20;
  118. if (fCurImage == &fImages.run1)
  119. fCurImage = &fImages.run2;
  120. else
  121. fCurImage = &fImages.run1;
  122. break;
  123. case kActionRunLeft:
  124. if (fTimer == 0 && fPos < 20*9)
  125. {
  126. // run the other way
  127. --fTimer;
  128. fCurAction = kActionRunRight;
  129. idle();
  130. break;
  131. }
  132. fPos -= 20;
  133. if (fCurImage == &fImages.run3)
  134. fCurImage = &fImages.run4;
  135. else
  136. fCurImage = &fImages.run3;
  137. break;
  138. case kActionCount:
  139. break;
  140. }
  141. repaint();
  142. }
  143. void run() override
  144. {
  145. while (isVisible())
  146. {
  147. idle();
  148. carla_msleep(10);
  149. }
  150. }
  151. private:
  152. enum Action {
  153. kActionNone, // bounce tail
  154. kActionClaw,
  155. kActionScratch,
  156. kActionRunRight,
  157. kActionRunLeft,
  158. kActionCount
  159. };
  160. struct Images {
  161. Image background;
  162. Image sit;
  163. Image tail;
  164. Image claw1;
  165. Image claw2;
  166. Image scratch1;
  167. Image scratch2;
  168. Image run1;
  169. Image run2;
  170. Image run3;
  171. Image run4;
  172. } fImages;
  173. int fPos;
  174. int fTimer;
  175. int fTimerSpeed;
  176. Action fCurAction;
  177. Image* fCurImage;
  178. };
  179. int main()
  180. {
  181. StandaloneWindow win;
  182. win.setSize(NekoArtwork::backgroundWidth, NekoArtwork::backgroundHeight);
  183. win.setWindowTitle("Neko Test");
  184. NekoWidget widget(&win);
  185. win.exec();
  186. return 0;
  187. }