comparison game/Engine.java @ 21:df494a65bf8c

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Jan 2011 19:46:41 +0200
parents 4507a431b410
children afde253ec705
comparison
equal deleted inserted replaced
20:932582867c11 21:df494a65bf8c
16 import game.*; 16 import game.*;
17 17
18 import javax.sound.sampled.*; 18 import javax.sound.sampled.*;
19 19
20 20
21 enum Sound { 21 enum Sound
22 PIECE_PLACED("placed.wav"), 22 {
23 MUSIC_GAME("gamemusic.wav"); 23 PIECE_PLACED("placed.wav", true),
24 24
25 final String name; 25 MUSIC_GAME1("gamemusic.wav", false);
26
27
28 private final String name;
29 private final boolean effect;
30
31 Sound(String name, boolean effect)
32 {
33 this.name = name;
34 this.effect = effect;
35 }
36
37 public String getName()
38 {
39 return this.name;
40 }
41
42 public boolean isEffect()
43 {
44 return effect;
45 }
46 }
47
48
49 class SoundElement implements Runnable
50 {
51 private final String name;
26 private Clip clip; 52 private Clip clip;
27 53 private AudioInputStream stream;
28 Sound(String name) 54 private AudioFormat format;
29 { 55 private SourceDataLine line;
30 this.name = name; 56 private Thread playThread;
31 } 57 private int loopCount;
32 58 private boolean doPlay;
33 public boolean initialized() 59
34 { 60 SoundElement(String filename, boolean effect) throws IOException
35 return clip != null; 61 {
36 } 62 this.name = filename;
37 63
38 public static void load(String path) throws IOException 64 ResourceLoader res = new ResourceLoader(name);
39 { 65 if (res == null || res.getStream() == null)
40 for (Sound snd : Sound.values()) { 66 {
41 String filename = path + snd.name; 67 throw new IOException("Could not load audio resource '"+name+"'.\n");
42 ResourceLoader res = new ResourceLoader(filename); 68 }
43 if (res == null || res.getStream() == null) 69
70 try {
71 stream = AudioSystem.getAudioInputStream(res.getStream());
72 }
73 catch (UnsupportedAudioFileException e) {
74 throw new IOException("Unsupported audio file format for '"+name+"'.\n");
75 }
76 catch (IOException e)
77 {
78 throw new IOException("Could not load audio resource '"+name+"'.\n");
79 }
80
81 format = stream.getFormat();
82
83 if (effect) {
84 System.out.print("Loading '"+name+"' as a clip\n");
85 try {
86 clip = AudioSystem.getClip();
87 clip.open(stream);
88 }
89 catch (LineUnavailableException e)
44 { 90 {
45 throw new IOException("Could not load audio resource '"+filename+"'.\n"); 91 throw new IOException("Line unavailable for '"+name+"'.\n");
46 } 92 }
93 finally {
94 stream.close();
95 }
96 }
97 else
98 {
99 clip = null;
100 System.out.print("Loading '"+name+"' as stream\n");
101
47 try { 102 try {
48 AudioInputStream is = AudioSystem.getAudioInputStream(res.getStream()); 103 SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
49 } 104 System.out.print("info: "+stream.getFrameLength() + ", " + format.getFrameSize() + "\n");
50 catch (UnsupportedAudioFileException e) { 105 line = (SourceDataLine) AudioSystem.getLine(info);
51 throw new IOException("Unsupported audio file format for '"+filename+"'.\n"); 106 line.open(format);
52 } 107 }
53 catch (IOException e) 108 catch (LineUnavailableException e) {
109 throw new IOException("Line unavailable for '"+name+"'.\n");
110 }
111 }
112 }
113
114 public boolean isClip()
115 {
116 return (clip != null && line == null);
117 }
118
119 public void play()
120 {
121 System.out.print("Sound("+name+").play()\n");
122 if (isClip())
123 {
124 clip.setFramePosition(0);
125 clip.start();
126 }
127 else
128 {
129 if (playThread == null)
54 { 130 {
55 throw new IOException("Could not load audio resource '"+filename+"'.\n"); 131 doPlay = true;
56 } 132 loopCount = 1;
57 } 133 playThread = new Thread(this);
58 } 134 playThread.start();
59 135 }
60 public void play() 136 }
61 {
62 if (clip == null)
63 return;
64
65 clip.setFramePosition(0);
66 clip.start();
67 } 137 }
68 138
69 public void loop(int n) 139 public void loop(int n)
70 { 140 {
71 if (clip == null) 141 System.out.print("Sound("+name+").loop("+n+")\n");
72 return; 142 if (isClip())
73 143 {
74 if (n < 0) 144 clip.setFramePosition(0);
75 clip.loop(Clip.LOOP_CONTINUOUSLY); 145 if (n < 0)
76 else 146 clip.loop(Clip.LOOP_CONTINUOUSLY);
77 clip.loop(n); 147 else
148 clip.loop(n);
149 }
150 else
151 {
152 if (playThread == null)
153 {
154 doPlay = true;
155 loopCount = n;
156 playThread = new Thread(this);
157 playThread.start();
158 }
159 }
78 } 160 }
79 161
80 public void stop() 162 public void stop()
81 { 163 {
82 if (clip == null) 164 if (isClip())
83 return; 165 {
84 166 if (clip.isRunning())
85 if (clip.isRunning()) 167 clip.stop();
86 clip.stop(); 168 }
169 else
170 {
171 if (playThread != null)
172 {
173 playThread.interrupt();
174 doPlay = false;
175 playThread = null;
176 }
177 }
87 } 178 }
88 179
89 public boolean isPlaying() 180 public boolean isPlaying()
90 { 181 {
91 if (clip == null) 182 if (isClip())
92 return false; 183 return clip.isRunning();
93 184 else
94 return clip.isRunning(); 185 return (playThread != null && doPlay);
186 }
187
188 public void run()
189 {
190 line.start();
191 byte[] buf = new byte[line.getBufferSize()];
192
193 while (doPlay && (loopCount > 0 || loopCount == -1))
194 {
195 try {
196 int numRead = 0;
197 while ((numRead = stream.read(buf, 0, buf.length)) >= 0 && doPlay)
198 {
199 int offset = 0;
200 while (offset < numRead)
201 {
202 System.out.print("audioThread: offs="+offset+", numread="+numRead+"\n");
203 offset += line.write(buf, offset, numRead - offset);
204 }
205 }
206 line.drain();
207
208 System.out.print("audioThread: stream.reset()\n");
209 stream.reset();
210 }
211 catch (IOException e) {
212 }
213
214 if (loopCount > 0)
215 loopCount--;
216 }
217
218 line.stop();
219 doPlay = false;
95 } 220 }
96 } 221 }
222
97 223
98 class PathInfo 224 class PathInfo
99 { 225 {
100 public int in, inX, inY, out, outX, outY; 226 public int in, inX, inY, out, outX, outY;
101 227
277 boolean animEnable = false; 403 boolean animEnable = false;
278 GameBoard lauta = null; 404 GameBoard lauta = null;
279 BufferedImage lautaBG = null, lautaBGScaled = null; 405 BufferedImage lautaBG = null, lautaBGScaled = null;
280 Dimension oldDim; 406 Dimension oldDim;
281 float clock; 407 float clock;
282 Sound sounds; 408 SoundElement[] sounds;
409
410 public SoundElement snd(Sound snd)
411 {
412 return sounds[snd.ordinal()];
413 }
283 414
284 public Engine() 415 public Engine()
285 { 416 {
286 BufferedImage img; 417 BufferedImage img;
287 clock = 0; 418 clock = 0;
290 421
291 try 422 try
292 { 423 {
293 ResourceLoader res = new ResourceLoader("graphics/board.png"); 424 ResourceLoader res = new ResourceLoader("graphics/board.png");
294 lautaBG = ImageIO.read(res.getStream()); 425 lautaBG = ImageIO.read(res.getStream());
295 sounds.load("sounds/"); 426
427 sounds = new SoundElement[16];
428 for (Sound s : Sound.values())
429 {
430 System.out.print(s +" = "+ s.ordinal() +"\n");
431 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isEffect());
432 }
296 } 433 }
297 catch (IOException e) 434 catch (IOException e)
298 { 435 {
299 /* 436 /*
300 JOptionPane.showMessageDialog(null, 437 JOptionPane.showMessageDialog(null,
301 "Could not load some of the resources.", 438 e.getMessage(),
302 "Initialization error", 439 "Initialization error",
303 JOptionPane.ERROR_MESSAGE); 440 JOptionPane.ERROR_MESSAGE);
304 */ 441 */
305 System.out.print("Could not load some of the resources.\n"); 442 System.out.print(e.getMessage());
306 } 443 }
307 444
308 lauta = new GameBoard(); 445 lauta = new GameBoard();
309 addKeyListener(this); 446 addKeyListener(this);
310 addMouseListener(this); 447 addMouseListener(this);
314 { 451 {
315 System.out.print("Engine(): requesting focus\n"); 452 System.out.print("Engine(): requesting focus\n");
316 requestFocus(); 453 requestFocus();
317 } 454 }
318 455
319 sounds.MUSIC_GAME.loop(-1); 456 snd(Sound.MUSIC_GAME1).loop(-1);
457 // snd(Sound.PIECE_PLACED).loop(-1);
320 } 458 }
321 459
322 public void startThreads() 460 public void startThreads()
323 { 461 {
324 System.out.print("startThreads()\n"); 462 System.out.print("startThreads()\n");
393 { 531 {
394 } 532 }
395 533
396 public void keyPressed(KeyEvent e) 534 public void keyPressed(KeyEvent e)
397 { 535 {
398 System.out.print("lol\n"); 536 System.out.print("running "+ snd(Sound.MUSIC_GAME1) + "\n");
399 switch (e.getKeyCode()) 537 switch (e.getKeyCode())
400 { 538 {
401 case KeyEvent.VK_LEFT: 539 case KeyEvent.VK_LEFT:
402 case KeyEvent.VK_UP: 540 case KeyEvent.VK_UP:
403 lauta.pieceRotate(false); 541 lauta.pieceRotate(false);
408 lauta.pieceRotate(true); 546 lauta.pieceRotate(true);
409 break; 547 break;
410 548
411 case KeyEvent.VK_ENTER: 549 case KeyEvent.VK_ENTER:
412 lauta.pieceFinishTurn(); 550 lauta.pieceFinishTurn();
551 snd(Sound.PIECE_PLACED).stop();
552 snd(Sound.PIECE_PLACED).play();
413 break; 553 break;
414 } 554 }
415 } 555 }
416 556
417 public void run() 557 public void run()