JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLWindowWarpPointer01NEWT.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.newt;
2
3import java.io.IOException;
4import java.util.Random;
5
6import com.jogamp.nativewindow.AbstractGraphicsDevice;
7import com.jogamp.opengl.GLAutoDrawable;
8import com.jogamp.opengl.GLCapabilities;
9import com.jogamp.opengl.GLCapabilitiesImmutable;
10import com.jogamp.opengl.GLEventListener;
11import com.jogamp.opengl.GLProfile;
12
13import org.junit.Assert;
14import org.junit.BeforeClass;
15import org.junit.Test;
16import org.junit.FixMethodOrder;
17import org.junit.runners.MethodSorters;
18
19import com.jogamp.newt.Screen;
20import com.jogamp.newt.event.MouseAdapter;
21import com.jogamp.newt.event.MouseEvent;
22import com.jogamp.newt.opengl.GLWindow;
23import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
24import com.jogamp.opengl.test.junit.util.UITestCase;
25import com.jogamp.opengl.util.Animator;
26
27@FixMethodOrder(MethodSorters.NAME_ASCENDING)
29 static GLProfile glp;
30 static int width, height;
31 static long durationPerTest = 2000; // ms
32
33 @BeforeClass
34 public static void initClass() {
35 width = 640;
36 height = 480;
37 glp = GLProfile.getDefault();
38 }
39
40 static GLWindow createWindow(final Screen screen, final GLCapabilitiesImmutable caps)
41 throws InterruptedException
42 {
43 Assert.assertNotNull(caps);
44 //
45 // Create native windowing resources .. X11/Win/OSX
46 //
47 GLWindow glWindow;
48 if(null!=screen) {
49 glWindow = GLWindow.create(screen, caps);
50 Assert.assertNotNull(glWindow);
51 } else {
52 glWindow = GLWindow.create(caps);
53 Assert.assertNotNull(glWindow);
54 }
55 glWindow.setUpdateFPSFrames(1, null);
56
57 final GLEventListener demo = new GearsES2();
58 glWindow.addGLEventListener(demo);
59
60 glWindow.setSize(512, 512);
61 glWindow.setVisible(true);
62 Assert.assertEquals(true,glWindow.isVisible());
63 Assert.assertEquals(true,glWindow.isNativeValid());
64
65 return glWindow;
66 }
67
68 static void destroyWindow(final GLWindow glWindow) {
69 if(null!=glWindow) {
70 glWindow.destroy();
71 Assert.assertEquals(false,glWindow.isNativeValid());
72 }
73 }
74
75 @Test
76 public void testWarp01Center() throws InterruptedException {
77 testWarpImpl(false);
78 }
79
80 @Test
81 public void testWarp02Random() throws InterruptedException {
82 testWarpImpl(true);
83 }
84
85 void testWarpImpl(final boolean random) throws InterruptedException {
86 final GLCapabilities caps = new GLCapabilities(glp);
87 Assert.assertNotNull(caps);
88 final GLWindow window1 = createWindow(null, caps); // local
89 Assert.assertEquals(true,window1.isNativeValid());
90 Assert.assertEquals(true,window1.isVisible());
91 final Animator animator = new Animator(0 /* w/o AWT */);
92 animator.setUpdateFPSFrames(1, null);
93 animator.add(window1);
94 animator.start();
96
97 System.err.println("GLProfiles window1: "+device1.getConnection()+": "+GLProfile.glAvailabilityToString(device1));
98
99 window1.warpPointer(width / 2, height / 2);
100 window1.requestFocus();
101
102 window1.addMouseListener(new MouseAdapter() {
103 void warpCenter() {
104 window1.warpPointer(width / 2, height / 2);
105 }
106
107 @Override
108 public void mouseEntered(final MouseEvent e) {
109 }
110
111 @Override
112 public void mouseExited(final MouseEvent e) {
113 warpCenter();
114 }
115
116 @Override
117 public void mouseMoved(final MouseEvent e) {
118 }
119 });
120
121 if( random ) {
122 window1.addGLEventListener(new GLEventListener() {
123 final Random r = new Random();
124
125 void warpRandom(final int width, final int height) {
126 final int x = r.nextInt(width);
127 final int y = r.nextInt(height);
128 window1.warpPointer(x, y);
129 }
130
131 @Override
132 public void init(final GLAutoDrawable drawable) {}
133
134 @Override
135 public void dispose(final GLAutoDrawable drawable) {}
136
137 @Override
138 public void display(final GLAutoDrawable drawable) {
139 warpRandom(drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
140 }
141
142 @Override
143 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {}
144
145 });
146 }
147
148 while(animator.isAnimating() && animator.getTotalFPSDuration()<durationPerTest) {
149 Thread.sleep(100);
150 }
151
152 destroyWindow(window1);
153 }
154
155 static int atoi(final String a) {
156 int i=0;
157 try {
158 i = Integer.parseInt(a);
159 } catch (final Exception ex) { ex.printStackTrace(); }
160 return i;
161 }
162
163 public static void main(final String args[]) throws IOException {
164 for(int i=0; i<args.length; i++) {
165 if(args[i].equals("-time")) {
166 durationPerTest = atoi(args[++i]);
167 }
168 }
169 System.out.println("durationPerTest: "+durationPerTest);
170 final String tstname = TestGLWindowWarpPointer01NEWT.class.getName();
171 org.junit.runner.JUnitCore.main(tstname);
172 }
173
174}
abstract AbstractGraphicsDevice getGraphicsDevice()
Return the AbstractGraphicsDevice used for depending resources lifecycle, i.e.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
abstract Display getDisplay()
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void warpPointer(final int x, final int y)
Moves the pointer to x/y relative to this window's origin in pixel units.
Definition: GLWindow.java:342
final void addMouseListener(final MouseListener l)
Appends the given MouseListener to the end of the list.
Definition: GLWindow.java:927
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 destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final void requestFocus()
Request focus for this native window.
Definition: GLWindow.java:416
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 String glAvailabilityToString(final AbstractGraphicsDevice device)
Definition: GLProfile.java:333
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
A interface describing a graphics device in a toolkit-independent manner.
String getConnection()
Returns the semantic GraphicsDevice connection.
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.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.