/*******************************************************************************
 * Copyright 2012 bmanuel
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.bitfire.utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;

public final class ShaderLoader {
	public static String BasePath = "";
	public static boolean Pedantic = true;

	public static ShaderProgram fromFile (String vertexFileName, String fragmentFileName) {
		return ShaderLoader.fromFile(vertexFileName, fragmentFileName, "");
	}

	public static ShaderProgram fromFile (String vertexFileName, String fragmentFileName, String defines) {
		String log = "\"" + vertexFileName + "/" + fragmentFileName + "\"";
		if (defines.length() > 0) {
			log += " w/ (" + defines.replace("\n", ", ") + ")";
		}
		log += "...";
		Gdx.app.debug("ShaderLoader", "Compiling " + log);

		String vpSrc = Gdx.files.internal(BasePath + vertexFileName + ".vertex").readString();
		String fpSrc = Gdx.files.internal(BasePath + fragmentFileName + ".fragment").readString();

		ShaderProgram program = ShaderLoader.fromString(vpSrc, fpSrc, vertexFileName, fragmentFileName, defines);
		return program;
	}

	public static ShaderProgram fromString (String vertex, String fragment, String vertexName, String fragmentName) {
		return ShaderLoader.fromString(vertex, fragment, vertexName, fragmentName, "");
	}

	public static ShaderProgram fromString (String vertex, String fragment, String vertexName, String fragmentName, String defines) {
		ShaderProgram.pedantic = ShaderLoader.Pedantic;
		ShaderProgram shader = new ShaderProgram(defines + "\n" + vertex, defines + "\n" + fragment);

		if (!shader.isCompiled()) {
			Gdx.app.error("ShaderLoader", shader.getLog());
			System.exit(-1);
		}

		return shader;
	}

	private ShaderLoader () {
	}
}

@@ -996,34 +996,34 @@
 Program fromFile
- 
 (
+ 
 String vertexFil
@@ -1048,24 +1048,25 @@
 mentFileName
+ 
 ) %7B%0A%09%09return
@@ -1088,16 +1088,17 @@
 romFile(
+ 
 vertexFi
@@ -1117,32 +1117,33 @@
 mentFileName, %22%22
+ 
 );%0A%09%7D%0A%0A%09public s
@@ -1170,18 +1170,18 @@
 fromFile
- 
 (
+ 
 String v
@@ -1230,24 +1230,25 @@
 ring defines
+ 
 ) %7B%0A%09%09String
@@ -1313,18 +1313,18 @@
 %22%22;%0A%09%09if
- 
 (
+ 
 defines.
@@ -1335,16 +1335,17 @@
 th() %3E 0
+ 
 ) %7B%0A%09%09%09l
@@ -1376,16 +1376,17 @@
 replace(
+ 
 %22%5Cn%22, %22,
@@ -1387,16 +1387,17 @@
 n%22, %22, %22
+ 
 ) + %22)%22;
@@ -1431,14 +1431,13 @@
 app.
-debu
+lo
 g(
+ 
 %22Sha
@@ -1466,16 +1466,17 @@
  %22 + log
+ 
 );%0A%0A%09%09St
@@ -1499,32 +1499,33 @@
 .files.internal(
+ 
 BasePath + verte
@@ -1545,16 +1545,17 @@
 .vertex%22
+ 
 ).readSt
@@ -1598,16 +1598,17 @@
 nternal(
+ 
 BasePath
@@ -1640,16 +1640,17 @@
 ragment%22
+ 
 ).readSt
@@ -1708,16 +1708,17 @@
 mString(
+ 
 vpSrc, f
@@ -1764,16 +1764,17 @@
  defines
+ 
 );%0A%09%09ret
@@ -1821,34 +1821,34 @@
 ogram fromString
- 
 (
+ 
 String vertex, S
@@ -1901,16 +1901,17 @@
 mentName
+ 
 ) %7B%0A%09%09re
@@ -1939,16 +1939,17 @@
 mString(
+ 
 vertex, 
@@ -1986,16 +1986,17 @@
 Name, %22%22
+ 
 );%0A%09%7D%0A%0A%09
@@ -2033,18 +2033,18 @@
 omString
- 
 (
+ 
 String v
@@ -2121,16 +2121,17 @@
  defines
+ 
 ) %7B%0A%09%09Sh
@@ -2219,16 +2219,17 @@
 Program(
+ 
 defines 
@@ -2274,16 +2274,17 @@
 ment
+ 
 );%0A%0A%09%09if
  (!s
@@ -2279,18 +2279,18 @@
 );%0A%0A%09%09if
- 
 (
+ 
 !shader.
@@ -2301,16 +2301,17 @@
 mpiled()
+ 
 ) %7B%0A%09%09%09G
@@ -2323,16 +2323,17 @@
 p.error(
+ 
 %22ShaderL
@@ -2355,16 +2355,17 @@
 getLog()
+ 
 );%0A%09%09%09Sy
@@ -2378,10 +2378,12 @@
 xit(
+ 
 -1
+ 
 );%0A%09
@@ -2428,17 +2428,16 @@
 erLoader
- 
 () %7B%0A%09%7D%0A

Revert ShaderLoader to default logging mechanism