Add Exception support

This commit is contained in:
Rob Loach
2022-01-22 00:50:24 -05:00
parent e318c6147f
commit 3b8ce73d49
11 changed files with 136 additions and 7 deletions

View File

@@ -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.

View File

@@ -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",

View File

@@ -26,6 +26,7 @@ install(FILES
Physics.hpp
Ray.hpp
RayCollision.hpp
RaylibException.hpp
raylib-cpp-utils.hpp
raylib-cpp.hpp
raylib.hpp

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -0,0 +1,36 @@
#ifndef RAYLIB_CPP_INCLUDE_RAYLIBEXCEPTION_HPP_
#define RAYLIB_CPP_INCLUDE_RAYLIBEXCEPTION_HPP_
#include <stdexcept>
#include <string>
#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_

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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"

View File

@@ -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");