Skip to content

Commit

Permalink
feat(audio): add audio sample
Browse files Browse the repository at this point in the history
Co-Authored-By: João Miguel Nogueira <[email protected]>
  • Loading branch information
diogomsmiranda and Dageus committed Nov 25, 2024
1 parent cc4169c commit 56439f3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions engine/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ macro(make_sample)
endif()
endmacro()
# Add samples
make_sample(DIR "audio" ASSETS)
make_sample(DIR "hello-cubos")
make_sample(DIR "settings")
make_sample(DIR "events")
Expand Down
Binary file added engine/samples/audio/assets/bg_music.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions engine/samples/audio/assets/bg_music.mp3.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "059c16e7-a439-44c7-9bdc-7f133dba0c80"
}
73 changes: 73 additions & 0 deletions engine/samples/audio/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/audio/plugin.hpp>
#include <cubos/engine/collisions/plugin.hpp>
#include <cubos/engine/fixed_step/plugin.hpp>
#include <cubos/engine/physics/plugin.hpp>
#include <cubos/engine/render/camera/camera.hpp>
#include <cubos/engine/render/camera/draws_to.hpp>
#include <cubos/engine/render/camera/perspective.hpp>
#include <cubos/engine/render/defaults/plugin.hpp>
#include <cubos/engine/render/defaults/target.hpp>
#include <cubos/engine/settings/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/transform/plugin.hpp>
#include <cubos/engine/window/plugin.hpp>

using namespace cubos::engine;

/// [Get handles to assets]
static const Asset<Audio> AudioAsset = AnyAsset("059c16e7-a439-44c7-9bdc-7f133dba0c80");
/// [Get handles to assets]

int main(int argc, char** argv)
{
Cubos cubos{argc, argv};

cubos.plugin(settingsPlugin);
cubos.plugin(windowPlugin);
cubos.plugin(transformPlugin);
cubos.plugin(fixedStepPlugin);
cubos.plugin(assetsPlugin);
cubos.plugin(renderDefaultsPlugin);
cubos.plugin(collisionsPlugin);
cubos.plugin(physicsPlugin);
cubos.plugin(audioPlugin);

cubos.startupSystem("configure Assets").before(settingsTag).call([](Settings& settings) {
settings.setString("assets.app.osPath", APP_ASSETS_PATH);
settings.setString("assets.builtin.osPath", BUILTIN_ASSETS_PATH);
});

cubos.startupSystem("create a camera").call([](Commands cmds) {
auto targetEnt = cmds.create().add(RenderTargetDefaults{}).entity();

cmds.create()
.relatedTo(targetEnt, DrawsTo{})
.add(Camera{.zNear = 0.1F, .zFar = 1000.0F})
.add(PerspectiveCamera{.fovY = 60.0F})
.add(Position{{0.0F, 0.0F, 0.0F}})
.add(Rotation::lookingAt({-1.0F, -1.0F, -1.0F}, glm::vec3{0.0F, 1.0F, 0.0F}))
.add(AudioListener{true});
});

cubos.startupSystem("create an audio source").after(audioStateInitTag).call([](Commands cmds) {
cmds.create()
.add(Position{{0.0F, 0.0F, 0.0F}})
.add(Velocity{.vec = {1.0F, 1.0F, 1.0F}})
.add(Rotation::lookingAt({-1.0F, -1.0F, -1.0F}, glm::vec3{0.0F, 1.0F, 0.0F}))
.add(AudioSource{});
});

cubos.system("play audio").call([](Commands cmds, Query<Entity, AudioSource&> query) {
for (auto [ent, src] : query)
{
if (src.sound.isNull())
{
src.sound = AudioAsset;
cmds.add(ent, AudioPlay{});
}
}
});

cubos.run();
}

0 comments on commit 56439f3

Please sign in to comment.