summaryrefslogtreecommitdiffstats
path: root/src/demos
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2009-03-20 14:29:27 +0000
committerSven Gothel <[email protected]>2009-03-20 14:29:27 +0000
commit9867704342ea36dfc5fb6662607a01513f3eb526 (patch)
tree09f29aa8a2fd10fe68816ae00327b601ce308b0b /src/demos
parent7d6a65e947395fcadd536a8b527888da305a9caa (diff)
Adding newt tests and a demos.util.TaskManager
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/jogl-demos/branches/JOGL_2_SANDBOX@326 3298f667-5e0e-4b4a-8ed4-a3559d26a5f4
Diffstat (limited to 'src/demos')
-rw-r--r--src/demos/newt/NEWTTest1.java94
-rw-r--r--src/demos/newt/TaskManagerTest1.java139
-rw-r--r--src/demos/util/TaskManager.java177
3 files changed, 410 insertions, 0 deletions
diff --git a/src/demos/newt/NEWTTest1.java b/src/demos/newt/NEWTTest1.java
new file mode 100644
index 0000000..08dd16f
--- /dev/null
+++ b/src/demos/newt/NEWTTest1.java
@@ -0,0 +1,94 @@
+package demos.newt;
+
+import javax.media.nativewindow.*;
+import com.sun.javafx.newt.*;
+
+public class NEWTTest1 implements WindowListener, KeyListener
+{
+ static
+ {
+ System.setProperty("java.awt.headless", "true");
+ }
+
+ public static void main(String[] args)
+ {
+ new NEWTTest1().run();
+ }
+
+ public void windowResized(WindowEvent e) {}
+ public void windowMoved(WindowEvent e) {}
+ public void windowDestroyNotify(WindowEvent e) {
+ // stop running ..
+ running = false;
+ }
+ boolean running = true;
+
+ public void keyPressed(KeyEvent e)
+ {
+ System.err.println("keyPressed "+e);
+ }
+ public void keyReleased(KeyEvent e)
+ {
+ System.err.println("keyReleased "+e);
+ }
+ public void keyTyped(KeyEvent e)
+ {
+ System.err.println("keyTyped "+e);
+ }
+
+ void render(long context)
+ {
+
+ }
+
+ void run()
+ {
+ try
+ {
+ Capabilities caps = new Capabilities();
+ caps.setRedBits(8);
+ caps.setGreenBits(8);
+ caps.setBlueBits(8);
+ //caps.setBackgroundOpaque(true);
+
+ Display display = NewtFactory.createDisplay(null);
+ Screen screen = NewtFactory.createScreen(display, 0);
+ Window window = NewtFactory.createWindow(screen, caps);
+ window.setTitle("GlassPrism");
+ window.setAutoDrawableClient(true);
+ window.setUndecorated(false);
+ window.setSize(256, 256);
+ window.addKeyListener(this);
+
+ // let's get notified if window is closed
+ window.addWindowListener(this);
+
+ window.setVisible(true);
+
+ while (running)
+ {
+ window.pumpMessages();
+
+ window.lockSurface();
+ try
+ {
+ render(window.getSurfaceHandle());
+ }
+ finally
+ {
+ window.unlockSurface();
+ }
+
+ Thread.yield();
+ // not necessary Thread.sleep(40);
+ }
+
+ window.destroy();
+ }
+ catch (Throwable t)
+ {
+ t.printStackTrace();
+ }
+ }
+}
+
diff --git a/src/demos/newt/TaskManagerTest1.java b/src/demos/newt/TaskManagerTest1.java
new file mode 100644
index 0000000..7aa801d
--- /dev/null
+++ b/src/demos/newt/TaskManagerTest1.java
@@ -0,0 +1,139 @@
+package demos.newt;
+
+import javax.media.nativewindow.*;
+import com.sun.javafx.newt.*;
+import demos.util.TaskManager;
+
+public class TaskManagerTest1 implements WindowListener, KeyListener
+{
+ final static TaskManager eventMgr;
+ final static TaskManager renderMgr;
+
+ static
+ {
+ System.setProperty("java.awt.headless", "true");
+
+ eventMgr = new TaskManager("Event Manager");
+ eventMgr.start();
+
+ renderMgr = new TaskManager("Render Manager");
+ renderMgr.start();
+ }
+
+ public static void main(String[] args)
+ {
+ new TaskManagerTest1().run();
+ }
+
+ Window window;
+
+ public void windowResized(WindowEvent e) {}
+ public void windowMoved(WindowEvent e) {}
+ public void windowDestroyNotify(WindowEvent e) {
+ System.err.println("Window Event Listener DestroyNotify send stop request - START");
+ renderMgr.stop();
+ eventMgr.stop();
+ System.err.println("Window Event Listener DestroyNotify send stop request - DONE");
+ }
+
+ public void keyPressed(KeyEvent e)
+ {
+ System.err.println("keyPressed "+e);
+ }
+ public void keyReleased(KeyEvent e)
+ {
+ System.err.println("keyReleased "+e);
+ }
+ public void keyTyped(KeyEvent e)
+ {
+ System.err.println("keyTyped "+e);
+ }
+
+ void render(long context)
+ {
+
+ }
+
+ private class EventThread implements Runnable {
+ public void run() {
+ try {
+ // prolog - lock whatever you need
+
+ // do it ..
+ if(null!=window) {
+ window.pumpMessages();
+ }
+ } catch (Throwable t) {
+ // handle errors ..
+ t.printStackTrace();
+ } finally {
+ // epilog - unlock locked stuff
+ }
+ }
+ }
+
+ private class RenderThread implements Runnable {
+ public void run() {
+ if(null==window) {
+ return;
+ }
+ try {
+ // prolog - lock whatever you need
+ window.lockSurface();
+
+ // render(window.getSurfaceHandle());
+ System.out.print(".");
+ Thread.sleep(100);
+ } catch (Throwable t) {
+ // handle errors ..
+ t.printStackTrace();
+ } finally {
+ // epilog - unlock locked stuff
+ window.unlockSurface();
+ }
+ }
+ }
+
+ void run()
+ {
+ try
+ {
+
+ Capabilities caps = new Capabilities();
+ caps.setRedBits(8);
+ caps.setGreenBits(8);
+ caps.setBlueBits(8);
+ //caps.setBackgroundOpaque(true);
+
+ Display display = NewtFactory.createDisplay(null);
+ Screen screen = NewtFactory.createScreen(display, 0);
+ window = NewtFactory.createWindow(screen, caps);
+ window.setTitle("GlassPrism");
+ window.setAutoDrawableClient(true);
+ window.setUndecorated(false);
+ window.setSize(256, 256);
+ window.addKeyListener(this);
+
+ // let's get notified if window is closed
+ window.addWindowListener(this);
+
+ window.setVisible(true);
+
+ eventMgr.addTask(new EventThread());
+ renderMgr.addTask(new RenderThread());
+
+ System.out.println("Main - wait until finished");
+ renderMgr.waitUntilStopped();
+ eventMgr.waitUntilStopped();
+ System.out.println("Main - finished");
+
+ window.destroy();
+ System.out.println("Main - window destroyed");
+ }
+ catch (Throwable t)
+ {
+ t.printStackTrace();
+ }
+ }
+}
+
diff --git a/src/demos/util/TaskManager.java b/src/demos/util/TaskManager.java
new file mode 100644
index 0000000..4c1cf9b
--- /dev/null
+++ b/src/demos/util/TaskManager.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * - Redistribution of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistribution in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any kind. ALL
+ * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
+ * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
+ * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
+ * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
+ * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
+ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
+ * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
+ * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
+ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that this software is not designed or intended for use
+ * in the design, construction, operation or maintenance of any nuclear
+ * facility.
+ *
+ * Sun gratefully acknowledges that this software was originally authored
+ * and developed by Kenneth Bradley Russell and Christopher John Kline.
+ */
+
+package demos.util;
+
+import java.util.*;
+
+public class TaskManager {
+ private ThreadGroup threadGroup;
+ private volatile boolean shouldStop = false;
+ private TaskWorker taskWorker = null;
+ private Object taskWorkerLock = new Object();
+ private ArrayList tasks = new ArrayList();
+ private String name;
+
+ public TaskManager(String name) {
+ threadGroup = new ThreadGroup(name);
+ this.name=name;
+ }
+
+ public String getName() { return name; }
+
+ public ThreadGroup getThreadGroup() { return threadGroup; }
+
+ public void start() {
+ synchronized(taskWorkerLock) {
+ if(null==taskWorker) {
+ taskWorker = new TaskWorker(name);
+ }
+ if(!taskWorker.isRunning()) {
+ taskWorker.start();
+ }
+ taskWorkerLock.notifyAll();
+ }
+ }
+
+ public void stop() {
+ synchronized(taskWorkerLock) {
+ if(null!=taskWorker && taskWorker.isRunning()) {
+ shouldStop = true;
+ }
+ taskWorkerLock.notifyAll();
+ }
+ }
+
+ public void addTask(Runnable task) {
+ if(task == null) {
+ return;
+ }
+ synchronized(taskWorkerLock) {
+ tasks.add(task);
+ taskWorkerLock.notifyAll();
+ }
+ }
+
+ public void removeTask(Runnable task) {
+ if (task == null) {
+ return;
+ }
+ synchronized(taskWorkerLock) {
+ tasks.remove(task);
+ taskWorkerLock.notifyAll();
+ }
+ }
+
+ public Runnable[] getTasks() {
+ Runnable[] res;
+ synchronized(taskWorkerLock) {
+ res = (Runnable[]) tasks.toArray();
+ }
+ return res;
+ }
+
+ public void waitUntilStopped() {
+ synchronized(taskWorkerLock) {
+ while(null!=taskWorker && taskWorker.isRunning()) {
+ try {
+ taskWorkerLock.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ class TaskWorker extends Thread {
+ volatile boolean isRunning = false;
+
+ public TaskWorker(String name) {
+ super(threadGroup, name);
+ }
+
+ public synchronized boolean isRunning() {
+ return isRunning;
+ }
+
+ public void run() {
+ synchronized(this) {
+ isRunning = true;
+ }
+ while(!shouldStop) {
+ try {
+ // prolog - lock stuff you may wanne do ..
+
+ // wait for something todo ..
+ synchronized(taskWorkerLock) {
+ while(!shouldStop && tasks.size()==0) {
+ try {
+ taskWorkerLock.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ // do it for all tasks,
+ // first make a safe clone of the list
+ // since it may change while working on it
+ ArrayList clonedTasks = (ArrayList) tasks.clone();
+ for(Iterator i = clonedTasks.iterator(); i.hasNext(); ) {
+ Runnable task = (Runnable) i.next();
+ task.run();
+ }
+ } catch (Throwable t) {
+ // handle errors ..
+ t.printStackTrace();
+ } finally {
+ // epilog - unlock locked stuff
+ }
+ }
+ synchronized(this) {
+ isRunning = false;
+ }
+ shouldStop=false;
+ synchronized(taskWorkerLock) {
+ taskWorkerLock.notifyAll();
+ }
+ }
+ }
+}
+