# HG changeset patch # User Matti Hamalainen # Date 1296482736 -7200 # Node ID 4507a431b410f71e048a32d7014bb3f3b26148ac # Parent 0dfa894b873dcc5ee2695099bc6d2d34d5c0b564 More work. diff -r 0dfa894b873d -r 4507a431b410 Makefile --- a/Makefile Mon Jan 31 08:13:11 2011 +0200 +++ b/Makefile Mon Jan 31 16:05:36 2011 +0200 @@ -3,7 +3,7 @@ RESOURCES=graphics/board.png -CLASSES=game/Piece.class game/PieceType.class game/Engine.class game/Interpolate.class +CLASSES=game/Piece.java game/PieceType.java game/Engine.java game/Interpolate.java game/ResourceLoader.java # Utils JAVAC=javac -g @@ -12,20 +12,14 @@ ### ### Targets ### -TARGETS=$(RUN) $(CLASSES) +TARGETS=$(RUN) all: $(TARGETS) -%.class: %.java +Ristipolku.class: Ristipolku.java $(CLASSES) $(JAVAC) $< -game/%.class: game/%.java - $(JAVAC) $< - - -Ristipolku.class: game/Piece.class game/PieceType.class game/Engine.class game/Interpolate.class - run: $(RUN) $(APPLETVIEWER) $(patsubst %.class,%.html,$<) @@ -35,12 +29,12 @@ ### Package ### Ristipolku.jar: $(RUN) $(CLASSES) $(RESOURCES) - jar cvf $@ $+ + jar cvfm $@ manifest.txt $(RUN) game/*.class $(RESOURCES) upload: Ristipolku.jar @scp $+ ccr@tnsp.org:public_html/ristipolku/ -# $(RM) $< + $(RM) $< ### diff -r 0dfa894b873d -r 4507a431b410 game/Engine.java --- a/game/Engine.java Mon Jan 31 08:13:11 2011 +0200 +++ b/game/Engine.java Mon Jan 31 16:05:36 2011 +0200 @@ -18,6 +18,83 @@ import javax.sound.sampled.*; +enum Sound { + PIECE_PLACED("placed.wav"), + MUSIC_GAME("gamemusic.wav"); + + final String name; + private Clip clip; + + Sound(String name) + { + this.name = name; + } + + public boolean initialized() + { + return clip != null; + } + + public static void load(String path) throws IOException + { + for (Sound snd : Sound.values()) { + String filename = path + snd.name; + ResourceLoader res = new ResourceLoader(filename); + if (res == null || res.getStream() == null) + { + throw new IOException("Could not load audio resource '"+filename+"'.\n"); + } + try { + AudioInputStream is = AudioSystem.getAudioInputStream(res.getStream()); + } + catch (UnsupportedAudioFileException e) { + throw new IOException("Unsupported audio file format for '"+filename+"'.\n"); + } + catch (IOException e) + { + throw new IOException("Could not load audio resource '"+filename+"'.\n"); + } + } + } + + public void play() + { + if (clip == null) + return; + + clip.setFramePosition(0); + clip.start(); + } + + public void loop(int n) + { + if (clip == null) + return; + + if (n < 0) + clip.loop(Clip.LOOP_CONTINUOUSLY); + else + clip.loop(n); + } + + public void stop() + { + if (clip == null) + return; + + if (clip.isRunning()) + clip.stop(); + } + + public boolean isPlaying() + { + if (clip == null) + return false; + + return clip.isRunning(); + } +} + class PathInfo { public int in, inX, inY, out, outX, outY; @@ -194,7 +271,7 @@ public class Engine extends JPanel - implements Runnable, KeyListener + implements Runnable, KeyListener, MouseListener { Thread animThread; boolean animEnable = false; @@ -202,6 +279,7 @@ BufferedImage lautaBG = null, lautaBGScaled = null; Dimension oldDim; float clock; + Sound sounds; public Engine() { @@ -212,20 +290,33 @@ try { - lautaBG = ImageIO.read(new File("board.png")); + ResourceLoader res = new ResourceLoader("graphics/board.png"); + lautaBG = ImageIO.read(res.getStream()); + sounds.load("sounds/"); } catch (IOException e) { - System.out.print("lol\n"); +/* JOptionPane.showMessageDialog(null, - "Could not load background image.", + "Could not load some of the resources.", "Initialization error", JOptionPane.ERROR_MESSAGE); +*/ + System.out.print("Could not load some of the resources.\n"); + } + + lauta = new GameBoard(); + addKeyListener(this); + addMouseListener(this); + + // Get initial focus + if (!hasFocus()) + { + System.out.print("Engine(): requesting focus\n"); + requestFocus(); } - lauta = new GameBoard(); - addKeyListener(this); - + sounds.MUSIC_GAME.loop(-1); } public void startThreads() @@ -250,6 +341,21 @@ } } + public void mousePressed(MouseEvent e) { } + public void mouseEntered(MouseEvent e) { } + public void mouseExited(MouseEvent e) { } + public void mouseReleased(MouseEvent e) { } + + public void mouseClicked(MouseEvent e) + { + System.out.print("mouseClicked()\n"); + if (!hasFocus()) + { + System.out.print("requesting focus\n"); + requestFocus(); + } + } + public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; @@ -277,13 +383,6 @@ lauta.paint(g2, 100, 150, 60); // Other elements - - // Request focus to keep keyboard input coming - if (!hasFocus()) - { - System.out.print(".\n"); - requestFocus(); - } } public void keyTyped(KeyEvent e) diff -r 0dfa894b873d -r 4507a431b410 game/ResourceLoader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/ResourceLoader.java Mon Jan 31 16:05:36 2011 +0200 @@ -0,0 +1,38 @@ +/* + * Ristipolku Resource Loader + * (C) Copyright 2011 Matti 'ccr' Hämäläinen + */ +package game; + +import java.util.*; +import java.io.*; +import java.net.*; + + +public class ResourceLoader +{ + InputStream stream; + String name; + URL resourceURL; + + public ResourceLoader(String name) + { + this.name = name; + resourceURL = getClass().getClassLoader().getResource(name); + if (resourceURL != null) + stream = getClass().getClassLoader().getResourceAsStream(name); + + System.out.print("ResourceLoader('"+ name +"'): "+ resourceURL +" - "+ stream +"\n"); + } + + public InputStream getStream() + { + System.out.print("ResourceLoader('"+ name +"').getStream()\n"); + return stream; + } + + public URL getURL() + { + return resourceURL; + } +}