Debug assertion failed

Just started working with SFML and cant get this to work im trying to get a sprite to move on the screen using this:

1st 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
#include <SFML/Graphics.hpp>
#include <iostream>

sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "GSRPG");
sf::Event Event;


sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "GSRPG");
sf::Event Event;

int tilesize = 50;
#include <SFML/Graphics.hpp>
class character
{

public: 
character()
{
    x = 0;
    y = 0;

    movespeed = 100;
    for (int i = 0; i < 4; i++)
        move[i] = false;



}


float x;
float y;

void keymove(); //apasare taste
void moving(); 
float movespeed; //viteza

enum MOVE { UP, DOWN, LEFT, RIGHT }; 
bool move[4];
bool walking;
int nextspot;

sf::Texture ptexture;
sf::Sprite pSprite;

};


2nd 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

int main()
{
Window.setVerticalSyncEnabled(true);
character midget;

sf::Texture ptexture;
if (!ptexture.loadFromFile("player.png", sf::IntRect(32, 0, 32, 32)))
    std::cout << "Error player.png";

sf::Sprite pSprite;
pSprite.setTexture(ptexture);
while (Window.isOpen())
{
    while (Window.pollEvent(Event))
    {
        if (Event.type == sf::Event::Closed) Window.close();
    }

    midget.keymove();
    midget.moving();
    midget.pSprite.setPosition(midget.x, midget.y);

    Window.clear(sf::Color(0, 200, 0));
    Window.draw(midget.pSprite);

    Window.display();
}
}


aand im clueless regarding the reason why the sprite won't show up
Last edited on
You didn't set midget's sprite, you set the other "pSprite"'s texture.
Try this:
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
int main()
{
Window.setVerticalSyncEnabled(true);
character midget;

sf::Texture ptexture;
if (!midget.ptexture.loadFromFile("player.png", sf::IntRect(32, 0, 32, 32)))
    std::cout << "Error player.png";

//sf::Sprite pSprite;
//pSprite.setTexture(ptexture);
midget.pSprite.setTexture(midget.pTexture);
while (Window.isOpen())
{
    while (Window.pollEvent(Event))
    {
        if (Event.type == sf::Event::Closed) Window.close();
    }

    midget.keymove();
    midget.moving();
    midget.pSprite.setPosition(midget.x, midget.y);

    Window.clear(sf::Color(0, 200, 0));
    Window.draw(midget.pSprite);

    Window.display();
}
}


You'll also need to change the sprite's position some way.
i almost finished what i wanted to do for now tho i keep getting an error;
here is the code
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
91
92
93
94

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

sf::RenderWindow Window(sf::VideoMode(800, 600), "GSRPG");
sf::Event Event;

std::ifstream openfile("map.txt");

#include <SFML/Graphics.hpp>
class character
{

public:
	character()
	{
		x = 0;
		y = 0;
	
		sf::Texture ptexture;
		sf::Sprite pSprite;
	}
	int x;
	int y;

	void keymove(); 
	
	sf::Texture ptexture;
	sf::Sprite pSprite;

};

/*...*/

int main()
{
	sf::Vector2i map[20][20];
	sf::Vector2i loadCounter = sf::Vector2i(0, 0);
	if (openfile.is_open())
	{
		sf::Texture tileTexture;
		sf::Sprite tiles;
		std::string tileLocation;
		openfile >> tileLocation;
		tileTexture.loadFromFile(tileLocation);
		tiles.setTexture(tileTexture);
		while (!openfile.eof())
	{/*...*/

		}
	}
	character midget;

	sf::Texture ptexture;
	if (!midget.ptexture.loadFromFile("player.png", sf::IntRect(32, 0, 32, 32)))
		std::cout << "Error player.png";
	ptexture.setSmooth(true);

	sf::Clock clock;
	sf::Time elapsed1 = clock.getElapsedTime();
	std::cout << elapsed1.asSeconds() << std::endl;
	clock.restart();
	sf::Time elapsed2 = clock.getElapsedTime();
	std::cout << elapsed2.asSeconds() << std::endl;
	//masoara timpul inapp


	while (Window.isOpen())
	{
		sf::Event Happen;
		while (Window.pollEvent(Happen))
		{
			switch (Happen.type)
			{
			case sf::Event::Closed:
				Window.close();
				break;
			}
		}


		midget.keymove();
		void sf::RenderTarget::clear(const Color&color = Color(0, 0, 0, 0));
		//midget.pSprite.setPosition(midget.x, midget.y);
		//mapCoordsToPixel(const Vector2i &point) const;
		void sf::RenderTarget::draw(const Drawable&midget.psprite);
		//Window.draw(midget.pSprite);
		Window.display();
	}
}



aand here are the errors:

error C2761: 'void sf::RenderTarget::clear(const sf::Color &)' : member function redeclaration not allowed
error C2143: syntax error : missing ',' before '.'
error C2761: 'draw' : member function redeclaration not allowed
10 IntelliSense: member function "sf::RenderTarget::clear" may not be redeclared outside its class
11 IntelliSense: no instance of overloaded function "sf::RenderTarget::draw" matches the specified type
12 IntelliSense: expected a ')'
Last edited on
The IntelliSense errors should tell you what line the errors are on.

The compiler thinks you are trying to redefine the SFML functions themselves (which your code is trying to do).
Instead, call the functions.

Window.draw(midget.pSprite); should work.

1
2
3
4
		void sf::RenderTarget::clear(const Color&color = Color(0, 0, 0, 0));
		//midget.pSprite.setPosition(midget.x, midget.y);
		//mapCoordsToPixel(const Vector2i &point) const;
		void sf::RenderTarget::draw(const Drawable&midget.psprite);

There, Visual Studio thinks you are trying to redefine the SFML functions themselves. Did you mean to implement these in your class? SFML can be complicated, so I would recommend trying to keep things simple. There is no need to redefine SFML functions.

Use Window.draw to draw things on the screen - that is what the draw function was meant for.
You didn't set "midget"'s texture, so if it draws it will just show up as a white rectangle.
The lack of sleep is showing its toll
put the drawing part in // and now im getting Debug assertion failed...
wat do?
Please post the errors themselves. Just copy them from the build console/window and post them here.
I can't really tell you what is wrong without seeing your code and the errors.
One thing to keep in mind is that defining global sfml objects is a no-no. Sfml requires its internal global objects to be constructed before any you declare and there is no way to accomplish that in C++ except not to use global objects of your own.
Topic archived. No new replies allowed.