Audio/OpenALDevice: Improve error when buffer/source creation fails

This commit is contained in:
Lynix 2023-11-29 23:27:42 +01:00
parent 150787971a
commit 36dcc03c11
3 changed files with 77 additions and 2 deletions

View File

@ -0,0 +1,24 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Audio module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_AUDIO_OPENALUTILS_HPP
#define NAZARA_AUDIO_OPENALUTILS_HPP
#if defined(NAZARA_AUDIO_OPENAL) || defined(NAZARA_AUDIO_BUILD)
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/OpenAL.hpp>
#include <string>
namespace Nz
{
NAZARA_AUDIO_API std::string TranslateOpenALError(ALenum code);
}
#endif // NAZARA_AUDIO_OPENAL
#endif // NAZARA_AUDIO_OPENALUTILS_HPP

View File

@ -6,6 +6,7 @@
#include <Nazara/Audio/OpenALBuffer.hpp>
#include <Nazara/Audio/OpenALLibrary.hpp>
#include <Nazara/Audio/OpenALSource.hpp>
#include <Nazara/Audio/OpenALUtils.hpp>
#include <cstring>
#include <stdexcept>
#include <Nazara/Audio/Debug.hpp>
@ -69,11 +70,16 @@ namespace Nz
{
MakeContextCurrent();
m_library.alGetError(); // Clear error flags
ALuint bufferId = 0;
m_library.alGenBuffers(1, &bufferId);
if (bufferId == 0)
if (ALenum lastError = m_library.alGetError(); lastError != AL_NO_ERROR)
{
NazaraErrorFmt("failed to create OpenAL buffer: {0}", TranslateOpenALError(lastError));
return {};
}
return std::make_shared<OpenALBuffer>(shared_from_this(), m_library, bufferId);
}
@ -82,11 +88,16 @@ namespace Nz
{
MakeContextCurrent();
m_library.alGetError(); // Clear error flags
ALuint sourceId = 0;
m_library.alGenSources(1, &sourceId);
if (sourceId == 0)
if (ALenum lastError = m_library.alGetError(); lastError != AL_NO_ERROR)
{
NazaraErrorFmt("failed to create OpenAL source: {0}", TranslateOpenALError(lastError));
return {};
}
return std::make_shared<OpenALSource>(shared_from_this(), m_library, sourceId);
}

View File

@ -0,0 +1,40 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Audio module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/OpenALUtils.hpp>
#include <Nazara/Core/Format.hpp>
#include <Nazara/Audio/Debug.hpp>
namespace Nz
{
std::string TranslateOpenALError(ALenum code)
{
// From https://www.openal.org/documentation/OpenAL_Programmers_Guide.pdf
switch (code)
{
case AL_NO_ERROR:
return "No error";
case AL_INVALID_NAME:
return "A bad name (ID) was passed to an OpenAL function";
case AL_INVALID_ENUM:
return "An invalid enum value was passed to an OpenAL function";
case AL_INVALID_VALUE:
return "An invalid value was passed to an OpenAL function";
case AL_INVALID_OPERATION:
return "The requested operation is not valid";
case AL_OUT_OF_MEMORY:
return "The requested operation resulted in OpenAL running out of memory";
default:
break;
}
return Format("Unknown OpenAL error ({0:#x})", code);
}
}