From 3b8ce73d49d26cd6e97fb12c193d9356308d7259 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sat, 22 Jan 2022 00:50:24 -0500 Subject: [PATCH] Add Exception support --- README.md | 20 ++++++++++++++++++++ clib.json | 1 + include/CMakeLists.txt | 1 + include/Image.hpp | 20 ++++++++++++++++++-- include/Music.hpp | 11 +++++++++-- include/RaylibException.hpp | 36 ++++++++++++++++++++++++++++++++++++ include/Sound.hpp | 9 ++++++++- include/Texture.hpp | 19 +++++++++++++++++++ include/Wave.hpp | 9 ++++++++- include/raylib-cpp.hpp | 1 + tests/raylib_test.cpp | 16 +++++++++++++++- 11 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 include/RaylibException.hpp diff --git a/README.md b/README.md index e9aeba5..13f8dea 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,26 @@ std::string url = "https://raylib.com"; raylib::OpenURL(url); ``` +### Exceptions + +When loading an asset fails, raylib will log a warning, but provide no other feedback for you to act upon. raylib-cpp will throw a [`RaylibException`](include/RaylibException.hpp) [runtime exception](https://en.cppreference.com/w/cpp/error/runtime_error), allowing to provide a safe method to catch failed loads. + +``` cpp +// raylib +Texture texture = LoadTexture("FileNotFound.png"); +if (texture.width == 0) { + TraceLog(LOG_ERROR, "Texture failed to load!"); +} + +// raylib-cpp +try { + raylib::Texture texture("FileNotFound.png"); +} +catch (raylib::RaylibException& error) { + error.TraceLog(LOG_ERROR); +} +``` + ### RayMath The [raymath](https://github.com/raysan5/raylib/blob/master/src/raymath.h) methods are included. diff --git a/clib.json b/clib.json index b7e0607..11453f5 100644 --- a/clib.json +++ b/clib.json @@ -43,6 +43,7 @@ "include/Physics.hpp", "include/Ray.hpp", "include/RayCollision.hpp", + "include/RaylibException.hpp", "include/raylib-cpp-utils.hpp", "include/raylib-cpp.hpp", "include/raylib.hpp", diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 5548acc..7677c50 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -26,6 +26,7 @@ install(FILES Physics.hpp Ray.hpp RayCollision.hpp + RaylibException.hpp raylib-cpp-utils.hpp raylib-cpp.hpp raylib.hpp diff --git a/include/Image.hpp b/include/Image.hpp index 4e7e898..a5de864 100644 --- a/include/Image.hpp +++ b/include/Image.hpp @@ -5,6 +5,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -20,22 +21,37 @@ class Image : public ::Image { Image(const std::string& fileName) { Load(fileName); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str())); + } } Image(const std::string& fileName, int width, int height, int format, int headerSize) { LoadRaw(fileName, width, height, format, headerSize); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Image from file: %s", fileName.c_str())); + } } Image(const std::string& fileName, int* frames) { LoadAnim(fileName, frames); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Image from animation: %s", fileName.c_str())); + } } Image(const std::string& fileType, const unsigned char* fileData, int dataSize) { LoadFromMemory(fileType, fileData, dataSize); + if (!IsReady()) { + throw RaylibException("Failed to load Image from memory"); + } } Image(const ::Texture2D& texture) { set(::LoadImageFromTexture(texture)); + if (!IsReady()) { + throw RaylibException("Failed to load Image from Texture"); + } } Image(int width, int height, ::Color color = {255, 255, 255, 255}) { @@ -621,9 +637,9 @@ class Image : public ::Image { /** * Retrieve whether or not the Image has been loaded. * - * @return True or false depending on whether the image has been loaded. + * @return True or false depending on whether the Image has been loaded. */ - inline bool IsLoaded() const { + inline bool IsReady() const { return data != nullptr; } diff --git a/include/Music.hpp b/include/Music.hpp index 5da27e2..9b34479 100644 --- a/include/Music.hpp +++ b/include/Music.hpp @@ -5,6 +5,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -21,6 +22,9 @@ class Music : public ::Music { */ Music(const std::string& fileName) { set(::LoadMusicStream(fileName.c_str())); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Music from file: %s", fileName.c_str())); + } } /** @@ -28,6 +32,9 @@ class Music : public ::Music { */ Music(const std::string& fileType, unsigned char* data, int dataSize) { set(::LoadMusicStreamFromMemory(fileType.c_str(), data, dataSize)); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Music from %s file", fileType.c_str())); + } } Music(const Music&) = delete; @@ -174,9 +181,9 @@ class Music : public ::Music { /** * Retrieve whether or not the Music has been loaded. * - * @return True or false depending on whether the music has been loaded. + * @return True or false depending on whether the Music has been loaded. */ - inline bool IsLoaded() const { + inline bool IsReady() const { return stream.buffer != nullptr; } diff --git a/include/RaylibException.hpp b/include/RaylibException.hpp new file mode 100644 index 0000000..49a0be2 --- /dev/null +++ b/include/RaylibException.hpp @@ -0,0 +1,36 @@ +#ifndef RAYLIB_CPP_INCLUDE_RAYLIBEXCEPTION_HPP_ +#define RAYLIB_CPP_INCLUDE_RAYLIBEXCEPTION_HPP_ + +#include + +#include + +#include "raylib.h" + +namespace raylib { +class RaylibException : public std::runtime_error { + protected: + public: + RaylibException(std::string msg) throw() : std::runtime_error(msg) + { + + } + + + /** Destructor. + * Virtual to allow for subclassing. + */ + //virtual ~Except() throw () {} + + /** + * Outputs the exception message to TraceLog(). + * + * @param logLevel The output status to use when outputing. + */ + void TraceLog(int logLevel = LOG_ERROR) { + ::TraceLog(logLevel, std::runtime_error::what()); + } +}; +} + +#endif // RAYLIB_CPP_INCLUDE_RAYLIBEXCEPTION_HPP_ diff --git a/include/Sound.hpp b/include/Sound.hpp index 4dff368..40eb05f 100644 --- a/include/Sound.hpp +++ b/include/Sound.hpp @@ -5,6 +5,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -29,10 +30,16 @@ class Sound : public ::Sound { Sound(const std::string& fileName) { set(LoadSound(fileName.c_str())); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Sound from file: %s", fileName.c_str())); + } } Sound(const ::Wave& wave) { set(LoadSoundFromWave(wave)); + if (!IsReady()) { + throw RaylibException("Failed to load Sound from Wave"); + } } ~Sound() { @@ -154,7 +161,7 @@ class Sound : public ::Sound { * * @return True or false depending on whether the Sound buffer is loaded. */ - bool IsLoaded() { + bool IsReady() { return stream.buffer != nullptr; } diff --git a/include/Texture.hpp b/include/Texture.hpp index a4ab2da..eb72638 100644 --- a/include/Texture.hpp +++ b/include/Texture.hpp @@ -7,6 +7,7 @@ #include "./raylib-cpp-utils.hpp" #include "./Vector2.hpp" #include "./Material.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -20,6 +21,9 @@ class Texture : public ::Texture { Texture(const ::Image& image) { LoadFromImage(image); + if (!IsReady()) { + throw RaylibException("Failed to load Texture from Image"); + } } /** @@ -29,6 +33,9 @@ class Texture : public ::Texture { */ Texture(const ::Image& image, int layout) { LoadCubemap(image, layout); + if (!IsReady()) { + throw RaylibException("Failed to load Texture from Cubemap"); + } } /** @@ -36,6 +43,9 @@ class Texture : public ::Texture { */ Texture(const std::string& fileName) { Load(fileName); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Texture from file: %s", fileName.c_str())); + } } Texture(const Texture&) = delete; @@ -271,6 +281,15 @@ class Texture : public ::Texture { return *this; } + /** + * Determines whether or not the Texture has been loaded and is ready. + * + * @return True or false depending on whether the Texture has data. + */ + bool IsReady() { + return width != 0; + } + private: inline void set(const ::Texture& texture) { id = texture.id; diff --git a/include/Wave.hpp b/include/Wave.hpp index cb18d30..9a14a61 100644 --- a/include/Wave.hpp +++ b/include/Wave.hpp @@ -5,6 +5,7 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" +#include "./RaylibException.hpp" namespace raylib { /** @@ -28,6 +29,9 @@ class Wave : public ::Wave { */ Wave(const std::string& fileName) { set(::LoadWave(fileName.c_str())); + if (!IsReady()) { + throw RaylibException(TextFormat("Failed to load Wave from file: %s", fileName.c_str())); + } } /** @@ -35,6 +39,9 @@ class Wave : public ::Wave { */ Wave(const std::string& fileType, const unsigned char *fileData, int dataSize) { set(::LoadWaveFromMemory(fileType.c_str(), fileData, dataSize)); + if (!IsReady()) { + throw RaylibException("Failed to load Wave from memory"); + } } Wave(const Wave& other) { @@ -179,7 +186,7 @@ class Wave : public ::Wave { * * @return True or false depending on whether the wave data has been loaded. */ - inline bool IsLoaded() const { + inline bool IsReady() const { return data != nullptr; } diff --git a/include/raylib-cpp.hpp b/include/raylib-cpp.hpp index b6161ff..f92bc5e 100644 --- a/include/raylib-cpp.hpp +++ b/include/raylib-cpp.hpp @@ -49,6 +49,7 @@ #include "./Mouse.hpp" #include "./Music.hpp" #include "./Ray.hpp" +#include "./RaylibException.hpp" #include "./RayCollision.hpp" #include "./Rectangle.hpp" #include "./RenderTexture.hpp" diff --git a/tests/raylib_test.cpp b/tests/raylib_test.cpp index 43b9ced..b0f876c 100644 --- a/tests/raylib_test.cpp +++ b/tests/raylib_test.cpp @@ -82,7 +82,21 @@ int main(int argc, char *argv[]) { // Sound { raylib::Wave wave(path + "/resources/weird.wav"); - assert(wave.IsLoaded()); + assert(wave.IsReady()); + } + + // RaylibException + { + bool passed = false; + try { + raylib::Texture texture("notfound.png"); + } + catch (raylib::RaylibException& error) { + error.TraceLog(LOG_INFO); + passed = true; + } + + assert(passed); } TraceLog(LOG_INFO, "TEST: raylib-cpp test complete");