SFML - sf::rectangle wont show up..?

Pages: 12
I have a project, in thia, a warrior fights against an archer.
The problem is, that everytime I texture.loadFromFile("sword.png") ,
for getting my sword, my Archer wont show up, if I delete this
texture.loadFromFile("sword.png") it pretty much works, if not, the Archer x position is -3.1643e+038 .
anyways I dont see any sense behind this, could be the compiler or whatever.

If u got an answer. Tell me ^^
The problem is, that everytime I texture.loadFromFile("sword.png")


"every time"? Hopefully you're only loading it once.


Anyway... it's impossible to tell what's happening without seeing your code. Only thing I can say for sure is that it's definitely not the compiler. It's more likely some kind of memory corruption.

Can you upload the code/images somewhere so we can try it out?
i got CGame.cpp, .hpp, CArcher.cpp hpp, CArrow.cpp hpp, CSword.cpp .hpp, CKnight.cpp .hpp main.cpp, and stdFunctions.cpp hpp, what u want?
enough to reproduce your problem
I have no idea, how that is possible, whatever. ill send all.
Archer.cpp
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
#include "CArcher.hpp"

CArcher::CArcher()
{
    rect.setSize(sf::Vector2f(20, 30));
    rect.setPosition(sf::Vector2f(ScW-20,465));
    rect.setFillColor(sf::Color(75, 20, 0));
    rect.setOrigin(rect.getSize().x/2, rect.getSize().y/2);
}

void CArcher::setCoordinates(float pups)
{
 if(rect.getPosition().y < 465) // wenn nicht auf Boden
    {
        Ydirec += pups/200;
    }
    else // wenn auf Boden
    {
        Ydirec = 0;
        rect.setPosition(rect.getPosition().x, 465);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) // W gedrückt
        {
            Ydirec = -pups;
        }
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && rect.getPosition().x > 0) // wenn A gedrückt und nicht links vom Bild
    {
        direction = 0;

        Xdirec -= pups/200;

    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && rect.getPosition().x < ScW) // wenn D gedrückt und nicht rechts vom Bild
    {
        direction = 1;

        Xdirec += pups/200;
    }
    if(rect.getPosition().x+rect.getSize().x/2 >= ScW && Xdirec > 0 ||
            rect.getPosition().x-rect.getSize().x/2 <= 0 && Xdirec < 0)
        Xdirec = 0;

    Xdirec /= 1.01f;

    rect.move(Xdirec, Ydirec);

}
Archer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CARCHER_HPP
#define CARCHER_HPP

#include "CGame.hpp"

class CArcher
{
public:
    CArcher();
    sf::RectangleShape rect;
    void setCoordinates(float pups);
    float Xdirec;
    float Ydirec;
    bool direction;
};
#endif // CARCHER_HPP 
main.cpp
1
2
3
4
5
6
7
#include "CGame.hpp"

int main ()
{
	CGame::getInstance().run();
    return 0;
}
CGame.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "CGame.hpp"

CGame::CGame()
{
    ground.setSize(sf::Vector2f(ScW, ScH/5));
    ground.setPosition(sf::Vector2f(0, ScH-ScH/5));
    ground.setFillColor(sf::Color::Black);
    arrowReady = true;
}

void CGame::run()
{
    float clockCounter = 0;
    sf::Clock clock;
    window = new sf::RenderWindow(sf::VideoMode(ScW, ScH), "Battle");
    knight = new CKnight;
    sword = new CSword;
    archer = new CArcher;

    while (window->isOpen())
    {
        sf::Event event;
        window->pollEvent(event);
        if (event.type == sf::Event::Closed)
            window->close();

        clockCounter += clock.restart().asMicroseconds();
        if(clockCounter >= 2000)
        {
            onTick(1);
            clockCounter = 0;
        }
    }

    delete knight;
    delete sword;
    delete window;
    delete archer;
}

CGame& CGame::getInstance()
{
    static CGame instance;
    return instance;
}

void CGame::onTick(float pups)
{
    knight->setCoordinates(pups);
    sword->setCoordinates();
    sword->attack(pups);
    archer->setCoordinates(pups);
    for(unsigned int i = 0; i < arrows.size(); i++)
    {
        arrows[i]->setCoordinates(pups);
    }
    for(unsigned int i = 0; i < arrows.size(); i++)
    {
        if(arrows[i]->rect.getPosition().x >= ScW ||
                arrows[i]->rect.getPosition().x <= 0)
        {
            delete arrows[i];
            arrows.erase(arrows.begin()+i);
        }

    }

    window->clear(sf::Color::White);
    window->draw(ground);
    window->draw(archer->rect);
    for(unsigned int i = 0; i < arrows.size(); i++)
    {
        window->draw(arrows[i]->rect);
    }
    window->draw(knight->rect);
    window->draw(sword->sprite);
    window->display();


    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && arrows.size() < 5 && arrowReady == true)
    {
        arrows.push_back(new CArrow);
        arrowReady = false;

    }
    if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        arrowReady = true;

} // onTick()
CGame.hpp
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
#ifndef CGAME_HPP
#define CGAME_HPP

#include <SFML/Graphics.hpp>
#include <iostream>



#define ScH 600
#define ScW 800

class CKnight;
class CSword;
class CArcher;
class CArrow;

class CGame
{
private:
    CGame();
    sf::RenderWindow* window;
    void onTick(float pups);
    sf::RectangleShape ground;
    bool arrowReady;



public:
    CSword* sword;
    CKnight* knight;
    CArcher* archer;
    std::vector<CArrow*> arrows;
	void run();
	static CGame& getInstance();

};

#include "CKnight.hpp"
#include "CSword.hpp"
#include "CArcher.hpp"
#include "CArrow.hpp"
#include "StdFunctions.hpp"

#endif // CGame 
CArrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "CArrow.hpp"

CArrow::CArrow()
{
    rect.setSize(sf::Vector2f(30, 5));
    rect.setFillColor(sf::Color(20, 75, 0));
    rect.setOrigin(rect.getSize().x/2, rect.getSize().y/2);
    direction = CGame::getInstance().archer->direction;
    rect.setPosition(CGame::getInstance().archer->rect.getPosition());
}

void CArrow::setCoordinates(float pups)
{
    if(direction)
    {
        rect.move(pups, 0);
    }

    else
    {
        rect.move(-pups, 0);
    }
}
CArrow.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CARROW_HPP
#define CARROW_HPP

#include "CGame.hpp"

class CArrow
{
public:
    CArrow();
    void setCoordinates(float pups);
    sf::RectangleShape rect;
    bool direction;
};
#endif // CARROW_HPP 
CKnight.cpp
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
#include "CKnight.hpp"

CKnight::CKnight()
{
    rect.setSize(sf::Vector2f(20, 30));
    rect.setPosition(sf::Vector2f(20,465));
    rect.setFillColor(sf::Color::Blue);
    rect.setOrigin(rect.getSize().x/2, rect.getSize().y/2);
}

void CKnight::setCoordinates(float pups)
{
    if(rect.getPosition().y < 465) // wenn nicht auf Boden
    {
        Ydirec += pups/200;
    }
    else // wenn auf Boden
    {
        Ydirec = 0;
        rect.setPosition(rect.getPosition().x, 465);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) // W gedrückt
        {
            Ydirec = -pups;
        }
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && rect.getPosition().x > 0) // wenn A gedrückt und nicht links vom Bild
    {
        direction = 0;

        Xdirec -= pups/200;

    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && rect.getPosition().x < ScW) // wenn D gedrückt und nicht rechts vom Bild
    {
        direction = 1;

        Xdirec += pups/200;
    }
    if(rect.getPosition().x+rect.getSize().x/2 >= ScW && Xdirec > 0 ||
            rect.getPosition().x-rect.getSize().x/2 <= 0 && Xdirec < 0)
        Xdirec = 0;

    Xdirec /= 1.01f;

    rect.move(Xdirec, Ydirec);
}
CKnight.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CKNIGHT_HPP
#define CKNIGHT_HPP

#include "CGame.hpp"

class CKnight
{
public:
    CKnight();
    sf::RectangleShape rect;
    void setCoordinates(float pups);
    float Ydirec;
    float Xdirec;
    bool direction;
};
#endif // CKNIGHT_HPP 
CSword.cpp
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
#include "CSword.hpp"

CSword::CSword()
{
    rect.setFillColor(sf::Color(100, 100, 100));
    rect.setSize(sf::Vector2f(10, 50));
    if(!texture.loadFromFile("png/sword.png"))
        std::cout << "sword.png file not found";
    sprite.setTexture(texture);
}

void CSword::setCoordinates()
{
    sprite.setPosition(rect.getPosition());
    sprite.setRotation(rect.getRotation());
    sprite.setOrigin(rect.getOrigin());
    if(attackOn == 0)
    {
        rect.setOrigin(rect.getSize().x/2, rect.getSize().y);
    }

    if(attackOn)
    {
 if(CGame::getInstance().knight->direction)
            rect.setPosition(CGame::getInstance().knight->rect.getPosition().x+10, CGame::getInstance().knight->rect.getPosition().y);
        else
            rect.setPosition(CGame::getInstance().knight->rect.getPosition().x-10, CGame::getInstance().knight->rect.getPosition().y);
    }
    else
    {
        if(CGame::getInstance().knight->direction)
            rect.setPosition(CGame::getInstance().knight->rect.getPosition().x+10, CGame::getInstance().knight->rect.getPosition().y+30);
        else
            rect.setPosition(CGame::getInstance().knight->rect.getPosition().x-10, CGame::getInstance().knight->rect.getPosition().y+30);
    }

}

void CSword::attack(float pups)
{
    rect.setOrigin(rect.getSize().x/2, rect.getSize().y+20);
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || attackOn > 0)
    {

        attackOn += 1;
        if(CGame::getInstance().knight->direction == 1)
        {
            rect.setRotation(rotationProp(attackOn-90));
        }
        else
        {
            rect.setRotation(90-attackOn);
        }


        if(attackOn >= 180)
        {
            attackOn = 0;
            rect.setRotation(0);
        }
    }

}
CSword.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CSWORD_HPP
#define CSWORD_HPP

#include "CGame.hpp"

class CSword
{
public:
    CSword();
    sf::RectangleShape rect;
    void setCoordinates();
    void attack(float pups);
    int attackOn;
    sf::Texture texture;
    sf::Sprite sprite;
};
#endif // SCWORD_HPP 
StdFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "StdFunctions.hpp"

float rotationProp(float rotationArg)
{
          while(rotationArg < 0)
    {
        rotationArg += 360;
    }

    while (rotationArg > 360)
    {
        rotationArg -= 360;
    }

    return rotationArg;



}

StdFunctions.hpp
 
float rotationProp(float rotationArg);


Thats it.
ne555 wrote:
enough to reproduce your problem
Bolbi wrote:
I have no idea, how that is possible, whatever. ill send all.
Remove all code that does not affect your problem. Once you are down the the bare minimum amount of code where the problem still happens, it's either obvious what the problem is, or it's small enough for us to find it for you.
Last edited on
I have no idea, where the problem is.
could be everywhere

probably in CGame.cpp / Carcher.cpp or CSword.cpp/.hpp
Last edited on
Yes, the whole point of removing code until the problem goes away is to find out which code is needed to cause the problem. You're not expected to know where the problem is.
Pages: 12