JIrr & SWT - looks & works great!

Discussion about Irrlicht's Java wrapper
Post Reply
Serg Nechaeff
Posts: 162
Joined: Wed Nov 26, 2003 5:24 pm
Location: Europe

JIrr & SWT - looks & works great!

Post by Serg Nechaeff »

If you want to use Jirr in your SWT application - it is really easy and requires no additional (3rd party) libraries. All you need is Jirr and SWT library.

Check out the following screenshot: there are 2 irrlicht devices in window, rendering real-time absolutely independently, a browser widget, top menus, and a couple of ExpandBar items (all standard SWT GUI stuff here):

Image

The code to start Irrlicht in the SWT window is the following:

Code: Select all

		SIrrlichtCreationParameters creationParams = new SIrrlichtCreationParameters();
		creationParams.setFullscreen(false);
		creationParams.setWindowSize(new dimension2di(1024, 768));
		creationParams.setStencilbuffer(false);
creationParams.setDriverType(E_DRIVER_TYPE.EDT_DIRECT3D9);
	[b]creationParams.setWindowId(shell.handle);[/b]

		IrrlichtDevice device = Jirr.createDeviceEx(creationParams);

Where shell is the SWT's composite object. In the screenshot above I used LiveSashForms (custom 3rd party widget) with Composite objects.
The code is not really clean but you will get the idea.

You may want to get LiveSashForm from here to resize widgets includeing irrlicht's windows at runtime:

http://www.novocode.com/swt/

Code: Select all

package com.leadercode;

import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

import net.sf.jirr.*;

public class Demo {

	static {
		System.loadLibrary("irrlicht_wrap");
	}

	private static Display display = new Display();;

	private static Shell shell;

	private static boolean isActive = true;

	public static void main(String argv[]) {

		shell = new Shell(display, SWT.MIN | SWT.MAX | SWT.CLOSE | SWT.RESIZE);
		shell.setSize(800, 600);

		shell.setText("SWT Application");

		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				isActive = false;
			}
		});

		shell.setLayout(new FillLayout());

		LiveSashForm sash1 = new LiveSashForm(shell, SWT.VERTICAL);

		Composite irr1 = new Composite(sash1, SWT.NONE);
		Browser bt = new Browser(sash1, SWT.None);
		bt.setUrl("http://irrlicht.sourceforge.net/");

		LiveSashForm sash = new LiveSashForm(shell, SWT.VERTICAL);
		sash.sashWidth = 8;
		Composite irr2 = new Composite(sash, SWT.NONE);

		createExpandBar(sash);

		createMenus(shell);
		shell.open();

		IrrlichtDevice device1 = createDevice(irr1);
		IrrlichtDevice device2 = createSecondSceneDevice(irr2);

		while (!shell.isDisposed()) {

			if (!display.readAndDispatch())
				display.sleep();

			while (device1.run() && isActive) {
				drawScene(device1);
				drawScene(device2);

			}

		}
		display.dispose();

		device1.closeDevice();
		device2.closeDevice();
	}

	private static void drawScene(IrrlichtDevice device) {
		IVideoDriver driver = device.getVideoDriver();
		ISceneManager smgr = device.getSceneManager();
		IGUIEnvironment guienv = device.getGUIEnvironment();

		driver.beginScene(true, true, new SColor(0, 255, 0, 0));

		smgr.drawAll();
		guienv.drawAll();

		driver.endScene();

	}

	private static IrrlichtDevice createDevice(Composite irr1) {
		SIrrlichtCreationParameters creationParams = new SIrrlichtCreationParameters();
		creationParams.setFullscreen(false);
		creationParams.setAntiAlias(false);
		creationParams.setBits(32);
		creationParams.setWindowSize(new dimension2di(1024, 768));
		creationParams.setStencilbuffer(false);
		creationParams.setDriverType(E_DRIVER_TYPE.EDT_DIRECT3D9);
		creationParams.setWindowId(irr1.handle);
		creationParams.setVsync(true);

		IrrlichtDevice device = Jirr.createDeviceEx(creationParams);

		System.out.println("idevice = " + device);
		String windowCaption = "Java@Hello World! - Irrlicht Engine Demo";
		device.setWindowCaption(windowCaption);
		IVideoDriver driver = device.getVideoDriver();
		ISceneManager smgr = device.getSceneManager();
		IGUIEnvironment guienv = device.getGUIEnvironment();

		IAnimatedMesh mesh = smgr.getMesh("media/sydney.md2");
		IAnimatedMeshSceneNode node = smgr.addAnimatedMeshSceneNode(mesh, null,
				-1, new vector3df(0, 0, 0), new vector3df(0, 0, 0),
				new vector3df(1, 1, 1));

		if (node != null) {
			node.setMaterialFlag(E_MATERIAL_FLAG.EMF_LIGHTING, false);
			node.setFrameLoop(0, 99999);
			node.setMaterialTexture(0, driver.getTexture("media/sydney.bmp"));
		}

		smgr.addCameraSceneNode(null, new vector3df(0, 10, -60), new vector3df(
				0, 0, 0), -1);

		return device;

	}

	private static IrrlichtDevice createSecondSceneDevice(Composite irr1) {
		SIrrlichtCreationParameters creationParams = new SIrrlichtCreationParameters();
		creationParams.setFullscreen(false);
		creationParams.setAntiAlias(false);
		creationParams.setBits(32);
		creationParams.setWindowSize(new dimension2di(1024, 768));
		creationParams.setStencilbuffer(false);
		creationParams.setDriverType(E_DRIVER_TYPE.EDT_DIRECT3D9);
		creationParams.setWindowId(irr1.handle);
		creationParams.setVsync(true);

		IrrlichtDevice device = Jirr.createDeviceEx(creationParams);

		IVideoDriver driver = device.getVideoDriver();
		ISceneManager smgr = device.getSceneManager();
		IGUIEnvironment env = device.getGUIEnvironment();

		driver.setTextureCreationFlag(
				E_TEXTURE_CREATION_FLAG.ETCF_ALWAYS_32_BIT, true);

		// add irrlicht logo
		env.addImage(driver.getTexture("media/irrlichtlogoalpha.tga"),
				new position2di(10, 10));

		// add some help text
		IGUIStaticText text = env.addStaticText(
				"Press 'W' to change wireframe mode", new recti(10, 465, 200,
						475), true);
		text.setOverrideColor(new SColor(100, 255, 255, 255));

		// add camera
		ICameraSceneNode camera = smgr.addCameraSceneNode(null, new vector3df(
				0, 10, -60), new vector3df(0, 0, 0), -1);

		camera.setPosition(new vector3df(1900 * 2, 255 * 2, 3700 * 2));
		camera.setTarget(new vector3df(2397 * 2, 343 * 2, 2700 * 2));
		camera.setFarValue(12000.0f);

		// disable mouse cursor
		device.getCursorControl().setVisible(false);

		// add terrain scene node
		ITerrainSceneNode terrain = smgr
				.addTerrainSceneNode("media/terrain-heightmap.bmp");

		terrain.setScale(new vector3df(40, 4.4f, 40));
		terrain.setMaterialFlag(E_MATERIAL_FLAG.EMF_LIGHTING, false);

		terrain.setMaterialTexture(0, driver
				.getTexture("media/terrain-texture.jpg"));

		// create triangle selector for the terrain
		ITriangleSelector selector = smgr.createTerrainTriangleSelector(
				terrain, 0);
		terrain.setTriangleSelector(selector);
		selector.drop();

		// create collision response animator and attach it to the camera
		ISceneNodeAnimator anim = smgr.createCollisionResponseAnimator(
				selector, camera, new vector3df(60, 100, 60), new vector3df(0,
						0, 0), new vector3df(0, 50, 0));
		camera.addAnimator(anim);
		anim.drop();

		// create event receiver
		MyEventReceiver receiver = new MyEventReceiver(terrain);
		device.setEventReceiver(receiver);

		// create skybox
		driver.setTextureCreationFlag(
				E_TEXTURE_CREATION_FLAG.ETCF_CREATE_MIP_MAPS, false);

		smgr.addSkyBoxSceneNode(driver.getTexture("media/irrlicht2_up.bmp"),
				driver.getTexture("media/irrlicht2_dn.bmp"), driver
						.getTexture("media/irrlicht2_lf.bmp"), driver
						.getTexture("media/irrlicht2_rt.bmp"), driver
						.getTexture("media/irrlicht2_ft.bmp"), driver
						.getTexture("media/irrlicht2_bk.bmp"));

		driver.setTextureCreationFlag(
				E_TEXTURE_CREATION_FLAG.ETCF_CREATE_MIP_MAPS, true);

		return device;

	}

	private static Menu menuBar, fileMenu, viewMenu, toolsMenu, helpMenu;

	// file menu items
	private static MenuItem fileMenuHeader;

	private static MenuItem addSiteMI, exitMI;

	// view menu items
	private static MenuItem viewMenuHeader;

	// tools menu items
	private static MenuItem toolsMenuHeader;

	// help menu items
	private static MenuItem helpMenuHeader;

	private static MenuItem helpMI, tutorialMI, startedMI, contactMI,
			feedbackMI, aboutMI, updatesMI;

	private static void createMenus(Shell shell) {
		menuBar = new Menu(shell, SWT.BAR);
		// FILE menu items
		fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		fileMenuHeader.setText("&File");
		fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileMenuHeader.setMenu(fileMenu);
		addSiteMI = new MenuItem(fileMenu, SWT.PUSH);
		addSiteMI.setText("&Open");
		addSiteMI.setAccelerator(SWT.CTRL + 'N');
		// addSiteMI.setImage(ImageRes.addSite);
		// addSiteMI.addSelectionListener(new MenuItemListener());
		MenuItem sep23a2 = new MenuItem(fileMenu, SWT.SEPARATOR);
		exitMI = new MenuItem(fileMenu, SWT.PUSH);
		exitMI.setText("&Exit");
		// exitMI.addSelectionListener(new MenuItemListener());
		shell.setMenuBar(menuBar);
		// VIEW menu items
		viewMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		viewMenuHeader.setText("View");
		viewMenu = new Menu(shell, SWT.DROP_DOWN);
		viewMenuHeader.setMenu(viewMenu);
		// Tools menu items
		toolsMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		toolsMenuHeader.setText("Tools");
		toolsMenu = new Menu(shell, SWT.DROP_DOWN);
		toolsMenuHeader.setMenu(toolsMenu);
		// Tools menu items
		helpMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
		helpMenuHeader.setText("Help");
		helpMenu = new Menu(shell, SWT.DROP_DOWN);
		helpMenuHeader.setMenu(helpMenu);
		helpMI = new MenuItem(helpMenu, SWT.PUSH);
		helpMI.setText("Help Contents");
		helpMI.setAccelerator(SWT.F1);
		helpMI.setImage(new Image(display, "images/help.gif"));
		// helpMI.addSelectionListener(new MenuItemListener());
	}

	public static class MyEventReceiver extends IEventReceiver {
		public MyEventReceiver(ISceneNode terrain) {
			// store pointer to terrain so we can change its drawing mode
			Terrain = terrain;
		}

		public boolean OnEvent(SEvent event) {
			if (event.getEventType() != EEVENT_TYPE.EET_KEY_INPUT_EVENT)
				return false;

			return false;
		}

		private ISceneNode Terrain;
	};

	private static Composite createExpandBar(Composite _composite) {
		Image image = new Image(display, "images/home.gif");
		ExpandBar bar = new ExpandBar(_composite, SWT.V_SCROLL);
		// First item
		Composite composite = new Composite(bar, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		Button button = new Button(composite, SWT.PUSH);
		button.setText("SWT.PUSH");
		button = new Button(composite, SWT.RADIO);
		button.setText("SWT.RADIO");
		button = new Button(composite, SWT.CHECK);
		button.setText("SWT.CHECK");
		button = new Button(composite, SWT.TOGGLE);
		button.setText("SWT.TOGGLE");
		ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
		item0.setText("What is your favorite button");
		item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item0.setControl(composite);
		item0.setImage(image);

		// Second item
		composite = new Composite(bar, SWT.NONE);
		layout = new GridLayout(2, false);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		Label label = new Label(composite, SWT.NONE);
		label.setImage(display.getSystemImage(SWT.ICON_ERROR));
		label = new Label(composite, SWT.NONE);
		label.setText("SWT.ICON_ERROR");
		label = new Label(composite, SWT.NONE);
		label.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
		label = new Label(composite, SWT.NONE);
		label.setText("SWT.ICON_INFORMATION");
		label = new Label(composite, SWT.NONE);
		label.setImage(display.getSystemImage(SWT.ICON_WARNING));
		label = new Label(composite, SWT.NONE);
		label.setText("SWT.ICON_WARNING");
		label = new Label(composite, SWT.NONE);
		label.setImage(display.getSystemImage(SWT.ICON_QUESTION));
		label = new Label(composite, SWT.NONE);
		label.setText("SWT.ICON_QUESTION");
		ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1);
		item1.setText("What is your favorite icon");
		item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item1.setControl(composite);
		item1.setImage(image);

		// Third item
		composite = new Composite(bar, SWT.NONE);
		layout = new GridLayout(2, true);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		label = new Label(composite, SWT.NONE);
		label.setText("Scale");
		new Scale(composite, SWT.NONE);
		label = new Label(composite, SWT.NONE);
		label.setText("Spinner");
		new Spinner(composite, SWT.BORDER);
		label = new Label(composite, SWT.NONE);
		label.setText("Slider");
		new Slider(composite, SWT.NONE);
		ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 2);
		item2.setText("What is your favorite range widget");
		item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item2.setControl(composite);
		item2.setImage(image);

		item1.setExpanded(true);
		bar.setSpacing(8);

		return bar;
	}

}

http://www.javazing.com
P-III-950, WinXP, GeForce FX5600 128 MB ForceWare 52.16, DX9, Eclipse IDE, JRE 1.6
de3000
Posts: 20
Joined: Tue Sep 19, 2006 4:57 pm
Location: South Africa

Post by de3000 »

The sample code here works fine but the problem with it is that messagebox or other dialogs do not show when opened.

Adding this listener to the shell disposed or the exitMI does not open the dialog when the irrlicht windows are used. Is there a way around the problem?

Code: Select all


Listener exitListener = new Listener()
	{
	    public void handleEvent(Event e)
	    {
		FileDialog fd = new FileDialog(p);
		MessageBox dialog = new MessageBox(p, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
		dialog.setText("Question");
		dialog.setMessage("Exit?");
		if (e.type == SWT.Close) e.doit = false;
		if (dialog.open() != SWT.OK) return;
		p.dispose();
	    }
	};
Post Reply