28package com.jogamp.opengl.demos.util;
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;
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;
51 public static boolean atob(
final String str,
final boolean def) {
53 return Boolean.parseBoolean(str);
54 }
catch (
final Exception ex) {
60 public static int atoi(
final String str,
final int def) {
62 return Integer.parseInt(str);
63 }
catch (
final Exception ex) {
69 public static long atol(
final String str,
final long def) {
71 return Long.parseLong(str);
72 }
catch (
final Exception ex) {
78 public static float atof(
final String str,
final float def) {
80 return Float.parseFloat(str);
81 }
catch (
final Exception ex) {
88 return "0x" + Integer.toHexString( hex & 0x000000FF );
92 return "0x" + Integer.toHexString( hex & 0x0000FFFF );
96 return "0x" + Integer.toHexString( hex );
100 return "0x" + Long.toHexString( hex );
103 public static void assertFloatBufferEquals(
final String errmsg,
final FloatBuffer expected,
final FloatBuffer actual,
final float delta) {
104 if(
null == expected &&
null == actual) {
107 final String msg =
null != errmsg ? errmsg +
" " :
"";
108 if(
null == expected) {
109 throw new AssertionError(msg+
"; Expected is null, but actual not: "+actual);
112 throw new AssertionError(msg+
"; Actual is null, but expected not: "+expected);
114 if(expected.remaining() != actual.remaining()) {
115 throw new AssertionError(msg+
"; Expected has "+expected.remaining()+
" remaining, but actual has "+actual.remaining());
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);
129 public static void assertFloatBufferNotEqual(
final String errmsg,
final FloatBuffer expected,
final FloatBuffer actual,
final float delta) {
130 if(
null == expected ||
null == actual) {
133 if(expected.remaining() != actual.remaining()) {
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 ) {
147 throw new AssertionError(msg+
"; Expected and actual are equal.");
150 public static boolean setFieldIfExists(
final Object instance,
final String fieldName,
final Object value) {
152 final Field f = instance.getClass().getField(fieldName);
153 if(value instanceof Boolean || f.getType().isInstance(value)) {
154 f.set(instance, value);
157 System.out.println(instance.getClass()+
" '"+fieldName+
"' field not assignable with "+value.getClass()+
", it's a: "+f.getType());
159 }
catch (
final IllegalAccessException ex) {
160 throw new RuntimeException(ex);
161 }
catch (
final NoSuchFieldException nsfe) {
168 final BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
169 System.err.println(preMessage+
"> Press enter to continue");
171 System.err.println(stdin.readLine());
172 }
catch (
final IOException e) { e.printStackTrace(); }
177 return FontFactory.
get(IOUtil.getResource(
"fonts/freefont/FreeSerif.ttf",
179 }
catch(
final IOException ioe) {
180 ioe.printStackTrace();
185 public static class StreamDump extends InterruptSource.Thread {
186 final InputStream is;
187 final StringBuilder outString;
188 final OutputStream outStream;
191 volatile boolean eos =
false;
193 public StreamDump(
final OutputStream out,
final String prefix,
final InputStream is,
final Object sync) {
195 this.outString =
null;
196 this.outStream = out;
197 this.prefix = prefix;
200 public StreamDump(
final StringBuilder sb,
final String prefix,
final InputStream is,
final Object sync) {
203 this.outStream =
null;
204 this.prefix = prefix;
207 public StreamDump(
final StringBuilder sb,
final InputStream is,
final Object sync) {
210 this.outStream =
null;
215 public final boolean eos() {
return eos; }
219 synchronized ( sync ) {
221 final BufferedReader in =
new BufferedReader(
new InputStreamReader(is) );
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());
230 outStream.write(line.getBytes());
231 outStream.write(Platform.getNewline().getBytes());
235 }
catch (
final IOException ioe) {
236 System.err.println(
"Caught "+ioe.getClass().getName()+
": "+ioe.getMessage());
237 ioe.printStackTrace();
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)+
")");
253 for (
final Iterator<GLContext> iter =
set.iterator(); iter.hasNext(); ) {
255 System.err.println(
" Created Ctx #"+(i++)+
": hash 0x"+Integer.toHexString(c.hashCode())+
", \t(created "+c.
isCreated()+
")");
260 for (
final Iterator<GLContext> iter =
set.iterator(); iter.hasNext(); ) {
262 System.err.println(
" Destroyed Ctx #"+(j++)+
": hash 0x"+Integer.toHexString(c.hashCode())+
", \t(created "+c.
isCreated()+
")");
265 System.err.println(
"\t Total created "+i+
" + destroyed "+j+
" = "+(i+j));
266 System.err.println();
272 System.err.println(
"Destroying window from thread "+Thread.currentThread());
276 if(
null != animator ) {
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.
final boolean isCreated()
Indicates whether the underlying native OpenGL context has been created.
final GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
final List< GLContext > getDestroyedShares()
Returns a new list of destroyed GLContext shared with this GLContext.
final List< GLContext > getCreatedShares()
Returns a new list of created GLContext shared with this GLContext.
StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync)
StreamDump(final StringBuilder sb, final InputStream is, final Object sync)
StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync)
static String toHexString(final short hex)
static void waitForKey(final String preMessage)
static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
static String toHexString(final int hex)
static void dumpSharedGLContext(final String prefix, final GLContext self)
static int atoi(final String str, final int def)
static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
static Font getSerifFont()
static boolean atob(final String str, final boolean def)
static String toHexString(final byte hex)
static void destroyWindow(final GLAutoDrawable glad)
static float atof(final String str, final float def)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
static String toHexString(final long hex)
static long atol(final String str, final long def)
Interface wrapper for font implementation.
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.