Exemplo n.º 1
0
  public void update(int delta) {
    if (enabled) {
      if (musicPlaying == null) {
        musicDelay -= delta;

        if (musicDelay <= 0) {
          musicPlaying = music.get((int) (Math.random() * music.size()));
          musicPlaying.playAsMusic(1.0f, 1.0f, false);
          soundStore.setSoundVolume(0.5f);
        }
      } else if (!soundStore.isMusicPlaying()) {
        musicPlaying.stop();
        musicDelay = (int) (Math.random() * 90000) + 30000;
        musicPlaying = null;
        soundStore.setSoundVolume(0.75f);
      }

      if (ambientMusicPlaying == null) {
        ambientMusicDelay -= delta;

        if (ambientMusicDelay <= 0) {
          ambientMusicPlaying = ambientMusic.get((int) (Math.random() * ambientMusic.size()));
          ambientMusicPlaying.playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientMusicPlaying.isPlaying()) {
        ambientMusicPlaying.stop();
        ambientMusicDelay = (int) (Math.random() * 6000) + 1000;
        ambientMusicPlaying = null;
      }

      if (ambientNoisePlaying[0] == null) {
        ambientNoiseDelay[0] -= delta;

        if (ambientNoiseDelay[0] <= 0) {
          ambientNoisePlaying[0] = ambientNoise.get((int) (Math.random() * ambientNoise.size()));
          ambientNoisePlaying[0].playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientNoisePlaying[0].isPlaying()) {
        ambientNoisePlaying[0].stop();
        ambientNoiseDelay[0] = (int) (Math.random() * 16000) + 1000;
        ambientNoisePlaying[0] = null;
      }

      if (ambientNoisePlaying[1] == null) {
        ambientNoiseDelay[1] -= delta;

        if (ambientNoiseDelay[1] <= 0) {
          ambientNoisePlaying[1] = ambientNoise.get((int) (Math.random() * ambientNoise.size()));
          ambientNoisePlaying[1].playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientNoisePlaying[1].isPlaying()) {
        ambientNoisePlaying[1].stop();
        ambientNoiseDelay[1] = (int) (Math.random() * 16000) + 1000;
        ambientNoisePlaying[1] = null;
      }

      soundStore.poll(delta);
    }
  }
Exemplo n.º 2
0
  public MusicPlayer() {
    soundStore = SoundStore.get();
    soundStore.setMaxSources(4);
    soundStore.setDeferredLoading(true);
    soundStore.init();

    music = new ArrayList<Audio>();
    ambientMusic = new ArrayList<Audio>();
    ambientNoise = new ArrayList<Audio>();
  }
Exemplo n.º 3
0
  /**
   * Stops and releases all sources, clears each of the specified Audio buffers, destroys the OpenAL
   * context, and resets SoundStore for future use.
   *
   * <p>Calling SoundStore.get().init() will re-initialize the OpenAL context after a call to
   * destroyOpenAL (Note: AudioLoader.getXXX calls init for you).
   *
   * @author davedes (http://slick.ninjacave.com/forum/viewtopic.php?t=3920)
   */
  private static void destroyOpenAL() {
    if (!trackExists()) return;
    stop();

    try {
      // get Music object's (private) Audio object reference
      Field sound = player.getClass().getDeclaredField("sound");
      sound.setAccessible(true);
      Audio audio = (Audio) (sound.get(player));

      // first clear the sources allocated by SoundStore
      int max = SoundStore.get().getSourceCount();
      IntBuffer buf = BufferUtils.createIntBuffer(max);
      for (int i = 0; i < max; i++) {
        int source = SoundStore.get().getSource(i);
        buf.put(source);

        // stop and detach any buffers at this source
        AL10.alSourceStop(source);
        AL10.alSourcei(source, AL10.AL_BUFFER, 0);
      }
      buf.flip();
      AL10.alDeleteSources(buf);
      int exc = AL10.alGetError();
      if (exc != AL10.AL_NO_ERROR) {
        throw new SlickException("Could not clear SoundStore sources, err: " + exc);
      }

      // delete any buffer data stored in memory, too...
      if (audio != null && audio.getBufferID() != 0) {
        buf = BufferUtils.createIntBuffer(1).put(audio.getBufferID());
        buf.flip();
        AL10.alDeleteBuffers(buf);
        exc = AL10.alGetError();
        if (exc != AL10.AL_NO_ERROR) {
          throw new SlickException(
              "Could not clear buffer " + audio.getBufferID() + ", err: " + exc);
        }
      }

      // clear OpenAL
      AL.destroy();

      // reset SoundStore so that next time we create a Sound/Music, it will reinit
      SoundStore.get().clear();

      player = null;
    } catch (Exception e) {
      ErrorHandler.error("Failed to destroy OpenAL.", e, true);
    }
  }
Exemplo n.º 4
0
 @Override
 public Audio loadAudioStream(final String name) throws IOException {
   org.newdawn.slick.openal.Audio retval =
       SoundStore.get().getOggStream("res" + File.separator + name);
   if (retval == null) return null;
   return new LWJGLAudioAdapter(retval);
 }
Exemplo n.º 5
0
  /** Game loop update */
  public void update() {
    while (Keyboard.next()) {
      if (Keyboard.getEventKeyState()) {
        if (Keyboard.getEventKey() == Keyboard.KEY_Q) {
          // play as a one off sound effect
          oggEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_W) {
          // replace the music thats curretly playing with
          // the ogg
          oggStream.playAsMusic(1.0f, 1.0f, true);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_E) {
          // replace the music thats curretly playing with
          // the mod
          modStream.playAsMusic(1.0f, 1.0f, true);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_R) {
          // play as a one off sound effect
          aifEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_T) {
          // play as a one off sound effect
          wavEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
      }
    }

    // polling is required to allow streaming to get a chance to
    // queue buffers.
    SoundStore.get().poll(0);
  }
Exemplo n.º 6
0
 /* (non-Javadoc)
  * @see chu.engine.Game#loop()
  */
 @Override
 public void loop() {
   while (!Display.isCloseRequested()) {
     final long time = System.nanoTime();
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
     glClearDepth(1.0f);
     getInput();
     final ArrayList<Message> messages = new ArrayList<>();
     if (client != null) {
       synchronized (client.messagesLock) {
         messages.addAll(client.messages);
         for (Message m : messages) client.messages.remove(m);
       }
     }
     SoundStore.get().poll(0);
     glPushMatrix();
     // Global resolution scale
     //			Renderer.scale(scaleX, scaleY);
     currentStage.beginStep(messages);
     currentStage.onStep();
     currentStage.processAddStack();
     currentStage.processRemoveStack();
     currentStage.render();
     //				FEResources.getBitmapFont("stat_numbers").render(
     //						(int)(1.0f/getDeltaSeconds())+"", 440f, 0f, 0f);
     currentStage.endStep();
     glPopMatrix();
     Display.update();
     timeDelta = System.nanoTime() - time;
   }
   AL.destroy();
   Display.destroy();
   if (client != null && client.isOpen()) client.quit();
 }
Exemplo n.º 7
0
  /** @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer) */
  public void init(GameContainer container) throws SlickException {
    SoundStore.get().setMaxSources(32);

    myContainer = container;
    sound = new Sound("testdata/restart.ogg");
    charlie = new Sound("testdata/cbrown01.wav");
    engine = new Sound("testdata/engine.wav");
    music = musica = new Music("testdata/SMB-X.XM");
    // music = musica = new Music("testdata/theme.ogg", true);
    musicb = new Music("testdata/kirby.ogg", true);
    burp = new Sound("testdata/burp.aif");

    music.play();
  }
Exemplo n.º 8
0
 /**
  * Inicia el Visor OpenGL indicando la instancia de partido, las dimensiones de la pantalla
  * (sx,sy), si Se ejecuta en pantalla completa(fullscreen), e indicando la instancia del jframe
  * Principal(dejar nulo)
  *
  * @param partido
  * @param sx
  * @param sy
  * @param fullscreen
  * @param principal
  * @throws SlickException
  */
 public VisorOpenGl(Partido partido, int sx, int sy, boolean fullscreen, PrincipalFrame principal)
     throws SlickException {
   this.partido = partido;
   this.sx = sx;
   this.sy = sy;
   this.dxsaque = (sx + 300 * 2) / 75;
   sx2 = sx / 2;
   sy2 = sy / 2;
   this.principal = principal;
   AppGameContainer container = new AppGameContainer(this);
   container.setForceExit(false);
   container.setDisplayMode(sx, sy, fullscreen);
   container.start();
   SoundStore.get().clear();
   InternalTextureLoader.get().clear();
 }
Exemplo n.º 9
0
  /**
   * Update and render the game
   *
   * @param delta The change in time since last update and render
   * @throws SlickException Indicates an internal fault to the game.
   */
  protected void updateAndRender(int delta) throws SlickException {
    storedDelta += delta;
    input.poll(width, height);

    SoundStore.get().poll(delta);
    if (storedDelta >= minimumLogicInterval) {
      try {
        if (maximumLogicInterval != 0) {
          long cycles = storedDelta / maximumLogicInterval;
          for (int i = 0; i < cycles; i++) {
            game.update(this, (int) maximumLogicInterval);
          }
          game.update(this, (int) (delta % maximumLogicInterval));
        } else {
          game.update(this, (int) storedDelta);
        }

        storedDelta = 0;
      } catch (Throwable e) {
        Log.error(e);
        throw new SlickException("Game.update() failure - check the game code.");
      }
    }

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();

    graphics.resetFont();
    graphics.resetLineWidth();
    graphics.setAntiAlias(false);
    try {
      game.render(this, graphics);
    } catch (Throwable e) {
      Log.error(e);
      throw new SlickException("Game.render() failure - check the game code.");
    }
    graphics.resetTransform();

    if (showFPS) {
      defaultFont.drawString(10, 10, "FPS: " + recordedFPS);
    }

    if (targetFPS != -1) {
      Display.sync(targetFPS);
    }
  }
Exemplo n.º 10
0
 void stop() {
   SoundStore.get().clear();
   InternalTextureLoader.get().clear();
   if (partido.fueGrabado()) {
     if (JOptionPane.showConfirmDialog(
             principal, "Desea guardar el partido?", "Guardar Partido", JOptionPane.YES_NO_OPTION)
         == 0) {
       if (jfc.showSaveDialog(principal) == JFileChooser.APPROVE_OPTION) {
         try {
           partido.getPartidoGuardado().save(jfc.getSelectedFile());
           if (principal != null) {
             principal.addGuardadoLocal(new File[] {jfc.getSelectedFile()});
           }
         } catch (Exception ex) {
           logger.error("Error al guardar partido", ex);
         }
       }
     }
   }
   if (sonidos) {
     for (Sound s : ambiente) {
       s.stop();
     }
     gol.stop();
     remate[0].stop();
     remate[1].stop();
     poste[0].stop();
     poste[1].stop();
     ovacion[0].stop();
     ovacion[1].stop();
     rebote.stop();
     silbato.stop();
   }
   if (principal != null) {
     principal.setVisible(true);
     principal.requestFocus();
   } else {
     System.exit(0);
   }
 }
Exemplo n.º 11
0
 /**
  * Inicia el Visor OpenGL indicando la instancia de partido guardado, las dimensiones de la
  * pantalla (sx,sy), si Se ejecuta en pantalla completa(fullscreen), e indicando la instancia del
  * jframe Principal(dejar nulo)
  */
 public VisorOpenGl(
     PartidoGuardado partido, int sx, int sy, boolean fullscreen, PrincipalFrame principal)
     throws SlickException {
   pg = (PartidoGuardado) partido;
   guardado = true;
   progreso = true;
   inicio = 0;
   fin = pg.getIterciones() - 1;
   // System.out.println("Reproduciendo partido guardado...");
   this.partido = partido;
   this.sx = sx;
   this.sy = sy;
   this.dxsaque = (sx + 300 * 2) / 75;
   sx2 = sx / 2;
   sy2 = sy / 2;
   this.principal = principal;
   AppGameContainer container = new AppGameContainer(this);
   container.setForceExit(false);
   container.setDisplayMode(sx, sy, fullscreen);
   container.start();
   SoundStore.get().clear();
   InternalTextureLoader.get().clear();
 }
Exemplo n.º 12
0
 /**
  * Sets the music pitch (and speed).
  *
  * @param pitch the new pitch
  */
 public static void setPitch(float pitch) {
   SoundStore.get().setMusicPitch(pitch);
 }
Exemplo n.º 13
0
 /**
  * Indicate whether sound effects should be enabled
  *
  * @param on True if sound effects should be enabled
  */
 public void setSoundOn(boolean on) {
   SoundStore.get().setSoundsOn(on);
 }
Exemplo n.º 14
0
 /**
  * Indicate whether music should be enabled
  *
  * @param on True if music should be enabled
  */
 public void setMusicOn(boolean on) {
   SoundStore.get().setMusicOn(on);
 }
Exemplo n.º 15
0
 /**
  * Check if music is enabled
  *
  * @return True if music is enabled
  */
 public boolean isMusicOn() {
   return SoundStore.get().musicOn();
 }
Exemplo n.º 16
0
 /**
  * Check if sound effects are enabled
  *
  * @return True if sound effects are enabled
  */
 public boolean isSoundOn() {
   return SoundStore.get().soundsOn();
 }
 /**
  * Set the default volume for music
  *
  * @param volume the new default value for music volume
  */
 public void setMusicVolume(float volume) {
   SoundStore.get().setMusicVolume(volume);
 }
 /**
  * Retrieve the current default volume for sound fx
  *
  * @return the current default volume for sound fx
  */
 public float getSoundVolume() {
   return SoundStore.get().getSoundVolume();
 }
Exemplo n.º 19
0
 public void addMusic(String res) throws IOException {
   music.add(soundStore.getOggStream(res));
 }
Exemplo n.º 20
0
 public void addAmbientNoise(String res) throws IOException {
   ambientNoise.add(soundStore.getOgg(res));
 }
Exemplo n.º 21
0
 /**
  * Sets the music volume.
  *
  * @param volume the new volume [0, 1]
  */
 public static void setVolume(float volume) {
   SoundStore.get().setMusicVolume((isTrackDimmed()) ? volume * dimLevel : volume);
 }
Exemplo n.º 22
0
 /** @see org.newdawn.slick.loading.DeferredResource#load() */
 @Override
 public void load() throws IOException {
   boolean before = SoundStore.get().isDeferredLoading();
   SoundStore.get().setDeferredLoading(false);
   if (in != null) {
     switch (type) {
       case OGG:
         target = SoundStore.get().getOgg(in);
         break;
       case WAV:
         target = SoundStore.get().getWAV(in);
         break;
       case MOD:
         target = SoundStore.get().getMOD(in);
         break;
       case AIF:
         target = SoundStore.get().getAIF(in);
         break;
       default:
         Log.error("Unrecognised sound type: " + type);
         break;
     }
   } else {
     switch (type) {
       case OGG:
         target = SoundStore.get().getOgg(ref);
         break;
       case WAV:
         target = SoundStore.get().getWAV(ref);
         break;
       case MOD:
         target = SoundStore.get().getMOD(ref);
         break;
       case AIF:
         target = SoundStore.get().getAIF(ref);
         break;
       default:
         Log.error("Unrecognised sound type: " + type);
         break;
     }
   }
   SoundStore.get().setDeferredLoading(before);
 }