chore: Refactor audio loading and handling in Controller class
This commit is contained in:
@@ -68,9 +68,6 @@ class ExperimentFlags {
|
||||
// Log to the console.
|
||||
bool kLogToConsole = false;
|
||||
|
||||
// Load audio device for emulator
|
||||
bool kLoadAudioDevice = false;
|
||||
|
||||
// Overworld flags
|
||||
struct Overworld {
|
||||
// Load and render overworld sprites to the screen. Unstable.
|
||||
@@ -132,8 +129,6 @@ class ExperimentFlags {
|
||||
result +=
|
||||
"kSaveDungeonMaps: " + std::to_string(flags_->kSaveDungeonMaps) + "\n";
|
||||
result += "kLogToConsole: " + std::to_string(flags_->kLogToConsole) + "\n";
|
||||
result +=
|
||||
"kLoadAudioDevice: " + std::to_string(flags_->kLoadAudioDevice) + "\n";
|
||||
result += "kDrawOverworldSprites: " +
|
||||
std::to_string(flags_->overworld.kDrawOverworldSprites) + "\n";
|
||||
result += "kSaveOverworldMaps: " +
|
||||
|
||||
@@ -117,21 +117,6 @@ void InitializeKeymap() {
|
||||
io.KeyMap[ImGuiKey_F12] = SDL_GetScancodeFromKey(SDLK_F12);
|
||||
}
|
||||
|
||||
void ImGui_ImplSDL2_SetClipboardText(void *user_data, const char *text) {
|
||||
SDL_SetClipboardText(text);
|
||||
}
|
||||
|
||||
const char *ImGui_ImplSDL2_GetClipboardText(void *user_data) {
|
||||
return SDL_GetClipboardText();
|
||||
}
|
||||
|
||||
void InitializeClipboard() {
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
||||
io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
||||
io.ClipboardUserData = nullptr;
|
||||
}
|
||||
|
||||
void HandleKeyDown(SDL_Event &event, editor::EditorManager &editor) {
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.KeysDown[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN);
|
||||
@@ -279,11 +264,8 @@ absl::Status Controller::OnEntry(std::string filename) {
|
||||
RETURN_IF_ERROR(CreateSDL_Window())
|
||||
RETURN_IF_ERROR(CreateRenderer())
|
||||
RETURN_IF_ERROR(CreateGuiContext())
|
||||
if (flags()->kLoadAudioDevice) {
|
||||
RETURN_IF_ERROR(LoadAudioDevice())
|
||||
editor_manager_.emulator().set_audio_buffer(audio_buffer_);
|
||||
editor_manager_.emulator().set_audio_device_id(audio_device_);
|
||||
}
|
||||
RETURN_IF_ERROR(LoadAudioDevice())
|
||||
|
||||
InitializeKeymap();
|
||||
editor_manager_.SetupScreen(filename);
|
||||
active_ = true;
|
||||
@@ -361,11 +343,8 @@ void Controller::DoRender() const {
|
||||
}
|
||||
|
||||
void Controller::OnExit() {
|
||||
if (flags()->kLoadAudioDevice) {
|
||||
SDL_PauseAudioDevice(audio_device_, 1);
|
||||
SDL_CloseAudioDevice(audio_device_);
|
||||
delete audio_buffer_;
|
||||
}
|
||||
SDL_PauseAudioDevice(audio_device_, 1);
|
||||
SDL_CloseAudioDevice(audio_device_);
|
||||
ImGui_ImplSDLRenderer2_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
@@ -373,15 +352,11 @@ void Controller::OnExit() {
|
||||
}
|
||||
|
||||
absl::Status Controller::CreateSDL_Window() {
|
||||
auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER;
|
||||
auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
|
||||
if (flags()->kUseNewImGuiInput) {
|
||||
sdl_flags |= SDL_INIT_GAMECONTROLLER;
|
||||
}
|
||||
|
||||
if (flags()->kLoadAudioDevice) {
|
||||
sdl_flags |= SDL_INIT_AUDIO;
|
||||
}
|
||||
|
||||
if (SDL_Init(sdl_flags) != 0) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
|
||||
@@ -546,9 +521,11 @@ absl::Status Controller::LoadAudioDevice() {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
|
||||
}
|
||||
audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
|
||||
editor_manager_.emulator().set_audio_buffer(audio_buffer_);
|
||||
// audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
|
||||
audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
|
||||
SDL_PauseAudioDevice(audio_device_, 0);
|
||||
editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
|
||||
editor_manager_.emulator().set_audio_device_id(audio_device_);
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include "imgui/imgui.h"
|
||||
#include "imgui/imgui_internal.h"
|
||||
#include "absl/status/status.h"
|
||||
|
||||
#include "app/core/platform/renderer.h"
|
||||
#include "app/editor/editor_manager.h"
|
||||
#include "app/editor/utils/editor.h"
|
||||
@@ -68,8 +67,8 @@ class Controller : public ExperimentFlags {
|
||||
editor::EditorManager editor_manager_;
|
||||
|
||||
int audio_frequency_ = 48000;
|
||||
int16_t *audio_buffer_;
|
||||
SDL_AudioDeviceID audio_device_;
|
||||
std::shared_ptr<int16_t> audio_buffer_;
|
||||
std::shared_ptr<SDL_Window> window_;
|
||||
};
|
||||
|
||||
|
||||
@@ -42,11 +42,6 @@ struct FlagsMenu : public core::ExperimentFlags {
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (BeginMenu("Emulator Flags")) {
|
||||
Checkbox("Load Audio Device", &mutable_flags()->kLoadAudioDevice);
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
Checkbox("Use built-in file dialog",
|
||||
&mutable_flags()->kNewFileDialogWrapper);
|
||||
Checkbox("Enable Console Logging", &mutable_flags()->kLogToConsole);
|
||||
|
||||
Reference in New Issue
Block a user