diff --git a/SFMLEngine/makingAGameTick/app b/SFMLEngine/makingAGameTick/app index db3781e..d6f4799 100755 Binary files a/SFMLEngine/makingAGameTick/app and b/SFMLEngine/makingAGameTick/app differ diff --git a/SFMLEngine/makingAGameTick/game.cpp b/SFMLEngine/makingAGameTick/game.cpp index e25f397..69d6b2c 100644 --- a/SFMLEngine/makingAGameTick/game.cpp +++ b/SFMLEngine/makingAGameTick/game.cpp @@ -26,13 +26,13 @@ class Game bool mIsMovingDown = false; private: sf::RenderWindow mWindow; - TextureHolder mTexture; + ResourceHolder mTexture; sf::Sprite mPlayer; }; Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer() { - //TextureHolder textures; + //ResourceHolder textures; mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE); mPlayer.setTexture(mTexture.get(Textures::Airplane)); diff --git a/SFMLEngine/makingAGameTick/game.o b/SFMLEngine/makingAGameTick/game.o index 5fc86b3..d04558e 100644 Binary files a/SFMLEngine/makingAGameTick/game.o and b/SFMLEngine/makingAGameTick/game.o differ diff --git a/SFMLEngine/makingAGameTick/resources.hpp b/SFMLEngine/makingAGameTick/resources.hpp index bfbbf0e..d107720 100644 --- a/SFMLEngine/makingAGameTick/resources.hpp +++ b/SFMLEngine/makingAGameTick/resources.hpp @@ -9,14 +9,15 @@ namespace Textures // This gives us a scope for the enumerators which allows us enum ID { Landscape, Airplane, Missile }; } -class TextureHolder +template +class ResourceHolder { public: - void load(Textures::ID id, const std::string& filename); - sf::Texture& get(Textures::ID id); - const sf::Texture& get(Textures::ID id) const; + void load(Identifier id, const std::string& filename); + Resource& get(Identifier id); + const Resource& get(Identifier id) const; private: - std::map< Textures::ID, std::unique_ptr > mTextureMap; + 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 f773553..b6d20bf 100644 --- a/SFMLEngine/makingAGameTick/resources.inl +++ b/SFMLEngine/makingAGameTick/resources.inl @@ -1,29 +1,31 @@ #ifndef RESOURCES_INL #define RESOURCES_INL - -void TextureHolder::load(Textures::ID id, const std::string& filename) +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 sf::Texture()); // Create sf:Texture and store it in the unique pointer + 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 { 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 + 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 assert(inserted.second); } -sf::Texture& TextureHolder::get(Textures::ID id) // returns a reference to a texture +template +Resource& ResourceHolder::get(Identifier 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()); + 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 } -const sf::Texture& TextureHolder::get(Textures::ID id) const // we need to be able to invoke get() also if we only have a pointer/reference to the const TextureHolder, it returns const sf::Texture so the texture cannot be changed by caller +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 { - auto found = mTextureMap.find(id); // find returns an iterator to the found element or end() if nothing was found + 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 }