From c035ebee9a6f6272827ae0ec4f695f027a7ca2dc Mon Sep 17 00:00:00 2001 From: PolishPigeon Date: Sat, 22 Jan 2022 11:53:03 +0100 Subject: [PATCH] feat: Added shader loading in resources --- SFMLEngine/makingAGameTick/resources.hpp | 3 +++ SFMLEngine/makingAGameTick/resources.inl | 28 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/SFMLEngine/makingAGameTick/resources.hpp b/SFMLEngine/makingAGameTick/resources.hpp index d107720..a94a191 100644 --- a/SFMLEngine/makingAGameTick/resources.hpp +++ b/SFMLEngine/makingAGameTick/resources.hpp @@ -16,6 +16,9 @@ class ResourceHolder void load(Identifier id, const std::string& filename); Resource& get(Identifier id); const Resource& get(Identifier id) const; + template + void load(Identifier id, const std::string& filename, const Parameter& secondParameter); + // Second parameter can be of sf::Shader::Type or std::string& private: std::map< Identifier, std::unique_ptr > mResourceMap; // unique_ptr are class templates that act like pointers, this allows us to work with heavyweight objects without copying them all the time, or we can store classes that are non-cpyable like sf::Shader diff --git a/SFMLEngine/makingAGameTick/resources.inl b/SFMLEngine/makingAGameTick/resources.inl index b6d20bf..0bae324 100644 --- a/SFMLEngine/makingAGameTick/resources.inl +++ b/SFMLEngine/makingAGameTick/resources.inl @@ -5,28 +5,42 @@ template void ResourceHolder::load(Identifier 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 Resource()); // Create sf:Texture and store it in the unique pointer - if(!texture -> loadFromFile(filename))// Load the texture from the filename + std::unique_ptr resource(new Resource()); // Create sf:Texture and store it in the unique pointer + if(!resource -> loadFromFile(filename))// Load the resource from the filename { throw std::runtime_error(TEXTURE_LOAD_ERROR + filename); } - auto inserted = mResourceMap.insert(std::make_pair(id, std::move(texture))); // Insert texture into map mResourceMap, 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 + auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource))); // Insert resource into map mResourceMap, std::move used to take ownership from resource 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); } template -Resource& ResourceHolder::get(Identifier id) // returns a reference to a texture +Resource& ResourceHolder::get(Identifier id) // returns a reference to a resource { auto found = mResourceMap.find(id); // find returns an iterator to the found element or end() if nothing was found assert(found != mResourceMap.end()); - return *found -> second; // We have to access the second member of the pointer, then we deference it and get a texture + return *found -> second; // We have to access the second member of the pointer, then we deference it and get a resource } template -const Resource& ResourceHolder::get(Identifier id) const // we need to be able to invoke get() also if we only have a pointer/reference to the const ResourceHolder, it returns const Resource so the texture cannot be changed by caller +const Resource& ResourceHolder::get(Identifier id) const // we need to be able to invoke get() also if we only have a pointer/reference to the const ResourceHolder, it returns const Resource so the resource cannot be changed by caller { auto found = mResourceMap.find(id); // find returns an iterator to the found element or end() if nothing was found - return *found -> second; // We have to access the second member of the pointer, then we deference it and get a texture + assert(found != mResourceMap.end()); + return *found -> second; // We have to access the second member of the pointer, then we deference it and get a resource +} + +template +template +void ResourceHolder::load(Identifier id, const std::string& filename, const Parameter& secondParameter) // loads Shaders +{ + std::unique_ptr resource(new Resource()); // Create sf:Texture and store it in the unique pointer + if(!resource -> loadFromFile(filename, secondParameter))// Load the resource from the filename + { + throw std::runtime_error(TEXTURE_LOAD_ERROR + filename); + } + auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource))); // Insert resource into map mResourceMap, std::move used to take ownership from resource 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); } #endif // RESOURCES_INL