Browse Source

Add audio setup and shutdown to boot state

master
C. J. Howard 1 year ago
parent
commit
4b3ebf3e7e
6 changed files with 128 additions and 29 deletions
  1. +1
    -0
      CMakeLists.txt
  2. +6
    -8
      src/application.cpp
  3. +4
    -7
      src/application.hpp
  4. +4
    -0
      src/game/context.hpp
  5. +96
    -13
      src/game/state/boot.cpp
  6. +17
    -1
      src/game/state/boot.hpp

+ 1
- 0
CMakeLists.txt View File

@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
option(VERSION_STRING "Project version string" "0.0.0") option(VERSION_STRING "Project version string" "0.0.0")
project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX) project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX)

+ 6
- 8
src/application.cpp View File

@ -32,7 +32,7 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
application::application():
application::application(debug::logger& log):
closed(false), closed(false),
fullscreen(true), fullscreen(true),
v_sync(false), v_sync(false),
@ -42,27 +42,24 @@ application::application():
window_dimensions({0, 0}), window_dimensions({0, 0}),
viewport_dimensions({0, 0}), viewport_dimensions({0, 0}),
mouse_position({0, 0}), mouse_position({0, 0}),
logger(nullptr),
logger(&log),
sdl_window(nullptr), sdl_window(nullptr),
sdl_gl_context(nullptr) sdl_gl_context(nullptr)
{ {
// Setup logging
logger = new debug::logger();
// Get SDL compiled version // Get SDL compiled version
SDL_version sdl_compiled_version; SDL_version sdl_compiled_version;
SDL_VERSION(&sdl_compiled_version); SDL_VERSION(&sdl_compiled_version);
std::string sdl_compiled_version_string = std::to_string(sdl_compiled_version.major) + "." + std::to_string(sdl_compiled_version.minor) + "." + std::to_string(sdl_compiled_version.patch); std::string sdl_compiled_version_string = std::to_string(sdl_compiled_version.major) + "." + std::to_string(sdl_compiled_version.minor) + "." + std::to_string(sdl_compiled_version.patch);
logger->log("Compiled against SDL " + sdl_compiled_version_string);
// Get SDL linked version // Get SDL linked version
SDL_version sdl_linked_version; SDL_version sdl_linked_version;
SDL_GetVersion(&sdl_linked_version); SDL_GetVersion(&sdl_linked_version);
std::string sdl_linked_version_string = std::to_string(sdl_linked_version.major) + "." + std::to_string(sdl_linked_version.minor) + "." + std::to_string(sdl_linked_version.patch); std::string sdl_linked_version_string = std::to_string(sdl_linked_version.major) + "." + std::to_string(sdl_linked_version.minor) + "." + std::to_string(sdl_linked_version.patch);
logger->log("Linking against SDL " + sdl_linked_version_string);
// Init SDL // Init SDL
logger->push_task("Initializing SDL"); logger->push_task("Initializing SDL");
logger->log("Compiled against SDL " + sdl_compiled_version_string);
logger->log("Linking against SDL " + sdl_linked_version_string);
if (SDL_Init(SDL_INIT_VIDEO) != 0) if (SDL_Init(SDL_INIT_VIDEO) != 0)
{ {
logger->pop_task(EXIT_FAILURE); logger->pop_task(EXIT_FAILURE);
@ -72,6 +69,7 @@ application::application():
{ {
logger->pop_task(EXIT_SUCCESS); logger->pop_task(EXIT_SUCCESS);
} }
// Load default OpenGL library // Load default OpenGL library
logger->push_task("Loading OpenGL library"); logger->push_task("Loading OpenGL library");
if (SDL_GL_LoadLibrary(nullptr) != 0) if (SDL_GL_LoadLibrary(nullptr) != 0)
@ -353,7 +351,7 @@ void application::hide_window()
void application::add_game_controller_mappings(const void* mappings, std::size_t size) void application::add_game_controller_mappings(const void* mappings, std::size_t size)
{ {
logger->push_task("Adding SDL game controller mappings"); logger->push_task("Adding SDL game controller mappings");
int mapping_count = SDL_GameControllerAddMappingsFromRW(SDL_RWFromConstMem(mappings, size), 0);
int mapping_count = SDL_GameControllerAddMappingsFromRW(SDL_RWFromConstMem(mappings, static_cast<int>(size)), 0);
if (mapping_count == -1) if (mapping_count == -1)
{ {
logger->pop_task(EXIT_FAILURE); logger->pop_task(EXIT_FAILURE);

+ 4
- 7
src/application.hpp View File

@ -28,18 +28,13 @@
#include "input/mouse.hpp" #include "input/mouse.hpp"
#include "input/gamepad.hpp" #include "input/gamepad.hpp"
#include "utility/fundamental-types.hpp" #include "utility/fundamental-types.hpp"
#include "debug/logger.hpp"
// Forward declarations // Forward declarations
typedef struct SDL_Window SDL_Window; typedef struct SDL_Window SDL_Window;
typedef void* SDL_GLContext; typedef void* SDL_GLContext;
class event_dispatcher; class event_dispatcher;
namespace debug
{
class logger;
class performance_sampler;
}
/** /**
* *
*/ */
@ -48,8 +43,10 @@ class application
public: public:
/** /**
* Creates and initializes an application. * Creates and initializes an application.
*
* @param logger Debug logger.
*/ */
application();
application(debug::logger& log);
/** /**
* Destroys an application. * Destroys an application.

+ 4
- 0
src/game/context.hpp View File

@ -58,6 +58,8 @@
#include "state-machine.hpp" #include "state-machine.hpp"
#include "debug/performance-sampler.hpp" #include "debug/performance-sampler.hpp"
#include <filesystem> #include <filesystem>
#include <AL/al.h>
#include <AL/alc.h>
// Forward declarations // Forward declarations
class animator; class animator;
@ -262,6 +264,8 @@ struct context
animation<float>* camera_flash_animation; animation<float>* camera_flash_animation;
// Sound // Sound
ALCdevice* alc_device;
ALCcontext* alc_context;
float master_volume; float master_volume;
float ambience_volume; float ambience_volume;
float effects_volume; float effects_volume;

+ 96
- 13
src/game/state/boot.cpp View File

@ -107,24 +107,25 @@ namespace state {
boot::boot(game::context& ctx, int argc, char** argv): boot::boot(game::context& ctx, int argc, char** argv):
game::state::base(ctx) game::state::base(ctx)
{ {
// Allocate application
ctx.app = new application();
// Get application logger
ctx.logger = ctx.app->get_logger();
// Allocate application logger
ctx.logger = new debug::logger();
// Boot process // Boot process
ctx.logger->push_task("Entering boot state");
ctx.logger->push_task("Booting up");
try try
{ {
// Allocate application
ctx.app = new application(*ctx.logger);
// Parse command line options // Parse command line options
parse_options(argc, argv); parse_options(argc, argv);
setup_resources(); setup_resources();
load_config(); load_config();
load_strings(); load_strings();
setup_window(); setup_window();
setup_rendering(); setup_rendering();
setup_sound();
setup_audio();
setup_scenes(); setup_scenes();
setup_animation(); setup_animation();
setup_entities(); setup_entities();
@ -152,11 +153,22 @@ boot::boot(game::context& ctx, int argc, char** argv):
boot::~boot() boot::~boot()
{ {
ctx.logger->push_task("Exiting boot state");
ctx.logger->push_task("Booting down");
// Close application
delete ctx.app;
ctx.app = nullptr;
try
{
shutdown_audio();
// Close application
delete ctx.app;
ctx.app = nullptr;
}
catch (const std::exception& e)
{
ctx.logger->error("Caught exception: \"" + std::string(e.what()) + "\"");
ctx.logger->pop_task(EXIT_FAILURE);
return;
}
ctx.logger->pop_task(EXIT_SUCCESS); ctx.logger->pop_task(EXIT_SUCCESS);
} }
@ -583,10 +595,10 @@ void boot::setup_rendering()
logger->pop_task(EXIT_SUCCESS); logger->pop_task(EXIT_SUCCESS);
} }
void boot::setup_sound()
void boot::setup_audio()
{ {
debug::logger* logger = ctx.logger; debug::logger* logger = ctx.logger;
logger->push_task("Setting up sound");
logger->push_task("Setting up audio");
// Load master volume config // Load master volume config
ctx.master_volume = 1.0f; ctx.master_volume = 1.0f;
@ -618,6 +630,59 @@ void boot::setup_sound()
if (ctx.config->contains("captions_size")) if (ctx.config->contains("captions_size"))
ctx.captions_size = (*ctx.config)["captions_size"].get<float>(); ctx.captions_size = (*ctx.config)["captions_size"].get<float>();
// Open audio device
logger->push_task("Opening audio device");
ctx.alc_device = alcOpenDevice(nullptr);
if (!ctx.alc_device)
{
logger->pop_task(EXIT_FAILURE);
return;
}
else
{
// Get audio device name
const ALCchar* alc_device_name = nullptr;
if (alcIsExtensionPresent(ctx.alc_device, "ALC_ENUMERATE_ALL_EXT"))
alc_device_name = alcGetString(ctx.alc_device, ALC_ALL_DEVICES_SPECIFIER);
if (alcGetError(ctx.alc_device) != AL_NO_ERROR || !alc_device_name)
alc_device_name = alcGetString(ctx.alc_device, ALC_DEVICE_SPECIFIER);
logger->log("Opened audio device \"" + std::string(alc_device_name) + "\"");
logger->pop_task(EXIT_SUCCESS);
}
// Create audio context
logger->push_task("Creating audio context");
ctx.alc_context = alcCreateContext(ctx.alc_device, nullptr);
if (!ctx.alc_context)
{
logger->pop_task(EXIT_FAILURE);
alcCloseDevice(ctx.alc_device);
return;
}
else
{
logger->pop_task(EXIT_SUCCESS);
}
// Make audio context current
logger->push_task("Making audio context current");
if (alcMakeContextCurrent(ctx.alc_context) == ALC_FALSE)
{
logger->pop_task(EXIT_FAILURE);
if (ctx.alc_context != nullptr)
{
alcDestroyContext(ctx.alc_context);
}
alcCloseDevice(ctx.alc_device);
return;
}
else
{
logger->pop_task(EXIT_SUCCESS);
}
logger->pop_task(EXIT_SUCCESS); logger->pop_task(EXIT_SUCCESS);
} }
@ -1171,5 +1236,23 @@ void boot::loop()
ctx.state_machine.pop(); ctx.state_machine.pop();
} }
void boot::shutdown_audio()
{
ctx.logger->push_task("Shutting down audio");
if (ctx.alc_context)
{
alcMakeContextCurrent(nullptr);
alcDestroyContext(ctx.alc_context);
}
if (ctx.alc_device)
{
alcCloseDevice(ctx.alc_device);
}
ctx.logger->pop_task(EXIT_SUCCESS);
}
} // namespace state } // namespace state
} // namespace game } // namespace game

+ 17
- 1
src/game/state/boot.hpp View File

@ -27,10 +27,24 @@
namespace game { namespace game {
namespace state { namespace state {
/**
* Boots the game up on construction, and down on destruction.
*/
class boot: public game::state::base class boot: public game::state::base
{ {
public: public:
/**
* Boots up the game.
*
* @param ctx Game context.
* @param argc Command line argument count.
* @param argv Command line argument vector.
*/
boot(game::context& ctx, int argc, char** argv); boot(game::context& ctx, int argc, char** argv);
/**
* Boots down the game.
*/
virtual ~boot(); virtual ~boot();
private: private:
@ -40,7 +54,7 @@ private:
void load_strings(); void load_strings();
void setup_window(); void setup_window();
void setup_rendering(); void setup_rendering();
void setup_sound();
void setup_audio();
void setup_scenes(); void setup_scenes();
void setup_animation(); void setup_animation();
void setup_entities(); void setup_entities();
@ -51,6 +65,8 @@ private:
void setup_loop(); void setup_loop();
void loop(); void loop();
void shutdown_audio();
// Command line options // Command line options
std::optional<bool> option_continue; std::optional<bool> option_continue;
std::optional<std::string> option_data; std::optional<std::string> option_data;

Loading…
Cancel
Save