From 739af170ad5b0b6972d055637a9959af9b742092 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Fri, 9 May 2014 13:24:56 -0700 Subject: [PATCH] compiles and runs on Windows!!! --- cocos/renderer/CCGLProgram.cpp | 3 + cocos/renderer/CCGLProgramState.cpp | 94 +++++++++++++++++++---------- cocos/renderer/CCGLProgramState.h | 15 ++++- 3 files changed, 77 insertions(+), 35 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index cc4df344e1..43a2f09e43 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -34,7 +34,10 @@ THE SOFTWARE. #include "2d/uthash.h" #include "deprecated/CCString.h" #include "CCGL.h" + +#ifndef WIN32 #include +#endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) #include "CCPrecompiledShaders.h" diff --git a/cocos/renderer/CCGLProgramState.cpp b/cocos/renderer/CCGLProgramState.cpp index 5e2b349f08..8487312a12 100644 --- a/cocos/renderer/CCGLProgramState.cpp +++ b/cocos/renderer/CCGLProgramState.cpp @@ -55,12 +55,14 @@ UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram) UniformValue::~UniformValue() { + if (_useCallback) + delete _value.callback; } void UniformValue::apply() { if(_useCallback) { - _value.callback(_uniform); + (*_value.callback)(_uniform); } else { @@ -75,15 +77,15 @@ void UniformValue::apply() break; case GL_FLOAT_VEC2: - _glprogram->setUniformLocationWith2f(_uniform->location, _value.v2Value.x, _value.v2Value.y); + _glprogram->setUniformLocationWith2f(_uniform->location, _value.v2Value[0], _value.v2Value[1]); break; case GL_FLOAT_VEC3: - _glprogram->setUniformLocationWith3f(_uniform->location, _value.v3Value.x, _value.v3Value.y, _value.v3Value.z); + _glprogram->setUniformLocationWith3f(_uniform->location, _value.v3Value[0], _value.v3Value[1], _value.v3Value[2]); break; case GL_FLOAT_VEC4: - _glprogram->setUniformLocationWith4f(_uniform->location, _value.v4Value.x, _value.v4Value.y, _value.v4Value.z, _value.v4Value.w); + _glprogram->setUniformLocationWith4f(_uniform->location, _value.v4Value[0], _value.v4Value[1], _value.v4Value[2], _value.v4Value[3]); break; case GL_FLOAT_MAT4: @@ -99,7 +101,16 @@ void UniformValue::apply() void UniformValue::setCallback(const std::function &callback) { - _value.callback = callback; + // delete previously set callback + // XXX TODO: memory will leak if the user does: + // value->setCallback(); + // value->setFloat(); + if (_useCallback) + delete _value.callback; + + _value.callback = new std::function(); + *_value.callback = callback; + _useCallback = true; } @@ -120,29 +131,29 @@ void UniformValue::setInt(int value) void UniformValue::setVec2(const Vector2& value) { CCASSERT (_uniform->type == GL_FLOAT_VEC2, ""); - _value.v2Value = value; + memcpy(_value.v2Value, &value, sizeof(_value.v2Value)); _useCallback = false; } void UniformValue::setVec3(const Vector3& value) { CCASSERT (_uniform->type == GL_FLOAT_VEC3, ""); - _value.v3Value = value; - _useCallback = false; + memcpy(_value.v3Value, &value, sizeof(_value.v3Value)); + _useCallback = false; } void UniformValue::setVec4(const Vector4& value) { CCASSERT (_uniform->type == GL_FLOAT_VEC4, ""); - _value.v4Value = value; - _useCallback = false; + memcpy(_value.v4Value, &value, sizeof(_value.v4Value)); + _useCallback = false; } void UniformValue::setMat4(const Matrix& value) { CCASSERT(_uniform->type == GL_FLOAT_MAT4, ""); - _value.matrixValue = value; - _useCallback = false; + memcpy(_value.matrixValue, &value, sizeof(_value.matrixValue)); + _useCallback = false; } // @@ -165,12 +176,14 @@ VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib) VertexAttribValue::~VertexAttribValue() { + if (_useCallback) + delete _value.callback; } void VertexAttribValue::apply() { if(_useCallback) { - _value.callback(_vertexAttrib); + (*_value.callback)(_vertexAttrib); } else { @@ -185,7 +198,8 @@ void VertexAttribValue::apply() void VertexAttribValue::setCallback(const std::function &callback) { - _value.callback = callback; + _value.callback = new std::function(); + *_value.callback = callback; _useCallback = true; } @@ -325,8 +339,8 @@ void GLProgramState::setVertexAttribCallback(const std::string &name, const std: } else { - CCASSERT(false, "attribute not found"); - } + CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str()); + } } void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer) @@ -338,7 +352,7 @@ void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, } else { - CCASSERT(false, "attribute not found"); + CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str()); } } @@ -347,51 +361,67 @@ void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function &callback) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setCallback(callback); + if (v) + v->setCallback(callback); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); } void GLProgramState::setUniformFloat(const std::string &uniformName, float value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setFloat(value); + if (v) + v->setFloat(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); + } void GLProgramState::setUniformInt(const std::string &uniformName, int value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setInt(value); + if(v) + v->setInt(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); + } void GLProgramState::setUniformVec2(const std::string &uniformName, const Vector2& value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setVec2(value); + if (v) + v->setVec2(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); + } void GLProgramState::setUniformVec3(const std::string &uniformName, const Vector3& value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setVec3(value); + if (v) + v->setVec3(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); } void GLProgramState::setUniformVec4(const std::string &uniformName, const Vector4& value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setVec4(value); + if (v) + v->setVec4(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); } void GLProgramState::setUniformMat4(const std::string &uniformName, const Matrix& value) { auto v = getUniformValue(uniformName); - CCASSERT(v, "unknown uniform value"); - v->setMat4(value); + if (v) + v->setMat4(value); + else + CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); } - NS_CC_END diff --git a/cocos/renderer/CCGLProgramState.h b/cocos/renderer/CCGLProgramState.h index 97f4dc5a1c..f68745911e 100644 --- a/cocos/renderer/CCGLProgramState.h +++ b/cocos/renderer/CCGLProgramState.h @@ -69,14 +69,23 @@ protected: bool _useCallback; union U{ - float floatValue; +#ifndef WIN32 + float floatValue; int intValue; Vector2 v2Value; Vector3 v3Value; Vector4 v4Value; Matrix matrixValue; std::function callback; - +#else + float floatValue; + int intValue; + float v2Value[2]; + float v3Value[3]; + float v4Value[4]; + float matrixValue[16]; + std::function *callback; +#endif U() { memset( this, 0, sizeof(*this) ); } ~U(){} U& operator=( const U& other ) { @@ -117,7 +126,7 @@ protected: GLsizei stride; GLvoid *pointer; } pointer; - std::function callback; + std::function *callback; U() { memset( this, 0, sizeof(*this) ); } ~U(){}