From: Daniel Karbach Date: Fri, 16 Oct 2015 07:15:55 +0000 (+0200) Subject: block sounds depending on block type X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=07b8335e7bfd631e0878e183c87238812d632c56;p=blank.git block sounds depending on block type well, potentially --- diff --git a/assets b/assets index 2e992fd..03a7d61 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 2e992fd6861d4c45c5938bd519600aeb8b8be386 +Subproject commit 03a7d614b7e7e7c57c87b022b9bf76b09c5242fb diff --git a/src/app/Assets.hpp b/src/app/Assets.hpp index c069c3e..ef63518 100644 --- a/src/app/Assets.hpp +++ b/src/app/Assets.hpp @@ -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; diff --git a/src/app/app.cpp b/src/app/app.cpp index ab5170f..22a3b58 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -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 ®, + 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 index 0000000..66e9cb0 --- /dev/null +++ b/src/audio/SoundBank.hpp @@ -0,0 +1,31 @@ +#ifndef BLANK_AUDIO_SOUNDBANK_HPP_ +#define BLANK_AUDIO_SOUNDBANK_HPP_ + +#include "Sound.hpp" + +#include + + +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 sounds; + +}; + +} + +#endif diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 93569f0..a223e05 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -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 #include @@ -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); + } +} + } diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index 02385e2..b86258e 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -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; diff --git a/src/client/client.cpp b/src/client/client.cpp index 236fa1a..89e306b 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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); diff --git a/src/shared/WorldResources.hpp b/src/shared/WorldResources.hpp index d92101d..40801f3 100644 --- a/src/shared/WorldResources.hpp +++ b/src/shared/WorldResources.hpp @@ -19,6 +19,7 @@ struct WorldResources { BlockTypeRegistry block_types; ModelRegistry models; + ResourceIndex snd_index; ResourceIndex tex_index; diff --git a/src/shared/shared.cpp b/src/shared/shared.cpp index 9541c95..3eebf43 100644 --- a/src/shared/shared.cpp +++ b/src/shared/shared.cpp @@ -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); } diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 7ae6741..3695b20 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -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); diff --git a/src/standalone/MasterState.hpp b/src/standalone/MasterState.hpp index 6e63020..97834e6 100644 --- a/src/standalone/MasterState.hpp +++ b/src/standalone/MasterState.hpp @@ -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; diff --git a/src/ui/InteractiveManipulator.hpp b/src/ui/InteractiveManipulator.hpp index 6975d9e..00d3285 100644 --- a/src/ui/InteractiveManipulator.hpp +++ b/src/ui/InteractiveManipulator.hpp @@ -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; }; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index c7d8b24..6a48874 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -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); + } } } diff --git a/src/world/BlockType.hpp b/src/world/BlockType.hpp index 1ea34a1..b7861e1 100644 --- a/src/world/BlockType.hpp +++ b/src/world/BlockType.hpp @@ -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 diff --git a/src/world/block.cpp b/src/world/block.cpp index 954a112..5059a99 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -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)