Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
audio_source.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <memory>
8#include <optional>
9#include <string>
10
29class AudioSource : public Component {
30 public:
31 explicit AudioSource(std::string audio_file_path, bool play_on_awake = true,
32 bool loop = false, float volume = 1.0f);
33 ~AudioSource() override = default;
34
35 void update(float dt) override;
36 void on_attach() override;
37 void on_detach() override;
38
39 void play(bool loop = false);
40 void stop();
41
42 [[nodiscard]] const std::string& audio_file_path() const noexcept;
43 void audio_file_path(const std::string& path) noexcept;
44
45 [[nodiscard]] float volume() const noexcept;
46 void volume(float value) noexcept;
47
48 [[nodiscard]] bool loop() const noexcept;
49 void loop(bool value) noexcept;
50
51 [[nodiscard]] bool play_on_awake() const noexcept;
52 void play_on_awake(bool value) noexcept;
53
54 [[nodiscard]] std::string audio_name() const noexcept;
55
56 [[nodiscard]] std::optional<std::reference_wrapper<SoundInstance>> instance()
57 const noexcept;
58
59 std::string type_name() const override;
60
61 private:
62 std::shared_ptr<SoundResource> get_or_register_resource();
63 std::optional<std::reference_wrapper<SoundInstance>> instance_opt_;
64
65 std::string audio_file_path_;
66 std::string audio_name_;
67 float volume_;
68
69 bool loop_;
70 bool play_on_awake_;
71};
Component that plays audio using the AudioService.
Definition audio_source.h:29
std::optional< std::reference_wrapper< SoundInstance > > instance() const noexcept
~AudioSource() override=default
bool play_on_awake() const noexcept
float volume() const noexcept
void on_detach() override
Called when the component is detached from a GameObject.
void on_attach() override
Called when the component is attached to a GameObject.
bool loop() const noexcept
AudioSource(std::string audio_file_path, bool play_on_awake=true, bool loop=false, float volume=1.0f)
const std::string & audio_file_path() const noexcept
std::string audio_name() const noexcept
void update(float dt) override
void play(bool loop=false)
std::string type_name() const override
Provides a consistent type name for the component.
Base class for all components that can be attached to GameObjects.
Definition component.h:24
Represents an instance of a generic sound within the audio system. The reason for separating SoundIns...
Definition sound_instance.h:14
Represents a generic sound resource in the audio engine. This class serves as a base for specific sou...
Definition sound_resource.h:12