JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLWindows02NEWTAnimated.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
29package com.jogamp.opengl.test.junit.newt;
30
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.nativewindow.util.Rectangle;
39import com.jogamp.nativewindow.util.RectangleImmutable;
40import com.jogamp.opengl.*;
41
42import com.jogamp.opengl.util.Animator;
43import com.jogamp.newt.*;
44import com.jogamp.newt.event.*;
45import com.jogamp.newt.opengl.*;
46
47import java.io.IOException;
48
49import com.jogamp.opengl.test.junit.util.UITestCase;
50import com.jogamp.opengl.test.junit.util.MiscUtils;
51import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
52
53@FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 static GLProfile glp;
56 static int width, height;
57 static long durationPerTest = 100; // ms
58
59 @BeforeClass
60 public static void initClass() {
61 width = 640;
62 height = 480;
63 glp = GLProfile.getDefault();
64 }
65
66 static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated, final boolean vsync) {
67 Assert.assertNotNull(caps);
68 caps.setOnscreen(onscreen);
69 // System.out.println("Requested: "+caps);
70
71 //
72 // Create native windowing resources .. X11/Win/OSX
73 //
74 GLWindow glWindow;
75 if(null!=screen) {
76 final Window window = NewtFactory.createWindow(screen, caps);
77 Assert.assertNotNull(window);
78 glWindow = GLWindow.create(window);
79 } else {
80 glWindow = GLWindow.create(caps);
81 }
82 glWindow.setUpdateFPSFrames(1, null);
83 Assert.assertNotNull(glWindow);
84 glWindow.setUndecorated(onscreen && undecorated);
85
86 final GLEventListener demo = new GearsES2(vsync ? 1 : 0);
87 setDemoFields(demo, glWindow);
88 glWindow.addGLEventListener(demo);
90 Assert.assertEquals(false,glWindow.isNativeValid());
91
92 glWindow.setSize(width, height);
93 Assert.assertEquals(false,glWindow.isVisible());
94 glWindow.setVisible(true);
95 Assert.assertEquals(true,glWindow.isVisible());
96 Assert.assertEquals(true,glWindow.isNativeValid());
97 // Assert.assertEquals(width,glWindow.getWidth());
98 // Assert.assertEquals(height,glWindow.getHeight());
99 // System.out.println("Created: "+glWindow);
100
101 //
102 // Create native OpenGL resources .. XGL/WGL/CGL ..
103 // equivalent to GLAutoDrawable methods: setVisible(true)
104 //
105 final GLCapabilitiesImmutable caps2 = glWindow.getChosenGLCapabilities();
106 Assert.assertNotNull(caps2);
107 Assert.assertTrue(caps2.getGreenBits()>=5);
108 Assert.assertTrue(caps2.getBlueBits()>=5);
109 Assert.assertTrue(caps2.getRedBits()>=5);
110 Assert.assertEquals(caps2.isOnscreen(),onscreen);
111
112 return glWindow;
113 }
114
115 static void destroyWindow(final GLWindow glWindow) {
116 if(null!=glWindow) {
117 glWindow.destroy();
118 }
119 }
120
121 @Test
122 public void testWindowDecor01Simple() throws InterruptedException {
123 final GLCapabilities caps = new GLCapabilities(glp);
124 Assert.assertNotNull(caps);
125 final GLWindow window = createWindow(null, caps, width, height, true /* onscreen */, false /* undecorated */, true /* vsync */);
126 final Animator animator = new Animator(window);
127 animator.setUpdateFPSFrames(1, null);
128 Assert.assertTrue(animator.start());
129 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest) {
130 Thread.sleep(100);
131 }
132 destroyWindow(window); // destroy - but still in animator
133 Assert.assertEquals(false, window.isNativeValid());
134 Assert.assertEquals(false, window.isVisible());
135 Assert.assertEquals(true, animator.isAnimating());
136 Assert.assertEquals(false, animator.isPaused());
137 Assert.assertEquals(true, animator.isStarted());
138
139 animator.remove(window);
140 Thread.sleep(250); // give animator a chance to become paused
141 Assert.assertEquals(false, animator.isAnimating());
142 Assert.assertEquals(true, animator.isPaused()); // zero drawables
143 Assert.assertEquals(true, animator.isStarted());
144 Assert.assertTrue(animator.stop());
145 }
146
147 @Test
148 public void testWindowDecor02DestroyWinTwiceA() throws InterruptedException {
149 final GLCapabilities caps = new GLCapabilities(glp);
150 Assert.assertNotNull(caps);
151 final GLWindow window = createWindow(null, caps, width, height, true /* onscreen */, false /* undecorated */, true /* vsync */);
152 final Animator animator = new Animator(0 /* w/o AWT */);
153 animator.setUpdateFPSFrames(1, null);
154 Assert.assertTrue(animator.start());
155 Thread.sleep(250); // give animator a chance to become paused
156 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
157 Assert.assertEquals(true, animator.isPaused()); // zero drawables
158 animator.add(window);
159 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest) {
160 Thread.sleep(100);
161 }
162 destroyWindow(window);
163 destroyWindow(window);
164 Assert.assertEquals(true, animator.isAnimating());
165 Assert.assertEquals(true, animator.isStarted());
166 Assert.assertEquals(false, animator.isPaused());
167 animator.remove(window);
168 Thread.sleep(250); // give animator a chance to become paused
169 Assert.assertEquals(false, animator.isAnimating());
170 Assert.assertEquals(true, animator.isStarted());
171 Assert.assertEquals(true, animator.isPaused()); // zero drawables
172 Assert.assertTrue(animator.stop());
173 }
174
175 @Test
176 public void testWindowDecor03TwoWinOneDisplay() throws InterruptedException {
177 final GLCapabilities caps = new GLCapabilities(glp);
178 Assert.assertNotNull(caps);
179
180 final Display display = NewtFactory.createDisplay(null); // local display
181 Assert.assertNotNull(display);
182
183 final Screen screen = NewtFactory.createScreen(display, 0); // screen 0
184 Assert.assertNotNull(screen);
185 final GLWindow window1 = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */, false /* vsync */);
186 Assert.assertNotNull(window1);
187 window1.setPosition(0, 0);
188
189 final GLWindow window2 = createWindow(screen, caps, width-10, height-10, true /* onscreen */, false /* undecorated */, true /* vsync */);
190 Assert.assertNotNull(window2);
191 final RectangleImmutable screenBoundsInWinU = screen.getViewportInWindowUnits();
192 window2.setPosition(screenBoundsInWinU.getWidth()-width, 0);
193
194 final Animator animator = new Animator(0 /* w/o AWT */);
195 animator.setUpdateFPSFrames(1, null);
196 Assert.assertEquals(false, animator.isStarted());
197 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
198 Assert.assertEquals(false, animator.isPaused()); // zero drawables, but not started
199
200 Assert.assertTrue(animator.start());
201 Assert.assertEquals(true, animator.isStarted());
202 Thread.sleep(250); // give animator a chance to become paused
203 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
204 Assert.assertEquals(true, animator.isPaused()); // zero drawables
205
206 animator.add(window1);
207 Assert.assertEquals(true, animator.isStarted());
208 Assert.assertEquals(true, animator.isAnimating());
209 Assert.assertEquals(false, animator.isPaused());
210
211 animator.add(window2);
212 Assert.assertEquals(true, animator.isStarted());
213 Assert.assertEquals(true, animator.isAnimating());
214 Assert.assertEquals(false, animator.isPaused());
215
216 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest) {
217 Thread.sleep(100);
218 }
219 window1.destroy();
220 animator.remove(window1);
221 Assert.assertEquals(true, animator.isStarted());
222 Assert.assertEquals(true, animator.isAnimating());
223 Assert.assertEquals(false, animator.isPaused());
224 // animator.resetFPSCounter();
225 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest+durationPerTest/10) {
226 Thread.sleep(100);
227 }
228 window2.destroy();
229 animator.remove(window2);
230 Assert.assertEquals(true, animator.isStarted());
231 Thread.sleep(250); // give animator a chance to become paused
232 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
233 Assert.assertEquals(true, animator.isPaused()); // zero drawables
234 Assert.assertTrue(animator.stop());
235 }
236
237 @Test
238 public void testWindowDecor03TwoWinTwoDisplays() throws InterruptedException {
239 final GLCapabilities caps = new GLCapabilities(glp);
240 Assert.assertNotNull(caps);
241
242 final Display display1 = NewtFactory.createDisplay(null, false); // local display
243 Assert.assertNotNull(display1);
244 final Display display2 = NewtFactory.createDisplay(null, false); // local display
245 Assert.assertNotNull(display2);
246 Assert.assertNotSame(display1, display2);
247
248 final Screen screen1 = NewtFactory.createScreen(display1, 0); // screen 0
249 Assert.assertNotNull(screen1);
250 final GLWindow window1 = createWindow(screen1, caps, width, height, true /* onscreen */, false /* undecorated */, false /* vsync */);
251 Assert.assertNotNull(window1);
252 window1.setPosition(0, 0);
253
254 final Screen screen2 = NewtFactory.createScreen(display2, 0); // screen 0
255 Assert.assertNotNull(screen2);
256 final GLWindow window2 = createWindow(screen2, caps, width-10, height-10, true /* onscreen */, false /* undecorated */, true /* vsync */);
257 Assert.assertNotNull(window2);
258 final RectangleImmutable screen2BoundsInWinU = screen2.getViewportInWindowUnits();
259 window2.setPosition(screen2BoundsInWinU.getWidth()-width, 0);
260
261 final Animator animator = new Animator(0 /* w/o AWT */);
262 animator.setUpdateFPSFrames(1, null);
263 Assert.assertEquals(false, animator.isStarted());
264 Assert.assertEquals(false, animator.isAnimating());
265 Assert.assertEquals(false, animator.isPaused());
266
267 Assert.assertTrue(animator.start());
268 Assert.assertEquals(true, animator.isStarted());
269 Thread.sleep(250); // give animator a chance to become paused
270 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
271 Assert.assertEquals(true, animator.isPaused()); // zero drawables
272
273 animator.add(window1);
274 Assert.assertEquals(true, animator.isStarted());
275 Assert.assertEquals(true, animator.isAnimating());
276 Assert.assertEquals(false, animator.isPaused());
277
278 animator.add(window2);
279 Assert.assertEquals(true, animator.isStarted());
280 Assert.assertEquals(true, animator.isAnimating());
281 Assert.assertEquals(false, animator.isPaused());
282
283 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest) {
284 Thread.sleep(100);
285 }
286 destroyWindow(window1);
287 animator.remove(window1);
288 Assert.assertEquals(true, animator.isStarted());
289 Assert.assertEquals(true, animator.isAnimating());
290 Assert.assertEquals(false, animator.isPaused());
291
292 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest+durationPerTest/10) {
293 Thread.sleep(100);
294 }
295
296 Assert.assertEquals(true, animator.isStarted());
297 Assert.assertEquals(true, animator.isAnimating());
298 Assert.assertEquals(false, animator.isPaused());
299
300 Assert.assertEquals(true, animator.pause());
301
302 Assert.assertEquals(true, animator.isStarted());
303 Assert.assertEquals(false, animator.isAnimating());
304 Assert.assertEquals(true, animator.isPaused());
305
306 Assert.assertEquals(true, animator.resume());
307
308 Assert.assertEquals(true, animator.isStarted());
309 Assert.assertEquals(true, animator.isAnimating());
310 Assert.assertEquals(false, animator.isPaused());
311
312 destroyWindow(window2);
313 animator.remove(window2);
314 Assert.assertEquals(true, animator.isStarted());
315 Thread.sleep(250); // give animator a chance to become paused
316 Assert.assertEquals(false, animator.isAnimating()); // zero drawables
317 Assert.assertEquals(true, animator.isPaused()); // zero drawables
318
319 Assert.assertTrue(animator.stop());
320 Assert.assertEquals(false, animator.isStarted());
321 Assert.assertEquals(false, animator.isAnimating());
322 Assert.assertEquals(false, animator.isPaused());
323 }
324
325 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow) {
326 Assert.assertNotNull(demo);
327 Assert.assertNotNull(glWindow);
328 if(!MiscUtils.setFieldIfExists(demo, "window", glWindow)) {
329 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
330 }
331 }
332
333 static int atoi(final String a) {
334 int i=0;
335 try {
336 i = Integer.parseInt(a);
337 } catch (final Exception ex) { ex.printStackTrace(); }
338 return i;
339 }
340
341 public static void main(final String args[]) throws IOException {
342 for(int i=0; i<args.length; i++) {
343 if(args[i].equals("-time")) {
344 durationPerTest = atoi(args[++i]);
345 }
346 }
347 final String tstname = TestGLWindows02NEWTAnimated.class.getName();
348 org.junit.runner.JUnitCore.main(tstname);
349 }
350
351}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
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 RectangleImmutable getViewportInWindowUnits()
See Coordinate System.
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 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 void setUndecorated(final boolean value)
Definition: GLWindow.java:337
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
static void setDemoFields(final GLEventListener demo, final GLWindow glWindow)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final synchronized void remove(final GLAutoDrawable drawable)
Removes a drawable from the animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
synchronized boolean isStarted()
Indicates whether this animator has been started.
final synchronized boolean pause()
Pauses this animator.
Definition: Animator.java:382
final synchronized boolean resume()
Resumes animation if paused.
Definition: Animator.java:397
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean isPaused()
Indicates whether this animator is started and either manually paused or paused automatically due to ...
Definition: Animator.java:326
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
int getBlueBits()
Returns the number of bits for the color buffer's blue component.
int getRedBits()
Returns the number of bits for the color buffer's red component.
int getGreenBits()
Returns the number of bits for the color buffer's green component.
boolean isOnscreen()
Returns whether an on- or offscreen surface is requested, available or chosen.
Immutable Rectangle interface, with its position on the top-left.
Specifying NEWT's Window functionality:
Definition: Window.java:115
void setUpdateFPSFrames(int frames, PrintStream out)
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Specifies an immutable set of OpenGL capabilities.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.