]> git.localhorst.tv Git - blank.git/commitdiff
block sounds depending on block type
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 16 Oct 2015 07:15:55 +0000 (09:15 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 16 Oct 2015 07:18:14 +0000 (09:18 +0200)
well, potentially

15 files changed:
assets
src/app/Assets.hpp
src/app/app.cpp
src/audio/SoundBank.hpp [new file with mode: 0644]
src/audio/audio.cpp
src/client/InteractiveState.hpp
src/client/client.cpp
src/shared/WorldResources.hpp
src/shared/shared.cpp
src/standalone/MasterState.cpp
src/standalone/MasterState.hpp
src/ui/InteractiveManipulator.hpp
src/ui/ui.cpp
src/world/BlockType.hpp
src/world/block.cpp

diff --git a/assets b/assets
index 2e992fd6861d4c45c5938bd519600aeb8b8be386..03a7d614b7e7e7c57c87b022b9bf76b09c5242fb 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit 2e992fd6861d4c45c5938bd519600aeb8b8be386
+Subproject commit 03a7d614b7e7e7c57c87b022b9bf76b09c5242fb
index c069c3ec32f7681e0c7573202567adde83327d00..ef63518ae270834ebc8d7862ad20471543eacb68 100644 (file)
@@ -25,7 +25,8 @@ public:
        void LoadBlockTypes(
                const std::string &set_name,
                BlockTypeRegistry &,
-               ResourceIndex &,
+               ResourceIndex &snd,
+               ResourceIndex &tex,
                const ShapeRegistry &) const;
        CubeMap LoadCubeMap(const std::string &name) const;
        Font LoadFont(const std::string &name, int size) const;
index ab5170f8ebc70233546b22b3d658ed92a629126d..22a3b586a1850a38d8ba11d45003754f3d05d05b 100644 (file)
@@ -318,6 +318,7 @@ CuboidBounds slab_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }});
 void AssetLoader::LoadBlockTypes(
        const string &set_name,
        BlockTypeRegistry &reg,
+       ResourceIndex &snd_index,
        ResourceIndex &tex_index,
        const ShapeRegistry &shapes
 ) const {
@@ -364,6 +365,12 @@ void AssetLoader::LoadBlockTypes(
                                in.ReadVec(type.outline_color);
                        } else if (name == "label") {
                                in.ReadString(type.label);
+                       } else if (name == "place_sound") {
+                               in.ReadString(tex_name);
+                               type.place_sound = snd_index.GetID(tex_name);
+                       } else if (name == "remove_sound") {
+                               in.ReadString(tex_name);
+                               type.remove_sound = snd_index.GetID(tex_name);
                        } else if (name == "luminosity") {
                                type.luminosity = in.GetInt();
                        } else if (name == "block_light") {
diff --git a/src/audio/SoundBank.hpp b/src/audio/SoundBank.hpp
new file mode 100644 (file)
index 0000000..66e9cb0
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef BLANK_AUDIO_SOUNDBANK_HPP_
+#define BLANK_AUDIO_SOUNDBANK_HPP_
+
+#include "Sound.hpp"
+
+#include <vector>
+
+
+namespace blank {
+
+class AssetLoader;
+class Audio;
+class ResourceIndex;
+
+class SoundBank {
+
+public:
+       SoundBank();
+
+       void Load(const AssetLoader &, const ResourceIndex &);
+
+       const Sound &operator [](std::size_t i) const noexcept { return sounds[i]; }
+
+private:
+       std::vector<Sound> sounds;
+
+};
+
+}
+
+#endif
index 93569f0cda2bbc694d484ef3e6553a1ca26b6fd4..a223e05a496d7e4889be4f02cb73ce626fb764b4 100644 (file)
@@ -1,6 +1,10 @@
 #include "ALError.hpp"
 #include "Audio.hpp"
 #include "Sound.hpp"
+#include "SoundBank.hpp"
+
+#include "../app/Assets.hpp"
+#include "../shared/ResourceIndex.hpp"
 
 #include <algorithm>
 #include <alut.h>
@@ -148,11 +152,7 @@ int Audio::NextFree() noexcept {
 Sound::Sound()
 : handle(AL_NONE)
 , duration(0) {
-       alGenBuffers(1, &handle);
-       ALenum err = alGetError();
-       if (err != AL_NO_ERROR) {
-               throw ALError(err, "alGenBuffers");
-       }
+
 }
 
 Sound::Sound(const char *file)
@@ -176,18 +176,19 @@ Sound::~Sound() {
                ALenum err = alGetError();
                if (err != AL_NO_ERROR) {
                        std::cerr << "warning: alDeleteBuffers failed with " << al_error_string(err) << std::endl;
-                       //throw ALError(err, "alDeleteBuffers");
                }
        }
 }
 
 Sound::Sound(Sound &&other)
-: handle(other.handle) {
+: handle(other.handle)
+, duration(other.duration) {
        other.handle = AL_NONE;
 }
 
 Sound &Sound::operator =(Sound &&other) {
        std::swap(handle, other.handle);
+       std::swap(duration, other.duration);
        return *this;
 }
 
@@ -195,4 +196,18 @@ void Sound::Bind(ALuint src) const {
        alSourcei(src, AL_BUFFER, handle);
 }
 
+
+SoundBank::SoundBank()
+: sounds() {
+
+}
+
+void SoundBank::Load(const AssetLoader &loader, const ResourceIndex &index) {
+       sounds.clear();
+       sounds.resize(index.Size());
+       for (const auto &entry : index.Entries()) {
+               sounds[entry.second] = loader.LoadSound(entry.first);
+       }
+}
+
 }
index 02385e2e5a5ff6904aa1031d3ebd12ed61759a0a..b86258e9f2bb892bb06309f3e29a9c39ff717dc2 100644 (file)
@@ -7,6 +7,7 @@
 #include "ChunkReceiver.hpp"
 #include "NetworkedInput.hpp"
 #include "../app/IntervalTimer.hpp"
+#include "../audio/SoundBank.hpp"
 #include "../graphics/SkyBox.hpp"
 #include "../io/WorldSave.hpp"
 #include "../net/Packet.hpp"
@@ -67,6 +68,7 @@ private:
 private:
        MasterState &master;
        WorldResources res;
+       SoundBank sounds;
        WorldSave save;
        World world;
        Player &player;
index 236fa1a41fe0872a35d5a819da2f186fa764b998..89e306bee60cb775dc1a61ff63b976f8a296a95a 100644 (file)
@@ -48,11 +48,12 @@ void InitialState::Render(Viewport &viewport) {
 InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 : master(master)
 , res()
+, sounds()
 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
 , world(res.block_types, master.GetWorldConf())
 , player(*world.AddPlayer(master.GetConfig().player.name))
 , hud(master.GetEnv(), master.GetConfig(), player)
-, manip(master.GetEnv(), player.GetEntity())
+, manip(master.GetEnv().audio, sounds, player.GetEntity())
 , input(world, player, master.GetClient())
 , interface(master.GetConfig(), master.GetEnv().keymap, input, *this)
 , chunk_receiver(world.Chunks(), save)
@@ -64,6 +65,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
                save.Write(master.GetWorldConf());
        }
        res.Load(master.GetEnv().loader, "default");
+       sounds.Load(master.GetEnv().loader, res.snd_index);
        interface.SetInventorySlots(res.block_types.size() - 1);
        chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index);
        chunk_renderer.FogDensity(master.GetWorldConf().fog_density);
index d92101d2479f5b8975efa803e07332a8beee5c05..40801f3c37caf7c1d0ea852c6eccc164e3540972 100644 (file)
@@ -19,6 +19,7 @@ struct WorldResources {
        BlockTypeRegistry block_types;
        ModelRegistry models;
 
+       ResourceIndex snd_index;
        ResourceIndex tex_index;
 
 
index 9541c951ccd77ddbf15e4cca050ac9d7c32353b8..3eebf43afac076f7fb4789d857c43de016d2371e 100644 (file)
@@ -26,13 +26,14 @@ WorldResources::WorldResources()
 : shapes()
 , block_types()
 , models()
+, snd_index()
 , tex_index() {
 
 }
 
 void WorldResources::Load(const AssetLoader &loader, const std::string &set) {
        loader.LoadShapes("default", shapes);
-       loader.LoadBlockTypes("default", block_types, tex_index, shapes);
+       loader.LoadBlockTypes("default", block_types, snd_index, tex_index, shapes);
        loader.LoadModels("default", models, tex_index, shapes);
 }
 
index 7ae67418a6160bec46081c23924eaf7e8bab357f..3695b201c71f83ec8764c17ec7f02f9786dcfb19 100644 (file)
@@ -21,13 +21,14 @@ MasterState::MasterState(
 : config(config)
 , env(env)
 , res()
+, sounds()
 , save(save)
 , world(res.block_types, wc)
 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , player(*world.AddPlayer(config.player.name))
 , spawn_player(false)
 , hud(env, config, player)
-, manip(env, player.GetEntity())
+, manip(env.audio, sounds, player.GetEntity())
 , input(world, player, manip)
 , interface(config, env.keymap, input, *this)
 , generator(gc)
@@ -41,6 +42,7 @@ MasterState::MasterState(
        if (res.models.size() < 2) {
                throw std::runtime_error("need at least two models to run");
        }
+       sounds.Load(env.loader, res.snd_index);
        spawner.LimitModels(0, res.models.size());
        interface.SetInventorySlots(res.block_types.size() - 1);
        generator.LoadTypes(res.block_types);
index 6e630200fa9eab8e60b653e7fb5bf76cae1b9d53..97834e6371db843f003fe5dbee5326a45452366c 100644 (file)
@@ -7,6 +7,7 @@
 #include "PreloadState.hpp"
 #include "UnloadState.hpp"
 #include "../ai/Spawner.hpp"
+#include "../audio/SoundBank.hpp"
 #include "../graphics/SkyBox.hpp"
 #include "../shared/WorldResources.hpp"
 #include "../ui/DirectInput.hpp"
@@ -62,6 +63,7 @@ private:
        Config &config;
        Environment &env;
        WorldResources res;
+       SoundBank sounds;
        const WorldSave &save;
        World world;
        ChunkIndex &spawn_index;
index 6975d9eb5599e87c9519ad299a713ced981702fb..00d3285f33251219c48f5fbed29d044752be6667 100644 (file)
@@ -10,21 +10,20 @@ namespace blank {
 
 class Audio;
 class Entity;
-class Environment;
+class SoundBank;
 
 class InteractiveManipulator
 : public WorldManipulator {
 
 public:
-       explicit InteractiveManipulator(Environment &, Entity &);
+       explicit InteractiveManipulator(Audio &, const SoundBank &, Entity &);
 
        void SetBlock(Chunk &, int, const Block &) override;
 
 private:
        Entity &player;
        Audio &audio;
-       Sound place_sound;
-       Sound remove_sound;
+       const SoundBank &sounds;
 
 };
 
index c7d8b2498c97a39e31b1ef75fa32002a96bc227f..6a48874116dea291e82dc22b0d8097a3df3c0fa8 100644 (file)
@@ -12,6 +12,7 @@
 #include "../app/FrameCounter.hpp"
 #include "../app/init.hpp"
 #include "../audio/Audio.hpp"
+#include "../audio/SoundBank.hpp"
 #include "../graphics/Font.hpp"
 #include "../graphics/Viewport.hpp"
 #include "../io/TokenStreamReader.hpp"
@@ -424,22 +425,26 @@ void HUD::Render(Viewport &viewport) noexcept {
 }
 
 
-InteractiveManipulator::InteractiveManipulator(Environment &env, Entity &player)
+InteractiveManipulator::InteractiveManipulator(Audio &audio, const SoundBank &sounds, Entity &player)
 : player(player)
-, audio(env.audio)
-, place_sound(env.loader.LoadSound("thump"))
-, remove_sound(env.loader.LoadSound("plop")) {
+, audio(audio)
+, sounds(sounds) {
 
 }
 
 void InteractiveManipulator::SetBlock(Chunk &chunk, int index, const Block &block) {
+       const BlockType &old_type = chunk.Type(index);
        chunk.SetBlock(index, block);
+       const BlockType &new_type = chunk.Type(index);
        glm::vec3 coords = chunk.ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(index));
-       // TODO: get sound effect from block type
-       if (block.type == 0) {
-               audio.Play(remove_sound, coords);
+       if (new_type.id == 0) {
+               if (old_type.remove_sound >= 0) {
+                       audio.Play(sounds[old_type.remove_sound], coords);
+               }
        } else {
-               audio.Play(place_sound, coords);
+               if (new_type.place_sound >= 0) {
+                       audio.Play(sounds[new_type.place_sound], coords);
+               }
        }
 }
 
index 1ea34a1f897558e28e5020dc4545eb19513b55a5..b7861e1dd24281088bdab5cbde4d594dff05b489 100644 (file)
@@ -26,6 +26,9 @@ struct BlockType {
        /// a string to display to the user
        std::string label;
 
+       int place_sound;
+       int remove_sound;
+
        Block::Type id;
 
        /// light level that blocks of this type emit
index 954a112579089d71b2e8478ad60f7393f4b47c31..5059a999ca85d5e0010dd2f3b1a3ebe4830c8170 100644 (file)
@@ -73,6 +73,8 @@ BlockType::BlockType() noexcept
 , rgb_mod(1.0f, 1.0f, 1.0f)
 , outline_color(-1, -1, -1)
 , label("some block")
+, place_sound(-1)
+, remove_sound(-1)
 , id(0)
 , luminosity(0)
 , visible(true)