From 599bf2dea34e82e32a7b443900a0c24c92b96b80 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 5 May 2010 15:30:33 +0200 Subject: Newt: Add generic NativeWindow parenting, inclusive using an AWT Component as a parent --- .../demos/gl2/gears/TestGearsNewtAWTWrapper.java | 115 +++++++++++ .../jogamp/test/junit/newt/TestParenting01AWT.java | 219 +++++++++++++++++++++ .../test/junit/newt/TestParenting01NEWT.java | 95 ++++----- 3 files changed, 382 insertions(+), 47 deletions(-) create mode 100755 src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/TestGearsNewtAWTWrapper.java create mode 100755 src/junit/com/jogamp/test/junit/newt/TestParenting01AWT.java (limited to 'src/junit/com/jogamp') diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/TestGearsNewtAWTWrapper.java b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/TestGearsNewtAWTWrapper.java new file mode 100755 index 000000000..fb25ae399 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/TestGearsNewtAWTWrapper.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2010 Sven Gothel. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name Sven Gothel or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.demos.gl2.gears; + +import javax.media.nativewindow.*; +import javax.media.opengl.*; +import com.jogamp.opengl.util.Animator; + +import com.jogamp.test.junit.jogl.demos.gl2.gears.Gears; +import com.jogamp.newt.*; +import com.jogamp.newt.event.*; +import com.jogamp.newt.opengl.*; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.After; +import org.junit.Test; + +public class TestGearsNewtAWTWrapper { + static GLProfile glp; + static int width, height; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getDefault(); + Assert.assertNotNull(glp); + width = 512; + height = 512; + } + + @AfterClass + public static void releaseClass() { + } + + protected void runTestGL(GLCapabilities caps) throws InterruptedException { + Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display + Screen nScreen = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0 + Window nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, nScreen, caps); + + GLWindow glWindow = GLWindow.create(nWindow); + Assert.assertNotNull(glWindow); + glWindow.setTitle("Gears NewtAWTWrapper Test"); + + glWindow.addGLEventListener(new Gears()); + + Animator animator = new Animator(glWindow); + QuitAdapter quitAdapter = new QuitAdapter(); + + glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter)); + glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter)); + + glWindow.setSize(width, height); + glWindow.setVisible(true); + animator.start(); + + while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getDuration()0 && !shouldQuit) { + glWindow2.display(); + Thread.sleep(step); + duration -= step; + + while( null != ( event = (NEWTEvent) eventFifo.get() ) ) { + Window source = (Window) event.getSource(); + if(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY == event.getEventType()) { + shouldQuit = true; + } else if(event instanceof KeyEvent) { + KeyEvent keyEvent = (KeyEvent) event; + switch(keyEvent.getKeyChar()) { + case 'q': + shouldQuit = true; + break; + case 'f': + source.setFullscreen(!source.isFullscreen()); + break; + } + } + } + } + destroyWindow(null, null, window2, glWindow2); + frame.dispose(); + } + + public static void setDemoFields(GLEventListener demo, Window window, GLWindow glWindow, boolean debug) { + Assert.assertNotNull(demo); + Assert.assertNotNull(window); + if(debug) { + MiscUtils.setFieldIfExists(demo, "glDebug", true); + MiscUtils.setFieldIfExists(demo, "glTrace", true); + } + if(!MiscUtils.setFieldIfExists(demo, "window", window)) { + MiscUtils.setFieldIfExists(demo, "glWindow", glWindow); + } + } + + public static void main(String args[]) throws IOException { + durationPerTest = 5000; + String tstname = TestParenting01AWT.class.getName(); + org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { + tstname, + "filtertrace=true", + "haltOnError=false", + "haltOnFailure=false", + "showoutput=true", + "outputtoformatters=true", + "logfailedtests=true", + "logtestlistenerevents=true", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + } + +} diff --git a/src/junit/com/jogamp/test/junit/newt/TestParenting01NEWT.java b/src/junit/com/jogamp/test/junit/newt/TestParenting01NEWT.java index 128c4358d..9528009ea 100755 --- a/src/junit/com/jogamp/test/junit/newt/TestParenting01NEWT.java +++ b/src/junit/com/jogamp/test/junit/newt/TestParenting01NEWT.java @@ -68,26 +68,18 @@ public class TestParenting01NEWT { height = 480; } - static Window createWindow(NativeWindow parent, Screen screen, Capabilities caps, int width, int height, boolean onscreen, boolean undecorated) { + static Window createWindow(NativeWindow parent, Screen screen, Capabilities caps, int width, int height) { Assert.assertNotNull(caps); - caps.setOnscreen(onscreen); - // System.out.println("Requested: "+caps); - // // Create native windowing resources .. X11/Win/OSX // Window window; - if(null==parent) { - window = NewtFactory.createWindow(screen, caps, onscreen && undecorated); - } else { - window = NewtFactory.createWindow(parent.getWindowHandle(), screen, caps, onscreen && undecorated); - } + window = ( null == parent ) ? NewtFactory.createWindow(screen, caps) : NewtFactory.createWindow(parent, screen, caps) ; Assert.assertNotNull(window); window.setSize(width, height); Assert.assertTrue(false==window.isVisible()); Assert.assertTrue(width==window.getWidth()); Assert.assertTrue(height==window.getHeight()); - System.out.println("Created: "+window); // // Create native OpenGL resources .. XGL/WGL/CGL .. @@ -98,7 +90,7 @@ public class TestParenting01NEWT { Assert.assertTrue(caps.getGreenBits()>5); Assert.assertTrue(caps.getBlueBits()>5); Assert.assertTrue(caps.getRedBits()>5); - Assert.assertTrue(caps.isOnscreen()==onscreen); + Assert.assertTrue(caps.isOnscreen()==true); return window; } @@ -130,28 +122,34 @@ public class TestParenting01NEWT { int x = 1; int y = 1; - NEWTEventFiFo eventFifo = new NEWTEventFiFo(); + final NEWTEventFiFo eventFifo = new NEWTEventFiFo(); - Window window1 = createWindow( null, screen, caps, width, height, true /* onscreen */, false /* undecorated */); + Window window1 = createWindow( null, screen, caps, width, height ); Assert.assertNotNull(window1); - window1.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - PARENT"); - window1.setPosition(x,y); - window1.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo))); GLWindow glWindow1 = GLWindow.create(window1); Assert.assertNotNull(glWindow1); - - Window window2 = createWindow(window1, screen, caps, width/2, height/2, true /* onscreen */, false /* undecorated */); + Assert.assertTrue(width==glWindow1.getWidth()); + Assert.assertTrue(height==glWindow1.getHeight()); + glWindow1.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - PARENT"); + glWindow1.setPosition(x,y); + glWindow1.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo))); + glWindow1.addWindowListener(new TraceWindowAdapter()); + + Window window2 = createWindow(window1, screen, caps, width/2, height/2); Assert.assertNotNull(window2); - window2.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - CHILD"); - System.out.println("Window1: "+window1); - Assert.assertTrue(width==window1.getWidth()); - Assert.assertTrue(height==window1.getHeight()); - window2.setPosition(window1.getWidth()/2, window1.getHeight()/2); - window2.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo))); - // window2.addMouseListener(new TraceMouseAdapter()); - window2.requestFocus(); GLWindow glWindow2 = GLWindow.create(window2); Assert.assertNotNull(glWindow2); + Assert.assertTrue(width/2==glWindow2.getWidth()); + Assert.assertTrue(height/2==glWindow2.getHeight()); + glWindow2.setTitle("testWindowParenting01NewtOnNewtParentChildDraw - CHILD"); + glWindow2.setPosition(glWindow1.getWidth()/2, glWindow1.getHeight()/2); + glWindow2.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo))); + glWindow2.addWindowListener(new TraceWindowAdapter(new WindowAdapter() { + public void windowDestroyNotify(WindowEvent e) { + eventFifo.put(e); + } + })); + // glWindow2.addMouseListener(new TraceMouseAdapter()); GLEventListener demo1 = new RedSquare(); setDemoFields(demo1, window1, glWindow1, false); @@ -161,8 +159,8 @@ public class TestParenting01NEWT { setDemoFields(demo2, window2, glWindow2, false); glWindow2.addGLEventListener(demo2); - window2.setVisible(true); - window1.setVisible(true); + glWindow2.setVisible(true); + glWindow1.setVisible(true); glWindow2.setVisible(true); glWindow1.setVisible(true); @@ -170,34 +168,37 @@ public class TestParenting01NEWT { glWindow2.display(); glWindow1.display(); + boolean shouldQuit = false; long duration = durationPerTest; long step = 20; - KeyEvent keyEvent; - boolean shouldQuit = false; + NEWTEvent event; while (duration>0 && !shouldQuit) { - while( null != ( keyEvent = (KeyEvent) eventFifo.get() ) ) { - Window source = (Window) keyEvent.getSource(); - switch(keyEvent.getKeyChar()) { - case 'q': - System.out.println(keyEvent); - shouldQuit = true; - break; - case 'f': - System.out.println(keyEvent); - source.setFullscreen(!source.isFullscreen()); - break; - } - } - glWindow1.display(); glWindow2.display(); - Thread.sleep(step); // 1000 ms + Thread.sleep(step); duration -= step; x += 1; y += 1; - window1.setPosition(x,y); - window2.setPosition(window1.getWidth()/2,window1.getHeight()/2-y); + glWindow1.setPosition(x,y); + glWindow2.setPosition(glWindow1.getWidth()/2,glWindow1.getHeight()/2-y); + + while( null != ( event = (NEWTEvent) eventFifo.get() ) ) { + Window source = (Window) event.getSource(); + if(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY == event.getEventType()) { + shouldQuit = true; + } else if(event instanceof KeyEvent) { + KeyEvent keyEvent = (KeyEvent) event; + switch(keyEvent.getKeyChar()) { + case 'q': + shouldQuit = true; + break; + case 'f': + source.setFullscreen(!source.isFullscreen()); + break; + } + } + } } destroyWindow(null, null, window2, glWindow2); destroyWindow(display, screen, window1, glWindow1); -- cgit v1.2.3