Add method to CCNode class to allow uploading data to GPU. Call it from CCSprite.

This commit is contained in:
James Gregory 2013-04-04 22:20:59 -07:00
parent aa2739a109
commit 089a47d937
2 changed files with 28 additions and 0 deletions

View File

@ -88,6 +88,10 @@ CCNode::CCNode(void)
, m_bReorderChildDirty(false)
, m_nScriptHandler(0)
, m_nUpdateScriptHandler(0)
#ifdef EMSCRIPTEN
, m_bufferObject(0)
, m_bufferSize(0)
#endif // EMSCRIPTEN
{
// set default scheduler and actionManager
CCDirector *director = CCDirector::sharedDirector();
@ -1192,6 +1196,24 @@ void CCNode::setAdditionalTransform(const CCAffineTransform& additionalTransform
m_bAdditionalTransformDirty = true;
}
#ifdef EMSCRIPTEN
void CCNode::setGLBufferData(void *buf, GLuint bufSize)
{
// WebGL doesn't support client-side arrays, so generate a buffer and load the data first.
if(m_bufferSize < bufSize)
{
if(m_bufferObject)
{
glDeleteBuffers(1, &m_bufferObject);
}
glGenBuffers(1, &m_bufferObject);
m_bufferSize = bufSize;
}
glBindBuffer(GL_ARRAY_BUFFER, m_bufferObject);
glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW);
}
#endif // EMSCRIPTEN
CCAffineTransform CCNode::parentToNodeTransform(void)
{
if ( m_bInverseDirty ) {

View File

@ -83,6 +83,7 @@ CCSprite* CCSprite::createWithTexture(CCTexture2D *pTexture, const CCRect& rect)
CCSprite* CCSprite::create(const char *pszFileName)
{
printf("CCSprite::create(%s)\n", pszFileName);
CCSprite *pobSprite = new CCSprite();
if (pobSprite && pobSprite->initWithFile(pszFileName))
{
@ -569,7 +570,12 @@ void CCSprite::draw(void)
ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );
#define kQuadSize sizeof(m_sQuad.bl)
#ifdef EMSCRIPTEN
long offset = 0;
setGLBufferData(&m_sQuad, 4 * kQuadSize);
#else
long offset = (long)&m_sQuad;
#endif // EMSCRIPTEN
// vertex
int diff = offsetof( ccV3F_C4B_T2F, vertices);