diff --git a/SFMLEngine/makingAGameTick/app b/SFMLEngine/makingAGameTick/app index fd7ef2f..f17f686 100755 Binary files a/SFMLEngine/makingAGameTick/app and b/SFMLEngine/makingAGameTick/app differ diff --git a/SFMLEngine/makingAGameTick/constants.hpp b/SFMLEngine/makingAGameTick/constants.hpp index 3dc4db4..6074f09 100644 --- a/SFMLEngine/makingAGameTick/constants.hpp +++ b/SFMLEngine/makingAGameTick/constants.hpp @@ -22,4 +22,7 @@ const float MOVING_LEFT_SPEED = -MOVING_RIGHT_SPEED; // Time constants const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 60.f); // 1 frame is 1 / 60 of the second so we get 60 frames in a second +// Error strings +const std::string TEXTURE_LOAD_ERROR = "TextureHolder::load - Failed to load "; + #endif // CONSTANTS_HPP diff --git a/SFMLEngine/makingAGameTick/game.o b/SFMLEngine/makingAGameTick/game.o index 10cc2fc..624a22e 100644 Binary files a/SFMLEngine/makingAGameTick/game.o and b/SFMLEngine/makingAGameTick/game.o differ diff --git a/SFMLEngine/makingAGameTick/resources.cpp b/SFMLEngine/makingAGameTick/resources.cpp index f41a4d6..6ca9833 100644 --- a/SFMLEngine/makingAGameTick/resources.cpp +++ b/SFMLEngine/makingAGameTick/resources.cpp @@ -1,5 +1,7 @@ #ifndef RESOURCES_CPP #define RESOURCES_CPP + +#include // Mostly Chapter 2 // Handles resource management namespace Textures // This gives us a scope for the enumerators which allows us to write Textures::Airplane instead of just Airplane to avoid name collisions in the global scope @@ -22,13 +24,18 @@ void TextureHolder::load(Textures::ID id, const std::string& filename) // Function to load a resource, it takes one parameter for filename and one for identifier { std::unique_ptr texture(new sf::Texture()); // Create sf:Texture and store it in the unique pointer - texture -> loadFromFile(filename); // Load the texture from the filename - mTextureMap.insert(std::make_pair(id, std::move(texture))); // Insert texture into map mTextureMap, std::move used to take ownership from texture variable and transfer it to std::make_pair(), std::move moves the resource into a new place and removes it from earlier place https://en.cppreference.com/w/cpp/utility/move + if(!texture -> loadFromFile(filename))// Load the texture from the filename + { + throw std::runtime_error(TEXTURE_LOAD_ERROR + filename); + } + auto inserted = mTextureMap.insert(std::make_pair(id, std::move(texture))); // Insert texture into map mTextureMap, std::move used to take ownership from texture variable and transfer it to std::make_pair(), std::move moves the resource into a new place and removes it from earlier place https://en.cppreference.com/w/cpp/utility/move + assert(inserted.second); } sf::Texture& TextureHolder::get(Textures::ID id) // returns a reference to a texture { auto found = mTextureMap.find(id); // find returns an iterator to the found element or end() if nothing was found + assert(found != mTextureMap.end()); return *found -> second; // We have to access the second member of the pointer, then we deference it and get a texture }