1
1
#import " CCTexture.h"
2
2
#import " CCNode_Private.h"
3
3
#import " CCSprite_Private.h"
4
+ #import " CCRenderer_Private.h"
5
+ #import " CCShader_Private.h"
4
6
5
7
6
8
@implementation CCNode (NoARC)
@@ -25,7 +27,7 @@ -(GLKMatrix4)transform:(const GLKMatrix4 *)parentTransform
25
27
return CCNodeTransform (self, *parentTransform);
26
28
}
27
29
28
- -(void ) visit : (__unsafe_unretained CCRenderer *)renderer parentTransform : (const GLKMatrix4 *)parentTransform
30
+ -(void ) visit : (CCRenderer *)renderer parentTransform : (const GLKMatrix4 *)parentTransform
29
31
{
30
32
// quick return if not visible. children won't be drawn.
31
33
if (!_visible) return ;
@@ -114,3 +116,124 @@ -(void)enqueueTriangles:(CCRenderer *)renderer transform:(const GLKMatrix4 *)tra
114
116
}
115
117
116
118
@end
119
+
120
+
121
+ @implementation CCRenderer (NoARC)
122
+
123
+ -(CCRenderBuffer)enqueueTriangles : (NSUInteger )triangleCount andVertexes : (NSUInteger )vertexCount withState : (CCRenderState *)renderState globalSortOrder : (NSInteger )globalSortOrder ;
124
+ {
125
+ // Need to record the first vertex or element index before pushing more vertexes.
126
+ size_t firstVertex = _vertexBuffer.count ;
127
+ size_t firstElement = _elementBuffer.count ;
128
+
129
+ size_t elementCount = 3 *triangleCount;
130
+ CCVertex *vertexes = CCGraphicsBufferPushElements (&_vertexBuffer, vertexCount, self);
131
+ GLushort *elements = CCGraphicsBufferPushElements (&_elementBuffer, elementCount, self);
132
+
133
+ CCRenderCommandDraw *previous = _lastDrawCommand;
134
+ if (previous && previous->_renderState == renderState && previous->_globalSortOrder == globalSortOrder){
135
+ // Batch with the previous command.
136
+ [previous batchElements: (GLsizei)elementCount];
137
+ } else {
138
+ // Start a new command.
139
+ CCRenderCommandDraw *command = [[CCRenderCommandDraw alloc ] initWithMode: GL_TRIANGLES renderState: renderState first: (GLint)firstElement elements: (GLsizei)elementCount globalSortOrder: globalSortOrder];
140
+ [_queue addObject: command];
141
+ [command release ];
142
+
143
+ _lastDrawCommand = command;
144
+ }
145
+
146
+ return (CCRenderBuffer){vertexes, elements, firstVertex};
147
+ }
148
+
149
+ -(CCRenderBuffer)enqueueLines : (NSUInteger )lineCount andVertexes : (NSUInteger )vertexCount withState : (CCRenderState *)renderState globalSortOrder : (NSInteger )globalSortOrder ;
150
+ {
151
+ // Need to record the first vertex or element index before pushing more vertexes.
152
+ size_t firstVertex = _vertexBuffer.count ;
153
+ size_t firstElement = _elementBuffer.count ;
154
+
155
+ size_t elementCount = 2 *lineCount;
156
+ CCVertex *vertexes = CCGraphicsBufferPushElements (&_vertexBuffer, vertexCount, self);
157
+ GLushort *elements = CCGraphicsBufferPushElements (&_elementBuffer, elementCount, self);
158
+
159
+ CCRenderCommandDraw *command = [[CCRenderCommandDraw alloc ] initWithMode: GL_LINES renderState: renderState first: (GLint)firstElement elements: (GLsizei)elementCount globalSortOrder: globalSortOrder];
160
+ [_queue addObject: command];
161
+ [command release ];
162
+
163
+ // Line drawing commands are currently intended for debugging and cannot be batched.
164
+ _lastDrawCommand = nil ;
165
+
166
+ return (CCRenderBuffer){vertexes, elements, firstVertex};
167
+ }
168
+
169
+ @end
170
+
171
+
172
+ @implementation CCRenderer (NoARCPrivate)
173
+
174
+ -(void )setRenderState : (CCRenderState *)renderState
175
+ {
176
+ [self bindVAO: YES ];
177
+ if (renderState == _renderState) return ;
178
+
179
+ glPushGroupMarkerEXT (0 , " CCRenderer: Render State" );
180
+
181
+ // Set the blending state.
182
+ NSDictionary *blendOptions = renderState->_blendMode ->_options ;
183
+ if (blendOptions != _blendOptions){
184
+ glInsertEventMarkerEXT (0 , " Blending mode" );
185
+
186
+ if (blendOptions == CCBLEND_DISABLED_OPTIONS){
187
+ if (_blendOptions != CCBLEND_DISABLED_OPTIONS) glDisable (GL_BLEND);
188
+ } else {
189
+ if (_blendOptions == nil || _blendOptions == CCBLEND_DISABLED_OPTIONS) glEnable (GL_BLEND);
190
+
191
+ glBlendFuncSeparate (
192
+ [blendOptions[CCBlendFuncSrcColor] unsignedIntValue ],
193
+ [blendOptions[CCBlendFuncDstColor] unsignedIntValue ],
194
+ [blendOptions[CCBlendFuncSrcAlpha] unsignedIntValue ],
195
+ [blendOptions[CCBlendFuncDstAlpha] unsignedIntValue ]
196
+ );
197
+
198
+ glBlendEquationSeparate (
199
+ [blendOptions[CCBlendEquationColor] unsignedIntValue ],
200
+ [blendOptions[CCBlendEquationAlpha] unsignedIntValue ]
201
+ );
202
+ }
203
+
204
+ _blendOptions = blendOptions;
205
+ }
206
+
207
+ // Bind the shader.
208
+ CCShader *shader = renderState->_shader ;
209
+ if (shader != _shader){
210
+ glInsertEventMarkerEXT (0 , " Shader" );
211
+
212
+ glUseProgram (shader->_program );
213
+
214
+ _shader = shader;
215
+ _shaderUniforms = nil ;
216
+ }
217
+
218
+ // Set the shader's uniform state.
219
+ NSDictionary *shaderUniforms = renderState->_shaderUniforms ;
220
+ NSDictionary *globalShaderUniforms = _globalShaderUniforms;
221
+ if (shaderUniforms != _shaderUniforms){
222
+ glInsertEventMarkerEXT (0 , " Uniforms" );
223
+
224
+ NSDictionary *setters = shader->_uniformSetters ;
225
+ for (NSString *uniformName in setters){
226
+ CCUniformSetter setter = setters[uniformName];
227
+ setter (self, shaderUniforms, globalShaderUniforms);
228
+ }
229
+ _shaderUniforms = shaderUniforms;
230
+ }
231
+
232
+ CC_CHECK_GL_ERROR_DEBUG ();
233
+ glPopGroupMarkerEXT ();
234
+
235
+ _renderState = renderState;
236
+ return ;
237
+ }
238
+
239
+ @end
0 commit comments