diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 06582226d..ec5c3390b 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -453,7 +453,7 @@ Canvas *Graphics::newCanvas(int width, int height, Canvas::TextureType texture_t return NULL; // never reached } -ShaderEffect *Graphics::newShaderEffect(const std::vector shadersources) +ShaderEffect *Graphics::newShaderEffect(const ShaderEffect::ShaderSources &shadersources) { ShaderEffect *effect = NULL; try diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index ad4c9ff01..931b62ac6 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -270,7 +270,7 @@ public: Canvas *newCanvas(int width, int height, Canvas::TextureType texture_type = Canvas::TYPE_NORMAL); - ShaderEffect *newShaderEffect(const std::vector shadersources); + ShaderEffect *newShaderEffect(const ShaderEffect::ShaderSources &shadersources); /** * Sets the foreground color. diff --git a/src/modules/graphics/opengl/ShaderEffect.cpp b/src/modules/graphics/opengl/ShaderEffect.cpp index 6cc442e59..79752b0d0 100644 --- a/src/modules/graphics/opengl/ShaderEffect.cpp +++ b/src/modules/graphics/opengl/ShaderEffect.cpp @@ -60,11 +60,11 @@ ShaderEffect *ShaderEffect::current = NULL; GLint ShaderEffect::_maxtextureunits = 0; std::vector ShaderEffect::_texturecounters; -ShaderEffect::ShaderEffect(const std::vector &shadersources) +ShaderEffect::ShaderEffect(const ShaderSources &shadersources) : _shadersources(shadersources) , _program(0) { - if (shadersources.size() == 0) + if (shadersources.empty()) throw love::Exception("Cannot create shader effect: no source code!"); GLint maxtextureunits; @@ -87,12 +87,12 @@ ShaderEffect::~ShaderEffect() unloadVolatile(); } -GLuint ShaderEffect::createShader(const ShaderSource &source) +GLuint ShaderEffect::createShader(const ShaderType &type, const std::string &code) { GLenum shadertype; const char *shadertypename = NULL; - switch (source.type) + switch (type) { case TYPE_VERTEX: shadertype = GL_VERTEX_SHADER; @@ -134,8 +134,8 @@ GLuint ShaderEffect::createShader(const ShaderSource &source) throw love::Exception("Cannot create %s shader object.", shadertypename); } - const char *src = source.code.c_str(); - size_t srclen = source.code.length(); + const char *src = code.c_str(); + size_t srclen = code.length(); glShaderSource(shaderid, 1, (const GLchar **)&src, (GLint *)&srclen); glCompileShader(shaderid); @@ -197,9 +197,12 @@ bool ShaderEffect::loadVolatile() std::vector shaderids; - std::vector::const_iterator source; + ShaderSources::const_iterator source; for (source = _shadersources.begin(); source != _shadersources.end(); ++source) - shaderids.push_back(createShader(*source)); + { + GLuint shaderid = createShader(source->first, source->second); + shaderids.push_back(shaderid); + } if (shaderids.size() == 0) throw love::Exception("Cannot create shader effect: no valid source code!"); diff --git a/src/modules/graphics/opengl/ShaderEffect.h b/src/modules/graphics/opengl/ShaderEffect.h index 62ea47e1b..3038a8d4c 100644 --- a/src/modules/graphics/opengl/ShaderEffect.h +++ b/src/modules/graphics/opengl/ShaderEffect.h @@ -52,20 +52,16 @@ public: TYPE_MAX_ENUM }; - // thin wrapper for GLSL source code. - struct ShaderSource - { - std::string code; - ShaderType type; - }; + // type for a list of shader source codes in the form of sources[shadertype] = code + typedef std::map ShaderSources; /** * Creates a new ShaderEffect using a list of source codes. * Must contain at least one vertex or fragment shader source. * - * @param shadersources Vector of shader source codes. + * @param shadersources map of shader source codes. **/ - ShaderEffect(const std::vector &shadersources); + ShaderEffect(const ShaderSources &shadersources); virtual ~ShaderEffect(); @@ -146,7 +142,7 @@ private: GLint getUniformLocation(const std::string &name); void checkSetUniformError(); - GLuint createShader(const ShaderSource &source); + GLuint createShader(const ShaderType &type, const std::string &code); void createProgram(const std::vector &shaderids); GLint getTextureUnit(const std::string &name); @@ -154,7 +150,7 @@ private: void sendTexture(const std::string &name, GLuint texture); // list of all shader code attached to this ShaderEffect - std::vector _shadersources; + ShaderSources _shadersources; GLuint _program; // volatile diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index ce8369090..3d469aa66 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -452,27 +452,23 @@ int w_newShaderEffect(lua_State *L) // call effectCodeToGLSL, returned values will be at the top of the stack lua_pcall(L, 2, 2, 0); - std::vector shaderlist; + ShaderEffect::ShaderSources shadersources; // vertex shader code if (lua_isstring(L, -2)) { - ShaderEffect::ShaderSource vertshader; - vertshader.type = ShaderEffect::TYPE_VERTEX; - vertshader.code = luaL_checkstring(L, -2); - shaderlist.push_back(vertshader); + std::string vertcode(luaL_checkstring(L, -2)); + shadersources[ShaderEffect::TYPE_VERTEX] = vertcode; } // fragment shader code if (lua_isstring(L, -1)) { - ShaderEffect::ShaderSource fragshader; - fragshader.type = ShaderEffect::TYPE_FRAGMENT; - fragshader.code = luaL_checkstring(L, -1); - shaderlist.push_back(fragshader); + std::string fragcode(luaL_checkstring(L, -1)); + shadersources[ShaderEffect::TYPE_FRAGMENT] = fragcode; } - ShaderEffect *effect = instance->newShaderEffect(shaderlist); + ShaderEffect *effect = instance->newShaderEffect(shadersources); luax_newtype(L, "ShaderEffect", GRAPHICS_SHADEREFFECT_T, (void *)effect); } catch(const love::Exception &e)