Hello,
I'm new by using SFML and already searched alot about how to use it.
I stucked by trying to make a function in a class.
Actually it works, ... but it seems to have problems with the return value.
I don't really program long time with c++, so dont slap me when some things could be
very wrong
The function "create()" in the class "doSprite" should shorten some things in the "main.cpp"
when i wanna make a sprite. The class isnt finished, but it is ready to test if it works.
Here the code:
doSprite.hpp#include <SFML/System.hpp>
class doSprite
{
private:
public:
//Member Variablen
sf::String directory;
//Position
int xpos;
int ypos;
//Farbe
int c_red;
int c_green;
int c_blue;
int c_trans;
//Funktionen
int create();
//Konstruktor
doSprite();
//Destruktor
~doSprite();
};
doSprite.cpp#include "doSprite.hpp"
int doSprite::create(sf::String directory, int xpos, int ypos, int c_red, int c_green, int c_blue, int c_trans)
{
sf::Texture texturName;
if(!tx_player.loadFromFile(directory))
return 1;
sf::Sprite sprSprite;
spr_player.setTexture(sprSprite);
spr_player.setPosition(xpos,ypos);
spr_player.setColor(sf::Color(c_red,c_green,c_blue,c_trans));
return sprSprite;
}
//Konstruktor
doSprite::doSprite()
{
//Member Variablen
directory = "Sprites/Player/2.png";
//Position
xpos = 0;
ypos = 0;
//Farbe
c_red = 255;
c_green = 255;
c_blue = 255;
c_trans = 255;
}
//Destruktor
doSprite::~doSprite()
{
}
main.cpp#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "doSprite.hpp"
int main()
{
sf::VideoMode VMode = sf::VideoMode::getDesktopMode();
sf::RenderWindow window(VMode, "SFML works!",sf::Style::Default);
/* Test Function in class instead of this piece of code :/
sf::Texture tx_player;
if(!tx_player.loadFromFile("Sprites/Player/2.png"))
return 1;
sf::Sprite spr_player;
spr_player.setTexture(tx_player);
spr_player.setPosition(100,100);
spr_player.setColor(sf::Color(255,255,255,255));
*/
doSprite dS;
sf::Sprite spr_player = dS.create("Sprites/Player/2.png", 200, 200, 255, 255, 255, 200);
// Das Programm läuft solange das Fenster offen ist
while (window.isOpen())
{
// Prüft alle Events, bis alle abgearbeitet sind
sf::Event event;
while (window.pollEvent(event))
{
// Mit dem "close requested" Event wird das Fenster geschlossen
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(0,255,255));
window.draw(spr_player);
window.display();
}
return 0;
}
Build Log:...\main.cpp:20: error: 'doSprite' was not declared in this scope
...\main.cpp:35: error: 'spr_player' was not declared in this scope
So the problem is, i dont really know how to return "sprSprite" from the "doSprite.cpp" to "main.cpp".
I hope you can help me
Edit: Added Build log under code.
~Unistyle~