Reputation: 47
My window closes when I draw a sprite.
It's definitely the drawing part of it because when I keep it out of my code, it works fine, except it doesn't draw my sprite of course.
Also I get this error when running: Segmentation fault (core dumped)
I don't know what that means :/ .
And here is my code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//create vars
sf::Color bgColour(20, 175, 215);
vector<sf::Sprite> tiles;
void CreateTile(string Texture, int x, int y)
{
sf::Vector2f Pos(x, y);
sf::Texture Ftexture;
Ftexture.loadFromFile(Texture);
sf::Sprite Tile;
Tile.setTexture(Ftexture);
Tile.setPosition(Pos);
tiles.push_back(Tile);
}
int main()
{
//create window
sf::RenderWindow window(sf::VideoMode(800, 600), "-\\\\-(Game)-//-");
CreateTile("Recources/grass.png", 40, 40);
//main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(bgColour);
window.draw(tiles[1]);
window.display();
}
return 0;
}
Thanks!
Upvotes: 1
Views: 484
Reputation: 66
You are trying to acces an element on the vector that do not exist.
change this
window.draw(tiles[1]);
to this
window.draw(tiles[0]);
Upvotes: 1