Skip to content

Commit

Permalink
Code refactor: use std::string instead of plain char buffer in compil…
Browse files Browse the repository at this point in the history
…eCombiner.
  • Loading branch information
gonetz committed Mar 12, 2016
1 parent b7369e9 commit 9d8ef24
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/GLES2/GLSLCombiner_gles2.cpp
Expand Up @@ -145,7 +145,7 @@ void DestroyShaderCombiner() {

ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCombine & _combine) : m_combine(_combine)
{
char strCombiner[2048];
std::string strCombiner;
m_nInputs = compileCombiner(_color, _alpha, strCombiner);

if (usesTexture()) {
Expand Down
2 changes: 1 addition & 1 deletion src/OGL3X/GLSLCombiner_ogl3x.cpp
Expand Up @@ -282,7 +282,7 @@ ShaderCombiner::ShaderCombiner() : m_bNeedUpdate(true)

ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCombine & _combine) : m_combine(_combine), m_bNeedUpdate(true)
{
char strCombiner[2048];
std::string strCombiner;
m_nInputs = compileCombiner(_color, _alpha, strCombiner);

const bool bUseLod = usesLOD();
Expand Down
36 changes: 18 additions & 18 deletions src/ShaderUtils.cpp
Expand Up @@ -185,15 +185,15 @@ void _correctSecondStageParams(CombinerStage & _stage) {
}

static
int _compileCombiner(const CombinerStage & _stage, const char** _Input, char * _strCombiner) {
int _compileCombiner(const CombinerStage & _stage, const char** _Input, std::string & _strShader) {
char buf[128];
bool bBracketOpen = false;
int nRes = 0;
for (int i = 0; i < _stage.numOps; ++i) {
switch (_stage.op[i].op) {
case LOAD:
sprintf(buf, "(%s ", _Input[_stage.op[i].param1]);
strcat(_strCombiner, buf);
_strShader += buf;
bBracketOpen = true;
nRes |= 1 << _stage.op[i].param1;
break;
Expand All @@ -204,7 +204,7 @@ int _compileCombiner(const CombinerStage & _stage, const char** _Input, char * _
}
else
sprintf(buf, "- %s", _Input[_stage.op[i].param1]);
strcat(_strCombiner, buf);
_strShader += buf;
nRes |= 1 << _stage.op[i].param1;
break;
case ADD:
Expand All @@ -214,7 +214,7 @@ int _compileCombiner(const CombinerStage & _stage, const char** _Input, char * _
}
else
sprintf(buf, "+ %s", _Input[_stage.op[i].param1]);
strcat(_strCombiner, buf);
_strShader += buf;
nRes |= 1 << _stage.op[i].param1;
break;
case MUL:
Expand All @@ -224,12 +224,12 @@ int _compileCombiner(const CombinerStage & _stage, const char** _Input, char * _
}
else
sprintf(buf, "*%s", _Input[_stage.op[i].param1]);
strcat(_strCombiner, buf);
_strShader += buf;
nRes |= 1 << _stage.op[i].param1;
break;
case INTER:
sprintf(buf, "mix(%s, %s, %s)", _Input[_stage.op[0].param2], _Input[_stage.op[0].param1], _Input[_stage.op[0].param3]);
strcat(_strCombiner, buf);
_strShader += buf;
nRes |= 1 << _stage.op[i].param1;
nRes |= 1 << _stage.op[i].param2;
nRes |= 1 << _stage.op[i].param3;
Expand All @@ -240,21 +240,21 @@ int _compileCombiner(const CombinerStage & _stage, const char** _Input, char * _
}
}
if (bBracketOpen)
strcat(_strCombiner, ")");
strcat(_strCombiner, "; \n");
_strShader.append(")");
_strShader.append("; \n");
return nRes;
}

int compileCombiner(Combiner & _color, Combiner & _alpha, char * _strShader)
int compileCombiner(Combiner & _color, Combiner & _alpha, std::string & _strShader)
{
if (gDP.otherMode.cycleType == G_CYC_1CYCLE) {
_correctFirstStageParams(_alpha.stage[0]);
_correctFirstStageParams(_color.stage[0]);
}
strcpy(_strShader, " alpha1 = ");
_strShader.append(" alpha1 = ");
int nInputs = _compileCombiner(_alpha.stage[0], AlphaInput, _strShader);

strcat(_strShader,
_strShader.append(
" if (uEnableAlphaTest != 0) { \n"
" lowp float alphaTestValue = (uAlphaCompareMode == 3) ? snoise() : uAlphaTestValue; \n"
" lowp float alphaValue = alpha1; \n"
Expand All @@ -269,24 +269,24 @@ int compileCombiner(Combiner & _color, Combiner & _alpha, char * _strShader)
" } \n"
);

strcat(_strShader, " color1 = ");
_strShader.append(" color1 = ");
nInputs |= _compileCombiner(_color.stage[0], ColorInput, _strShader);
strcat(_strShader, fragment_shader_blender);
_strShader.append(fragment_shader_blender);

strcat(_strShader, " combined_color = vec4(color1, alpha1); \n");
_strShader.append(" combined_color = vec4(color1, alpha1); \n");
if (_alpha.numStages == 2) {
strcat(_strShader, " alpha2 = ");
_strShader.append(" alpha2 = ");
_correctSecondStageParams(_alpha.stage[1]);
nInputs |= _compileCombiner(_alpha.stage[1], AlphaInput, _strShader);
}
else
strcat(_strShader, " alpha2 = alpha1; \n");
_strShader.append(" alpha2 = alpha1; \n");
if (_color.numStages == 2) {
strcat(_strShader, " color2 = ");
_strShader.append(" color2 = ");
_correctSecondStageParams(_color.stage[1]);
nInputs |= _compileCombiner(_color.stage[1], ColorInput, _strShader);
}
else
strcat(_strShader, " color2 = color1; \n");
_strShader.append(" color2 = color1; \n");
return nInputs;
}
2 changes: 1 addition & 1 deletion src/ShaderUtils.h
Expand Up @@ -8,6 +8,6 @@ GLuint createShaderProgram(const char * _strVertex, const char * _strFragment);
bool checkShaderCompileStatus(GLuint obj);
bool checkProgramLinkStatus(GLuint obj);
void logErrorShader(GLenum _shaderType, const std::string & _strShader);
int compileCombiner(Combiner & _color, Combiner & _alpha, char * _strShader);
int compileCombiner(Combiner & _color, Combiner & _alpha, std::string & _strShader);

#endif // SHADER_UTILS_H

0 comments on commit 9d8ef24

Please sign in to comment.