JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
MiscUtils.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 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 */
28package com.jogamp.opengl.demos.util;
29
30import java.io.BufferedReader;
31import java.io.IOException;
32import java.io.InputStream;
33import java.io.InputStreamReader;
34import java.io.OutputStream;
35import java.lang.reflect.*;
36import java.nio.FloatBuffer;
37import java.util.Iterator;
38import java.util.List;
39
40import com.jogamp.opengl.GLAnimatorControl;
41import com.jogamp.opengl.GLAutoDrawable;
42import com.jogamp.opengl.GLContext;
43import com.jogamp.opengl.demos.graph.FontSetDemos;
44import com.jogamp.common.os.Platform;
45import com.jogamp.common.util.IOUtil;
46import com.jogamp.common.util.InterruptSource;
47import com.jogamp.graph.font.Font;
48import com.jogamp.graph.font.FontFactory;
49
50public class MiscUtils {
51 public static boolean atob(final String str, final boolean def) {
52 try {
53 return Boolean.parseBoolean(str);
54 } catch (final Exception ex) {
55 ex.printStackTrace();
56 }
57 return def;
58 }
59
60 public static int atoi(final String str, final int def) {
61 try {
62 return Integer.parseInt(str);
63 } catch (final Exception ex) {
64 ex.printStackTrace();
65 }
66 return def;
67 }
68
69 public static long atol(final String str, final long def) {
70 try {
71 return Long.parseLong(str);
72 } catch (final Exception ex) {
73 ex.printStackTrace();
74 }
75 return def;
76 }
77
78 public static float atof(final String str, final float def) {
79 try {
80 return Float.parseFloat(str);
81 } catch (final Exception ex) {
82 ex.printStackTrace();
83 }
84 return def;
85 }
86
87 public static String toHexString(final byte hex) {
88 return "0x" + Integer.toHexString( hex & 0x000000FF );
89 }
90
91 public static String toHexString(final short hex) {
92 return "0x" + Integer.toHexString( hex & 0x0000FFFF );
93 }
94
95 public static String toHexString(final int hex) {
96 return "0x" + Integer.toHexString( hex );
97 }
98
99 public static String toHexString(final long hex) {
100 return "0x" + Long.toHexString( hex );
101 }
102
103 public static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta) {
104 if(null == expected && null == actual) {
105 return;
106 }
107 final String msg = null != errmsg ? errmsg + " " : "";
108 if(null == expected) {
109 throw new AssertionError(msg+"; Expected is null, but actual not: "+actual);
110 }
111 if(null == actual) {
112 throw new AssertionError(msg+"; Actual is null, but expected not: "+expected);
113 }
114 if(expected.remaining() != actual.remaining()) {
115 throw new AssertionError(msg+"; Expected has "+expected.remaining()+" remaining, but actual has "+actual.remaining());
116 }
117 final int a0 = expected.position();
118 final int b0 = actual.position();
119 for(int i=0; i<expected.remaining(); i++) {
120 final float ai = expected.get(a0 + i);
121 final float bi = actual.get(b0 + i);
122 final float daibi = Math.abs(ai - bi);
123 if( daibi > delta ) {
124 throw new AssertionError(msg+"; Expected @ ["+a0+"+"+i+"] has "+ai+", but actual @ ["+b0+"+"+i+"] has "+bi+", it's delta "+daibi+" > "+delta);
125 }
126 }
127 }
128
129 public static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta) {
130 if(null == expected || null == actual) {
131 return;
132 }
133 if(expected.remaining() != actual.remaining()) {
134 return;
135 }
136 final String msg = null != errmsg ? errmsg + " " : "";
137 final int a0 = expected.position();
138 final int b0 = actual.position();
139 for(int i=0; i<expected.remaining(); i++) {
140 final float ai = expected.get(a0 + i);
141 final float bi = actual.get(b0 + i);
142 final float daibi = Math.abs(ai - bi);
143 if( daibi > delta ) {
144 return;
145 }
146 }
147 throw new AssertionError(msg+"; Expected and actual are equal.");
148 }
149
150 public static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value) {
151 try {
152 final Field f = instance.getClass().getField(fieldName);
153 if(value instanceof Boolean || f.getType().isInstance(value)) {
154 f.set(instance, value);
155 return true;
156 } else {
157 System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
158 }
159 } catch (final IllegalAccessException ex) {
160 throw new RuntimeException(ex);
161 } catch (final NoSuchFieldException nsfe) {
162 // OK - throw new RuntimeException(instance.getClass()+" has no '"+fieldName+"' field", nsfe);
163 }
164 return false;
165 }
166
167 public static void waitForKey(final String preMessage) {
168 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
169 System.err.println(preMessage+"> Press enter to continue");
170 try {
171 System.err.println(stdin.readLine());
172 } catch (final IOException e) { e.printStackTrace(); }
173 }
174
175 public static Font getSerifFont() {
176 try {
177 return FontFactory.get(IOUtil.getResource("fonts/freefont/FreeSerif.ttf",
178 FontSetDemos.class.getClassLoader(), FontSetDemos.class).getInputStream(), true);
179 } catch(final IOException ioe) {
180 ioe.printStackTrace();
181 return null;
182 }
183 }
184
185 public static class StreamDump extends InterruptSource.Thread {
186 final InputStream is;
187 final StringBuilder outString;
188 final OutputStream outStream;
189 final String prefix;
190 final Object sync;
191 volatile boolean eos = false;
192
193 public StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync) {
194 this.is = is;
195 this.outString = null;
196 this.outStream = out;
197 this.prefix = prefix;
198 this.sync = sync;
199 }
200 public StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync) {
201 this.is = is;
202 this.outString = sb;
203 this.outStream = null;
204 this.prefix = prefix;
205 this.sync = sync;
206 }
207 public StreamDump(final StringBuilder sb, final InputStream is, final Object sync) {
208 this.is = is;
209 this.outString = sb;
210 this.outStream = null;
211 this.prefix = null;
212 this.sync = sync;
213 }
214
215 public final boolean eos() { return eos; }
216
217 @Override
218 public void run() {
219 synchronized ( sync ) {
220 try {
221 final BufferedReader in = new BufferedReader( new InputStreamReader(is) );
222 String line = null;
223 while ((line = in.readLine()) != null) {
224 if( null != outString ) {
225 outString.append(line).append(Platform.getNewline());
226 } else if( null != outStream ) {
227 if( null != prefix ) {
228 outStream.write(prefix.getBytes());
229 }
230 outStream.write(line.getBytes());
231 outStream.write(Platform.getNewline().getBytes());
232 outStream.flush();
233 }
234 }
235 } catch (final IOException ioe) {
236 System.err.println("Caught "+ioe.getClass().getName()+": "+ioe.getMessage());
237 ioe.printStackTrace();
238 } finally {
239 eos = true;
240 sync.notifyAll();
241 }
242 }
243 }
244 }
245
246 public static void dumpSharedGLContext(final String prefix, final GLContext self) {
247 int i = 0, j = 0;
248 final GLContext master = self.getSharedMaster();
249 final int masterHash = null != master ? master.hashCode() : 0;
250 System.err.println(prefix+": hash 0x"+Integer.toHexString(self.hashCode())+", \t(isShared "+self.isShared()+", created "+self.isCreated()+", master 0x"+Integer.toHexString(masterHash)+")");
251 {
252 final List<GLContext> set = self.getCreatedShares();
253 for (final Iterator<GLContext> iter = set.iterator(); iter.hasNext(); ) {
254 final GLContext c = iter.next();
255 System.err.println(" Created Ctx #"+(i++)+": hash 0x"+Integer.toHexString(c.hashCode())+", \t(created "+c.isCreated()+")");
256 }
257 }
258 {
259 final List<GLContext> set = self.getDestroyedShares();
260 for (final Iterator<GLContext> iter = set.iterator(); iter.hasNext(); ) {
261 final GLContext c = iter.next();
262 System.err.println(" Destroyed Ctx #"+(j++)+": hash 0x"+Integer.toHexString(c.hashCode())+", \t(created "+c.isCreated()+")");
263 }
264 }
265 System.err.println("\t Total created "+i+" + destroyed "+j+" = "+(i+j));
266 System.err.println();
267 }
268
269 public static void destroyWindow(final GLAutoDrawable glad) {
270 if( glad.isRealized() ) {
271 glad.setExclusiveContextThread(null);
272 System.err.println("Destroying window from thread "+Thread.currentThread());
273 // Thread.dumpStack();
274 glad.destroy();
275 final GLAnimatorControl animator = glad.getAnimator();
276 if( null != animator ) {
277 animator.stop(); // Avoiding a ThreadDeath of animator at shutdown
278 }
279 }
280 }
281}
282
283
284
The optional property jogamp.graph.font.ctor allows user to specify the FontConstructor implementatio...
static final FontSet get(final int font)
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
final boolean isCreated()
Indicates whether the underlying native OpenGL context has been created.
Definition: GLContext.java:604
final GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
Definition: GLContext.java:272
final List< GLContext > getDestroyedShares()
Returns a new list of destroyed GLContext shared with this GLContext.
Definition: GLContext.java:282
final List< GLContext > getCreatedShares()
Returns a new list of created GLContext shared with this GLContext.
Definition: GLContext.java:277
StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync)
Definition: MiscUtils.java:193
StreamDump(final StringBuilder sb, final InputStream is, final Object sync)
Definition: MiscUtils.java:207
StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync)
Definition: MiscUtils.java:200
static String toHexString(final short hex)
Definition: MiscUtils.java:91
static void waitForKey(final String preMessage)
Definition: MiscUtils.java:167
static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
Definition: MiscUtils.java:103
static String toHexString(final int hex)
Definition: MiscUtils.java:95
static void dumpSharedGLContext(final String prefix, final GLContext self)
Definition: MiscUtils.java:246
static int atoi(final String str, final int def)
Definition: MiscUtils.java:60
static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
Definition: MiscUtils.java:129
static boolean atob(final String str, final boolean def)
Definition: MiscUtils.java:51
static String toHexString(final byte hex)
Definition: MiscUtils.java:87
static void destroyWindow(final GLAutoDrawable glad)
Definition: MiscUtils.java:269
static float atof(final String str, final float def)
Definition: MiscUtils.java:78
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:150
static String toHexString(final long hex)
Definition: MiscUtils.java:99
static long atol(final String str, final long def)
Definition: MiscUtils.java:69
Interface wrapper for font implementation.
Definition: Font.java:60
An animator control interface, which implementation may drive a com.jogamp.opengl....
boolean stop()
Stops this animator.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GLAnimatorControl getAnimator()
Thread setExclusiveContextThread(Thread t)
Dedicates this instance's GLContext to the given thread.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
boolean isRealized()
Returns true if this drawable is realized, otherwise false.