From 8ff629e1cd01fb8aa0fb8667646a46bc9d743343 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 17 May 2024 15:46:54 +0300 Subject: [PATCH] minor refactor --- src/assets/assetload_funcs.cpp | 4 ++-- src/audio/AL/ALAudio.cpp | 17 ++++++++--------- src/audio/AL/ALAudio.hpp | 8 ++++---- src/audio/AL/alutil.cpp | 20 ++++++++++--------- src/audio/NoAudio.cpp | 8 ++++---- src/audio/NoAudio.hpp | 8 ++++---- src/audio/audio.cpp | 29 ++++++++++++++-------------- src/audio/audio.hpp | 20 +++++++++---------- src/coders/ogg.cpp | 11 +++++++---- src/coders/ogg.hpp | 4 ++-- src/coders/wav.cpp | 23 ++++++++++++++-------- src/coders/wav.hpp | 4 ++-- src/util/command_line.cpp | 35 ++++++++++++++++++++++++++++++++++ src/util/command_line.hpp | 35 +--------------------------------- src/util/stringutil.hpp | 1 - 15 files changed, 120 insertions(+), 107 deletions(-) diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index 336f2067..ea4560b5 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -167,12 +167,12 @@ assetload::postfunc assetload::sound( // looking for 'sound_name' as base sound auto soundFile = paths->find(file+extension); if (fs::exists(soundFile)) { - baseSound.reset(audio::load_sound(soundFile, keepPCM)); + baseSound = audio::load_sound(soundFile, keepPCM); } // looking for 'sound_name_0' as base sound auto variantFile = paths->find(file+"_0"+extension); if (fs::exists(variantFile)) { - baseSound.reset(audio::load_sound(variantFile, keepPCM)); + baseSound = audio::load_sound(variantFile, keepPCM); } // loading sound variants diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index f6a999f1..bf5f7f13 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -4,7 +4,6 @@ #include "../../debug/Logger.hpp" #include -#include static debug::Logger logger("al-audio"); @@ -24,14 +23,14 @@ ALSound::~ALSound() { buffer = 0; } -Speaker* ALSound::newInstance(int priority, int channel) const { +std::unique_ptr ALSound::newInstance(int priority, int channel) const { uint source = al->getFreeSource(); if (source == 0) { return nullptr; } AL_CHECK(alSourcei(source, AL_BUFFER, buffer)); - auto speaker = new ALSpeaker(al, source, priority, channel); + auto speaker = std::make_unique(al, source, priority, channel); speaker->duration = duration; return speaker; } @@ -67,7 +66,7 @@ bool ALStream::preloadBuffer(uint buffer, bool loop) { return true; } -Speaker* ALStream::createSpeaker(bool loop, int channel) { +std::unique_ptr ALStream::createSpeaker(bool loop, int channel) { this->loop = loop; uint source = al->getFreeSource(); if (source == 0) { @@ -80,7 +79,7 @@ Speaker* ALStream::createSpeaker(bool loop, int channel) { } AL_CHECK(alSourceQueueBuffers(source, 1, &buffer)); } - return new ALSpeaker(al, source, PRIORITY_HIGH, channel); + return std::make_unique(al, source, PRIORITY_HIGH, channel); } @@ -379,15 +378,15 @@ ALAudio::~ALAudio() { context = nullptr; } -Sound* ALAudio::createSound(std::shared_ptr pcm, bool keepPCM) { +std::unique_ptr ALAudio::createSound(std::shared_ptr pcm, bool keepPCM) { auto format = AL::to_al_format(pcm->channels, pcm->bitsPerSample); uint buffer = getFreeBuffer(); AL_CHECK(alBufferData(buffer, format, pcm->data.data(), pcm->data.size(), pcm->sampleRate)); - return new ALSound(this, buffer, pcm, keepPCM); + return std::make_unique(this, buffer, pcm, keepPCM); } -Stream* ALAudio::openStream(std::shared_ptr stream, bool keepSource) { - return new ALStream(this, stream, keepSource); +std::unique_ptr ALAudio::openStream(std::shared_ptr stream, bool keepSource) { + return std::make_unique(this, stream, keepSource); } ALAudio* ALAudio::create() { diff --git a/src/audio/AL/ALAudio.hpp b/src/audio/AL/ALAudio.hpp index b1080cb1..294b0224 100644 --- a/src/audio/AL/ALAudio.hpp +++ b/src/audio/AL/ALAudio.hpp @@ -40,7 +40,7 @@ namespace audio { return pcm; } - Speaker* newInstance(int priority, int channel) const override; + std::unique_ptr newInstance(int priority, int channel) const override; }; class ALStream : public Stream { @@ -65,7 +65,7 @@ namespace audio { std::shared_ptr getSource() const override; void bindSpeaker(speakerid_t speaker) override; - Speaker* createSpeaker(bool loop, int channel) override; + std::unique_ptr createSpeaker(bool loop, int channel) override; speakerid_t getSpeaker() const override; void update(double delta) override; @@ -148,8 +148,8 @@ namespace audio { std::vector getAvailableDevices() const; - Sound* createSound(std::shared_ptr pcm, bool keepPCM) override; - Stream* openStream(std::shared_ptr stream, bool keepSource) override; + std::unique_ptr createSound(std::shared_ptr pcm, bool keepPCM) override; + std::unique_ptr openStream(std::shared_ptr stream, bool keepSource) override; void setListener( glm::vec3 position, diff --git a/src/audio/AL/alutil.cpp b/src/audio/AL/alutil.cpp index 839a7545..420aa030 100644 --- a/src/audio/AL/alutil.cpp +++ b/src/audio/AL/alutil.cpp @@ -1,6 +1,7 @@ #include "alutil.hpp" -#include +#include "../../debug/Logger.hpp" + #include #include #include @@ -14,30 +15,31 @@ #include #endif +static debug::Logger logger("open-al"); + bool AL::check_errors(const std::string& filename, const std::uint_fast32_t line){ ALenum error = alGetError(); if(error != AL_NO_ERROR){ - std::cerr << "OpenAL ERROR (" << filename << ": " << line << ")\n" ; + logger.error() << filename << ": " << line; switch(error){ case AL_INVALID_NAME: - std::cerr << "AL_INVALID_NAME: a bad name (ID) was passed to an OpenAL function"; + logger.error() << "a bad name (ID) was passed to an OpenAL function"; break; case AL_INVALID_ENUM: - std::cerr << "AL_INVALID_ENUM: an invalid enum value was passed to an OpenAL function"; + logger.error() << "an invalid enum value was passed to an OpenAL function"; break; case AL_INVALID_VALUE: - std::cerr << "AL_INVALID_VALUE: an invalid value was passed to an OpenAL function"; + logger.error() << "an invalid value was passed to an OpenAL function"; break; case AL_INVALID_OPERATION: - std::cerr << "AL_INVALID_OPERATION: the requested operation is not valid"; + logger.error() << "the requested operation is not valid"; break; case AL_OUT_OF_MEMORY: - std::cerr << "AL_OUT_OF_MEMORY: the requested operation resulted in OpenAL running out of memory"; + logger.error() << "the requested operation resulted in OpenAL running out of memory"; break; default: - std::cerr << "UNKNOWN AL ERROR: " << error; + logger.error() << "UNKNOWN AL ERROR: " << error; } - std::cerr << std::endl; return false; } return true; diff --git a/src/audio/NoAudio.cpp b/src/audio/NoAudio.cpp index 3c40277d..3000b891 100644 --- a/src/audio/NoAudio.cpp +++ b/src/audio/NoAudio.cpp @@ -9,12 +9,12 @@ NoSound::NoSound(std::shared_ptr pcm, bool keepPCM) { } } -Sound* NoAudio::createSound(std::shared_ptr pcm, bool keepPCM) { - return new NoSound(pcm, keepPCM); +std::unique_ptr NoAudio::createSound(std::shared_ptr pcm, bool keepPCM) { + return std::make_unique(pcm, keepPCM); } -Stream* NoAudio::openStream(std::shared_ptr stream, bool keepSource) { - return new NoStream(stream, keepSource); +std::unique_ptr NoAudio::openStream(std::shared_ptr stream, bool keepSource) { + return std::make_unique(stream, keepSource); } NoAudio* NoAudio::create() { diff --git a/src/audio/NoAudio.hpp b/src/audio/NoAudio.hpp index f14ee1c2..e24fb5c6 100644 --- a/src/audio/NoAudio.hpp +++ b/src/audio/NoAudio.hpp @@ -19,7 +19,7 @@ namespace audio { return pcm; } - Speaker* newInstance(int priority, int channel) const override { + std::unique_ptr newInstance(int priority, int channel) const override { return nullptr; } }; @@ -42,7 +42,7 @@ namespace audio { void bindSpeaker(speakerid_t speaker) override { } - Speaker* createSpeaker(bool loop, int channel) override{ + std::unique_ptr createSpeaker(bool loop, int channel) override { return nullptr; } @@ -65,8 +65,8 @@ namespace audio { public: ~NoAudio() {} - Sound* createSound(std::shared_ptr pcm, bool keepPCM) override; - Stream* openStream(std::shared_ptr stream, bool keepSource) override; + std::unique_ptr createSound(std::shared_ptr pcm, bool keepPCM) override; + std::unique_ptr openStream(std::shared_ptr stream, bool keepSource) override; void setListener( glm::vec3 position, diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index ab46836e..5160bc09 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -156,7 +156,7 @@ void audio::initialize(bool enabled) { create_channel("master"); } -PCM* audio::load_PCM(const fs::path& file, bool headerOnly) { +std::unique_ptr audio::load_PCM(const fs::path& file, bool headerOnly) { if (!fs::exists(file)) { throw std::runtime_error("file not found '"+file.u8string()+"'"); } @@ -169,16 +169,16 @@ PCM* audio::load_PCM(const fs::path& file, bool headerOnly) { throw std::runtime_error("unsupported audio format"); } -Sound* audio::load_sound(const fs::path& file, bool keepPCM) { - std::shared_ptr pcm(load_PCM(file, !keepPCM && backend->isDummy())); +std::unique_ptr audio::load_sound(const fs::path& file, bool keepPCM) { + std::shared_ptr pcm(load_PCM(file, !keepPCM && backend->isDummy()).release()); return create_sound(pcm, keepPCM); } -Sound* audio::create_sound(std::shared_ptr pcm, bool keepPCM) { +std::unique_ptr audio::create_sound(std::shared_ptr pcm, bool keepPCM) { return backend->createSound(pcm, keepPCM); } -PCMStream* audio::open_PCM_stream(const fs::path& file) { +std::unique_ptr audio::open_PCM_stream(const fs::path& file) { std::string ext = file.extension().u8string(); if (ext == ".wav" || ext == ".WAV") { return wav::create_stream(file); @@ -188,7 +188,7 @@ PCMStream* audio::open_PCM_stream(const fs::path& file) { throw std::runtime_error("unsupported audio stream format"); } -Stream* audio::open_stream(const fs::path& file, bool keepSource) { +std::unique_ptr audio::open_stream(const fs::path& file, bool keepSource) { if (!keepSource && backend->isDummy()) { auto header = load_PCM(file, true); // using void source sized as audio instead of actual audio file @@ -203,7 +203,7 @@ Stream* audio::open_stream(const fs::path& file, bool keepSource) { ); } -Stream* audio::open_stream(std::shared_ptr stream, bool keepSource) { +std::unique_ptr audio::open_stream(std::shared_ptr stream, bool keepSource) { return backend->openStream(stream, keepSource); } @@ -255,7 +255,7 @@ speakerid_t audio::play( sound = sound->variants.at(index).get(); } } - Speaker* speaker = sound->newInstance(priority, channel); + auto speaker = sound->newInstance(priority, channel); if (speaker == nullptr) { remove_lower_priority_speaker(priority); speaker = sound->newInstance(priority, channel); @@ -264,7 +264,7 @@ speakerid_t audio::play( return 0; } speakerid_t id = nextId++; - speakers.emplace(id, speaker); + speakers.emplace(id, std::move(speaker)); speaker->setPosition(position); speaker->setVolume(volume); speaker->setPitch(pitch); @@ -283,17 +283,18 @@ speakerid_t audio::play( bool loop, int channel ) { - Speaker* speaker = stream->createSpeaker(loop, channel); - if (speaker == nullptr) { + auto speaker_ptr = stream->createSpeaker(loop, channel); + if (speaker_ptr == nullptr) { remove_lower_priority_speaker(PRIORITY_HIGH); - speaker = stream->createSpeaker(loop, channel); + speaker_ptr = stream->createSpeaker(loop, channel); } - if (speaker == nullptr) { + if (speaker_ptr == nullptr) { return 0; } + auto speaker = speaker_ptr.get(); speakerid_t id = nextId++; streams.emplace(id, stream); - speakers.emplace(id, speaker); + speakers.emplace(id, std::move(speaker_ptr)); stream->bindSpeaker(id); speaker->setPosition(position); diff --git a/src/audio/audio.hpp b/src/audio/audio.hpp index 4a5ca9e0..daf26a9b 100644 --- a/src/audio/audio.hpp +++ b/src/audio/audio.hpp @@ -179,7 +179,7 @@ namespace audio { /// @param loop is stream looped (required for correct buffers preload) /// @param channel channel index /// @return speaker id or 0 - virtual Speaker* createSpeaker(bool loop, int channel) = 0; + virtual std::unique_ptr createSpeaker(bool loop, int channel) = 0; /// @brief Unbind previous speaker and bind new speaker to the stream /// @param speaker speaker id or 0 if all you need is unbind speaker @@ -225,7 +225,7 @@ namespace audio { /// @param channel channel index /// @return new speaker with sound bound or nullptr /// if all speakers are in use - virtual Speaker* newInstance(int priority, int channel) const = 0; + virtual std::unique_ptr newInstance(int priority, int channel) const = 0; }; /// @brief Audio source controller interface. @@ -336,8 +336,8 @@ namespace audio { public: virtual ~Backend() {}; - virtual Sound* createSound(std::shared_ptr pcm, bool keepPCM) = 0; - virtual Stream* openStream(std::shared_ptr stream, bool keepSource) = 0; + virtual std::unique_ptr createSound(std::shared_ptr pcm, bool keepPCM) = 0; + virtual std::unique_ptr openStream(std::shared_ptr stream, bool keepSource) = 0; virtual void setListener( glm::vec3 position, glm::vec3 velocity, @@ -360,38 +360,38 @@ namespace audio { /// @param headerOnly read header only /// @throws std::runtime_error if I/O error ocurred or format is unknown /// @return PCM audio data - PCM* load_PCM(const fs::path& file, bool headerOnly); + std::unique_ptr load_PCM(const fs::path& file, bool headerOnly); /// @brief Load sound from file /// @param file audio file path /// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM /// @throws std::runtime_error if I/O error ocurred or format is unknown /// @return new Sound instance - Sound* load_sound(const fs::path& file, bool keepPCM); + std::unique_ptr load_sound(const fs::path& file, bool keepPCM); /// @brief Create new sound from PCM data /// @param pcm PCM data /// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM /// @return new Sound instance - Sound* create_sound(std::shared_ptr pcm, bool keepPCM); + std::unique_ptr create_sound(std::shared_ptr pcm, bool keepPCM); /// @brief Open new PCM stream from file /// @param file audio file path /// @throws std::runtime_error if I/O error ocurred or format is unknown /// @return new PCMStream instance - PCMStream* open_PCM_stream(const fs::path& file); + std::unique_ptr open_PCM_stream(const fs::path& file); /// @brief Open new audio stream from file /// @param file audio file path /// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource /// @return new Stream instance - Stream* open_stream(const fs::path& file, bool keepSource); + std::unique_ptr open_stream(const fs::path& file, bool keepSource); /// @brief Open new audio stream from source /// @param stream PCM data source /// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource /// @return new Stream instance - Stream* open_stream(std::shared_ptr stream, bool keepSource); + std::unique_ptr open_stream(std::shared_ptr stream, bool keepSource); /// @brief Configure 3D listener /// @param position listener position diff --git a/src/coders/ogg.cpp b/src/coders/ogg.cpp index f2fd629f..5c337893 100644 --- a/src/coders/ogg.cpp +++ b/src/coders/ogg.cpp @@ -10,6 +10,7 @@ static debug::Logger logger("ogg"); +namespace fs = std::filesystem; using namespace audio; static inline std::string vorbis_error_message(int code) { @@ -33,7 +34,7 @@ static inline std::string vorbis_error_message(int code) { } } -audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) { +std::unique_ptr ogg::load_pcm(const fs::path& file, bool headerOnly) { OggVorbis_File vf; int code; if ((code = ov_fopen(file.u8string().c_str(), &vf))) { @@ -66,7 +67,9 @@ audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) { totalSamples = data.size() / channels / 2; } ov_clear(&vf); - return new PCM(std::move(data), totalSamples, channels, 16, sampleRate, seekable); + return std::make_unique( + std::move(data), totalSamples, channels, 16, sampleRate, seekable + ); } class OggStream : public PCMStream { @@ -149,11 +152,11 @@ public: } }; -PCMStream* ogg::create_stream(const std::filesystem::path& file) { +std::unique_ptr ogg::create_stream(const fs::path& file) { OggVorbis_File vf; int code; if ((code = ov_fopen(file.u8string().c_str(), &vf))) { throw std::runtime_error("vorbis: "+vorbis_error_message(code)); } - return new OggStream(std::move(vf)); + return std::make_unique(std::move(vf)); } diff --git a/src/coders/ogg.hpp b/src/coders/ogg.hpp index fd0b69ce..e704febe 100644 --- a/src/coders/ogg.hpp +++ b/src/coders/ogg.hpp @@ -9,8 +9,8 @@ namespace audio { } namespace ogg { - extern audio::PCM* load_pcm(const std::filesystem::path& file, bool headerOnly); - extern audio::PCMStream* create_stream(const std::filesystem::path& file); + std::unique_ptr load_pcm(const std::filesystem::path& file, bool headerOnly); + std::unique_ptr create_stream(const std::filesystem::path& file); } #endif // CODERS_OGG_HPP_ diff --git a/src/coders/wav.cpp b/src/coders/wav.cpp index ac45ee32..809d4f2a 100644 --- a/src/coders/wav.cpp +++ b/src/coders/wav.cpp @@ -1,14 +1,18 @@ #include "wav.hpp" #include "../audio/audio.hpp" +#include "../debug/Logger.hpp" #include #include #include #include -#include #include +namespace fs = std::filesystem; + +static debug::Logger logger("wav-coder"); + bool is_big_endian() { uint32_t ui32_v = 0x01020304; char bytes[sizeof(uint32_t)]; @@ -65,7 +69,7 @@ public: return 0; } if (in.fail()) { - std::cerr << "Wav::load_pcm: I/O error ocurred" << std::endl; + logger.error() << "Wav::load_pcm: I/O error ocurred"; return -1; } return in.gcount(); @@ -114,7 +118,7 @@ public: } }; -audio::PCMStream* wav::create_stream(const std::filesystem::path& file) { +std::unique_ptr wav::create_stream(const fs::path& file) { std::ifstream in(file, std::ios::binary); if(!in.is_open()){ throw std::runtime_error("could not to open file '"+file.u8string()+"'"); @@ -197,7 +201,6 @@ audio::PCMStream* wav::create_stream(const std::filesystem::path& file) { } if(std::strncmp(buffer, "data", 4) != 0){ - std::cerr << buffer << std::endl; throw std::runtime_error("file is not a valid WAVE file (doesn't have 'data' tag)"); } @@ -214,11 +217,13 @@ audio::PCMStream* wav::create_stream(const std::filesystem::path& file) { if(in.fail()){ throw std::runtime_error("fail state set on the file"); } - return new WavStream(std::move(in), channels, bitsPerSample, sampleRate, size, initialOffset); + return std::make_unique( + std::move(in), channels, bitsPerSample, sampleRate, size, initialOffset + ); } -audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) { - std::unique_ptr stream(wav::create_stream(file)); +std::unique_ptr wav::load_pcm(const fs::path& file, bool headerOnly) { + auto stream = wav::create_stream(file); size_t totalSamples = stream->getTotalSamples(); uint channels = stream->getChannels(); @@ -233,5 +238,7 @@ audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) { data.resize(size); stream->readFully(data.data(), size, false); } - return new audio::PCM(std::move(data), totalSamples, channels, bitsPerSample, sampleRate, true); + return std::make_unique( + std::move(data), totalSamples, channels, bitsPerSample, sampleRate, true + ); } diff --git a/src/coders/wav.hpp b/src/coders/wav.hpp index be648535..19428c4e 100644 --- a/src/coders/wav.hpp +++ b/src/coders/wav.hpp @@ -9,8 +9,8 @@ namespace audio { } namespace wav { - extern audio::PCM* load_pcm(const std::filesystem::path& file, bool headerOnly); - extern audio::PCMStream* create_stream(const std::filesystem::path& file); + std::unique_ptr load_pcm(const std::filesystem::path& file, bool headerOnly); + std::unique_ptr create_stream(const std::filesystem::path& file); } #endif // CODERS_WAV_HPP_ diff --git a/src/util/command_line.cpp b/src/util/command_line.cpp index c9751d9b..16fbdade 100644 --- a/src/util/command_line.cpp +++ b/src/util/command_line.cpp @@ -1,9 +1,44 @@ #include "command_line.hpp" +#include "../files/engine_paths.hpp" + +#include #include +#include +#include +#include namespace fs = std::filesystem; +class ArgsReader { + int argc; + char** argv; + int pos = 0; + const char* last = ""; +public: + ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} + + void skip() { + pos++; + } + + bool hasNext() const { + return pos < argc && strlen(argv[pos]); + } + + bool isKeywordArg() const { + return last[0] == '-'; + } + + std::string next() { + if (pos >= argc) { + throw std::runtime_error("unexpected end"); + } + last = argv[pos]; + return argv[pos++]; + } +}; + bool perform_keyword(ArgsReader& reader, const std::string& keyword, EnginePaths& paths) { if (keyword == "--res") { auto token = reader.next(); diff --git a/src/util/command_line.hpp b/src/util/command_line.hpp index b23ea9b1..179ece70 100644 --- a/src/util/command_line.hpp +++ b/src/util/command_line.hpp @@ -1,40 +1,7 @@ #ifndef UTIL_COMMAND_LINE_HPP_ #define UTIL_COMMAND_LINE_HPP_ -#include -#include -#include -#include -#include "../files/engine_paths.hpp" - -class ArgsReader { - int argc; - char** argv; - int pos = 0; - const char* last = ""; -public: - ArgsReader(int argc, char** argv) : argc(argc), argv(argv) {} - - void skip() { - pos++; - } - - bool hasNext() const { - return pos < argc && strlen(argv[pos]); - } - - bool isKeywordArg() const { - return last[0] == '-'; - } - - std::string next() { - if (pos >= argc) { - throw std::runtime_error("unexpected end"); - } - last = argv[pos]; - return argv[pos++]; - } -}; +class EnginePaths; /// @return false if engine start can bool parse_cmdline(int argc, char** argv, EnginePaths& paths); diff --git a/src/util/stringutil.hpp b/src/util/stringutil.hpp index 1f9a65ff..22596572 100644 --- a/src/util/stringutil.hpp +++ b/src/util/stringutil.hpp @@ -5,7 +5,6 @@ #include #include -#include namespace util { /// @brief Function used for string serialization in text formats