Utility.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/platformGL.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "math/mRandom.h"
  27. #include "2d/sceneobject/SceneObject.h"
  28. #include "2d/core/Utility.h"
  29. #include "Utility_ScriptBinding.h"
  30. //-----------------------------------------------------------------------------
  31. ConsoleType( b2AABB, Typeb2AABB, sizeof(b2AABB), "" )
  32. ConsoleGetType( Typeb2AABB )
  33. {
  34. // Fetch AABB.
  35. const b2AABB* pAABB = (b2AABB*)dptr;
  36. // Format AABB.
  37. char* pBuffer = Con::getReturnBuffer(64);
  38. dSprintf(pBuffer, 64, "%.5g %.5g %.5g %.5g", pAABB->lowerBound.x, pAABB->lowerBound.y, pAABB->upperBound.x, pAABB->upperBound.y );
  39. return pBuffer;
  40. }
  41. ConsoleSetType( Typeb2AABB )
  42. {
  43. // Fetch AABB.
  44. b2AABB* pAABB = (b2AABB*)dptr;
  45. // "lowerX lowerY upperX upperY".
  46. if( argc == 1 )
  47. {
  48. if ( dSscanf(argv[0], "%g %g %g %g", &(pAABB->lowerBound.x), &(pAABB->lowerBound.y), &(pAABB->upperBound.x), &(pAABB->upperBound.y) ) == 4 )
  49. return;
  50. }
  51. // "lowerX,lowerY,upperX,upperY".
  52. else if( argc == 4 )
  53. {
  54. pAABB->lowerBound.Set( dAtof(argv[0]), dAtof(argv[1] ) );
  55. pAABB->upperBound.Set( dAtof(argv[2]), dAtof(argv[3] ) );
  56. return;
  57. }
  58. // Reset the AABB.
  59. pAABB->lowerBound.SetZero();
  60. pAABB->upperBound.SetZero();
  61. // Warn.
  62. Con::printf("Typeb2AABB must be set as { lowerX, lowerY, upperX, upperY } or \"lowerX lowerY upperX upperY\"");
  63. }
  64. //-----------------------------------------------------------------------------
  65. namespace Utility
  66. {
  67. //-----------------------------------------------------------------------------
  68. const char* mGetFirstNonWhitespace( const char* inString )
  69. {
  70. // Search for first non-whitespace.
  71. while(*inString == ' ' || *inString == '\n' || *inString == '\t')
  72. inString++;
  73. // Return found point.
  74. return inString;
  75. }
  76. //------------------------------------------------------------------------------
  77. // NOTE:- You must verify that elements (index/index+1) are valid first!
  78. Vector2 mGetStringElementVector( const char* inString, const U32 index )
  79. {
  80. const U32 elementCount = Utility::mGetStringElementCount(inString);
  81. if ((index + 1) >= elementCount )
  82. {
  83. const F32 element = dAtof(mGetStringElement(inString,index));
  84. return Vector2(element, element);
  85. }
  86. // Get String Element Vector.
  87. return Vector2( dAtof(mGetStringElement(inString,index)), dAtof(Utility::mGetStringElement(inString,index+1)) );
  88. }
  89. //------------------------------------------------------------------------------
  90. // NOTE:- You must verify that elements (index/index+1/index+2) are valid first!
  91. VectorF mGetStringElementVector3D( const char* inString, const U32 index )
  92. {
  93. if ((index + 2) >= Utility::mGetStringElementCount(inString))
  94. return VectorF(0.0f, 0.0f, 0.0f);
  95. // Get String Element Vector.
  96. return VectorF( dAtof(mGetStringElement(inString,index)), dAtof(Utility::mGetStringElement(inString,index+1)), dAtof(Utility::mGetStringElement(inString,index+2)) );
  97. }
  98. //-----------------------------------------------------------------------------
  99. const char* mGetStringElement( const char* inString, const U32 index, const bool copyBuffer )
  100. {
  101. // Non-whitespace chars.
  102. static const char* set = " \t\n";
  103. U32 wordCount = 0;
  104. U8 search = 0;
  105. const char* pWordStart = NULL;
  106. // End of string?
  107. if ( *inString != 0 )
  108. {
  109. // No, so search string.
  110. while( *inString )
  111. {
  112. // Get string element.
  113. search = *inString;
  114. // End of string?
  115. if ( search == 0 )
  116. break;
  117. // Move to next element.
  118. inString++;
  119. // Search for separators.
  120. for( U32 i = 0; set[i]; i++ )
  121. {
  122. // Found one?
  123. if( search == set[i] )
  124. {
  125. // Yes...
  126. search = 0;
  127. break;
  128. }
  129. }
  130. // Found a separator?
  131. if ( search == 0 )
  132. continue;
  133. // Found are word?
  134. if ( wordCount == index )
  135. {
  136. // Yes, so mark it.
  137. pWordStart = inString-1;
  138. }
  139. // We've found a non-separator.
  140. wordCount++;
  141. // Search for end of non-separator.
  142. while( 1 )
  143. {
  144. // Get string element.
  145. search = *inString;
  146. // End of string?
  147. if ( search == 0 )
  148. break;
  149. // Move to next element.
  150. inString++;
  151. // Search for separators.
  152. for( U32 i = 0; set[i]; i++ )
  153. {
  154. // Found one?
  155. if( search == set[i] )
  156. {
  157. // Yes...
  158. search = 0;
  159. break;
  160. }
  161. }
  162. // Found separator?
  163. if ( search == 0 )
  164. break;
  165. }
  166. // Have we found our word?
  167. if ( pWordStart )
  168. {
  169. // Yes, so return position if not copying buffer.
  170. if ( !copyBuffer )
  171. return pWordStart;
  172. // Result Buffer.
  173. static char buffer[4096];
  174. // Calculate word length.
  175. const U32 length = (const U32)(inString - pWordStart - ((*inString)?1:0));
  176. // Copy Word.
  177. dStrncpy( buffer, pWordStart, length);
  178. buffer[length] = '\0';
  179. // Return Word.
  180. return buffer;
  181. }
  182. // End of string?
  183. if ( *inString == 0 )
  184. {
  185. // Bah!
  186. break;
  187. }
  188. }
  189. }
  190. // Sanity!
  191. AssertFatal( false, "Utility::mGetStringElement() - Couldn't find specified string element!" );
  192. // Didn't find it
  193. return StringTable->EmptyString;
  194. }
  195. //-----------------------------------------------------------------------------
  196. U32 mGetStringElementCount( const char* inString )
  197. {
  198. // Non-whitespace chars.
  199. static const char* set = " \t\n";
  200. // End of string.
  201. if ( *inString == 0 )
  202. return 0;
  203. U32 wordCount = 0;
  204. U8 search = 0;
  205. // Search String.
  206. while( *inString )
  207. {
  208. // Get string element.
  209. search = *inString;
  210. // End of string?
  211. if ( search == 0 )
  212. break;
  213. // Move to next element.
  214. inString++;
  215. // Search for separators.
  216. for( U32 i = 0; set[i]; i++ )
  217. {
  218. // Found one?
  219. if( search == set[i] )
  220. {
  221. // Yes...
  222. search = 0;
  223. break;
  224. }
  225. }
  226. // Found a separator?
  227. if ( search == 0 )
  228. continue;
  229. // We've found a non-separator.
  230. wordCount++;
  231. // Search for end of non-separator.
  232. while( 1 )
  233. {
  234. // Get string element.
  235. search = *inString;
  236. // End of string?
  237. if ( search == 0 )
  238. break;
  239. // Move to next element.
  240. inString++;
  241. // Search for separators.
  242. for( U32 i = 0; set[i]; i++ )
  243. {
  244. // Found one?
  245. if( search == set[i] )
  246. {
  247. // Yes...
  248. search = 0;
  249. break;
  250. }
  251. }
  252. // Found Separator?
  253. if ( search == 0 )
  254. break;
  255. }
  256. // End of string?
  257. if ( *inString == 0 )
  258. {
  259. // Bah!
  260. break;
  261. }
  262. }
  263. // We've finished.
  264. return wordCount;
  265. }
  266. //-----------------------------------------------------------------------------
  267. U32 mConvertStringToMask( const char* string )
  268. {
  269. // Grab the element count of the first parameter.
  270. const U32 elementCount = Utility::mGetStringElementCount(string);
  271. // Make sure we get at least one number.
  272. if (elementCount < 1)
  273. return MASK_ALL;
  274. else if ( elementCount == 1 )
  275. {
  276. if ( dStricmp( string, "all" ) == 0 )
  277. return MASK_ALL;
  278. else if ( dStricmp( string, "none" ) == 0 || dStricmp( string, "off" ) == 0 )
  279. return 0;
  280. }
  281. // The mask.
  282. U32 mask = 0;
  283. // Convert the string to a mask.
  284. for (U32 i = 0; i < elementCount; i++)
  285. {
  286. S32 bit = dAtoi(Utility::mGetStringElement(string, i));
  287. // Make sure the group is valid.
  288. if ((bit < 0) || (bit >= MASK_BITCOUNT))
  289. {
  290. Con::warnf("Utility::mConvertStringToMask() - Invalid group specified (%d); skipped!", bit);
  291. continue;
  292. }
  293. mask |= (1 << bit);
  294. }
  295. return mask;
  296. }
  297. //-----------------------------------------------------------------------------
  298. const char* mConvertMaskToString( const U32 mask )
  299. {
  300. bool first = true;
  301. static char bits[128];
  302. bits[0] = '\0';
  303. if (!mask)
  304. {
  305. dSprintf(bits, 8, "none");
  306. return bits;
  307. }
  308. for (S32 i = 0; i < MASK_BITCOUNT; i++)
  309. {
  310. if (mask & BIT(i))
  311. {
  312. char bit[4];
  313. dSprintf(bit, 4, "%s%d", first ? "" : " ", i);
  314. first = false;
  315. dStrcat(bits, bit);
  316. }
  317. }
  318. return bits;
  319. }
  320. } // Namespace Utility