mystery/src/graphics/shader.cpp
// compiles a shader source code using the provided type
compiles a shader source code using the provided type
compiles a shader source code using the provided type
unsigned int Shader::CompileShader(unsigned int shader_type, const std::string& shader_src_code) {
			unsigned int shader_id = glCreateShader(shader_type);
			const char* src_char = shader_src_code.c_str();
			glShaderSource(shader_id, 1, &src_char, nullptr);
			glCompileShader(shader_id);
			int result;
			glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
			if (result == GL_FALSE) {
				int length;
				glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &length);
				char* message = (char*)alloca(length * sizeof(char));
				glGetShaderInfoLog(shader_id, length, &length, message);
				std::cout << "SHADER: Failed to compile: " << std::endl;
				std::cout << (shader_type == GL_VERTEX_SHADER ? "vertex shader" : "fragment shader") << std::endl;
				std::cout << message << std::endl;
				glDeleteShader(shader_id);
				return 0;
			}
			return shader_id;
		}
unsigned int Shader :: CompileShader ( unsigned int shader_type , const std :: string & shader_src_code ) { unsigned int shader_id = glCreateShader ( shader_type ) ; const char * src_char = shader_src_code . c_str ( ) ; glShaderSource ( shader_id , 1 , & src_char , nullptr ) ; glCompileShader ( shader_id ) ; int result ; glGetShaderiv ( shader_id , GL_COMPILE_STATUS , & result ) ; if ( result == GL_FALSE ) { int length ; glGetShaderiv ( shader_id , GL_INFO_LOG_LENGTH , & length ) ; char * message = ( char * ) alloca ( length * sizeof ( char ) ) ; glGetShaderInfoLog ( shader_id , length , & length , message ) ; std :: cout << " " << std :: endl ; std :: cout << ( shader_type == GL_VERTEX_SHADER ? " " : " " ) << std :: endl ; std :: cout << message << std :: endl ; glDeleteShader ( shader_id ) ; return 0 ; } return shader_id ; }
compiles a shader source code using the provided type
compiles a shader source code using the provided type
// error handling // if something went wrong // message length // here we are allocating a array of chars in the stack (yes, this is kinda hacky)