/*
 * OpenGL.java
 *
 * Created on August 2, 2005, 2:05 AM
 */

package org.slage.ui;
import java.io.*;
import java.net.*;
import org.slage.Tools;

/**
 * Wrapper for basic OpenGL loading stuff just to clean it up out of SLAGE.java
 * @author  Jaeden
 */
public abstract class OpenGL 
{
    
    /** Creates a new instance of OpenGL */
    private OpenGL() {}
    
       
    /** If 'true', we want to load Cg support.
     *  This would normally be on all the time, but JOGL's linking to it is a bit behind the times. 
     *  JOGL needs to update every time Cg does, or the .DLL won't properly load. As we're not planning 
     *  on using Cg in SLAGE at this time, we'll leave it disabled by default. */
    private static boolean bLoadCg = false;
    
  
    /** Static initialization  - load libraries */
      /** Ensure we load GL on startup. This is a replacement for the command-line switches
     *  -Djava.library.path=. and -cp .;jogl.jar. 
     *
     * 
     */
    public static void loadOpenGL()
    {         
        // workaround to allow our loader to do its job
        // see http://192.18.37.44/forums/index.php?topic=10262.0;topicseen
        net.java.games.jogl.impl.NativeLibLoader.disableLoading();
        
        // Manually load AWT to work around the problem with error boxes coming up claiming it's not found. 
        // Apparently AWT has to be loaded -before- jogl's .dll files or all Hell breaks loose.
        java.awt.Toolkit.getDefaultToolkit();
        System.loadLibrary("jawt");        
        
        //  load in the JOGL.jar file 
        try
        {
            URL urlJOGL = (new File(Tools.GetRelativePath(null) + Tools.GetFileSeparator() + "jogl.jar")).toURL();
            URL[] urls = { urlJOGL };            
            URLClassLoader classLoader = new URLClassLoader(urls);
        }
        catch (MalformedURLException exc) 
        { 
            // TODO: Log 
            System.err.println("MalformedURLException while loading jogl.jar"); 
        }                                
          
        // Now, load the .dll/.so files for the OpenGL C bindings 
        String strOS = Tools.GetOS();        
        String strJOGL = Tools.GetRelativePath("lib") + Tools.GetFileSeparator();                        
        
        if (strOS.startsWith("Windows"))
        {                        
            System.load(strJOGL + "jogl.dll");            
            
            if (bLoadCg)
                System.load(strJOGL + "jogl_cg.dll");                
        }        
        if (strOS.startsWith("Mac OS"))
        {
              System.load(strJOGL + "libjogl.jnlib");
              if (bLoadCg)
                System.load(strJOGL + "libjogl_cg.jnlib");        
        }                
        if (strOS.startsWith("Linux"))
        {
                System.load(strJOGL + "libjogl.so");
                if (bLoadCg)
                    System.load(strJOGL + "libjogl_cg.so");
        }
        if (strOS.startsWith("Solaris"))
        {
               String strArch = System.getProperty("os.arch");
               if (strArch.equalsIgnoreCase("sparc"))
               {
                   System.load(strJOGL + "libjogl_solsparc.so");
               }
               if (strArch.equalsIgnoreCase("x86"))
               {
                   System.load(strJOGL + "libjogl_solx86.so");
               }
        }
    }
    
}