Ich habe mich auch mal daran versucht, funktz auch soweit, fange halt keine Fehler ab zur Zeit.
Ach ja fast vergessen du must das ganze nur auf sf::Image umschreiben, da ich schon SFML 2.0 nutze und man dort sf::Texture verwenden muss.
Vielleicht hilft es dir ja weiter.
RessourceMgr.h
|
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
|
#include <string>
#include <map>
#include <iostream>
#include <SFML\Graphics.hpp>
#ifndef RESSOURCEMGR_H
#define RESSOURCEMGR_H
class RessourceMgr
{
private:
std::string m_TexturePath;
std::map<std::string, sf::Texture> m_Textures;
sf::Texture m_Texture;
public:
RessourceMgr(void);
~RessourceMgr(void);
sf::Texture &GetTexture(const std::string sourceName);
};
#endif
|
RessourceMgr.cpp
|
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
|
#include "RessourceMgr.h"
RessourceMgr::RessourceMgr(void)
{
this->m_TexturePath = "assets\\textures\\";
}
RessourceMgr::~RessourceMgr(void)
{
}
sf::Texture &RessourceMgr::GetTexture(const std::string sourceName)
{
std::map<std::string, sf::Texture>::iterator it;
it = this->m_Textures.find(sourceName);
if (it != this->m_Textures.end())
{
return it->second;
}else {
std::cout << "RessourcenManager: texture " << sourceName << " is not available... try to loading" << std::endl;
}
sf::Texture texture;
if (!texture.LoadFromFile(this->m_TexturePath + sourceName + ".png"))
{
std::cout << "RessourcenManager: loading texture " << sourceName << " failed" << std::endl;
}
std::pair<std::string, sf::Texture> tex = std::make_pair(sourceName, texture);
this->m_Textures.insert(tex);
return this->m_Textures.at(sourceName);
}
|
Player.h (entspricht bei dir CBall)
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <iostream>
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
private:
sf::Sprite m_Sprite;
public:
Player(void);
Player(sf::Texture &texture);
~Player(void);
sf::Sprite GetSprite(void);
void Move(float elapsedTime);
};
#endif
|
Player.cpp
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include "Player.h"
Player::Player(void)
{
}
Player::Player(sf::Texture &texture)
{
this->m_Sprite.SetTexture(texture);
}
Player::~Player(void)
{
}
|
Hier meine Game Klasse wo ich eine Instanz von Player erzeuge. Somit habe ich nur eine Texture geladen und kann sie unabhängig von einander nutzen.
Game.cpp (Konstruktor)
|
C-/C++-Quelltext
|
1
2
|
this->m_Player = Player(this->ResMgr.GetTexture("player"));
this->m_Player2 = Player(this->ResMgr.GetTexture("player"));
|
Ich bin jetzt kein super pro wie manch andere hier und es müsste noch ein bisschen was verfeinert werden aber das überlasse ich dann dir.