when a resource is not found, an exceptions is thrown

This commit is contained in:
arthur.jamet
2021-06-17 11:17:18 +02:00
parent 5a948bca46
commit 1f1379a5a5
11 changed files with 67 additions and 16 deletions
+7 -1
View File
@@ -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),
+8 -2
View File
@@ -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):
+5
View File
@@ -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")
{
}
+17
View File
@@ -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_ */
+7 -1
View File
@@ -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))
+7 -1
View File
@@ -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,
+7 -1
View File
@@ -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)),
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

+1 -1
View File
@@ -196,7 +196,7 @@ namespace BBM
auto &ready = scene->addEntity("ready")
.addComponent<PositionComponent>(224 * (i + 1) + 200 * i, 1080 / 3, 0)
// todo check why it does this | hacky way to fix ready texture
.addComponent<Drawable2DComponent, RAY::Texture>("");
.addComponent<Drawable2DComponent, RAY::Texture>();
player.addComponent<LobbyComponent>(i, ready, playerTile);
}
scene->addEntity("camera")
+2 -8
View File
@@ -145,13 +145,7 @@ namespace BBM
Runner::enableRaylib(wal);
Runner::loadScenes();
wal.changeScene(Runner::gameState._loadedScenes[GameState::SceneID::SplashScreen]);
try {
wal.run<GameState>(Runner::updateState, Runner::gameState);
return 0;
} catch (const std::exception &ex) {
std::cerr << ex.what() << std::endl;
return 1;
}
wal.run<GameState>(Runner::updateState, Runner::gameState);
return 0;
}
}
+6 -1
View File
@@ -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;
}
}