compiles and runs on Windows!!!

This commit is contained in:
Ricardo Quesada 2014-05-09 13:24:56 -07:00
parent 9ffaeec515
commit 739af170ad
3 changed files with 77 additions and 35 deletions

View File

@ -34,7 +34,10 @@ THE SOFTWARE.
#include "2d/uthash.h" #include "2d/uthash.h"
#include "deprecated/CCString.h" #include "deprecated/CCString.h"
#include "CCGL.h" #include "CCGL.h"
#ifndef WIN32
#include <alloca.h> #include <alloca.h>
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
#include "CCPrecompiledShaders.h" #include "CCPrecompiledShaders.h"

View File

@ -55,12 +55,14 @@ UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
UniformValue::~UniformValue() UniformValue::~UniformValue()
{ {
if (_useCallback)
delete _value.callback;
} }
void UniformValue::apply() void UniformValue::apply()
{ {
if(_useCallback) { if(_useCallback) {
_value.callback(_uniform); (*_value.callback)(_uniform);
} }
else else
{ {
@ -75,15 +77,15 @@ void UniformValue::apply()
break; break;
case GL_FLOAT_VEC2: 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; break;
case GL_FLOAT_VEC3: 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; break;
case GL_FLOAT_VEC4: 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; break;
case GL_FLOAT_MAT4: case GL_FLOAT_MAT4:
@ -99,7 +101,16 @@ void UniformValue::apply()
void UniformValue::setCallback(const std::function<void(Uniform*)> &callback) void UniformValue::setCallback(const std::function<void(Uniform*)> &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<void(Uniform*)>();
*_value.callback = callback;
_useCallback = true; _useCallback = true;
} }
@ -120,29 +131,29 @@ void UniformValue::setInt(int value)
void UniformValue::setVec2(const Vector2& value) void UniformValue::setVec2(const Vector2& value)
{ {
CCASSERT (_uniform->type == GL_FLOAT_VEC2, ""); CCASSERT (_uniform->type == GL_FLOAT_VEC2, "");
_value.v2Value = value; memcpy(_value.v2Value, &value, sizeof(_value.v2Value));
_useCallback = false; _useCallback = false;
} }
void UniformValue::setVec3(const Vector3& value) void UniformValue::setVec3(const Vector3& value)
{ {
CCASSERT (_uniform->type == GL_FLOAT_VEC3, ""); CCASSERT (_uniform->type == GL_FLOAT_VEC3, "");
_value.v3Value = value; memcpy(_value.v3Value, &value, sizeof(_value.v3Value));
_useCallback = false; _useCallback = false;
} }
void UniformValue::setVec4(const Vector4& value) void UniformValue::setVec4(const Vector4& value)
{ {
CCASSERT (_uniform->type == GL_FLOAT_VEC4, ""); CCASSERT (_uniform->type == GL_FLOAT_VEC4, "");
_value.v4Value = value; memcpy(_value.v4Value, &value, sizeof(_value.v4Value));
_useCallback = false; _useCallback = false;
} }
void UniformValue::setMat4(const Matrix& value) void UniformValue::setMat4(const Matrix& value)
{ {
CCASSERT(_uniform->type == GL_FLOAT_MAT4, ""); CCASSERT(_uniform->type == GL_FLOAT_MAT4, "");
_value.matrixValue = value; memcpy(_value.matrixValue, &value, sizeof(_value.matrixValue));
_useCallback = false; _useCallback = false;
} }
// //
@ -165,12 +176,14 @@ VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
VertexAttribValue::~VertexAttribValue() VertexAttribValue::~VertexAttribValue()
{ {
if (_useCallback)
delete _value.callback;
} }
void VertexAttribValue::apply() void VertexAttribValue::apply()
{ {
if(_useCallback) { if(_useCallback) {
_value.callback(_vertexAttrib); (*_value.callback)(_vertexAttrib);
} }
else else
{ {
@ -185,7 +198,8 @@ void VertexAttribValue::apply()
void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback) void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback)
{ {
_value.callback = callback; _value.callback = new std::function<void(VertexAttrib*)>();
*_value.callback = callback;
_useCallback = true; _useCallback = true;
} }
@ -325,8 +339,8 @@ void GLProgramState::setVertexAttribCallback(const std::string &name, const std:
} }
else 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) 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 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<void(Uniform*)> &callback) void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function<void(Uniform*)> &callback)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setCallback(callback); v->setCallback(callback);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformFloat(const std::string &uniformName, float value) void GLProgramState::setUniformFloat(const std::string &uniformName, float value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setFloat(value); v->setFloat(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformInt(const std::string &uniformName, int value) void GLProgramState::setUniformInt(const std::string &uniformName, int value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if(v)
v->setInt(value); v->setInt(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformVec2(const std::string &uniformName, const Vector2& value) void GLProgramState::setUniformVec2(const std::string &uniformName, const Vector2& value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setVec2(value); v->setVec2(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformVec3(const std::string &uniformName, const Vector3& value) void GLProgramState::setUniformVec3(const std::string &uniformName, const Vector3& value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setVec3(value); v->setVec3(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformVec4(const std::string &uniformName, const Vector4& value) void GLProgramState::setUniformVec4(const std::string &uniformName, const Vector4& value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setVec4(value); v->setVec4(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
void GLProgramState::setUniformMat4(const std::string &uniformName, const Matrix& value) void GLProgramState::setUniformMat4(const std::string &uniformName, const Matrix& value)
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value"); if (v)
v->setMat4(value); v->setMat4(value);
else
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
} }
NS_CC_END NS_CC_END

View File

@ -69,14 +69,23 @@ protected:
bool _useCallback; bool _useCallback;
union U{ union U{
float floatValue; #ifndef WIN32
float floatValue;
int intValue; int intValue;
Vector2 v2Value; Vector2 v2Value;
Vector3 v3Value; Vector3 v3Value;
Vector4 v4Value; Vector4 v4Value;
Matrix matrixValue; Matrix matrixValue;
std::function<void(Uniform*)> callback; std::function<void(Uniform*)> callback;
#else
float floatValue;
int intValue;
float v2Value[2];
float v3Value[3];
float v4Value[4];
float matrixValue[16];
std::function<void(Uniform*)> *callback;
#endif
U() { memset( this, 0, sizeof(*this) ); } U() { memset( this, 0, sizeof(*this) ); }
~U(){} ~U(){}
U& operator=( const U& other ) { U& operator=( const U& other ) {
@ -117,7 +126,7 @@ protected:
GLsizei stride; GLsizei stride;
GLvoid *pointer; GLvoid *pointer;
} pointer; } pointer;
std::function<void(VertexAttrib*)> callback; std::function<void(VertexAttrib*)> *callback;
U() { memset( this, 0, sizeof(*this) ); } U() { memset( this, 0, sizeof(*this) ); }
~U(){} ~U(){}