Load natives from external path's, with the Reflection API

Started by broumbroum, December 21, 2010, 20:30:15

Previous topic - Next topic

broumbroum

[got from my own research at s3jswing project]
Also, LWJGL has an own library.path (which you can find by browsing the source code), use it like that : 
            System.setProperty("org.lwjgl.librarypath", System.getProperty("org.lwjgl.librarypath") + File.pathSeparator + "your library path");
There's also a specific JInput library path : net.java.games.input.librarypath that you can update the same way.

A trick to load native correctly is to use reflection. Because as soon as the JVM discovers the LWJGL classes, it will look for the natives to be loaded (lwjgl.dll/so/jnilib,etc.), you have one chance to make this "happen later". Here it is :
/** have a class that will use all LWJGL / SLick code, e.g. the LWJGLInterface.initComponent(JFrame) will use a Container as argument, the JFrame content pane*/
/* but here is the trick : before to use it, update the library path as seen above */
System.setProperty("org.lwjgl.librarypath", System.getProperty("org.lwjgl.librarypath") + File.pathSeparator + "your library path i recommended the user.home");
/* eventually load some additional libraries */
for(String yourLib : libs) {
    /** you can make self extraction to the library.path (remember user.home )*¨/
File ....
FileOutputStream ....
/** immediately load it */
System.load(pathToyourLib); 
}
/* load your interface with Reflection API*/
// Swing EDT must be invoked (it will act better for rendering)
SwingUtilities.invokeLater(new Runnable() {public void run() {
/** load a generic Frame or Applet (optional) */
JFrame f = new JFrame();
f.setPreferredSize(640,480);
/** your app is started here */
LWJGLinterface lwjgl = Class.forName("MyAppThatImplementsLWGLInterface").newInstance(); // it is the no-arg contructor, getConstructor(yourArgClasses) can do it for constructors with args
lwjgl.initComponents(f.getContentPane());
/** (optional) */
f.pack();
f.setVisible(true);
lwjgl.start(); // will use Display or AWTGLCanvas to start the rendering loop
}});


:)
See that your LWJGInterface is public and cannot import any LWJGL or Slick classes :
public interface LWJGLInterface {
/** implement by e.g. starting with a Display.create() */
public void initComponents(Container genericContainer);
/** run the rendering loop e.g. while(true) Display.update()*/
public void start();
}