From 1f1379a5a5c095c1b0648d918c834b8aeb27d63a Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 17 Jun 2021 11:17:18 +0200 Subject: [PATCH 1/2] when a resource is not found, an exceptions is thrown --- lib/Ray/sources/Drawables/Image.cpp | 8 +++++++- lib/Ray/sources/Drawables/Texture.cpp | 10 ++++++++-- lib/Ray/sources/Exceptions/RayError.cpp | 5 +++++ lib/Ray/sources/Exceptions/RayError.hpp | 17 +++++++++++++++++ lib/Ray/sources/Font.cpp | 8 +++++++- lib/Ray/sources/Model/Model.cpp | 8 +++++++- lib/Ray/sources/Model/ModelAnimations.cpp | 8 +++++++- screenshot000.png | Bin 82420 -> 0 bytes sources/Runner/LobbyScene.cpp | 2 +- sources/Runner/Runner.cpp | 10 ++-------- sources/main.cpp | 7 ++++++- 11 files changed, 67 insertions(+), 16 deletions(-) delete mode 100644 screenshot000.png 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 e4060bf1c23c5d11d6397ecd306201439e42a350..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 82420 zcmeI&KWkrA0LS5L37QlOj#5w%1aZqy(7~mDY#Y!b;w%9jy16)XaHtLiL2;<7lg_>Y z7ex?pkjX4;(zi@#&DE_Y%TGe&6%S<$n6som-bKUcR_(+ohA+H{Ra1 zr|xgt!K;T)p8e0CkAJwnJ#qEBlN(pxx%cjS@1LIj^Xu`&o7YdT9Gv^hgO3i6KmY0X zyN91W`2O5q{yO~l(a~>@zI^TSg|A;YKHrxgUp%<^(*0Wx|33Fmb@%Y;htE9w)%N_G zznuH0I{OB1Ts!^n!o4dmfAK$n9Utvr-vIlMzkk3Mf9rq&>+0FC#p4MWu!M-L2+6!6 zfdT8E1xkPMFC}2WIt|6JL0RyJ=d%7&Htd(6KO}QCV8B|h!gXGU>#beuk<@D-rn){PNGAHg`|5r z-U4jYm|N{vfdNzcJ&uKI$3wUZ*M3rjt8mR3Dl0y)O_glkCV zYQTUg{hlt1Ype8ub!e5o((kl*d&k>5i4GkWlJ4nv3$RgRZna+p22APqI2NuQ58*0Y z`$-Y5!Zl~8tO!Z>bi4)Fs4=$^u8UT{fHm^>que8ErQfwx`buBvFD?TxV4c#Ghtdzp zTn!ko)~j$`Tw7qkIt|6JL0RyJ=d%7&HtI&@e_x~JnUz($R^)qWKiFs0w)Sh#jPgsX7vCq=jl*PNlUA|&0@@fKjC#@tG{ zE?NNt*2v$Fa*wE$e%Ds%D}ANExD3F6bxKnnN9V-CN*`E|M^zl=xr;Q#;t 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; + } } From 57f895b563036449d4765f8714dee988a2782dd9 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Thu, 17 Jun 2021 12:07:07 +0200 Subject: [PATCH 2/2] remve ascii escape --- lib/Ray/sources/Exceptions/RayError.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Ray/sources/Exceptions/RayError.cpp b/lib/Ray/sources/Exceptions/RayError.cpp index 24fb4be8..ee0d4fa7 100644 --- a/lib/Ray/sources/Exceptions/RayError.cpp +++ b/lib/Ray/sources/Exceptions/RayError.cpp @@ -28,6 +28,6 @@ RAY::Exception::WrongInputError::WrongInputError(const std::string &what): } RAY::Exception::ResourceNotFound::ResourceNotFound(const std::string &path): - RayError("\033[36m" + path + "\033[31m couldn't be loaded\033[0m") + RayError(path + " couldn't be loaded") { }