From 089a47d937247a6043f596512f52baea7071b9e7 Mon Sep 17 00:00:00 2001 From: James Gregory Date: Thu, 4 Apr 2013 22:20:59 -0700 Subject: [PATCH] Add method to CCNode class to allow uploading data to GPU. Call it from CCSprite. --- cocos2dx/base_nodes/CCNode.cpp | 22 ++++++++++++++++++++++ cocos2dx/sprite_nodes/CCSprite.cpp | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index 35ec2846b7..3751f267fb 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -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 ) { diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index ee47318d8f..3984e4ff1a 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -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);