package org.helioviewer.viewmodel.view.opengl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.media.opengl.GL2;
import javax.media.opengl.awt.GLCanvas;

import org.helioviewer.base.logging.Log;
import org.helioviewer.viewmodel.view.jp2view.J2KRenderGlobalOptions;

/**
 * Class to check and manage some OpenGL properties.
 *
 * @author Markus Langenberg
 * @author Andre Dau
 *
 */
public class GLInfo {

    /*
     * TBD
     * 
     * - pixel scale at startup
     * 
     * if (!GLInfo.glIsUsable()) { Message.err("Could not initialize OpenGL",
     * "OpenGL could not be initialized properly during startup. JHelioviewer
     * will start in Software Mode. For detailed information please read the log
     * output. ", false); }
     */

    public static int maxTextureSize;
    public static int[] pixelScale = new int[] { 1, 1 };

    /**
     * Updates the OpenGL settings by reading the OpenGL properties.
     *
     */
    public static void update(GLCanvas canvas) {
        final GL2 gl = canvas.getGL().getGL2();

        String version = gl.glGetString(GL2.GL_VERSION);
        Log.debug(">> GLInfo > Version string: " + version);
        Matcher versionMatcher = Pattern.compile("\\d+(\\.(\\d+))*").matcher(version);
        if (!versionMatcher.find()) {
            Log.error(">> GLInfo > Could not strip version from version string. Display complete version in status bar");
        } else {
            version = versionMatcher.group();
        }

        String extensionStr = gl.glGetString(GL2.GL_EXTENSIONS);
        Log.debug(">> GLInfo.update(GL) > Extensions: " + extensionStr);

        boolean glUsable = true;
        if (!gl.isExtensionAvailable("GL_VERSION_1_3")) {
            Log.error(">> GLInfo.update(GL) > OpenGL 1.3 not supported. JHelioviewer will run in software mode.");
            glUsable = false;
        }

        if (glUsable == true) {
            int[] out = new int[1];

            out[0] = 0;
            gl.glGetIntegerv(GL2.GL_MAX_TEXTURE_SIZE, out, 0);
            maxTextureSize = out[0];
            Log.debug(">> GLInfo > max texture size: " + out[0]);

            pixelScale = canvas.getCurrentSurfaceScale(pixelScale);

            J2KRenderGlobalOptions.setDoubleBufferingOption(true);
        }
    }

}

@@ -176,16 +176,51 @@
 LCanvas;
+%0Aimport javax.media.opengl.glu.GLU;
 %0A%0Aimport
@@ -485,25 +485,24 @@
 * TBD%0A     *
- 
 %0A     * - pi
@@ -528,17 +528,16 @@
 p%0A     *
- 
 %0A     * 
@@ -2327,10 +2327,1510 @@
 %0A    %7D%0A%0A
+    public boolean checkGLErrors(String message, GL2 gl) %7B%0A        if (gl == null) %7B%0A            Log.warn(%22OpenGL not yet Initialised!%22);%0A            return true;%0A        %7D%0A        /*%0A         * To allow for distributed implementations, there may be several error%0A         * flags. If any single error flag has recorded an error, the value of%0A         * that flag is returned and that flag is reset to GL_NO_ERROR when%0A         * glGetError is called. If more than one flag has recorded an error,%0A         * glGetError returns and clears an arbitrary error flag value. Thus,%0A         * glGetError should always be called in a loop, until it returns%0A         * GL_NO_ERROR, if all error flags are to be reset.%0A         */%0A        int glErrorCode = gl.glGetError();%0A%0A        if (glErrorCode != GL2.GL_NO_ERROR) %7B%0A            GLU glu = new GLU();%0A            Log.error(%22GL Error (%22 + glErrorCode + %22): %22 + glu.gluErrorString(glErrorCode) + %22 - @%22 + message);%0A%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    public boolean checkGLErrors(GL2 gl) %7B%0A        if (gl == null) %7B%0A            Log.warn(%22OpenGL not yet Initialised!%22);%0A            return true;%0A        %7D%0A        int glErrorCode = gl.glGetError();%0A%0A        if (glErrorCode != GL2.GL_NO_ERROR) %7B%0A            GLU glu = new GLU();%0A            Log.error(%22GL Error (%22 + glErrorCode + %22): %22 + glu.gluErrorString(glErrorCode));%0A%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A
 %7D%0A

read function