comparison game/Engine.java @ 22:afde253ec705

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Jan 2011 21:28:59 +0200
parents df494a65bf8c
children 0741dc117808
comparison
equal deleted inserted replaced
21:df494a65bf8c 22:afde253ec705
13 import javax.swing.*; 13 import javax.swing.*;
14 import java.util.*; 14 import java.util.*;
15 import java.io.*; 15 import java.io.*;
16 import game.*; 16 import game.*;
17 17
18 import javax.sound.sampled.*;
19
20
21 enum Sound
22 {
23 PIECE_PLACED("placed.wav", true),
24
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;
52 private Clip clip;
53 private AudioInputStream stream;
54 private AudioFormat format;
55 private SourceDataLine line;
56 private Thread playThread;
57 private int loopCount;
58 private boolean doPlay;
59
60 SoundElement(String filename, boolean effect) throws IOException
61 {
62 this.name = filename;
63
64 ResourceLoader res = new ResourceLoader(name);
65 if (res == null || res.getStream() == null)
66 {
67 throw new IOException("Could not load audio resource '"+name+"'.\n");
68 }
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)
90 {
91 throw new IOException("Line unavailable for '"+name+"'.\n");
92 }
93 finally {
94 stream.close();
95 }
96 }
97 else
98 {
99 clip = null;
100 System.out.print("Loading '"+name+"' as stream\n");
101
102 try {
103 SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
104 System.out.print("info: "+stream.getFrameLength() + ", " + format.getFrameSize() + "\n");
105 line = (SourceDataLine) AudioSystem.getLine(info);
106 line.open(format);
107 }
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)
130 {
131 doPlay = true;
132 loopCount = 1;
133 playThread = new Thread(this);
134 playThread.start();
135 }
136 }
137 }
138
139 public void loop(int n)
140 {
141 System.out.print("Sound("+name+").loop("+n+")\n");
142 if (isClip())
143 {
144 clip.setFramePosition(0);
145 if (n < 0)
146 clip.loop(Clip.LOOP_CONTINUOUSLY);
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 }
160 }
161
162 public void stop()
163 {
164 if (isClip())
165 {
166 if (clip.isRunning())
167 clip.stop();
168 }
169 else
170 {
171 if (playThread != null)
172 {
173 playThread.interrupt();
174 doPlay = false;
175 playThread = null;
176 }
177 }
178 }
179
180 public boolean isPlaying()
181 {
182 if (isClip())
183 return clip.isRunning();
184 else
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;
220 }
221 }
222
223 18
224 class PathInfo 19 class PathInfo
225 { 20 {
226 public int in, inX, inY, out, outX, outY; 21 public int in, inX, inY, out, outX, outY;
227 22
403 boolean animEnable = false; 198 boolean animEnable = false;
404 GameBoard lauta = null; 199 GameBoard lauta = null;
405 BufferedImage lautaBG = null, lautaBGScaled = null; 200 BufferedImage lautaBG = null, lautaBGScaled = null;
406 Dimension oldDim; 201 Dimension oldDim;
407 float clock; 202 float clock;
203
204 /*
408 SoundElement[] sounds; 205 SoundElement[] sounds;
409
410 public SoundElement snd(Sound snd) 206 public SoundElement snd(Sound snd)
411 { 207 {
412 return sounds[snd.ordinal()]; 208 return sounds[snd.ordinal()];
413 } 209 }
210 */
414 211
415 public Engine() 212 public Engine()
416 { 213 {
417 BufferedImage img; 214 BufferedImage img;
418 clock = 0; 215 clock = 0;
422 try 219 try
423 { 220 {
424 ResourceLoader res = new ResourceLoader("graphics/board.png"); 221 ResourceLoader res = new ResourceLoader("graphics/board.png");
425 lautaBG = ImageIO.read(res.getStream()); 222 lautaBG = ImageIO.read(res.getStream());
426 223
224 /*
427 sounds = new SoundElement[16]; 225 sounds = new SoundElement[16];
428 for (Sound s : Sound.values()) 226 for (Sound s : Sound.values())
429 { 227 {
430 System.out.print(s +" = "+ s.ordinal() +"\n"); 228 System.out.print(s +" = "+ s.ordinal() +"\n");
431 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isEffect()); 229 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isEffect());
432 } 230 }
231 */
433 } 232 }
434 catch (IOException e) 233 catch (IOException e)
435 { 234 {
436 /* 235 /*
437 JOptionPane.showMessageDialog(null, 236 JOptionPane.showMessageDialog(null,
450 if (!hasFocus()) 249 if (!hasFocus())
451 { 250 {
452 System.out.print("Engine(): requesting focus\n"); 251 System.out.print("Engine(): requesting focus\n");
453 requestFocus(); 252 requestFocus();
454 } 253 }
455 254
255 /*
456 snd(Sound.MUSIC_GAME1).loop(-1); 256 snd(Sound.MUSIC_GAME1).loop(-1);
457 // snd(Sound.PIECE_PLACED).loop(-1); 257 */
458 } 258 }
459 259
460 public void startThreads() 260 public void startThreads()
461 { 261 {
462 System.out.print("startThreads()\n"); 262 System.out.print("startThreads()\n");
475 { 275 {
476 animThread.interrupt(); 276 animThread.interrupt();
477 animEnable = false; 277 animEnable = false;
478 animThread = null; 278 animThread = null;
479 } 279 }
280
281 /*
282 for (Sound s : Sound.values())
283 {
284 if (snd(s) != null)
285 snd(s).stop();
286 }
287 */
480 } 288 }
481 289
482 public void mousePressed(MouseEvent e) { } 290 public void mousePressed(MouseEvent e) { }
483 public void mouseEntered(MouseEvent e) { } 291 public void mouseEntered(MouseEvent e) { }
484 public void mouseExited(MouseEvent e) { } 292 public void mouseExited(MouseEvent e) { }
531 { 339 {
532 } 340 }
533 341
534 public void keyPressed(KeyEvent e) 342 public void keyPressed(KeyEvent e)
535 { 343 {
536 System.out.print("running "+ snd(Sound.MUSIC_GAME1) + "\n");
537 switch (e.getKeyCode()) 344 switch (e.getKeyCode())
538 { 345 {
539 case KeyEvent.VK_LEFT: 346 case KeyEvent.VK_LEFT:
540 case KeyEvent.VK_UP: 347 case KeyEvent.VK_UP:
541 lauta.pieceRotate(false); 348 lauta.pieceRotate(false);
546 lauta.pieceRotate(true); 353 lauta.pieceRotate(true);
547 break; 354 break;
548 355
549 case KeyEvent.VK_ENTER: 356 case KeyEvent.VK_ENTER:
550 lauta.pieceFinishTurn(); 357 lauta.pieceFinishTurn();
551 snd(Sound.PIECE_PLACED).stop(); 358 // snd(Sound.PIECE_PLACED).stop();
552 snd(Sound.PIECE_PLACED).play(); 359 // snd(Sound.PIECE_PLACED).play();
553 break; 360 break;
554 } 361 }
555 } 362 }
556 363
557 public void run() 364 public void run()
558 { 365 {
559 while (animEnable) 366 while (animEnable)
560 { 367 {
561 clock++; 368 clock++;
562
563 // System.out.print("clock=" + clock + "\n");
564 369
565 lauta.animate(clock); 370 lauta.animate(clock);
566 371
567 if (clock % 2 == 1) 372 if (clock % 2 == 1)
568 repaint(); 373 repaint();