PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ChaosDS/arm9/source/Misc.cpp

https://code.google.com/p/quirkysoft/
C++ | 193 lines | 151 code | 14 blank | 28 comment | 21 complexity | c6fb8eee468e7721ef317a7de7df0231 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright (C) 2007 Richard Quirk
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <ctype.h>
  15. #include <stdio.h>
  16. #include "libnds.h"
  17. #include "GameState.h"
  18. #include "Misc.h"
  19. #include "Rectangle.h"
  20. #include "Text16.h"
  21. #include "Wizard.h"
  22. // default seed
  23. static unsigned int s_random(14071977);
  24. void Misc::seedRand(unsigned int seed) {
  25. s_random = seed;
  26. }
  27. // churn the random number
  28. void Misc::churnRand(void) {
  29. s_random *= 663608941;
  30. }
  31. unsigned int Misc::rand(unsigned int maximum) {
  32. unsigned long long result = maximum;
  33. result *= s_random;
  34. churnRand();
  35. return (unsigned int)(result >> 32);
  36. }
  37. char * Misc::trim(char * str) {
  38. char *s = str;
  39. char *d = str;
  40. // eat trailing white space
  41. while (*s)
  42. s++;
  43. while (str < s and isspace(*(s-1)))
  44. *s-- = '\0';
  45. *s = '\0';
  46. s = str;
  47. // has leading space?
  48. if (isspace(*s))
  49. {
  50. // eat white space
  51. while (isspace(*s))
  52. s++;
  53. // copy
  54. while (*s)
  55. *d++ = *s++;
  56. *d = '\0';
  57. }
  58. return str;
  59. }
  60. // based on code at c64c
  61. void Misc::orderTable(int count, unsigned char * table) {
  62. // orders "count" entries in the given table by priority
  63. // table contains priority, index pairs
  64. for (int i = 0; i < count; i++) {
  65. int index = 0;
  66. for (int j = 0; j < count; j++) {
  67. // priority 1, 2, value 1, 2
  68. unsigned char p1(table[index]);
  69. index++;
  70. unsigned char val1(table[index]);
  71. index++;
  72. unsigned char p2(table[index]);
  73. index++;
  74. unsigned char val2(table[index]);
  75. index--; // points to prio 2
  76. if (p2 > p1) {
  77. index++; // points to val 2
  78. table[index] = val1;
  79. index--; // points to prio 2
  80. table[index] = p1;
  81. index--; // points to val 1
  82. table[index] = val2;
  83. index--; // points to prio 1
  84. table[index] = p2;
  85. index++; // points to val 1
  86. index++; // points to prio 2
  87. }
  88. }
  89. }
  90. }
  91. bool Misc::keypressWait()
  92. {
  93. scanKeys();
  94. swiWaitForVBlank();
  95. u16 keysSlow = keysDownRepeat();
  96. if ( keysSlow & (KEY_A|KEY_B|KEY_L|KEY_R|
  97. KEY_UP|KEY_DOWN|KEY_LEFT|KEY_RIGHT|
  98. KEY_START|KEY_SELECT|KEY_X|KEY_Y|KEY_TOUCH))
  99. return true;
  100. return false;
  101. }
  102. void Misc::delay(int time, bool callMainLoop)
  103. {
  104. GameState & state(GameState::instance());
  105. state.resetGameFrame();
  106. while(state.gameFrame() < time)
  107. {
  108. swiWaitForVBlank();
  109. if (callMainLoop)
  110. state.mainLoopExecute();
  111. }
  112. }
  113. void Misc::waitForLetgo()
  114. {
  115. if (Wizard::currentPlayer().isCpu())
  116. return;
  117. bool prsd(true);
  118. while (prsd) {
  119. swiWaitForVBlank();
  120. scanKeys();
  121. u16 keys = keysHeld();
  122. if ( (keys & KEY_A)
  123. or (keys & KEY_B)
  124. or (keys & KEY_L)
  125. or (keys & KEY_R)
  126. or (keys & KEY_UP)
  127. or (keys & KEY_DOWN)
  128. or (keys & KEY_LEFT)
  129. or (keys & KEY_RIGHT)
  130. or (keys & KEY_START)
  131. or (keys & KEY_SELECT)
  132. )
  133. {
  134. prsd = true;
  135. GameState::instance().mainLoopExecute();
  136. }
  137. else
  138. {
  139. prsd = false;
  140. }
  141. }
  142. }
  143. bool Misc::confirm()
  144. {
  145. waitForLetgo();
  146. int yes_pressed = 0;
  147. // 012345678901234567890123456
  148. // DISMOUNT WIZARD? A=YES B=NO
  149. nds::Rectangle yesRect(17*8, Text16::MESSAGE_Y*8, 5*8, 16 );
  150. nds::Rectangle noRect(23*8, Text16::MESSAGE_Y*8, 4*8, 16 );
  151. while (yes_pressed == 0) {
  152. swiWaitForVBlank();
  153. scanKeys();
  154. u16 keysSlow = keysDownRepeat();
  155. if (keysSlow & KEY_A)
  156. yes_pressed = 2;
  157. if (keysSlow & KEY_B)
  158. yes_pressed = 1;
  159. if (keysSlow & KEY_TOUCH)
  160. {
  161. touchPosition pos;
  162. touchRead(&pos);
  163. if ( yesRect.hit(pos.px, pos.py) )
  164. {
  165. yes_pressed = 2;
  166. }
  167. else if ( noRect.hit(pos.px, pos.py) )
  168. {
  169. yes_pressed = 1;
  170. }
  171. }
  172. }
  173. waitForLetgo();
  174. yes_pressed--;
  175. return yes_pressed;
  176. }