1
0

ccUtils.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /****************************************************************************
  2. Copyright (c) 2010 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __SUPPORT_CC_UTILS_H__
  22. #define __SUPPORT_CC_UTILS_H__
  23. #include <vector>
  24. #include <string>
  25. #include "2d/CCNode.h"
  26. #include "base/ccMacros.h"
  27. /** @file ccUtils.h
  28. Misc free functions
  29. */
  30. NS_CC_BEGIN
  31. /*
  32. ccNextPOT function is licensed under the same license that is used in Texture2D.m.
  33. */
  34. /** Returns the Next Power of Two value.
  35. Examples:
  36. - If "value" is 15, it will return 16.
  37. - If "value" is 16, it will return 16.
  38. - If "value" is 17, it will return 32.
  39. @param value The value to get next power of two.
  40. @return Returns the next power of two value.
  41. @since v0.99.5
  42. */
  43. int ccNextPOT(int value);
  44. class Sprite;
  45. class Image;
  46. namespace utils
  47. {
  48. /** Capture the entire screen.
  49. * To ensure the snapshot is applied after everything is updated and rendered in the current frame,
  50. * we need to wrap the operation with a custom command which is then inserted into the tail of the render queue.
  51. * @param afterCaptured specify the callback function which will be invoked after the snapshot is done.
  52. * @param filename specify a filename where the snapshot is stored. This parameter can be either an absolute path or a simple
  53. * base filename ("hello.png" etc.), don't use a relative path containing directory names.("mydir/hello.png" etc.).
  54. * @since v3.2
  55. */
  56. CC_DLL void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
  57. /** Capture a specific Node.
  58. * @param startNode specify the snapshot Node. It should be cocos2d::Scene
  59. * @param scale
  60. * @returns: return a Image, then can call saveToFile to save the image as "xxx.png or xxx.jpg".
  61. * @since v3.11
  62. * !!! remark: Caller is responsible for releasing it by calling delete.
  63. */
  64. CC_DLL Image* captureNode(Node* startNode, float scale = 1.0f);
  65. /** Find children by name, it will return all child that has the same name.
  66. * It supports c++ 11 regular expression. It is a helper function of `Node::enumerateChildren()`.
  67. * You can refer to `Node::enumerateChildren()` for detail information.
  68. *
  69. * @param node The node to find
  70. * @param name The name to search for, it supports c++ 11 expression
  71. * @return Array of Nodes that matches the name
  72. * @since v3.2
  73. */
  74. CC_DLL std::vector<Node*> findChildren(const Node &node, const std::string &name);
  75. /** Same to ::atof, but strip the string, remain 7 numbers after '.' before call atof.
  76. * Why we need this? Because in android c++_static, atof ( and std::atof ) is unsupported for numbers have long decimal part and contain
  77. * several numbers can approximate to 1 ( like 90.099998474121094 ), it will return inf. This function is used to fix this bug.
  78. * @param str The string be to converted to double.
  79. * @return Returns converted value of a string.
  80. */
  81. CC_DLL double atof(const char* str);
  82. /** Get current exact time, accurate to nanoseconds.
  83. * @return Returns the time in seconds since the Epoch.
  84. */
  85. CC_DLL double gettime();
  86. /**
  87. * Get current time in milliseconds, accurate to nanoseconds
  88. *
  89. * @return Returns the time in milliseconds since the Epoch.
  90. */
  91. CC_DLL long long getTimeInMilliseconds();
  92. /**
  93. * Calculate unionof bounding box of a node and its children.
  94. * @return Returns unionof bounding box of a node and its children.
  95. */
  96. CC_DLL Rect getCascadeBoundingBox(Node *node);
  97. /**
  98. * Create a sprite instance from base64 encoded image and adds the texture to the Texture Cache.
  99. * @return Returns an instance of sprite
  100. */
  101. CC_DLL Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key);
  102. /**
  103. * Create a sprite instance from base64 encoded image.
  104. * @return Returns an instance of sprite
  105. */
  106. CC_DLL Sprite* createSpriteFromBase64(const char* base64String);
  107. /**
  108. * Find a child by name recursively
  109. * @return Returns found node or nullptr
  110. */
  111. CC_DLL Node* findChild(Node* levelRoot, const std::string& name);
  112. /**
  113. * Find a child by tag recursively
  114. * @return Returns found node or nullptr
  115. */
  116. CC_DLL Node* findChild(Node* levelRoot, int tag);
  117. /**
  118. * Find a child by name recursively
  119. * @return Returns found node or nullptr with specified type 'T'
  120. */
  121. template<typename T> inline
  122. T findChild(Node* levelRoot, const std::string& name)
  123. {
  124. return dynamic_cast<T>(findChild(levelRoot, name));
  125. }
  126. /**
  127. * Find a child by tag recursively
  128. * @return Returns found node or nullptr with specified type 'T'
  129. */
  130. template<typename T> inline
  131. T findChild(Node* levelRoot, int tag)
  132. {
  133. return dynamic_cast<T>(findChild(levelRoot, tag));
  134. }
  135. /**
  136. * Gets the md5 hash for the given file.
  137. * @param filename The file to calculate md5 hash.
  138. * @return The md5 hash for the file
  139. */
  140. CC_DLL std::string getFileMD5Hash(const std::string &filename);
  141. }
  142. NS_CC_END
  143. #endif // __SUPPORT_CC_UTILS_H__