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