Bonjour,
j'essaye de precharger des textures en memoire, pour un chargement plus dynamique.
Voici ma classe qui va garder tout en memoire...
Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
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 class Memory { public: Memory() {}; ~Memory() { std::cout << "Call ~Memory() " << std::endl; } /// Pas important. void preload(const std::string& pack) { Reader* own_reader = new Reader(); Node* child = own_reader->Read(pack); m_nodes.insert(std::make_pair(Filesystem::extractFilename(pack), std::make_pair(own_reader, child))); } /// Ceci, marche a merveille ! Aucune exception levée void loadGFX(const std::string& pack, const long id) { try { Node& node(m_nodes[pack].second->find(id)); MemoryRessource<sf::Texture>* res = new MemoryRessource<sf::Texture>(node.get(m_nodes[pack].first->stream())); m_loaded_gfx.insert(std::make_pair(MemoryKey(node.getId(), node.getOffset()), res)); } catch(...) { throw false; } } Node& operator [] (const std::string& key) { return *(m_nodes[key].second); } Node& operator [] (const char* key) { return *(m_nodes[std::string(key)].second); } inline bool isLoaded(const MemoryKey& key) { return m_loaded_gfx.find(key) != m_loaded_gfx.end(); } template<class S> static inline S& getGFX(const long id) /// longer to perform ...! { std::for_each(std::begin(m_loaded_gfx), std::end(m_loaded_gfx), [&](std::pair<MemoryKey, MemoryRessource<sf::Texture>*> res) { if(res.first.first == id) { return res.second->get(); } }); } /// Retournera un sf::Texture template<class S> static inline S& getGFX(const MemoryKey& key) { return m_loaded_gfx[key]->get(); } private: static std::map<std::string, std::pair<Reader*, Node*>> m_nodes; static std::map<MemoryKey, MemoryRessource<sf::Texture>*> m_loaded_gfx; };
En premier, je fais un void preload(), pas important, cela va remplir le membre m_nodes.
Ensuite, je vais faire loadGFX() avec un identifiant. Cela va charger une sf::Texture dans m_loaded_gfx.
Voici la classe MemoryRessource:
Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
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 template<class S> class MemoryRessource { public: MemoryRessource() {} MemoryRessource(const std::vector<char>& bytes) : m_buffer() { if(!m_buffer.loadFromMemory(&bytes[0], bytes.size())) { std::cout << "Failed to load from memory ! " << std::endl; throw false; } else { std::cout << "Loaded " << bytes.size() << std::endl; } } ~MemoryRessource() { std::cout << "Destroying !"<<std::endl; } inline const S& get() const { return m_buffer; } private: S m_buffer; };
On remarquera que si le loadFromMemory est mauvais, une exception est levée, et attrapée dans loadGFX.
Jusqu'ici , tout va bien.
Voici maintenant le main :
Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
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
66 short SplashScreen::Run() { /// Preloading archives std::for_each(std::begin(Config::ARCHIVES), std::end(Config::ARCHIVES), [&](const std::string& pack) { std::string full_name = pack; full_name += Config::EXT; m_game.getMemory().preload(full_name); // Tout va bien ... }); Memory mem; Node& gfx(mem["Graphics"]); std::vector<long> ids_to_load = {2, 5}; std::for_each(std::begin(ids_to_load), std::end(ids_to_load), [&](long id) { try { mem.loadGFX("Graphics", id); } catch(...) { std::cout << "Could not load: " << "Graphics:" << id << std::endl; } }); /// Aucune exception levée, nous avons donc nos sf::Texture chargés en mémoire std::cout << "loading texture into sprite ......."<<std::endl; sf::Sprite s_sprite(mem.getGFX<sf::Texture>(static_cast<long>(2))); std::cout << "OK loaded"<<std::endl; /// Rendering loop while(m_game.getRenderer().getApp().isOpen() && m_game.isRunning()) { /// Clearing the window m_game.getRenderer().getApp().clear(); sf::Event event; while(m_game.getRenderer().getApp().pollEvent(event)) { /// Event handling /// SFML if(event.type == sf::Event::Closed) { /// This will automatically terminate the networker thread & nicely exit the game m_game.crash(); } } /// Drawings (SFML) /// The sprite (background) m_game.getRenderer().getApp().draw(s_sprite); /// Displayings m_game.getRenderer().getApp().display(); } return -1; }
Bref, aucune erreur d'affichée malgrès les nombreuses conditions et try{}, pourtant: écran blanc
Une idée quelqu'un ?
Merci d'avance, nico
Partager