-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCTextureAtlas.cpp
More file actions
executable file
·691 lines (535 loc) · 19.4 KB
/
CCTextureAtlas.cpp
File metadata and controls
executable file
·691 lines (535 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
// cocos2d
#include "CCTextureAtlas.h"
#include "CCTextureCache.h"
#include "ccMacros.h"
#include "shaders/CCGLProgram.h"
#include "shaders/ccGLStateCache.h"
#include "support/CCNotificationCenter.h"
#include "CCEventType.h"
#include "CCGL.h"
// support
#include "CCTexture2D.h"
#include "cocoa/CCString.h"
#include <stdlib.h>
//According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong
// implementation CCTextureAtlas
NS_CC_BEGIN
CCTextureAtlas::CCTextureAtlas()
:m_pIndices(NULL)
,m_bDirty(false)
,m_pTexture(NULL)
,m_pQuads(NULL)
{}
CCTextureAtlas::~CCTextureAtlas()
{
CCLOGINFO("cocos2d: CCTextureAtlas deallocing %p.", this);
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
glDeleteBuffers(2, m_pBuffersVBO);
#if CC_TEXTURE_ATLAS_USE_VAO
glDeleteVertexArrays(1, &m_uVAOname);
ccGLBindVAO(0);
#endif
CC_SAFE_RELEASE(m_pTexture);
#if CC_ENABLE_CACHE_TEXTURE_DATA
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVENT_COME_TO_FOREGROUND);
#endif
}
unsigned int CCTextureAtlas::getTotalQuads()
{
return m_uTotalQuads;
}
unsigned int CCTextureAtlas::getCapacity()
{
return m_uCapacity;
}
CCTexture2D* CCTextureAtlas::getTexture()
{
return m_pTexture;
}
void CCTextureAtlas::setTexture(CCTexture2D * var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pTexture);
m_pTexture = var;
}
ccV3F_C4B_T2F_Quad* CCTextureAtlas::getQuads()
{
//if someone accesses the quads directly, presume that changes will be made
m_bDirty = true;
return m_pQuads;
}
void CCTextureAtlas::setQuads(ccV3F_C4B_T2F_Quad *var)
{
m_pQuads = var;
}
// TextureAtlas - alloc & init
CCTextureAtlas * CCTextureAtlas::create(const char* file, unsigned int capacity)
{
CCTextureAtlas * pTextureAtlas = new CCTextureAtlas();
if(pTextureAtlas && pTextureAtlas->initWithFile(file, capacity))
{
pTextureAtlas->autorelease();
return pTextureAtlas;
}
CC_SAFE_DELETE(pTextureAtlas);
return NULL;
}
CCTextureAtlas * CCTextureAtlas::createWithTexture(CCTexture2D *texture, unsigned int capacity)
{
CCTextureAtlas * pTextureAtlas = new CCTextureAtlas();
if (pTextureAtlas && pTextureAtlas->initWithTexture(texture, capacity))
{
pTextureAtlas->autorelease();
return pTextureAtlas;
}
CC_SAFE_DELETE(pTextureAtlas);
return NULL;
}
bool CCTextureAtlas::initWithFile(const char * file, unsigned int capacity)
{
// retained in property
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(file);
if (texture)
{
return initWithTexture(texture, capacity);
}
else
{
CCLOG("cocos2d: Could not open file: %s", file);
return false;
}
}
bool CCTextureAtlas::initWithTexture(CCTexture2D *texture, unsigned int capacity)
{
// CCAssert(texture != NULL, "texture should not be null");
m_uCapacity = capacity;
m_uTotalQuads = 0;
// retained in property
this->m_pTexture = texture;
CC_SAFE_RETAIN(m_pTexture);
// Re-initialization is not allowed
CCAssert(m_pQuads == NULL && m_pIndices == NULL, "");
m_pQuads = (ccV3F_C4B_T2F_Quad*)malloc( m_uCapacity * sizeof(ccV3F_C4B_T2F_Quad) );
m_pIndices = (GLushort *)malloc( m_uCapacity * 6 * sizeof(GLushort) );
if( ! ( m_pQuads && m_pIndices) && m_uCapacity > 0)
{
//CCLOG("cocos2d: CCTextureAtlas: not enough memory");
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
// release texture, should set it to null, because the destruction will
// release it too. see cocos2d-x issue #484
CC_SAFE_RELEASE_NULL(m_pTexture);
return false;
}
memset( m_pQuads, 0, m_uCapacity * sizeof(ccV3F_C4B_T2F_Quad) );
memset( m_pIndices, 0, m_uCapacity * 6 * sizeof(GLushort) );
#if CC_ENABLE_CACHE_TEXTURE_DATA
// listen the event when app go to background
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(CCTextureAtlas::listenBackToForeground),
EVENT_COME_TO_FOREGROUND,
NULL);
#endif
this->setupIndices();
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
m_bDirty = true;
return true;
}
void CCTextureAtlas::listenBackToForeground(CCObject *obj)
{
#if CC_TEXTURE_ATLAS_USE_VAO
setupVBOandVAO();
#else
setupVBO();
#endif
// set m_bDirty to true to force it rebinding buffer
m_bDirty = true;
}
const char* CCTextureAtlas::description()
{
return CCString::createWithFormat("<CCTextureAtlas | totalQuads = %u>", m_uTotalQuads)->getCString();
}
void CCTextureAtlas::setupIndices()
{
if (m_uCapacity == 0)
return;
for( unsigned int i=0; i < m_uCapacity; i++)
{
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
m_pIndices[i*6+0] = i*4+0;
m_pIndices[i*6+1] = i*4+0;
m_pIndices[i*6+2] = i*4+2;
m_pIndices[i*6+3] = i*4+1;
m_pIndices[i*6+4] = i*4+3;
m_pIndices[i*6+5] = i*4+3;
#else
m_pIndices[i*6+0] = i*4+0;
m_pIndices[i*6+1] = i*4+1;
m_pIndices[i*6+2] = i*4+2;
// inverted index. issue #179
m_pIndices[i*6+3] = i*4+3;
m_pIndices[i*6+4] = i*4+2;
m_pIndices[i*6+5] = i*4+1;
#endif
}
}
//TextureAtlas - VAO / VBO specific
#if CC_TEXTURE_ATLAS_USE_VAO
void CCTextureAtlas::setupVBOandVAO()
{
glGenVertexArrays(1, &m_uVAOname);
ccGLBindVAO(m_uVAOname);
#define kQuadSize sizeof(m_pQuads[0].bl)
glGenBuffers(2, &m_pBuffersVBO[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uCapacity, m_pQuads, GL_DYNAMIC_DRAW);
// vertices
glEnableVertexAttribArray(kCCVertexAttrib_Position);
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, vertices));
// colors
glEnableVertexAttribArray(kCCVertexAttrib_Color);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, colors));
// tex coords
glEnableVertexAttribArray(kCCVertexAttrib_TexCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( ccV3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uCapacity * 6, m_pIndices, GL_STATIC_DRAW);
// Must unbind the VAO before changing the element buffer.
ccGLBindVAO(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
}
#else // CC_TEXTURE_ATLAS_USE_VAO
void CCTextureAtlas::setupVBO()
{
glGenBuffers(2, &m_pBuffersVBO[0]);
mapBuffers();
}
#endif // ! // CC_TEXTURE_ATLAS_USE_VAO
void CCTextureAtlas::mapBuffers()
{
// Avoid changing the element buffer for whatever VAO might be bound.
ccGLBindVAO(0);
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * m_uCapacity, m_pQuads, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_pIndices[0]) * m_uCapacity * 6, m_pIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
}
// TextureAtlas - Update, Insert, Move & Remove
void CCTextureAtlas::updateQuad(ccV3F_C4B_T2F_Quad *quad, unsigned int index)
{
CCAssert( index >= 0 && index < m_uCapacity, "updateQuadWithTexture: Invalid index");
m_uTotalQuads = MAX( index+1, m_uTotalQuads);
m_pQuads[index] = *quad;
m_bDirty = true;
}
void CCTextureAtlas::insertQuad(ccV3F_C4B_T2F_Quad *quad, unsigned int index)
{
CCAssert( index < m_uCapacity, "insertQuadWithTexture: Invalid index");
m_uTotalQuads++;
CCAssert( m_uTotalQuads <= m_uCapacity, "invalid totalQuads");
// issue #575. index can be > totalQuads
unsigned int remaining = (m_uTotalQuads-1) - index;
// last object doesn't need to be moved
if( remaining > 0)
{
// texture coordinates
memmove( &m_pQuads[index+1],&m_pQuads[index], sizeof(m_pQuads[0]) * remaining );
}
m_pQuads[index] = *quad;
m_bDirty = true;
}
void CCTextureAtlas::insertQuads(ccV3F_C4B_T2F_Quad* quads, unsigned int index, unsigned int amount)
{
CCAssert(index + amount <= m_uCapacity, "insertQuadWithTexture: Invalid index + amount");
m_uTotalQuads += amount;
CCAssert( m_uTotalQuads <= m_uCapacity, "invalid totalQuads");
// issue #575. index can be > totalQuads
int remaining = (m_uTotalQuads-1) - index - amount;
// last object doesn't need to be moved
if( remaining > 0)
{
// tex coordinates
memmove( &m_pQuads[index+amount],&m_pQuads[index], sizeof(m_pQuads[0]) * remaining );
}
unsigned int max = index + amount;
unsigned int j = 0;
for (unsigned int i = index; i < max ; i++)
{
m_pQuads[index] = quads[j];
index++;
j++;
}
m_bDirty = true;
}
void CCTextureAtlas::insertQuadFromIndex(unsigned int oldIndex, unsigned int newIndex)
{
CCAssert( newIndex >= 0 && newIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
CCAssert( oldIndex >= 0 && oldIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
if( oldIndex == newIndex )
{
return;
}
// because it is ambiguous in iphone, so we implement abs ourselves
// unsigned int howMany = abs( oldIndex - newIndex);
unsigned int howMany = (oldIndex - newIndex) > 0 ? (oldIndex - newIndex) : (newIndex - oldIndex);
unsigned int dst = oldIndex;
unsigned int src = oldIndex + 1;
if( oldIndex > newIndex)
{
dst = newIndex+1;
src = newIndex;
}
// texture coordinates
ccV3F_C4B_T2F_Quad quadsBackup = m_pQuads[oldIndex];
memmove( &m_pQuads[dst],&m_pQuads[src], sizeof(m_pQuads[0]) * howMany );
m_pQuads[newIndex] = quadsBackup;
m_bDirty = true;
}
void CCTextureAtlas::removeQuadAtIndex(unsigned int index)
{
CCAssert( index < m_uTotalQuads, "removeQuadAtIndex: Invalid index");
unsigned int remaining = (m_uTotalQuads-1) - index;
// last object doesn't need to be moved
if( remaining )
{
// texture coordinates
memmove( &m_pQuads[index],&m_pQuads[index+1], sizeof(m_pQuads[0]) * remaining );
}
m_uTotalQuads--;
m_bDirty = true;
}
void CCTextureAtlas::removeQuadsAtIndex(unsigned int index, unsigned int amount)
{
CCAssert(index + amount <= m_uTotalQuads, "removeQuadAtIndex: index + amount out of bounds");
unsigned int remaining = (m_uTotalQuads) - (index + amount);
m_uTotalQuads -= amount;
if ( remaining )
{
memmove( &m_pQuads[index], &m_pQuads[index+amount], sizeof(m_pQuads[0]) * remaining );
}
m_bDirty = true;
}
void CCTextureAtlas::removeAllQuads()
{
m_uTotalQuads = 0;
}
// TextureAtlas - Resize
bool CCTextureAtlas::resizeCapacity(unsigned int newCapacity)
{
if( newCapacity == m_uCapacity )
{
return true;
}
unsigned int uOldCapactiy = m_uCapacity;
// update capacity and totolQuads
m_uTotalQuads = MIN(m_uTotalQuads, newCapacity);
m_uCapacity = newCapacity;
ccV3F_C4B_T2F_Quad* tmpQuads = NULL;
GLushort* tmpIndices = NULL;
// when calling initWithTexture(fileName, 0) on bada device, calloc(0, 1) will fail and return NULL,
// so here must judge whether m_pQuads and m_pIndices is NULL.
if (m_pQuads == NULL)
{
tmpQuads = (ccV3F_C4B_T2F_Quad*)malloc( m_uCapacity * sizeof(m_pQuads[0]) );
if (tmpQuads != NULL)
{
memset(tmpQuads, 0, m_uCapacity * sizeof(m_pQuads[0]) );
}
}
else
{
tmpQuads = (ccV3F_C4B_T2F_Quad*)realloc( m_pQuads, sizeof(m_pQuads[0]) * m_uCapacity );
if (tmpQuads != NULL && m_uCapacity > uOldCapactiy)
{
memset(tmpQuads+uOldCapactiy, 0, (m_uCapacity - uOldCapactiy)*sizeof(m_pQuads[0]) );
}
}
if (m_pIndices == NULL)
{
tmpIndices = (GLushort*)malloc( m_uCapacity * 6 * sizeof(m_pIndices[0]) );
if (tmpIndices != NULL)
{
memset( tmpIndices, 0, m_uCapacity * 6 * sizeof(m_pIndices[0]) );
}
}
else
{
tmpIndices = (GLushort*)realloc( m_pIndices, sizeof(m_pIndices[0]) * m_uCapacity * 6 );
if (tmpIndices != NULL && m_uCapacity > uOldCapactiy)
{
memset( tmpIndices+uOldCapactiy, 0, (m_uCapacity-uOldCapactiy) * 6 * sizeof(m_pIndices[0]) );
}
}
if( ! ( tmpQuads && tmpIndices) ) {
CCLOG("cocos2d: CCTextureAtlas: not enough memory");
CC_SAFE_FREE(tmpQuads);
CC_SAFE_FREE(tmpIndices);
CC_SAFE_FREE(m_pQuads);
CC_SAFE_FREE(m_pIndices);
m_uCapacity = m_uTotalQuads = 0;
return false;
}
m_pQuads = tmpQuads;
m_pIndices = tmpIndices;
setupIndices();
mapBuffers();
m_bDirty = true;
return true;
}
void CCTextureAtlas::increaseTotalQuadsWith(unsigned int amount)
{
m_uTotalQuads += amount;
}
void CCTextureAtlas::moveQuadsFromIndex(unsigned int oldIndex, unsigned int amount, unsigned int newIndex)
{
CCAssert(newIndex + amount <= m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
CCAssert(oldIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index");
if( oldIndex == newIndex )
{
return;
}
//create buffer
size_t quadSize = sizeof(ccV3F_C4B_T2F_Quad);
ccV3F_C4B_T2F_Quad* tempQuads = (ccV3F_C4B_T2F_Quad*)malloc( quadSize * amount);
memcpy( tempQuads, &m_pQuads[oldIndex], quadSize * amount );
if (newIndex < oldIndex)
{
// move quads from newIndex to newIndex + amount to make room for buffer
memmove( &m_pQuads[newIndex], &m_pQuads[newIndex+amount], (oldIndex-newIndex)*quadSize);
}
else
{
// move quads above back
memmove( &m_pQuads[oldIndex], &m_pQuads[oldIndex+amount], (newIndex-oldIndex)*quadSize);
}
memcpy( &m_pQuads[newIndex], tempQuads, amount*quadSize);
free(tempQuads);
m_bDirty = true;
}
void CCTextureAtlas::moveQuadsFromIndex(unsigned int index, unsigned int newIndex)
{
CCAssert(newIndex + (m_uTotalQuads - index) <= m_uCapacity, "moveQuadsFromIndex move is out of bounds");
memmove(m_pQuads + newIndex,m_pQuads + index, (m_uTotalQuads - index) * sizeof(m_pQuads[0]));
}
void CCTextureAtlas::fillWithEmptyQuadsFromIndex(unsigned int index, unsigned int amount)
{
ccV3F_C4B_T2F_Quad quad;
memset(&quad, 0, sizeof(quad));
unsigned int to = index + amount;
for (unsigned int i = index ; i < to ; i++)
{
m_pQuads[i] = quad;
}
}
// TextureAtlas - Drawing
void CCTextureAtlas::drawQuads()
{
this->drawNumberOfQuads(m_uTotalQuads, 0);
}
void CCTextureAtlas::drawNumberOfQuads(unsigned int n)
{
this->drawNumberOfQuads(n, 0);
}
void CCTextureAtlas::drawNumberOfQuads(unsigned int n, unsigned int start)
{
if (0 == n)
{
return;
}
ccGLBindTexture2D(m_pTexture->getName());
#if CC_TEXTURE_ATLAS_USE_VAO
//
// Using VBO and VAO
//
// XXX: update is done in draw... perhaps it should be done in a timer
if (m_bDirty)
{
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
// option 1: subdata
//glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0])*start, sizeof(m_pQuads[0]) * n , &m_pQuads[start] );
// option 2: data
// glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW);
// option 3: orphaning + glMapBuffer
glBufferData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0]) * getTotalQuads(), NULL, GL_DYNAMIC_DRAW);
void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(buf, m_pQuads, sizeof(m_pQuads[0])* getTotalQuads());
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_bDirty = false;
}
ccGLBindVAO(m_uVAOname);
#if CC_REBIND_INDICES_BUFFER
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
#endif
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
glDrawElements(GL_TRIANGLE_STRIP, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])) );
#else
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])) );
#endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
#if CC_REBIND_INDICES_BUFFER
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif
// glBindVertexArray(0);
#else // ! CC_TEXTURE_ATLAS_USE_VAO
//
// Using VBO without VAO
//
#define kQuadSize sizeof(m_pQuads[0].bl)
glBindBuffer(GL_ARRAY_BUFFER, m_pBuffersVBO[0]);
// XXX: update is done in draw... perhaps it should be done in a timer
if (m_bDirty)
{
glBufferSubData(GL_ARRAY_BUFFER, sizeof(m_pQuads[0])*0, sizeof(m_pQuads[0]) * getTotalQuads() , &m_pQuads[0] );
m_bDirty = false;
}
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
// vertices
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(ccV3F_C4B_T2F, vertices));
// colors
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(ccV3F_C4B_T2F, colors));
// tex coords
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(ccV3F_C4B_T2F, texCoords));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO[1]);
#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
glDrawElements(GL_TRIANGLE_STRIP, (GLsizei)n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])));
#else
glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(m_pIndices[0])));
#endif // CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif // CC_TEXTURE_ATLAS_USE_VAO
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
NS_CC_END