diff --git a/SFMLEngine/makingAGameTick/Textures/shipsheetparts2-highercontrast.PNG b/SFMLEngine/makingAGameTick/Textures/shipsheetparts2-highercontrast.PNG new file mode 100644 index 0000000..b344a4b Binary files /dev/null and b/SFMLEngine/makingAGameTick/Textures/shipsheetparts2-highercontrast.PNG differ diff --git a/SFMLEngine/makingAGameTick/app b/SFMLEngine/makingAGameTick/app index 02be0e6..09051c4 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 13f3e1b..b825e6f 100644 --- a/SFMLEngine/makingAGameTick/game.cpp +++ b/SFMLEngine/makingAGameTick/game.cpp @@ -1,8 +1,14 @@ +#ifndef GAME_CPP +#define GAME_CPP + #include #include #include "constants.hpp" +#include "resources.cpp" #include "basic.cpp" + + class Game { public: @@ -20,18 +26,16 @@ class Game bool mIsMovingDown = false; private: sf::RenderWindow mWindow; - sf::Texture mTexture; + TextureHolder mTexture; sf::Sprite mPlayer; }; Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer() { - if (!mTexture.loadFromFile(PATH_TO_PLAYER_TEXTURE)) - { - std::cout << "NO FILE: " << PATH_TO_PLAYER_TEXTURE << std::endl; - mWindow.close(); - } - mPlayer.setTexture(mTexture); + //TextureHolder textures; + mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE); + + mPlayer.setTexture(mTexture.get(Textures::Airplane)); mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION); } @@ -124,3 +128,5 @@ int main() Game game; game.run(); } + +#endif // GAME_CPP diff --git a/SFMLEngine/makingAGameTick/game.o b/SFMLEngine/makingAGameTick/game.o index 5a1e7b0..bbecd15 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 new file mode 100644 index 0000000..f41a4d6 --- /dev/null +++ b/SFMLEngine/makingAGameTick/resources.cpp @@ -0,0 +1,41 @@ +#ifndef RESOURCES_CPP +#define RESOURCES_CPP +// 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 +{ + enum ID { Landscape, Airplane, Missile }; +} + +class TextureHolder +{ + public: + void load(Textures::ID id, const std::string& filename); + sf::Texture& get(Textures::ID id); + const sf::Texture& get(Textures::ID id) const; + private: + std::map< Textures::ID, std::unique_ptr > mTextureMap; + // 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 +}; + +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 +} + +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 + 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 +{ + auto found = mTextureMap.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 +} + +#endif //RESOUIRCES_CPP