diff --git a/lib/Ray/sources/Drawables/Image.cpp b/lib/Ray/sources/Drawables/Image.cpp index a95246be..6b971898 100644 --- a/lib/Ray/sources/Drawables/Image.cpp +++ b/lib/Ray/sources/Drawables/Image.cpp @@ -11,7 +11,13 @@ #include "Exceptions/RayError.hpp" namespace RAY { - Cache<::Image> Image::_imagesCache(LoadImage, UnloadImage); + Cache<::Image> Image::_imagesCache([] (const char *str) { + ::Image image = LoadImage(str); + + if (image.data == nullptr) + throw Exception::ResourceNotFound(std::string(str)); + return image; + }, UnloadImage); Image::Image(const std::string &filename, bool lonely): Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE), diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index ee017382..8114f98d 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -11,10 +11,16 @@ namespace RAY { - Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture); + Cache<::Texture> Texture::_texturesCache([] (const char *str) { + ::Texture texture = LoadTexture(str); + + if (texture.id <= 0) + throw Exception::ResourceNotFound(std::string(str)); + return texture; + }, UnloadTexture); Texture::Texture() - : Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE, 0, 0) + : Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE, 1, 0), _resourcePath("") {} Texture::Texture(const std::string &filename, bool lonely, float scale, float rotation): diff --git a/lib/Ray/sources/Exceptions/RayError.cpp b/lib/Ray/sources/Exceptions/RayError.cpp index a4de1200..24fb4be8 100644 --- a/lib/Ray/sources/Exceptions/RayError.cpp +++ b/lib/Ray/sources/Exceptions/RayError.cpp @@ -26,3 +26,8 @@ RAY::Exception::WrongInputError::WrongInputError(const std::string &what): RayError(what) { } + +RAY::Exception::ResourceNotFound::ResourceNotFound(const std::string &path): + RayError("\033[36m" + path + "\033[31m couldn't be loaded\033[0m") +{ +} diff --git a/lib/Ray/sources/Exceptions/RayError.hpp b/lib/Ray/sources/Exceptions/RayError.hpp index 158640d6..43c5590f 100644 --- a/lib/Ray/sources/Exceptions/RayError.hpp +++ b/lib/Ray/sources/Exceptions/RayError.hpp @@ -76,6 +76,23 @@ namespace RAY::Exception { //! @brief A default assignment operator WrongInputError &operator=(const WrongInputError &) = default; }; + + //! @brief exception used when a resource is not found + class ResourceNotFound: public RayError { + public: + //! @brief Create a new exception instance + //! @param path path of the un-loadable path + explicit ResourceNotFound(const std::string &path); + + //! @brief A default destructor + ~ResourceNotFound() override = default; + + //! @brief An exception is copy constructable + ResourceNotFound(const ResourceNotFound &) = default; + + //! @brief A default assignment operator + ResourceNotFound &operator=(const ResourceNotFound &) = default; + }; } #endif /* !RAYERROR_HPP_ */ diff --git a/lib/Ray/sources/Font.cpp b/lib/Ray/sources/Font.cpp index 89bf9517..50bb26a8 100644 --- a/lib/Ray/sources/Font.cpp +++ b/lib/Ray/sources/Font.cpp @@ -7,7 +7,13 @@ #include "Font.hpp" -RAY::Cache<::Font> RAY::Font::_fontsCache(LoadFont, UnloadFont); +RAY::Cache<::Font> RAY::Font::_fontsCache([] (const char *str) { + ::Font font = LoadFont(str); + + if (font.texture.id <= 0) + throw Exception::ResourceNotFound(std::string(str)); + return font; + }, UnloadFont); RAY::Font::Font(const std::string &filename, bool lonely): _font(_fontsCache.fetch(filename, lonely)) diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index b694fb4d..59a508c6 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -13,7 +13,13 @@ namespace RAY::Drawables::Drawables3D { - RAY::Cache<::Model> Model::_modelsCache(LoadModel, UnloadModel); + RAY::Cache<::Model> Model::_modelsCache([] (const char *str) { + ::Model model = LoadModel(str); + + if (model.meshCount == 0) + throw Exception::ResourceNotFound(std::string(str)); + return model; + }, UnloadModel); Model::Model(const std::string &filename, bool lonely, diff --git a/lib/Ray/sources/Model/ModelAnimations.cpp b/lib/Ray/sources/Model/ModelAnimations.cpp index 6d41832d..b1eedebc 100644 --- a/lib/Ray/sources/Model/ModelAnimations.cpp +++ b/lib/Ray/sources/Model/ModelAnimations.cpp @@ -7,7 +7,13 @@ #include "Model/ModelAnimations.hpp" -RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations); +RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache([] (const char *str, int *counter) { + ::ModelAnimation *modelanimations = LoadModelAnimations(str, counter); + + if (modelanimations == nullptr) + throw Exception::ResourceNotFound(std::string(str)); + return modelanimations; + }, UnloadModelAnimations); RAY::ModelAnimations::ModelAnimations(const std::string &filePath): _animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount)), diff --git a/screenshot000.png b/screenshot000.png deleted file mode 100644 index e4060bf1..00000000 Binary files a/screenshot000.png and /dev/null differ diff --git a/sources/Runner/LobbyScene.cpp b/sources/Runner/LobbyScene.cpp index 7254006e..667e84f6 100644 --- a/sources/Runner/LobbyScene.cpp +++ b/sources/Runner/LobbyScene.cpp @@ -196,7 +196,7 @@ namespace BBM auto &ready = scene->addEntity("ready") .addComponent(224 * (i + 1) + 200 * i, 1080 / 3, 0) // todo check why it does this | hacky way to fix ready texture - .addComponent(""); + .addComponent(); player.addComponent(i, ready, playerTile); } scene->addEntity("camera") diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2bcc5fc4..15fcbb64 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -145,13 +145,7 @@ namespace BBM Runner::enableRaylib(wal); Runner::loadScenes(); wal.changeScene(Runner::gameState._loadedScenes[GameState::SceneID::SplashScreen]); - - try { - wal.run(Runner::updateState, Runner::gameState); - return 0; - } catch (const std::exception &ex) { - std::cerr << ex.what() << std::endl; - return 1; - } + wal.run(Runner::updateState, Runner::gameState); + return 0; } } \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index c4e69c4a..9cf4bf85 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -23,5 +23,10 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } - return BBM::Runner::run(); + try { + return BBM::Runner::run(); + } catch (const std::exception &ex) { + std::cerr << ex.what() << std::endl; + return 84; + } }