feat: Adding resource handling Chapter 2, changed game.cpp so it comfors to the new way of handling textures

This commit is contained in:
PolishPigeon 2022-01-22 09:38:29 +01:00 committed by KRZYSZTOF RUDNICKI
parent 5ff5476304
commit 26f0b0c0ec
5 changed files with 54 additions and 7 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

View File

@ -1,8 +1,14 @@
#ifndef GAME_CPP
#define GAME_CPP
#include <vector>
#include <SFML/Graphics.hpp>
#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

Binary file not shown.

View File

@ -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<sf::Texture> > 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<sf::Texture> 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