ccTypes.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __BASE_CCTYPES_H__
  25. #define __BASE_CCTYPES_H__
  26. #include <string>
  27. #include "math/CCGeometry.h"
  28. #include "math/CCMath.h"
  29. #include "base/CCRef.h"
  30. #include "platform/CCGL.h"
  31. /**
  32. * @addtogroup base
  33. * @{
  34. */
  35. NS_CC_BEGIN
  36. struct Color4B;
  37. struct Color4F;
  38. /**
  39. * RGB color composed of bytes 3 bytes.
  40. * @since v3.0
  41. */
  42. struct CC_DLL Color3B
  43. {
  44. Color3B();
  45. Color3B(GLubyte _r, GLubyte _g, GLubyte _b);
  46. explicit Color3B(const Color4B& color);
  47. explicit Color3B(const Color4F& color);
  48. bool operator==(const Color3B& right) const;
  49. bool operator==(const Color4B& right) const;
  50. bool operator==(const Color4F& right) const;
  51. bool operator!=(const Color3B& right) const;
  52. bool operator!=(const Color4B& right) const;
  53. bool operator!=(const Color4F& right) const;
  54. bool equals(const Color3B& other) const
  55. {
  56. return (*this == other);
  57. }
  58. GLubyte r;
  59. GLubyte g;
  60. GLubyte b;
  61. static const Color3B WHITE;
  62. static const Color3B YELLOW;
  63. static const Color3B BLUE;
  64. static const Color3B GREEN;
  65. static const Color3B RED;
  66. static const Color3B MAGENTA;
  67. static const Color3B BLACK;
  68. static const Color3B ORANGE;
  69. static const Color3B GRAY;
  70. };
  71. /**
  72. * RGBA color composed of 4 bytes.
  73. * @since v3.0
  74. */
  75. struct CC_DLL Color4B
  76. {
  77. Color4B();
  78. Color4B(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a);
  79. explicit Color4B(const Color3B& color, GLubyte _a = 255);
  80. explicit Color4B(const Color4F& color);
  81. inline void set(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a)
  82. {
  83. r = _r;
  84. g = _g;
  85. b = _b;
  86. a = _a;
  87. }
  88. bool operator==(const Color4B& right) const;
  89. bool operator==(const Color3B& right) const;
  90. bool operator==(const Color4F& right) const;
  91. bool operator!=(const Color4B& right) const;
  92. bool operator!=(const Color3B& right) const;
  93. bool operator!=(const Color4F& right) const;
  94. GLubyte r;
  95. GLubyte g;
  96. GLubyte b;
  97. GLubyte a;
  98. static const Color4B WHITE;
  99. static const Color4B YELLOW;
  100. static const Color4B BLUE;
  101. static const Color4B GREEN;
  102. static const Color4B RED;
  103. static const Color4B MAGENTA;
  104. static const Color4B BLACK;
  105. static const Color4B ORANGE;
  106. static const Color4B GRAY;
  107. };
  108. /**
  109. * RGBA color composed of 4 floats.
  110. * @since v3.0
  111. */
  112. struct CC_DLL Color4F
  113. {
  114. Color4F();
  115. Color4F(float _r, float _g, float _b, float _a);
  116. explicit Color4F(const Color3B& color, float _a = 1.0f);
  117. explicit Color4F(const Color4B& color);
  118. bool operator==(const Color4F& right) const;
  119. bool operator==(const Color3B& right) const;
  120. bool operator==(const Color4B& right) const;
  121. bool operator!=(const Color4F& right) const;
  122. bool operator!=(const Color3B& right) const;
  123. bool operator!=(const Color4B& right) const;
  124. bool equals(const Color4F &other) const
  125. {
  126. return (*this == other);
  127. }
  128. GLfloat r;
  129. GLfloat g;
  130. GLfloat b;
  131. GLfloat a;
  132. static const Color4F WHITE;
  133. static const Color4F YELLOW;
  134. static const Color4F BLUE;
  135. static const Color4F GREEN;
  136. static const Color4F RED;
  137. static const Color4F MAGENTA;
  138. static const Color4F BLACK;
  139. static const Color4F ORANGE;
  140. static const Color4F GRAY;
  141. };
  142. Color4F& operator+=(Color4F& lhs, const Color4F& rhs);
  143. Color4F operator+(Color4F lhs, const Color4F& rhs);
  144. Color4F& operator-=(Color4F& lhs, const Color4F& rhs);
  145. Color4F operator-(Color4F lhs, const Color4F& rhs);
  146. Color4F& operator*=(Color4F& lhs, const Color4F& rhs);
  147. Color4F operator*(Color4F lhs, const Color4F& rhs);
  148. Color4F& operator*=(Color4F& lhs, float rhs);
  149. Color4F operator*(Color4F lhs, float rhs);
  150. Color4F& operator/=(Color4F& lhs, const Color4F& rhs);
  151. Color4F operator/(Color4F lhs, const Color4F& rhs);
  152. Color4F& operator/=(Color4F& lhs, float rhs);
  153. Color4F operator/(Color4F lhs, float rhs);
  154. /** A vertex composed of 2 floats: x, y
  155. @since v3.0
  156. */
  157. // struct Vertex2F
  158. // {
  159. // Vertex2F(float _x, float _y) :x(_x), y(_y) {}
  160. // Vertex2F(): x(0.f), y(0.f) {}
  161. // GLfloat x;
  162. // GLfloat y;
  163. // };
  164. /** A vertex composed of 2 floats: x, y
  165. @since v3.0
  166. */
  167. // struct Vertex3F
  168. // {
  169. // Vertex3F(float _x, float _y, float _z)
  170. // : x(_x)
  171. // , y(_y)
  172. // , z(_z)
  173. // {}
  174. // Vertex3F(): x(0.f), y(0.f), z(0.f) {}
  175. // GLfloat x;
  176. // GLfloat y;
  177. // GLfloat z;
  178. // };
  179. /** @struct Tex2F
  180. * A TEXCOORD composed of 2 floats: u, y
  181. * @since v3.0
  182. */
  183. struct CC_DLL Tex2F {
  184. Tex2F(float _u, float _v): u(_u), v(_v) {}
  185. Tex2F(): u(0.f), v(0.f) {}
  186. GLfloat u;
  187. GLfloat v;
  188. };
  189. /** @struct PointSprite
  190. * Vec2 Sprite component.
  191. */
  192. struct CC_DLL PointSprite
  193. {
  194. Vec2 pos; // 8 bytes
  195. Color4B color; // 4 bytes
  196. GLfloat size; // 4 bytes
  197. };
  198. /** @struct Quad2
  199. * A 2D Quad. 4 * 2 floats.
  200. */
  201. struct CC_DLL Quad2
  202. {
  203. Vec2 tl;
  204. Vec2 tr;
  205. Vec2 bl;
  206. Vec2 br;
  207. };
  208. /** @struct Quad3
  209. * A 3D Quad. 4 * 3 floats.
  210. */
  211. struct CC_DLL Quad3 {
  212. Vec3 bl;
  213. Vec3 br;
  214. Vec3 tl;
  215. Vec3 tr;
  216. };
  217. /** @struct V2F_C4B_T2F
  218. * A Vec2 with a vertex point, a tex coord point and a color 4B.
  219. */
  220. struct V2F_C4B_T2F
  221. {
  222. /// vertices (2F)
  223. Vec2 vertices;
  224. /// colors (4B)
  225. Color4B colors;
  226. /// tex coords (2F)
  227. Tex2F texCoords;
  228. };
  229. /** @struct V2F_C4B_PF
  230. *
  231. */
  232. struct V2F_C4B_PF
  233. {
  234. /// vertices (2F)
  235. Vec2 vertices;
  236. /// colors (4B)
  237. Color4B colors;
  238. /// pointsize
  239. float pointSize;
  240. };
  241. /** @struct V2F_C4F_T2F
  242. * A Vec2 with a vertex point, a tex coord point and a color 4F.
  243. */
  244. struct CC_DLL V2F_C4F_T2F
  245. {
  246. /// vertices (2F)
  247. Vec2 vertices;
  248. /// colors (4F)
  249. Color4F colors;
  250. /// tex coords (2F)
  251. Tex2F texCoords;
  252. };
  253. /** @struct V3F_C4B_T2F
  254. * A Vec2 with a vertex point, a tex coord point and a color 4B.
  255. */
  256. struct CC_DLL V3F_C4B_T2F
  257. {
  258. /// vertices (3F)
  259. Vec3 vertices; // 12 bytes
  260. /// colors (4B)
  261. Color4B colors; // 4 bytes
  262. // tex coords (2F)
  263. Tex2F texCoords; // 8 bytes
  264. };
  265. /** @struct V3F_T2F
  266. * A Vec2 with a vertex point, a tex coord point.
  267. */
  268. struct CC_DLL V3F_T2F
  269. {
  270. /// vertices (2F)
  271. Vec3 vertices;
  272. /// tex coords (2F)
  273. Tex2F texCoords;
  274. };
  275. /** @struct V2F_C4B_T2F_Triangle
  276. * A Triangle of V2F_C4B_T2F.
  277. */
  278. struct CC_DLL V2F_C4B_T2F_Triangle
  279. {
  280. V2F_C4B_T2F a;
  281. V2F_C4B_T2F b;
  282. V2F_C4B_T2F c;
  283. };
  284. /** @struct V2F_C4B_T2F_Quad
  285. * A Quad of V2F_C4B_T2F.
  286. */
  287. struct CC_DLL V2F_C4B_T2F_Quad
  288. {
  289. /// bottom left
  290. V2F_C4B_T2F bl;
  291. /// bottom right
  292. V2F_C4B_T2F br;
  293. /// top left
  294. V2F_C4B_T2F tl;
  295. /// top right
  296. V2F_C4B_T2F tr;
  297. };
  298. /** @struct V3F_C4B_T2F_Quad
  299. * 4 Vertex3FTex2FColor4B.
  300. */
  301. struct CC_DLL V3F_C4B_T2F_Quad
  302. {
  303. /// top left
  304. V3F_C4B_T2F tl;
  305. /// bottom left
  306. V3F_C4B_T2F bl;
  307. /// top right
  308. V3F_C4B_T2F tr;
  309. /// bottom right
  310. V3F_C4B_T2F br;
  311. };
  312. /** @struct V2F_C4F_T2F_Quad
  313. * 4 Vertex2FTex2FColor4F Quad.
  314. */
  315. struct CC_DLL V2F_C4F_T2F_Quad
  316. {
  317. /// bottom left
  318. V2F_C4F_T2F bl;
  319. /// bottom right
  320. V2F_C4F_T2F br;
  321. /// top left
  322. V2F_C4F_T2F tl;
  323. /// top right
  324. V2F_C4F_T2F tr;
  325. };
  326. /** @struct V3F_T2F_Quad
  327. *
  328. */
  329. struct CC_DLL V3F_T2F_Quad
  330. {
  331. /// bottom left
  332. V3F_T2F bl;
  333. /// bottom right
  334. V3F_T2F br;
  335. /// top left
  336. V3F_T2F tl;
  337. /// top right
  338. V3F_T2F tr;
  339. };
  340. /** @struct BlendFunc
  341. * Blend Function used for textures.
  342. */
  343. struct CC_DLL BlendFunc
  344. {
  345. /** source blend function */
  346. GLenum src;
  347. /** destination blend function */
  348. GLenum dst;
  349. /** Blending disabled. Uses {GL_ONE, GL_ZERO} */
  350. static const BlendFunc DISABLE;
  351. /** Blending enabled for textures with Alpha premultiplied. Uses {GL_ONE, GL_ONE_MINUS_SRC_ALPHA} */
  352. static const BlendFunc ALPHA_PREMULTIPLIED;
  353. /** Blending enabled for textures with Alpha NON premultiplied. Uses {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA} */
  354. static const BlendFunc ALPHA_NON_PREMULTIPLIED;
  355. /** Enables Additive blending. Uses {GL_SRC_ALPHA, GL_ONE} */
  356. static const BlendFunc ADDITIVE;
  357. bool operator==(const BlendFunc &a) const
  358. {
  359. return src == a.src && dst == a.dst;
  360. }
  361. bool operator!=(const BlendFunc &a) const
  362. {
  363. return src != a.src || dst != a.dst;
  364. }
  365. bool operator<(const BlendFunc &a) const
  366. {
  367. return src < a.src || (src == a.src && dst < a.dst);
  368. }
  369. };
  370. /** @enum TextVAlignment
  371. * Vertical text alignment type.
  372. *
  373. * @note If any of these enums are edited and/or reordered, update Texture2D.m.
  374. */
  375. enum class CC_DLL TextVAlignment
  376. {
  377. TOP,
  378. CENTER,
  379. BOTTOM
  380. };
  381. /** @enum TextHAlignment
  382. * Horizontal text alignment type.
  383. *
  384. * @note If any of these enums are edited and/or reordered, update Texture2D.m.
  385. */
  386. enum class CC_DLL TextHAlignment
  387. {
  388. LEFT,
  389. CENTER,
  390. RIGHT
  391. };
  392. /**
  393. * @brief Possible GlyphCollection used by Label.
  394. *
  395. * Specify a collections of characters to be load when Label created.
  396. * Consider using DYNAMIC.
  397. */
  398. enum class GlyphCollection {
  399. DYNAMIC,
  400. NEHE,
  401. ASCII,
  402. CUSTOM
  403. };
  404. // Types for animation in particle systems
  405. /** @struct T2F_Quad
  406. * Texture coordinates for a quad.
  407. */
  408. struct CC_DLL T2F_Quad
  409. {
  410. /// bottom left
  411. Tex2F bl;
  412. /// bottom right
  413. Tex2F br;
  414. /// top left
  415. Tex2F tl;
  416. /// top right
  417. Tex2F tr;
  418. };
  419. /** @struct AnimationFrameData
  420. * Struct that holds the size in pixels, texture coordinates and delays for animated ParticleSystemQuad.
  421. */
  422. struct CC_DLL AnimationFrameData
  423. {
  424. T2F_Quad texCoords;
  425. float delay;
  426. Size size;
  427. };
  428. /**
  429. types used for defining fonts properties (i.e. font name, size, stroke or shadow)
  430. */
  431. /** @struct FontShadow
  432. * Shadow attributes.
  433. */
  434. struct CC_DLL FontShadow
  435. {
  436. public:
  437. // shadow is not enabled by default
  438. FontShadow()
  439. : _shadowEnabled(false)
  440. , _shadowBlur(0)
  441. , _shadowOpacity(0)
  442. {}
  443. /// true if shadow enabled
  444. bool _shadowEnabled;
  445. /// shadow x and y offset
  446. Size _shadowOffset;
  447. /// shadow blurriness
  448. float _shadowBlur;
  449. /// shadow opacity
  450. float _shadowOpacity;
  451. };
  452. /** @struct FontStroke
  453. * Stroke attributes.
  454. */
  455. struct CC_DLL FontStroke
  456. {
  457. public:
  458. // stroke is disabled by default
  459. FontStroke()
  460. : _strokeEnabled(false)
  461. , _strokeColor(Color3B::BLACK)
  462. , _strokeAlpha(255)
  463. , _strokeSize(0)
  464. {}
  465. /// true if stroke enabled
  466. bool _strokeEnabled;
  467. /// stroke color
  468. Color3B _strokeColor;
  469. /// stroke alpha
  470. GLubyte _strokeAlpha;
  471. /// stroke size
  472. float _strokeSize;
  473. };
  474. /** @struct FontDefinition
  475. * Font attributes.
  476. */
  477. struct CC_DLL FontDefinition
  478. {
  479. public:
  480. /**
  481. * @js NA
  482. * @lua NA
  483. */
  484. FontDefinition()
  485. : _fontSize(0)
  486. , _alignment(TextHAlignment::CENTER)
  487. , _vertAlignment(TextVAlignment::TOP)
  488. , _dimensions(Size::ZERO)
  489. , _fontFillColor(Color3B::WHITE)
  490. , _fontAlpha(255)
  491. , _enableWrap(true)
  492. , _overflow(0)
  493. {}
  494. /// font name
  495. std::string _fontName;
  496. /// font size
  497. int _fontSize;
  498. /// horizontal alignment
  499. TextHAlignment _alignment;
  500. /// vertical alignment
  501. TextVAlignment _vertAlignment;
  502. /// rendering box
  503. Size _dimensions;
  504. /// font color
  505. Color3B _fontFillColor;
  506. /// font alpha
  507. GLubyte _fontAlpha;
  508. /// font shadow
  509. FontShadow _shadow;
  510. /// font stroke
  511. FontStroke _stroke;
  512. /// enable text wrap
  513. bool _enableWrap;
  514. /** There are 4 overflows: none, clamp, shrink and resize_height.
  515. * The corresponding integer values are 0, 1, 2, 3 respectively
  516. * For more information, please refer to Label::Overflow enum class.
  517. */
  518. int _overflow;
  519. };
  520. /**
  521. * @brief Effects used by `Label`
  522. *
  523. */
  524. enum class LabelEffect {
  525. // FIXME: Covert them to bitwise. More than one effect should be supported
  526. NORMAL,
  527. OUTLINE,
  528. SHADOW,
  529. GLOW,
  530. ITALICS,
  531. BOLD,
  532. UNDERLINE,
  533. STRIKETHROUGH,
  534. ALL
  535. };
  536. /** @struct Acceleration
  537. * The device accelerometer reports values for each axis in units of g-force.
  538. */
  539. class CC_DLL Acceleration
  540. : public Ref
  541. {
  542. public:
  543. double x;
  544. double y;
  545. double z;
  546. double timestamp;
  547. Acceleration(): x(0), y(0), z(0), timestamp(0) {}
  548. };
  549. extern const std::string CC_DLL STD_STRING_EMPTY;
  550. extern const ssize_t CC_DLL CC_INVALID_INDEX;
  551. enum class SetIntervalReason : char
  552. {
  553. BY_GAME = 0,
  554. BY_ENGINE,
  555. BY_SYSTEM,
  556. BY_SCENE_CHANGE,
  557. BY_DIRECTOR_PAUSE
  558. };
  559. NS_CC_END
  560. // end group
  561. /// @}
  562. #endif //__BASE_CCTYPES_H__