mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(audio): add audio fade in/out and fix delta time naming
This commit is contained in:
@@ -38,7 +38,7 @@ public:
|
||||
static int start_cubed_application(int argc, char** argv);
|
||||
|
||||
static unsigned int seed();
|
||||
static float delte_time();
|
||||
static float delta_time();
|
||||
static float get_fps();
|
||||
|
||||
Camera& camera();
|
||||
@@ -67,7 +67,7 @@ private:
|
||||
|
||||
inline static double last_time = glfwGetTime();
|
||||
inline static double current_time = glfwGetTime();
|
||||
inline static double delta_time = 0.0f;
|
||||
inline static double dt = 0.0f;
|
||||
inline static double fps_time_count = 0.0f;
|
||||
inline static int frame_count = 0;
|
||||
inline static int fps = 0;
|
||||
|
||||
@@ -12,9 +12,11 @@ public:
|
||||
AudioBuffer& operator=(AudioBuffer&&) = delete;
|
||||
~AudioBuffer();
|
||||
ALuint buffer() const;
|
||||
float duration() const;
|
||||
|
||||
private:
|
||||
ALuint m_buffer = 0;
|
||||
float m_duration = 0.0f;
|
||||
void set_data(const AudioData& data);
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/audio/audio_fade.hpp"
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
#include "Cubed/audio/sound_manager.hpp"
|
||||
|
||||
@@ -24,15 +25,18 @@ public:
|
||||
|
||||
void init();
|
||||
void play_bgm();
|
||||
void update(glm::vec3 listener_pos);
|
||||
void update_listener(glm::vec3 listener_pos);
|
||||
void update(float dt);
|
||||
|
||||
private:
|
||||
using SourcePool = std::unordered_map<std::string, ALuint>;
|
||||
using FadeMap = std::unordered_map<std::string, AudioFade>;
|
||||
bool m_init{false};
|
||||
ALCdevice* device{nullptr};
|
||||
ALCcontext* context{nullptr};
|
||||
glm::vec3 listener_pos;
|
||||
std::unique_ptr<AudioSource> m_bgm;
|
||||
FadeMap m_fade_map;
|
||||
SoundManager m_sounds;
|
||||
};
|
||||
} // namespace Cubed
|
||||
19
include/Cubed/audio/audio_fade.hpp
Normal file
19
include/Cubed/audio/audio_fade.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
namespace Cubed {
|
||||
class AudioFade {
|
||||
public:
|
||||
AudioFade(AudioSource* source, float fade_in = 0.0f, float fade_out = 0.0f);
|
||||
~AudioFade();
|
||||
void update();
|
||||
|
||||
private:
|
||||
AudioSource* m_source = nullptr;
|
||||
float m_in_duration = 0.0f;
|
||||
float m_out_duration = 0.0f;
|
||||
float m_start_gain = 0.0f;
|
||||
float m_end_gain = 0.0f;
|
||||
bool m_fade_in = true;
|
||||
bool m_active = true;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -11,16 +11,20 @@ public:
|
||||
AudioSource();
|
||||
~AudioSource();
|
||||
|
||||
void set_buffer(const AudioBuffer& buffer);
|
||||
void set_buffer_2d(const AudioBuffer& buffer);
|
||||
void set_loop(bool on = true);
|
||||
|
||||
void set_volume(float volume);
|
||||
void play();
|
||||
void stop();
|
||||
void pause();
|
||||
|
||||
float duration() const;
|
||||
float current_time() const;
|
||||
float volume() const;
|
||||
AudioState state();
|
||||
|
||||
private:
|
||||
ALuint m_source = 0;
|
||||
float m_volume = 1.0f;
|
||||
float m_duration = 0.0f;
|
||||
};
|
||||
} // namespace Cubed
|
||||
|
||||
0
include/Cubed/audio/source_pool.hpp
Normal file
0
include/Cubed/audio/source_pool.hpp
Normal file
@@ -104,6 +104,8 @@ inline float distance2(const glm::vec3& a, const glm::vec3& b) {
|
||||
return glm::dot(diff, diff);
|
||||
}
|
||||
|
||||
inline float lerp(float a, float b, float t) { return a + t * (b - a); }
|
||||
|
||||
} // namespace Math
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user