Skip to content

Commit

Permalink
Merge branch 'main' into 1330-add-active-component-for-all-kinds-of-d…
Browse files Browse the repository at this point in the history
…isabling-purposes
  • Loading branch information
RodrigoVintem authored Nov 8, 2024
2 parents 84e161d + 69c606d commit b3ce57a
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Allow identifying assets in code from their path (#1177. **@GalaxyCrush**).
- Added an Audio asset (#230, **@Dageus**, **@diogomsmiranda**).

### Fixed

- Crash in ecs when removing or destroying components with observers (#1348, **@SrGesus**)
- Crash when opening the Play Pause menu (**SrGesus**)

## [v0.4.0] - 2024-10-13

Expand Down Expand Up @@ -45,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Replaced OpenAL audio device with Miniaudio backend (#1005, **@Dageus**, **@diogomsmiranda**).

### Fixed

- Spot light angle mismatch between light and shadows (#1310, **@tomas7770**).
- Spot shadows cause light range cutoff (#1312, **@tomas7770**).
- Precision error in split screen size calculations (**@mkuritsu**).
Expand Down
3 changes: 3 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ set(CUBOS_ENGINE_SOURCE
"src/utils/free_camera/plugin.cpp"
"src/utils/free_camera/controller.cpp"

"src/audio/audio.cpp"
"src/audio/bridge.cpp"

"src/assets/plugin.cpp"
"src/assets/assets.cpp"
"src/assets/bridge.cpp"
Expand Down
29 changes: 29 additions & 0 deletions engine/include/cubos/engine/audio/audio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// @file
/// @brief Class @ref cubos::engine::Audio.
/// @ingroup audio-plugin

#pragma once

#include <cubos/core/al/miniaudio_context.hpp>
#include <cubos/core/memory/stream.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/asset.hpp>

namespace cubos::engine
{
/// @brief Asset containing raw Audio data.
///
/// @ingroup audio-plugin
struct CUBOS_ENGINE_API Audio
{
CUBOS_REFLECT;

/// @brief Audio buffer.
cubos::core::al::Buffer buffer;

explicit Audio(const std::shared_ptr<cubos::core::al::AudioContext>& context, core::memory::Stream& stream);
Audio(Audio&& other) noexcept;
~Audio();
};
} // namespace cubos::engine
36 changes: 36 additions & 0 deletions engine/include/cubos/engine/audio/bridge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// @file
/// @brief Class @ref cubos::engine::AudioBridge.
/// @ingroup audio-plugin

#pragma once

#include <utility>

#include <cubos/engine/assets/bridges/file.hpp>
#include <cubos/engine/audio/audio.hpp>

namespace cubos::engine
{
/// @brief Bridge which loads and saves @ref Audio assets.
///
/// Uses the default supported file formats from miniaudio.h, which are WAV, FLAC, and MP3.
///
/// @ingroup audio-plugin
class AudioBridge : public FileBridge
{

std::shared_ptr<cubos::core::al::AudioContext> mContext;

public:
/// @brief Constructs a bridge.
AudioBridge(std::shared_ptr<cubos::core::al::AudioContext> context)
: FileBridge(core::reflection::reflect<Audio>())
, mContext(std::move(context))
{
}

protected:
bool loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override;
bool saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override;
};
} // namespace cubos::engine
39 changes: 39 additions & 0 deletions engine/src/audio/audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cubos/core/reflection/external/cstring.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

#include <cubos/engine/audio/audio.hpp>

CUBOS_REFLECT_IMPL(cubos::engine::Audio)
{
return core::reflection::Type::create("cubos::engine::Audio");
}

cubos::engine::Audio::Audio(const std::shared_ptr<cubos::core::al::AudioContext>& context, core::memory::Stream& stream)
{
stream.seek(0, cubos::core::memory::SeekOrigin::End);
size_t streamSize = stream.tell();
void* contents = operator new(sizeof(char) * streamSize);
stream.seek(0, cubos::core::memory::SeekOrigin::Begin);
if (!stream.readExact((char*)contents, streamSize))
{
CUBOS_ERROR("Couldn't load audio: stream read failed");
buffer = nullptr;
operator delete(contents);
return;
}

buffer = context->createBuffer(contents, (size_t)streamSize);
if (buffer == nullptr)
{
CUBOS_ERROR("Couldn't create audio buffer");
}
operator delete(contents);
}

cubos::engine::Audio::~Audio() = default;

cubos::engine::Audio::Audio(Audio&& other) noexcept
: buffer(std::move(other.buffer))
{
}
26 changes: 26 additions & 0 deletions engine/src/audio/bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cubos/core/tel/logging.hpp>

#include <cubos/engine/assets/assets.hpp>
#include <cubos/engine/audio/audio.hpp>
#include <cubos/engine/audio/bridge.hpp>

using namespace cubos::engine;

bool AudioBridge::loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream)
{
Audio audio{mContext, stream};
if (audio.buffer == nullptr)
{
return false;
}
assets.store(handle, std::move(audio));
return true;
}
bool AudioBridge::saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream)
{
(void)assets;
(void)handle;
(void)stream;
CUBOS_ERROR("Saving audios is currently unsupported");
return false;
}
3 changes: 1 addition & 2 deletions engine/src/tools/play_pause/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ void cubos::engine::playPauseToolPlugin(Cubos& cubos)
{
state.scale = 1.0F;
}

ImGui::End();
}
ImGui::End();

if (!state.paused)
{
Expand Down

0 comments on commit b3ce57a

Please sign in to comment.