JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting02NEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29
30package com.jogamp.opengl.test.junit.newt.parenting;
31
32import org.junit.Assert;
33import org.junit.BeforeClass;
34import org.junit.Test;
35import org.junit.FixMethodOrder;
36import org.junit.runners.MethodSorters;
37
38import com.jogamp.opengl.*;
39import com.jogamp.nativewindow.*;
40import com.jogamp.nativewindow.util.Point;
41import com.jogamp.nativewindow.util.PointImmutable;
42import com.jogamp.newt.*;
43import com.jogamp.newt.event.*;
44import com.jogamp.newt.opengl.*;
45
46import java.io.IOException;
47
48import com.jogamp.opengl.test.junit.util.*;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
50import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
51// import com.jogamp.opengl.test.junit.jogl.demos.es1.RedSquareES1;
52// import com.jogamp.opengl.test.junit.jogl.demos.es1.GearsES1;
53
54@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55public class TestParenting02NEWT extends UITestCase {
56 static int width, height;
57 static long durationPerTest = 500;
58
59 @BeforeClass
60 public static void initClass() {
61 width = 640;
62 height = 480;
63 }
64
65 static Window createWindow(final Screen screen, final Capabilities caps) {
66 Assert.assertNotNull(caps);
67 final Window window = NewtFactory.createWindow(screen, caps) ;
68 Assert.assertNotNull(window);
69 return window;
70 }
71
72 static Window createWindow(final NativeWindow parent, final Capabilities caps) {
73 Assert.assertNotNull(caps);
74 final Window window = NewtFactory.createWindow(parent, caps);
75 window.setUndecorated(true);
76 Assert.assertNotNull(window);
77 return window;
78 }
79
80 static void destroyWindow(final Display display, final Screen screen, final Window window, final GLWindow glWindow) {
81 if(null!=glWindow) {
82 glWindow.destroy();
83 }
84 if(null!=window) {
85 window.destroy();
86 }
87 if(null!=screen) {
88 screen.destroy();
89 }
90 if(null!=display) {
91 display.destroy();
92 }
93 }
94
95 @Test
96 public void test01NewtOnNewtParentChildDraw() throws InterruptedException {
97 final GLCapabilities caps = new GLCapabilities(null);
98 Assert.assertNotNull(caps);
99 final Display display = NewtFactory.createDisplay(null); // local display
100 Assert.assertNotNull(display);
101 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
102 Assert.assertNotNull(screen);
103
104 int x = 1;
105 int y = 1;
106
107 final NEWTEventFiFo eventFifo = new NEWTEventFiFo();
108
109 final Window window1 = createWindow(screen, caps);
110 Assert.assertNotNull(window1);
111 final GLWindow glWindow1 = GLWindow.create(window1);
112 Assert.assertNotNull(glWindow1);
113 glWindow1.setSize(width, height);
114 Assert.assertEquals(width,glWindow1.getWidth());
115 Assert.assertEquals(height,glWindow1.getHeight());
116 glWindow1.setTitle("test01NewtOnNewtParentChildDraw - PARENT");
117 glWindow1.setPosition(x,y);
118 //glWindow1.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
119 //glWindow1.addWindowListener(new TraceWindowAdapter());
120
121 final GLEventListener demo1 = new RedSquareES2();
122 setDemoFields(demo1, window1, glWindow1, false);
123 // glWindow1.addGLEventListener(demo1);
124
125 glWindow1.setVisible(true);
127 Assert.assertNotNull(capsChosen);
128 Assert.assertTrue(capsChosen.isOnscreen()==true);
129
130 final Window window2 = createWindow(window1, caps);
131 Assert.assertNotNull(window2);
132 final GLWindow glWindow2 = GLWindow.create(window2);
133 Assert.assertNotNull(glWindow2);
134 glWindow2.setSize(width/2, height/2);
135 //Assert.assertEquals(width/2,glWindow2.getWidth());
136 //Assert.assertEquals(height/2,glWindow2.getHeight());
137 glWindow2.setTitle("test01NewtOnNewtParentChildDraw - CHILD");
138 glWindow2.setPosition(glWindow1.getWidth()/2, glWindow1.getHeight()/2);
139 //glWindow2.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
140 //glWindow2.addWindowListener(new TraceWindowAdapter(new WindowAction(eventFifo)));
141 // glWindow2.addMouseListener(new TraceMouseAdapter());
142
143 final GLEventListener demo2 = new GearsES2();
144 setDemoFields(demo2, window2, glWindow2, false);
145 // glWindow2.addGLEventListener(demo2);
146
147 glWindow2.setVisible(true);
148 capsChosen = glWindow2.getGraphicsConfiguration().getChosenCapabilities();
149 Assert.assertNotNull(capsChosen);
150 Assert.assertTrue(capsChosen.isOnscreen()==true);
151
152 glWindow1.addGLEventListener(demo1);
153 glWindow2.addGLEventListener(demo2);
154 glWindow2.addWindowListener(windowMoveDetection);
155
156 boolean shouldQuit = false;
157 long duration = durationPerTest;
158 final long step = 20;
159 NEWTEvent event;
160
161 while (duration>0 && !shouldQuit) {
162 glWindow1.display();
163 glWindow2.display();
164 duration -= step;
165 x += 1;
166 y += 1;
167 // glWindow1.setPosition(x,y);
168 final PointImmutable expPos = new Point(glWindow1.getWidth()/2, glWindow1.getHeight()/2-y);
169 glWindow2.setPosition(expPos.getX(), expPos.getY());
170 {
171 int waitCount=0;
172 do {
173 Thread.sleep(step);
174 waitCount++;
175 } while( !windowMoved && waitCount < 10);
176 final boolean didWindowMove = windowMoved;
177 windowMoved = false;
178 final PointImmutable hasPos = new Point(glWindow2.getX(), glWindow2.getY());
179 System.err.println("Moved: exp "+expPos+", has "+hasPos+", equals "+expPos.equals(hasPos)+", didWindowMove "+didWindowMove+", waitCount "+waitCount);
180 }
181
182 while( null != ( event = eventFifo.get() ) ) {
183 final Window source = (Window) event.getSource();
185 shouldQuit = true;
186 } else if(event instanceof KeyEvent) {
187 final KeyEvent keyEvent = (KeyEvent) event;
188 switch(keyEvent.getKeyChar()) {
189 case 'q':
190 shouldQuit = true;
191 break;
192 case 'f':
193 source.setFullscreen(!source.isFullscreen());
194 break;
195 }
196 }
197 }
198 }
199 destroyWindow(null, null, window2, glWindow2);
200 destroyWindow(display, screen, window1, glWindow1);
201 }
202 volatile boolean windowMoved = false;
203 final WindowListener windowMoveDetection = new WindowAdapter() {
204 @Override
205 public void windowMoved(final WindowEvent e) {
206 windowMoved = true;
207 }
208 };
209
210 public static void setDemoFields(final GLEventListener demo, final Window window, final GLWindow glWindow, final boolean debug) {
211 Assert.assertNotNull(demo);
212 Assert.assertNotNull(window);
213 if(debug) {
214 MiscUtils.setFieldIfExists(demo, "glDebug", true);
215 MiscUtils.setFieldIfExists(demo, "glTrace", true);
216 }
217 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
218 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
219 }
220 }
221
222 static int atoi(final String a) {
223 int i=0;
224 try {
225 i = Integer.parseInt(a);
226 } catch (final Exception ex) { ex.printStackTrace(); }
227 return i;
228 }
229
230 public static void main(final String args[]) throws IOException {
231 for(int i=0; i<args.length; i++) {
232 if(args[i].equals("-time")) {
233 durationPerTest = atoi(args[++i]);
234 }
235 }
236 final String tstname = TestParenting02NEWT.class.getName();
237 org.junit.runner.JUnitCore.main(tstname);
238 }
239
240}
Specifies a set of capabilities that a window's rendering context must support, such as color depth p...
abstract void destroy()
Manually trigger the destruction, incl.
static Display createDisplay(final String name)
Create a Display entity.
static Window createWindow(final CapabilitiesImmutable caps)
Create a top level Window entity on the default Display and default Screen.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
abstract void destroy()
Manually trigger the destruction, incl.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
synchronized NEWTEvent get()
Remove NEWTEvent from head.
NEWT events are provided for notification purposes ONLY; The NEWT will automatically handle the even...
Definition: NEWTEvent.java:52
final short getEventType()
Returns the event type of this event.
Definition: NEWTEvent.java:72
NEWT Window events are provided for notification purposes ONLY.
static final short EVENT_WINDOW_DESTROY_NOTIFY
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setTitle(final String title)
Definition: GLWindow.java:297
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final AbstractGraphicsConfiguration getGraphicsConfiguration()
Returns the graphics configuration corresponding to this window.
Definition: GLWindow.java:1052
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final int getHeight()
Returns the height of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:451
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final int getWidth()
Returns the width of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:446
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
static void setDemoFields(final GLEventListener demo, final Window window, final GLWindow glWindow, final boolean debug)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
CapabilitiesImmutable getChosenCapabilities()
Return the capabilities reflecting this graphics configuration, which may differ from the capabilitie...
Specifies an immutable set of capabilities that a window's rendering context must support,...
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
Extend the NativeSurface interface with windowing information such as window-handle,...
boolean equals(Object obj)
Checks whether two points objects are equal.
Specifying NEWT's Window functionality:
Definition: Window.java:115
boolean setFullscreen(boolean fullscreen)
Enable or disable fullscreen mode for this window.
void setUndecorated(boolean value)
void destroy()
Destroys this window incl.releasing all related resources.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.