audio::reset_channel + minor refactor

This commit is contained in:
MihailRis 2024-03-05 00:19:46 +03:00
parent 9fbf46169f
commit 0ab39fa06f
9 changed files with 62 additions and 40 deletions

View File

@ -52,6 +52,12 @@ class AssetsLoader {
public:
AssetsLoader(Assets* assets, const ResPaths* paths);
void addLoader(int tag, aloader_func func);
/// @brief Enqueue asset load
/// @param tag asset type
/// @param filename asset file path
/// @param alias internal asset name
/// @param settings asset loading settings (based on asset type)
void add(
int tag,
const std::string filename,
@ -59,10 +65,12 @@ public:
std::shared_ptr<AssetCfg> settings=nullptr
);
bool hasNext() const;
bool loadNext();
/// @brief Enqueue core and content assets
/// @param loader target loader
/// @param content engine content
static void addDefaults(AssetsLoader& loader, const Content* content);
const ResPaths* getPaths() const;

View File

@ -20,6 +20,14 @@
namespace fs = std::filesystem;
static bool animation(
Assets* assets,
const ResPaths* paths,
const std::string directory,
const std::string name,
Atlas* dstAtlas
);
bool assetload::texture(
AssetsLoader&,
Assets* assets,
@ -102,7 +110,7 @@ bool assetload::atlas(
Atlas* atlas = builder.build(2);
assets->store(atlas, name);
for (const auto& file : builder.getNames()) {
assetload::animation(assets, paths, "textures", file, atlas);
animation(assets, paths, "textures", file, atlas);
}
return true;
}
@ -172,7 +180,7 @@ bool assetload::sound(
return true;
}
bool assetload::animation(
static bool animation(
Assets* assets,
const ResPaths* paths,
const std::string directory,

View File

@ -10,6 +10,7 @@ class AssetsLoader;
class Atlas;
struct AssetCfg;
/// @brief see AssetsLoader.h: aloader_func
namespace assetload {
bool texture(
AssetsLoader&,
@ -60,14 +61,6 @@ namespace assetload {
const std::string name,
std::shared_ptr<AssetCfg> settings
);
bool animation(
Assets*,
const ResPaths* paths,
const std::string directory,
const std::string name,
Atlas* dstAtlas
);
}
#endif // ASSETS_ASSET_LOADERS_H_
#endif // ASSETS_ASSET_LOADERS_H_

View File

@ -157,10 +157,6 @@ void audio::initialize(bool enabled) {
backend = NoAudio::create();
}
create_channel("master");
create_channel("regular");
create_channel("music");
create_channel("ambient");
create_channel("ui");
}
PCM* audio::load_PCM(const fs::path& file, bool headerOnly) {
@ -379,20 +375,32 @@ void audio::update(double delta) {
}
}
void audio::reset() {
void audio::reset_channel(int index) {
auto channel = get_channel(index);
if (channel == nullptr) {
return;
}
for (auto& entry : speakers) {
entry.second->stop();
if (entry.second->getChannel() == index) {
entry.second->stop();
}
}
for (auto& entry : streams) {
entry.second->update(0.0f);
}
for (auto& channel : channels) {
if (channel->isPaused()) {
channel->resume();
for (auto it = speakers.begin(); it != speakers.end();) {
auto speaker = it->second.get();
int speakerChannel = speaker->getChannel();
if (speakerChannel == index) {
streams.erase(it->first);
it = speakers.erase(it);
} else {
it++;
}
}
streams.clear();
speakers.clear();
if (channel->isPaused()) {
channel->resume();
}
}
void audio::close() {

View File

@ -474,8 +474,8 @@ namespace audio {
/// @param delta time elapsed since the last update (seconds)
extern void update(double delta);
/// @brief Stop all playing audio, destroy all non-builtin channels
extern void reset();
/// @brief Stop all playing audio in channel, reset channel state
extern void reset_channel(int channel);
/// @brief Finalize audio system
extern void close();

View File

@ -9,7 +9,7 @@
#include <set>
#include "../typedefs.h"
using DrawGroups = std::set<unsigned char>;
using DrawGroups = std::set<ubyte>;
class Block;
class ItemDef;
@ -134,4 +134,4 @@ public:
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const;
};
#endif // CONTENT_CONTENT_H_
#endif // CONTENT_CONTENT_H_

View File

@ -57,6 +57,10 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
throw initialize_error("could not initialize window");
}
audio::initialize(true);
audio::create_channel("regular");
audio::create_channel("music");
audio::create_channel("ambient");
audio::create_channel("ui");
auto resdir = paths->getResources();
scripting::initialize(this);
@ -238,7 +242,8 @@ double Engine::getDelta() const {
}
void Engine::setScreen(std::shared_ptr<Screen> screen) {
audio::reset();
audio::reset_channel(audio::get_channel_index("regular"));
audio::reset_channel(audio::get_channel_index("ambient"));
this->screen = screen;
}

View File

@ -17,18 +17,18 @@
#include "../voxels/Chunk.h"
const uint REGION_HEADER_SIZE = 10;
inline constexpr uint REGION_HEADER_SIZE = 10;
const uint REGION_LAYER_VOXELS = 0;
const uint REGION_LAYER_LIGHTS = 1;
const uint REGION_LAYER_INVENTORIES = 2;
inline constexpr uint REGION_LAYER_VOXELS = 0;
inline constexpr uint REGION_LAYER_LIGHTS = 1;
inline constexpr uint REGION_LAYER_INVENTORIES = 2;
const uint REGION_SIZE_BIT = 5;
const uint REGION_SIZE = (1 << (REGION_SIZE_BIT));
const uint REGION_CHUNKS_COUNT = ((REGION_SIZE) * (REGION_SIZE));
const uint REGION_FORMAT_VERSION = 2;
const uint WORLD_FORMAT_VERSION = 1;
const uint MAX_OPEN_REGION_FILES = 16;
inline constexpr uint REGION_SIZE_BIT = 5;
inline constexpr uint REGION_SIZE = (1 << (REGION_SIZE_BIT));
inline constexpr uint REGION_CHUNKS_COUNT = ((REGION_SIZE) * (REGION_SIZE));
inline constexpr uint REGION_FORMAT_VERSION = 2;
inline constexpr uint WORLD_FORMAT_VERSION = 1;
inline constexpr uint MAX_OPEN_REGION_FILES = 16;
#define REGION_FORMAT_MAGIC ".VOXREG"
#define WORLD_FORMAT_MAGIC ".VOXWLD"

View File

@ -445,12 +445,12 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
if (isFocused() && multiline) {
batch->setColor(glm::vec4(1, 1, 1, 0.1f));
glm::vec2 lcoord = label->calcCoord();
lcoord.y -= 4;
lcoord.y -= 2;
uint line = label->getLineByTextIndex(caret);
int lineY = label->getLineYOffset(line);
int lineHeight = font->getLineHeight() * label->getLineInterval();
batch->rect(lcoord.x, lcoord.y+lineY, label->getSize().x, 1);
batch->rect(lcoord.x, lcoord.y+lineY+lineHeight, label->getSize().x, 1);
batch->rect(lcoord.x, lcoord.y+lineY+lineHeight-2, label->getSize().x, 1);
}
label->setColor(glm::vec4(input.empty() ? 0.5f : 1.0f));