-
Notifications
You must be signed in to change notification settings - Fork 0
Java and Ruby Libraries
Ruby-Processing can load a whole mess o’ libraries. From pure-Java libraries, to hybrid Java-C native libs, to pure-Ruby ones, you can make them all get along. Use the class method load_library "my_lib". This will look in the directory library/my_lib, and load in the Java or Ruby library that it finds by that name. So, all together now, an example that loads both OpenGL and the Boids algorithmic flocking library would look like this:
require 'ruby-processing' class Sketch < Processing::App load_libraries :opengl, :boids # We need the OpenGL classes to be included here. include_package "processing.opengl" # Code goes here . . . end
Which can be used to make some pretty glowing flocking business. The glow is a freely configurable ability of OPENGL, using the method gl_blend_func. Read the complete code.
There’s also a way to check if a library has been successfully loaded, so you can conditionally enable bits of your app that need certain libraries. For example: trying to use OpenGL acceleration if it’s available, and falling back on P3D when it’s not. library_loaded? :library_name will let you know.
Simply loading in the code for a library via load_library may not be enough to actually import the library’s classes into your namespace. For some libraries, such as Minim, you’ll want to import the package as well (as you would in Java):
load_library 'minim' import 'ddf.minim' import 'ddf.minim.analysis'Some libraries use Java’s reflective capacities to try and invoke methods on your sketch. If the methods that they’re looking for happen to be defined in Ruby alone, Java won’t be able to find them. Better support for this sort of thing should be forthcoming in a future JRuby release, but for now you’ll either need to patch the library to stop using reflection on the sketch, or insert a thin Java interface that lets the library know where your methods are. For example, the Carnivore library uses reflection to try and find a
packetEvent() method on the sketch. A simple fix is to create, compile and jar-up a java interface, and then include it into your sketch…
# The interface (PacketListener.java)
public interface PacketListener {
public void packetEvent(org.rsg.carnivore.CarnivorePacket packet);
}
# Compile it, jar it up...
javac -cp path/to/carnivore.jar PacketListener.java
jar cvf packet_listener.jar PacketListener.java
# And then include it inside your sketch...
require 'packet_listener'
include Java::PacketListener
def packetEvent(packet)
# codes goes here...
end
