middleware-adapter.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  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 deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "math/Vec2.h"
  22. #include "base/ccTypes.h"
  23. #include <functional>
  24. #include "MiddlewareMacro.h"
  25. #include "renderer/renderer/Effect.h"
  26. MIDDLEWARE_BEGIN
  27. /**
  28. * Texture format with u v.
  29. */
  30. struct Tex2F
  31. {
  32. GLfloat u;
  33. GLfloat v;
  34. };
  35. /**
  36. * Vertex Format with x y u v color.
  37. */
  38. struct V2F_T2F_C4B
  39. {
  40. // vertices (2F)
  41. cocos2d::Vec2 vertex; // 8 bytes
  42. // tex coords (2F)
  43. Tex2F texCoord; // 8 bytes
  44. // colors (4B)
  45. cocos2d::Color4B color; // 4 bytes
  46. };
  47. /**
  48. * Vertex Format with x y u v color1 color2.
  49. */
  50. struct V2F_T2F_C4B_C4B
  51. {
  52. // vertices (2F)
  53. cocos2d::Vec2 vertex; // 8 bytes
  54. // tex coords (2F)
  55. Tex2F texCoord; // 8 bytes
  56. // colors (4B)
  57. cocos2d::Color4B color; // 4 bytes
  58. // colors (4B)
  59. cocos2d::Color4B color2; // 4 bytes
  60. };
  61. struct Triangles
  62. {
  63. /**Vertex data pointer.*/
  64. V2F_T2F_C4B* verts = nullptr;
  65. /**Index data pointer.*/
  66. unsigned short* indices = nullptr;
  67. /**The number of vertices.*/
  68. int vertCount = 0;
  69. /**The number of indices.*/
  70. int indexCount = 0;
  71. };
  72. struct TwoColorTriangles
  73. {
  74. /**Vertex data pointer.*/
  75. V2F_T2F_C4B_C4B* verts = nullptr;
  76. /**Index data pointer.*/
  77. unsigned short* indices = nullptr;
  78. /**The number of vertices.*/
  79. int vertCount = 0;
  80. /**The number of indices.*/
  81. int indexCount = 0;
  82. };
  83. ///////////////////////////////////////////////////////////////////////
  84. // adapt to editor texture,this is a texture delegate,not real texture
  85. ///////////////////////////////////////////////////////////////////////
  86. class Texture2D : public cocos2d::Ref
  87. {
  88. public:
  89. Texture2D();
  90. virtual ~Texture2D();
  91. /**
  92. Extension to set the Min / Mag filter
  93. */
  94. typedef struct _TexParams
  95. {
  96. GLuint minFilter;
  97. GLuint magFilter;
  98. GLuint wrapS;
  99. GLuint wrapT;
  100. }TexParams;
  101. /**
  102. * set texture param callback
  103. */
  104. typedef std::function<void(int,GLuint,GLuint,GLuint,GLuint)> texParamCallback;
  105. /** Sets the min filter, mag filter, wrap s and wrap t texture parameters.
  106. If the texture size is NPOT (non power of 2), then in can only use GL_CLAMP_TO_EDGE in GL_TEXTURE_WRAP_{S,T}.
  107. @warning Calling this method could allocate additional texture memory.
  108. @since v0.8
  109. * @code
  110. * When this function bound into js or lua,the input parameter will be changed
  111. * In js: var setBlendFunc(var arg1, var arg2, var arg3, var arg4)
  112. * In lua: local setBlendFunc(local arg1, local arg2, local arg3, local arg4)
  113. * @endcode
  114. */
  115. void setTexParameters(const TexParams& texParams);
  116. /** Gets the width of the texture in pixels. */
  117. int getPixelsWide() const;
  118. /** Gets the height of the texture in pixels. */
  119. int getPixelsHigh() const;
  120. /** Gets real texture index */
  121. int getRealTextureIndex() const;
  122. /** Sets the width of the texture in pixels. */
  123. void setPixelsWide(int wide);
  124. /** Sets the height of the texture in pixels. */
  125. void setPixelsHigh(int high);
  126. /** Sets real texture index.*/
  127. void setRealTextureIndex(int textureIndex);
  128. /** Sets texture param callback*/
  129. void setTexParamCallback(const texParamCallback& callback);
  130. /** Sets texture native ptr*/
  131. void setNativeTexture(cocos2d::renderer::Texture* texture);
  132. /** Gets texture native ptr*/
  133. cocos2d::renderer::Texture* getNativeTexture() const;
  134. private:
  135. /** width in pixels */
  136. int _pixelsWide = 0;
  137. /** height in pixels */
  138. int _pixelsHigh = 0;
  139. /** js texture */
  140. int _realTextureIndex = 0;
  141. texParamCallback _texParamCallback = nullptr;
  142. cocos2d::renderer::Texture* _texture = nullptr;
  143. };
  144. ///////////////////////////////////////////////////////////////////////
  145. // adapt to editor sprite frame
  146. ///////////////////////////////////////////////////////////////////////
  147. class SpriteFrame : public cocos2d::Ref
  148. {
  149. public:
  150. static SpriteFrame* createWithTexture(Texture2D* pobTexture, const cocos2d::Rect& rect);
  151. static SpriteFrame* createWithTexture(Texture2D* pobTexture, const cocos2d::Rect& rect, bool rotated, const cocos2d::Vec2& offset, const cocos2d::Size& originalSize);
  152. SpriteFrame();
  153. virtual ~SpriteFrame();
  154. /** Initializes a SpriteFrame with a texture, rect in points.
  155. It is assumed that the frame was not trimmed.
  156. */
  157. bool initWithTexture(Texture2D* pobTexture, const cocos2d::Rect& rect);
  158. /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
  159. The originalSize is the size in points of the frame before being trimmed.
  160. */
  161. bool initWithTexture(Texture2D* pobTexture, const cocos2d::Rect& rect, bool rotated, const cocos2d::Vec2& offset, const cocos2d::Size& originalSize);
  162. /** Get texture of the frame.
  163. *
  164. * @return The texture of the sprite frame.
  165. */
  166. Texture2D* getTexture();
  167. /** Set texture of the frame, the texture is retained.
  168. *
  169. * @param pobTexture The texture of the sprite frame.
  170. */
  171. void setTexture(Texture2D* pobTexture);
  172. protected:
  173. cocos2d::Vec2 _anchorPoint;
  174. cocos2d::Rect _rectInPixels;
  175. bool _rotated = false;
  176. cocos2d::Vec2 _offsetInPixels;
  177. cocos2d::Size _originalSizeInPixels;
  178. Texture2D* _texture = nullptr;
  179. };
  180. MIDDLEWARE_END