Search in sources :

Example 1 with Sound

use of de.gurkenlabs.litiengine.sound.Sound in project litiengine by gurkenlabs.

the class ResourcesTests method testSoundResources.

@Test
void testSoundResources() {
    Sound sound = Resources.sounds().get("de/gurkenlabs/litiengine/resources/bip.ogg");
    Sound nonExisting = Resources.sounds().get("randomname.mp3");
    assertNotNull(sound);
    assertEquals("bip", sound.getName());
    assertNull(nonExisting);
}
Also used : Sound(de.gurkenlabs.litiengine.sound.Sound) Test(org.junit.jupiter.api.Test)

Example 2 with Sound

use of de.gurkenlabs.litiengine.sound.Sound in project litiengine by gurkenlabs.

the class SpeechBubbleTests method testSetTypeSound.

@Test
void testSetTypeSound() {
    Sound s1 = Resources.sounds().get("de/gurkenlabs/litiengine/resources/bip.ogg");
    Sound s2 = Resources.sounds().get("de/gurkenlabs/litiengine/resources/bop.ogg");
    // act
    SpeechBubble bubble = new SpeechBubble(entity, "test");
    bubble.setTypeSound(s1);
    // assert
    assertEquals(s1, bubble.getTypeSound());
    // act
    bubble.setTypeSound(s2);
    // assert
    assertEquals(s2, bubble.getTypeSound());
    assertNotEquals(s1, s2);
}
Also used : Sound(de.gurkenlabs.litiengine.sound.Sound) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Sound

use of de.gurkenlabs.litiengine.sound.Sound in project litiengine by gurkenlabs.

the class Resources method load.

/**
 * Load {@code Spritesheets}, {@code Tilesets} and {@code Maps} from a game resource file created with the utiLITI
 * editor. After loading, these resources can be accessed via this API (e.g. {@code Resources.maps().get("mapname")}.
 *
 * @param gameResourceFile
 *          The URL to the game resource file
 */
public static void load(final URL gameResourceFile) {
    final long loadStart = System.nanoTime();
    final ResourceBundle file = ResourceBundle.load(gameResourceFile);
    if (file == null) {
        return;
    }
    file.getMaps().parallelStream().forEach(m -> Resources.maps().add(m.getName(), m));
    log.log(Level.INFO, "{0} maps loaded from {1}", new Object[] { file.getMaps().size(), gameResourceFile });
    file.getBluePrints().parallelStream().forEach(m -> Resources.blueprints().add(m.getName(), m));
    log.log(Level.INFO, "{0} blueprints loaded from {1}", new Object[] { file.getBluePrints().size(), gameResourceFile });
    int tileCnt = 0;
    for (final Tileset tileset : file.getTilesets()) {
        if (Resources.tilesets().contains(tileset.getName())) {
            continue;
        }
        Resources.tilesets().add(tileset.getName(), tileset);
        tileCnt++;
    }
    log.log(Level.INFO, "{0} tilesets loaded from {1}", new Object[] { tileCnt, gameResourceFile });
    final List<Spritesheet> loadedSprites = Collections.synchronizedList(new ArrayList<>());
    file.getSpriteSheets().parallelStream().forEach(spriteSheetInfo -> {
        final Spritesheet sprite = Resources.spritesheets().load(spriteSheetInfo);
        loadedSprites.add(sprite);
    });
    log.log(Level.INFO, "{0} spritesheets loaded from {1}", new Object[] { loadedSprites.size(), gameResourceFile });
    final List<Sound> loadedSounds = Collections.synchronizedList(new ArrayList<>());
    file.getSounds().parallelStream().forEach(soundResource -> {
        final Sound sound = Resources.sounds().load(soundResource);
        loadedSounds.add(sound);
    });
    log.log(Level.INFO, "{0} sounds loaded from {1}", new Object[] { loadedSounds.size(), gameResourceFile });
    int spriteload = 0;
    for (final Spritesheet s : loadedSprites) {
        for (int i = 0; i < s.getRows() * s.getColumns(); i++) {
            BufferedImage sprite = s.getSprite(i);
            if (sprite != null) {
                spriteload++;
            }
        }
    }
    log.log(Level.INFO, "{0} sprites loaded to memory", new Object[] { spriteload });
    for (final EmitterData emitter : file.getEmitters()) {
        EmitterLoader.load(emitter);
    }
    final double loadTime = TimeUtilities.nanoToMs(System.nanoTime() - loadStart);
    log.log(Level.INFO, "loading game resources from {0} took {1} ms", new Object[] { gameResourceFile, loadTime });
}
Also used : Spritesheet(de.gurkenlabs.litiengine.graphics.Spritesheet) Sound(de.gurkenlabs.litiengine.sound.Sound) Tileset(de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint) BufferedImage(java.awt.image.BufferedImage) EmitterData(de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData)

Example 4 with Sound

use of de.gurkenlabs.litiengine.sound.Sound in project litiengine by gurkenlabs.

the class Sounds method load.

/**
 * Loads a sound from the specified XML resource.
 *
 * @param resource
 *          The XML resource that contains the sound as Base64 string.
 *
 * @return The {@code Sound} instance loaded from the specified resource.
 *
 * @see Codec#decode(String)
 */
public Sound load(final SoundResource resource) {
    byte[] data = Codec.decode(resource.getData());
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    Sound sound;
    try {
        sound = new Sound(input, resource.getName());
        this.add(resource.getName(), sound);
        return sound;
    } catch (IOException | UnsupportedAudioFileException e) {
        log.log(Level.SEVERE, "The audio file {0} could not be loaded.", new Object[] { resource.getName() });
    }
    return null;
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) Sound(de.gurkenlabs.litiengine.sound.Sound) IOException(java.io.IOException)

Aggregations

Sound (de.gurkenlabs.litiengine.sound.Sound)4 Test (org.junit.jupiter.api.Test)2 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)1 Tileset (de.gurkenlabs.litiengine.environment.tilemap.xml.Tileset)1 Spritesheet (de.gurkenlabs.litiengine.graphics.Spritesheet)1 EmitterData (de.gurkenlabs.litiengine.graphics.emitters.xml.EmitterData)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1