Sunday, April 12, 2009

Back Online With GNU/Linux

It's been a few weeks since I've had a reliable PC to use at home. My PC running Windows XP had been on all night, until we had a brief power outage, and disk 2 of my RAID-0 array had a hardware failure. One of the bad blocks landed right on user32.dll, and I was unable to get the system back up and running. I figured that during the drive replacement, it was not only a good chance to finally switch over to a RAID-1 so that I wouldn't be down for so long next time a drive dies, but I also partitioned so that I could have a dedicated install of Ubuntu.

For the last year or two, I had been running Ubuntu inside VirtualBox in Windows. Although it was a decent setup, it lacked the ability to easily try some of the lower-level features like support for sound and other hardware. I had long been suggesting that people switch from Windows to Linux, but hadn't fully made the jump myself. I'm still working my way through my list of software and selecting free software alternatives, and I'll shortly post a list of some of the applications that I've found.

This was one of the best sights, and I'm glad that Wifi is finally getting good support on the Linux desktop.

Friday, February 13, 2009

JRuby Eventmachine

One of these days I need to work on ingressing JRuby, Rails, et al into my workplace. Our security policy is pretty tight, so getting things in the door is difficult. In fact, it took months just to get a JUnit add-on for testing XSLT. But that's neither here nor there. I'm here, in my few brief moments of borrowed time online, to toss a quick note out about EventMachine's Java-native IO reactor being included in the currently available gem package.

EventMachine is great in C-Ruby, letting you write highly scalable asynchronous servers. The usage is very Ruby-ish, with a tight API for specifying how to react to events like connection, receiving data, and disconnection. No massive hierarchy of Channels, Buffers, Protocol Chains, etc to figure out and import. For a while, there's been a development branch with a Java-based reactor, but it wasn't available directly from the gem. Now that's changed (probably while I was off reading a fifty page functional requirements specification at work, but I digress). Just fire up JRuby, gem install eventmachine, and have at it. The Echo server example ran just fine, other than having to kill my terminal when Control-C didn't shut it down. I think there's a standard way to trap it and get around that, though. I'm psyched that this has finally made it to JRuby.

Wednesday, August 27, 2008

Control Your Monkey: JRuby + JMonkeyEngine

Continuing our previous example, here's how we can use JRuby's Java Integration to create a Ruby class that extends a Java class. We'll use it to override the default DebugGameState first-person controls and add our own. In the process, we'll add an input handler to listen for the Escape key and use it to quit the program, removing the need for a Swing quit button.


game=com.jmex.game.StandardGame.new 'test game'
game.start

class InputGameState < com.jmex.game.state.DebugGameState
def initialize sphere, game
super false
@ih = com.jme.input.InputHandler.new
@keyboard = com.jme.input.KeyBindingManager.getKeyBindingManager
@keyboard.set "forward", com.jme.input.KeyInput::KEY_UP
@ih.add_action com.jme.input.action.KeyNodeForwardAction.new(sphere,1), 'forward', true
@keyboard.set "quit", com.jme.input.KeyInput::KEY_ESCAPE
@ih.add_action com.jme.input.action.KeyExitAction.new(game), "quit",false
end

def update t
super
@ih.update t
end
end

sphere = com.jme.scene.shape.Sphere.new('my sphere',10,10,5)
state = InputGameState.new sphere, game

sphere.set_random_colors
sphere.update_render_state
state.root_node.attach_child sphere
com.jmex.game.state.GameStateManager.instance.attach_child state
state.active=true


So what we're doing is pretty much the same as before. Create a StandardGame and start it, to start up the engine and OpenGL. Then we define our InputGameState class, which adds a custom input handler. Calling super false invokes the DebugGameState constructor, which takes a boolean that says whether or not it should handle input for us. We're using our own input handler, so we pass it false.

The KeyBindingManager is a singleton that maps virtual key codes to action name strings. An InputHandler then uses its update loop (invoked by the game state's update method every frame) to check which of its registered actions are currently being executed.

KeyNodeForwardAction is a utility action that moves the specified scene graph node (our sphere) forward (toward the camera in our setup) by the specified rate of units per second. The last boolean parameter specifies whether or not the action will repeat if the key is held down.

KeyExitAction is another utility action that shuts down the specified game. So pressing ESC will now quit the game, and as long as no other Java threads are running, it will exit the VM.

The two KeyAction classes are examples of a wide range of utility classes that jME provides. They all implement an InputActionInterface, so to create a custom action in JRuby, you can simply say:


a = com.jme.input.action.InputActionInterface.impl do |m,event|
puts event
end


That will create a Java proxy class implementing the interface. The m symbol will be filled in with the name of the implemented method, in this case performAction.