Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
namespace Textures { enum ID { Landscape, Airplane, Missile }; } class Game { public: Game(); void run(); private: void processEvents(); void update(sf::Time deltaTime); void render(); void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed); void updateStatistics(sf::Time elapsedTime); sf::RenderWindow mWindow; bool mIsMovingUp; bool mIsMovingDown; bool mIsMovingLeft; bool mIsMovingRight; static const float PlayerSpeed; static const sf::Time TimePerFrame; sf::Texture mTexture; sf::Sprite mPlayer; sf::Text mStatisticsText; sf::Time mStatisticsUpdateTime; sf::Font mFont; std::size_t mStatisticsNumFrames; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
template <typename Ressource, typename Identifier> class RessourceHolder { public: void load(Identifier id, const std::string& filename); template <typename Parameter> void load(Identifier id, const std::string& filename, const Parameter& secondParam); Ressource& get(Identifier id); const Ressource& get(Identifier id) const; private: std::map<Identifier, std::unique_ptr<Ressource>> mRessourceMap; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
template <typename Ressource, typename Identifier> void RessourceHolder<Ressource, Identifier>::load(Identifier id, const std::string& filename) { std::unique_ptr<Ressource> ressource(new Ressource()); if (!ressource->loadFromFile(filename)) throw std::runtime_error("RessourceHolder::load - Failed to load" + filename); //Nachfolgendes gibt die passende Consolenausgabe "xy loaded" aus if (ressource->loadFromFile(filename)) { std::cout << filename << " loaded" << std::endl; } auto inserted = mRessourceMap.insert(std::make_pair(id, std::move(ressource))); assert(inserted.second); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
template <typename Ressource, typename Identifier> Ressource& RessourceHolder<Ressource, Identifier>::get(Identifier id) { auto found = mRessourceMap.find(id); assert(found != mRessourceMap.end()); return *found->second; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 |
{ mTexture.loadFromFile("Media/Textures/Eagle.png"); mPlayer.setTexture(mTexture); // So funktioniert es RessourceHolder<sf::Texture, Textures::ID> textures; textures.load(Textures::Airplane, "Media/Textures/Eagle.png"); mPlayer.setTexture(textures.get(Textures::Airplane)); // so funktionert es nicht! } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Alter Hase
Beruf: Softwareentwickler (aktuell Web/Node); Freiberuflicher Google Proxy
Ändere mal das return *found->second in return *(found->second.get());
Quellcode |
|
1 |
error C2039: 'get' : is not a member of 'sf::Texture' |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Deine Textur wird irgendwo ungültig. Oft geschieht das dadurch, dass die Texture zum Zeitpunkt des Draw-Befehls nicht mehr existiert. Ein Blick auf deine Resource-Klasse wäre noch schön.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
template <typename Ressource, typename Identifier> class RessourceHolder { public: void load(Identifier id, const std::string& filename); template <typename Parameter> void load(Identifier id, const std::string& filename, const Parameter& secondParam); Ressource& get(Identifier id); const Ressource& get(Identifier id) const; private: std::map<Identifier, std::unique_ptr<Ressource>> mRessourceMap; }; //Template Implementation template <typename Ressource, typename Identifier> void RessourceHolder<Ressource, Identifier>::load(Identifier id, const std::string& filename) { std::unique_ptr<Ressource> ressource(new Ressource()); if (!ressource->loadFromFile(filename)) throw std::runtime_error("RessourceHolder::load - Failed to load" + filename); if (ressource->loadFromFile(filename)) { std::cout << filename << " loaded" << std::endl; } auto inserted = mRessourceMap.insert(std::make_pair(id, std::move(ressource))); assert(inserted.second); } template <typename Ressource, typename Identifier> template <typename Parameter> void RessourceHolder<Ressource, Identifier>::load(Identifier id, const std::string& filename, const Parameter& secondParam) { std::unique_ptr<Ressource> ressource(new Ressource()); if (!ressource->loadFromFile(filename, secondParam)) throw std::runtime_error("RessourceHolder::load - Failed to load " + filename); auto inserted = mRessourceMap.insert(std::make_pair(id, std::move(ressource))); assert(inserted.second); } template <typename Ressource, typename Identifier> Ressource& RessourceHolder<Ressource, Identifier>::get(Identifier id) { auto found = mRessourceMap.find(id); assert(found != mRessourceMap.end()); return *found->second; } template <typename Ressource, typename Identifier> const Ressource& RessourceHolder<Ressource, Identifier>::get(Identifier id) const { auto found = mRessourceMap.find(id); assert(found != mRessourceMap.end()); return *found->second; } |
Wieso sf::Texture? Sollte da nicht ein unique_ptr rauskommen?
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Ja, aber woher kommt dann der Compile-Fehler? Du willst einen sf::Texture& zurückgeben und .get() sollte den sf::Texture* liefern.
"found" ist der Iterator, "found->second()" der unique_ptr, "found->second().get()" der sf:Texture*. Oder was übersehe ich?
C-/C++-Quelltext |
|
1 2 |
mTexture = textures.get(Textures::Airplane); mPlayer.setTexture(mTexture); |
C-/C++-Quelltext |
|
1 |
mPlayer.setTexture(textures.get(Textures::Airplane)); |
Werbeanzeige