diff options
Diffstat (limited to 'src')
4 files changed, 197 insertions, 69 deletions
diff --git a/src/java/demos/devmaster/lesson1/SingleStaticSource.java b/src/java/demos/devmaster/lesson1/SingleStaticSource.java index 4c4c945..eb15b26 100755 --- a/src/java/demos/devmaster/lesson1/SingleStaticSource.java +++ b/src/java/demos/devmaster/lesson1/SingleStaticSource.java @@ -40,6 +40,11 @@ import java.nio.ByteBuffer; import net.java.games.joal.*; import net.java.games.joal.util.*; +// For the GUI +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + /** * Adapted from <a href="http://www.devmaster.net/">DevMaster</a> * <a href="http://www.devmaster.net/articles/openal-tutorials/lesson1.php">SingleStaticSource Tutorial</a> @@ -74,6 +79,31 @@ public class SingleStaticSource { // Orientation of the listener. (first 3 elems are "at", second 3 are "up") static float[] listenerOri = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }; + static boolean initialized = false; + static boolean initialize() { + if (initialized) { + return true; + } + + // Initialize OpenAL and clear the error bit. + try { + ALut.alutInit(); + al = ALFactory.getAL(); + al.alGetError(); + } catch (ALException e) { + e.printStackTrace(); + return false; + } + // Load the wav data. + if (loadALData() == AL.AL_FALSE) + return false; + + setListenerValues(); + + initialized = true; + return true; + } + static int loadALData() { // variables to load into @@ -135,50 +165,92 @@ public class SingleStaticSource { } public static void main(String[] args) { - // Initialize OpenAL and clear the error bit. - try { - ALut.alutInit(); - al = ALFactory.getAL(); - al.alGetError(); - } catch (ALException e) { - e.printStackTrace(); - return; + boolean gui = false; + + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-gui")) + gui = true; } - // Load the wav data. - if (loadALData() == AL.AL_FALSE) - System.exit(1); - setListenerValues(); + if (gui) { + JFrame frame = new JFrame("Single Static Source - DevMaster OpenAL Lesson 1"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new GridLayout(4, 1)); + JButton button = new JButton("Play sound"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (!initialize()) + System.exit(1); + al.alSourcePlay(source[0]); + } + }); + frame.getContentPane().add(button); + button = new JButton("Stop playing"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (!initialize()) + System.exit(1); + al.alSourceStop(source[0]); + } + }); + frame.getContentPane().add(button); + button = new JButton("Pause sound"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (!initialize()) + System.exit(1); + al.alSourcePause(source[0]); + } + }); + frame.getContentPane().add(button); + button = new JButton("Quit"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (!initialize()) + System.exit(1); + killAllData(); + System.exit(0); + } + }); + frame.getContentPane().add(button); + frame.pack(); + frame.setVisible(true); + } else { + // Initialize OpenAL and clear the error bit. + if (!initialize()) { + System.exit(1); + } - char[] c = new char[1]; - while (c[0] != 'q') { - try { - BufferedReader buf = - new BufferedReader(new InputStreamReader(System.in)); - System.out.println( - "Press a key and hit ENTER: \n" - + "'p' to play, 's' to stop, " + - "'h' to pause and 'q' to quit"); - buf.read(c); - switch (c[0]) { - case 'p' : - // Pressing 'p' will begin playing the sample. - al.alSourcePlay(source[0]); - break; - case 's' : - // Pressing 's' will stop the sample from playing. - al.alSourceStop(source[0]); - break; - case 'h' : - // Pressing 'n' will pause (hold) the sample. - al.alSourcePause(source[0]); - break; - case 'q' : - killAllData(); - break; + char[] c = new char[1]; + while (c[0] != 'q') { + try { + BufferedReader buf = + new BufferedReader(new InputStreamReader(System.in)); + System.out.println( + "Press a key and hit ENTER: \n" + + "'p' to play, 's' to stop, " + + "'h' to pause and 'q' to quit"); + buf.read(c); + switch (c[0]) { + case 'p' : + // Pressing 'p' will begin playing the sample. + al.alSourcePlay(source[0]); + break; + case 's' : + // Pressing 's' will stop the sample from playing. + al.alSourceStop(source[0]); + break; + case 'h' : + // Pressing 'n' will pause (hold) the sample. + al.alSourcePause(source[0]); + break; + case 'q' : + killAllData(); + break; + } + } catch (IOException e) { + System.exit(1); } - } catch (IOException e) { - System.exit(1); } } } diff --git a/src/java/demos/devmaster/lesson2/LoopingAndFadeaway.java b/src/java/demos/devmaster/lesson2/LoopingAndFadeaway.java index 5095481..115be24 100755 --- a/src/java/demos/devmaster/lesson2/LoopingAndFadeaway.java +++ b/src/java/demos/devmaster/lesson2/LoopingAndFadeaway.java @@ -144,5 +144,6 @@ public class LoopingAndFadeaway { ticker += System.currentTimeMillis() - lastTime; lastTime = System.currentTimeMillis(); } + System.exit(0); } } diff --git a/src/java/demos/devmaster/lesson3/MultipleSources.java b/src/java/demos/devmaster/lesson3/MultipleSources.java index fbe13f8..508844b 100755 --- a/src/java/demos/devmaster/lesson3/MultipleSources.java +++ b/src/java/demos/devmaster/lesson3/MultipleSources.java @@ -207,5 +207,6 @@ public class MultipleSources { } } killAllData(); + System.exit(0); } } diff --git a/src/java/demos/devmaster/lesson5/SourcesSharingBuffers.java b/src/java/demos/devmaster/lesson5/SourcesSharingBuffers.java index 091f8b3..6e3e3a9 100755 --- a/src/java/demos/devmaster/lesson5/SourcesSharingBuffers.java +++ b/src/java/demos/devmaster/lesson5/SourcesSharingBuffers.java @@ -38,6 +38,12 @@ import java.io.*; import java.nio.*; import java.util.*; +// For the gui +import java.awt.GridLayout; +import java.awt.event.*; +import javax.swing.*; + + import net.java.games.joal.*; import net.java.games.joal.util.*; @@ -224,45 +230,93 @@ public class SourcesSharingBuffers { exitOpenAL(); } - public static void main(String[] args) { + static boolean initialized = false; + static void initialize() { + if (initialized) + return; + initialized = true; try { initOpenAL(); } catch (ALException e) { e.printStackTrace(); System.exit(1); } - if (loadALData() == AL.AL_FALSE) { + if (loadALData() == AL.AL_FALSE) System.exit(1); - } setListenerValues(); - char[] c = new char[1]; - while(c[0] != 'q') { - try { - BufferedReader buf = - new BufferedReader(new InputStreamReader(System.in)); - System.out.println("Press a key and hit ENTER: \n" + - "\t'w' for Water Drop\n" + - "\t't' for Thunder\n" + - "\t's' for Stream\n" + - "\t'r' for Rain\n" + - "\t'o' for Ocean\n" + - "\t'c' for Chimes\n" + - "\n'q' to Quit\n"); - - buf.read(c); - switch(c[0]) { - case 'w': addSource(WATERDROP); break; - case 't': addSource(THUNDER); break; - case 's': addSource(STREAM); break; - case 'r': addSource(RAIN); break; - case 'o': addSource(OCEAN); break; - case 'c': addSource(CHIMES); break; + } + + private static void addButton(JFrame frame, String text, final int whichSound) { + JButton button = new JButton(text); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + initialize(); + addSource(whichSound); + } + }); + frame.getContentPane().add(button); + } + + public static void main(String[] args) { + boolean gui = false; + + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-gui")) + gui = true; + } + + if (gui) { + JFrame frame = new JFrame("Sources Sharing Buffers - DevMaster OpenAL Lesson 5"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new GridLayout(7, 1)); + addButton(frame, "Add Water Drop", WATERDROP); + addButton(frame, "Add Thunder", THUNDER); + addButton(frame, "Add Stream", STREAM); + addButton(frame, "Add Rain", RAIN); + addButton(frame, "Add Ocean", OCEAN); + addButton(frame, "Add Chimes", CHIMES); + + JButton button = new JButton("Quit"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + frame.getContentPane().add(button); + + frame.pack(); + frame.setVisible(true); + } else { + initialize(); + char[] c = new char[1]; + while(c[0] != 'q') { + try { + BufferedReader buf = + new BufferedReader(new InputStreamReader(System.in)); + System.out.println("Press a key and hit ENTER: \n" + + "\t'w' for Water Drop\n" + + "\t't' for Thunder\n" + + "\t's' for Stream\n" + + "\t'r' for Rain\n" + + "\t'o' for Ocean\n" + + "\t'c' for Chimes\n" + + "\n'q' to Quit\n"); + + buf.read(c); + switch(c[0]) { + case 'w': addSource(WATERDROP); break; + case 't': addSource(THUNDER); break; + case 's': addSource(STREAM); break; + case 'r': addSource(RAIN); break; + case 'o': addSource(OCEAN); break; + case 'c': addSource(CHIMES); break; + } + } catch (IOException e) { + killALData(); + System.exit(1); } - } catch (IOException e) { - killALData(); - System.exit(1); } + killALData(); } - killALData(); } } |