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.

NekoWidget.hpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Neko widget animation
  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 doc/GPL.txt file.
  16. */
  17. #ifndef NEKO_WIDGET_HPP_INCLUDED
  18. #define NEKO_WIDGET_HPP_INCLUDED
  19. #include "DistrhoArtworkNekobi.hpp"
  20. #include "Image.hpp"
  21. #include "Widget.hpp"
  22. #include <cstdlib> // rand
  23. using DGL::Image;
  24. USE_NAMESPACE_DGL;
  25. // -----------------------------------------------------------------------
  26. class NekoWidget
  27. {
  28. public:
  29. NekoWidget()
  30. : fPos(0),
  31. fTimer(0),
  32. fTimerSpeed(20),
  33. fCurAction(kActionNone),
  34. fCurImage(&fImages.sit)
  35. {
  36. // load images
  37. {
  38. using namespace DistrhoArtworkNekobi;
  39. #define JOIN(a, b) a ## b
  40. #define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));
  41. LOAD_IMAGE(sit)
  42. LOAD_IMAGE(tail)
  43. LOAD_IMAGE(claw1)
  44. LOAD_IMAGE(claw2)
  45. LOAD_IMAGE(scratch1)
  46. LOAD_IMAGE(scratch2)
  47. LOAD_IMAGE(run1)
  48. LOAD_IMAGE(run2)
  49. LOAD_IMAGE(run3)
  50. LOAD_IMAGE(run4)
  51. #undef JOIN
  52. #undef LOAD_IMAGE
  53. }
  54. }
  55. void draw()
  56. {
  57. int x = fPos+108;
  58. int y = -2;
  59. if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
  60. {
  61. x += 2;
  62. y += 12;
  63. }
  64. fCurImage->draw(x, y);
  65. }
  66. // returns true if needs repaint
  67. bool idle()
  68. {
  69. if (++fTimer % fTimerSpeed != 0) // target is 20ms
  70. return false;
  71. if (fTimer == fTimerSpeed*9)
  72. {
  73. if (fCurAction == kActionNone)
  74. fCurAction = static_cast<Action>(std::rand() % kActionCount);
  75. else
  76. fCurAction = kActionNone;
  77. fTimer = 0;
  78. }
  79. switch (fCurAction)
  80. {
  81. case kActionNone:
  82. if (fCurImage == &fImages.sit)
  83. fCurImage = &fImages.tail;
  84. else
  85. fCurImage = &fImages.sit;
  86. break;
  87. case kActionClaw:
  88. if (fCurImage == &fImages.claw1)
  89. fCurImage = &fImages.claw2;
  90. else
  91. fCurImage = &fImages.claw1;
  92. break;
  93. case kActionScratch:
  94. if (fCurImage == &fImages.scratch1)
  95. fCurImage = &fImages.scratch2;
  96. else
  97. fCurImage = &fImages.scratch1;
  98. break;
  99. case kActionRunRight:
  100. if (fTimer == 0 && fPos > 20*9)
  101. {
  102. // run the other way
  103. --fTimer;
  104. fCurAction = kActionRunLeft;
  105. idle();
  106. break;
  107. }
  108. fPos += 20;
  109. if (fCurImage == &fImages.run1)
  110. fCurImage = &fImages.run2;
  111. else
  112. fCurImage = &fImages.run1;
  113. break;
  114. case kActionRunLeft:
  115. if (fTimer == 0 && fPos < 20*9)
  116. {
  117. // run the other way
  118. --fTimer;
  119. fCurAction = kActionRunRight;
  120. idle();
  121. break;
  122. }
  123. fPos -= 20;
  124. if (fCurImage == &fImages.run3)
  125. fCurImage = &fImages.run4;
  126. else
  127. fCurImage = &fImages.run3;
  128. break;
  129. case kActionCount:
  130. break;
  131. }
  132. return true;
  133. }
  134. void setTimerSpeed(int speed)
  135. {
  136. fTimer = 0;
  137. fTimerSpeed = speed;
  138. }
  139. // -------------------------------------------------------------------
  140. private:
  141. enum Action {
  142. kActionNone, // bounce tail
  143. kActionClaw,
  144. kActionScratch,
  145. kActionRunRight,
  146. kActionRunLeft,
  147. kActionCount
  148. };
  149. struct Images {
  150. Image sit;
  151. Image tail;
  152. Image claw1;
  153. Image claw2;
  154. Image scratch1;
  155. Image scratch2;
  156. Image run1;
  157. Image run2;
  158. Image run3;
  159. Image run4;
  160. } fImages;
  161. int fPos;
  162. int fTimer;
  163. int fTimerSpeed;
  164. Action fCurAction;
  165. Image* fCurImage;
  166. };
  167. // -----------------------------------------------------------------------
  168. #endif // NEKO_WIDGET_HPP_INCLUDED