1

I'm working on a game engine. I have a loading class that loads every single texture, audio, and music file before the game starts up into a shared ptr that goes into a hashmap.Specific datatype:

std::unordered_map<std::string, std::shared_ptr<Texture2D> >

I have an Entity class which has a textureMap of the following dataType:

std::shared_ptr <std::unordered_map<std::string, std::shared_ptr<Texture2D> >>

I'm using raylib for graphics and DrawTexture takes Texture2D as a parameter, not Texture2D pointer. I'm confused about how to go from the ptr to the map to the ptr and then dereference the texture, as it doesn't work to just do this:

DrawTexture(*textureMap->find("Entity"),100, 100, WHITE);

I get the following error from that line:

no suitable user-defined conversion from "std::pair<const std::string, std::shared_ptr<Texture2D>>" to "Texture2D" exists

What am I doing wrong?

I looked through the C++ docs. No C++ std call that might help worked.

/* loader */

class loaderTemp {
    std::unordered_map<std::string, std::shared_ptr<Texture2D>> textureMap;

    loaderTemp() {
        this->textureMap["Entity"] = std::make_shared<Texture2D>(LoadTexture("resources/KeplerEngineLogo.png"));
    }
};

loaderTemp tempLoader();    

/* entity */

class entityTemp {
    std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Texture2D>>> textureMap;
    entityTemp(std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Texture2D>>> textureMapArg) {

        textureMap = textureMapArg;

        DrawTexture(*textureMap->find("Entity"), 100, 100, WHITE);
    }
};

Entity testentity(std::make_shared<std::unordered_map<std::string, std::shared_ptr<Texture2D>>>(tempLoader.textureMap)); // Constructor

Keep in mind this is using raylib, so some functions are just unreproducable.

What I want to come out is of course a derefrencing of texture2D from the loader class inside of the entity class.

philipxy
  • 14,867
  • 6
  • 39
  • 83
  • 2
    [`std::unordered_map::find`](https://en.cppreference.com/w/cpp/container/unordered_map/find) returns an iterator to a pair `std::pair` of the key and the associated value. You want `textureMap->find("Entity")->second` to get the associated value of type `std::shared_ptr`. – kotatsuyaki Dec 17 '22 at 11:47
  • the way i have to write it is *(*textureMap->find("Entity")).second to get Texture2D but thank you – Bradley Fernandez Dec 18 '22 at 15:39

0 Answers0