Backends: Improved miniaudio and GLFW3 error handling

Signed-off-by: Gabriel Ravier <gabravier@gmail.com>
This commit is contained in:
Gabriel Ravier 2020-04-11 16:57:04 +02:00
parent 0bd968d78d
commit 16441ee87b
4 changed files with 51 additions and 10 deletions

View file

@ -9,6 +9,7 @@
#include "../../Organya.h" #include "../../Organya.h"
#include "../../WindowsWrapper.h" #include "../../WindowsWrapper.h"
#include "../Misc.h"
#include "SoftwareMixer.h" #include "SoftwareMixer.h"
@ -80,13 +81,19 @@ BOOL AudioBackend_Init(void)
config.dataCallback = Callback; config.dataCallback = Callback;
config.pUserData = NULL; config.pUserData = NULL;
if (ma_device_init(NULL, &config, &device) == MA_SUCCESS) ma_result return_value;
return_value = ma_device_init(NULL, &config, &device);
if (return_value == MA_SUCCESS)
{ {
if (ma_mutex_init(device.pContext, &mutex) == MA_SUCCESS) return_value = ma_mutex_init(device.pContext, &mutex);
if (return_value == MA_SUCCESS)
{ {
if (ma_mutex_init(device.pContext, &organya_mutex) == MA_SUCCESS) return_value = ma_mutex_init(device.pContext, &organya_mutex);
if (return_value == MA_SUCCESS)
{ {
if (ma_device_start(&device) == MA_SUCCESS) return_value = ma_device_start(&device);
if (return_value == MA_SUCCESS)
{ {
output_frequency = device.sampleRate; output_frequency = device.sampleRate;
@ -94,22 +101,43 @@ BOOL AudioBackend_Init(void)
return TRUE; return TRUE;
} }
else
{
Backend_PrintError("Failed to start playback device: %s", ma_result_description(return_value));
}
ma_mutex_uninit(&organya_mutex); ma_mutex_uninit(&organya_mutex);
} }
else
{
Backend_PrintError("Failed to create organya mutex: %s", ma_result_description(return_value));
}
ma_mutex_uninit(&mutex); ma_mutex_uninit(&mutex);
} }
else
{
Backend_PrintError("Failed to create mutex: %s", ma_result_description(return_value));
}
ma_device_uninit(&device); ma_device_uninit(&device);
} }
else
{
Backend_PrintError("Failed to initialize playback device: %s", ma_result_description(return_value));
}
return FALSE; return FALSE;
} }
void AudioBackend_Deinit(void) void AudioBackend_Deinit(void)
{ {
ma_device_stop(&device); ma_result return_value;
return_value = ma_device_stop(&device);
if (return_value != MA_SUCCESS)
Backend_PrintError("Failed to stop playback device: %s", ma_result_description(return_value));
ma_mutex_uninit(&organya_mutex); ma_mutex_uninit(&organya_mutex);

View file

@ -8,6 +8,7 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../../WindowsWrapper.h" #include "../../WindowsWrapper.h"
#include "../Misc.h"
#define DEADZONE (10000.0f / 32767.0f) #define DEADZONE (10000.0f / 32767.0f)
@ -21,7 +22,7 @@ static void JoystickCallback(int joystick_id, int event)
switch (event) switch (event)
{ {
case GLFW_CONNECTED: case GLFW_CONNECTED:
printf("Joystick #%d connected - %s\n", joystick_id, glfwGetJoystickName(joystick_id)); Backend_PrintInfo("Joystick #%d connected - %s", joystick_id, glfwGetJoystickName(joystick_id));
if (!joystick_connected) if (!joystick_connected)
{ {
@ -44,8 +45,13 @@ static void JoystickCallback(int joystick_id, int event)
// Set up neutral axes // Set up neutral axes
axis_neutrals = (float*)malloc(sizeof(float) * total_axes); axis_neutrals = (float*)malloc(sizeof(float) * total_axes);
for (int i = 0; i < total_axes; ++i) if (axis_neutrals)
axis_neutrals[i] = axes[i]; {
for (int i = 0; i < total_axes; ++i)
axis_neutrals[i] = axes[i];
}
else
Backend_PrintError("Couldn't allocate memory for axis");
} }
} }
} }
@ -55,7 +61,7 @@ static void JoystickCallback(int joystick_id, int event)
case GLFW_DISCONNECTED: case GLFW_DISCONNECTED:
if (joystick_connected && joystick_id == connected_joystick_id) if (joystick_connected && joystick_id == connected_joystick_id)
{ {
printf("Joystick #%d disconnected\n", connected_joystick_id); Backend_PrintInfo("Joystick #%d disconnected", connected_joystick_id);
joystick_connected = FALSE; joystick_connected = FALSE;
free(axis_neutrals); free(axis_neutrals);

View file

@ -152,8 +152,15 @@ static void DragAndDropCallback(GLFWwindow *window, int count, const char **path
LoadProfile(paths[0]); LoadProfile(paths[0]);
} }
static void ErrorCallback(int code, const char *description)
{
Backend_PrintError("GLFW error received (%d) : %s", code, description);
}
BOOL Backend_Init(void) BOOL Backend_Init(void)
{ {
glfwSetErrorCallback(ErrorCallback);
if (glfwInit() == GL_TRUE) if (glfwInit() == GL_TRUE)
return TRUE; return TRUE;

View file

@ -71,7 +71,7 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid
} }
else else
{ {
Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions"); Backend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not initialize OpenGL context");
} }
#endif #endif