Dear reader,
I have a problem which is as follow; I add some sprites to my program which are loaded by a function from within the class.
In the end I give them a command win.draw(*sprite).
It successfully does draw the sprites, but after x-amount of seconds the screen gets white and crashes (not responding Windows pop-up).
Does anyone know what is wrong?
My code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <character.h>
#include <level_1.h>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Crystallibrium", sf::Style::Close);
window.setVerticalSyncEnabled(true);
// Create clock
sf::Clock Clock;
// Create a level
level_1 level;
while (window.isOpen())
{
sf::Time Time = Clock.getElapsedTime();
float Elapsed = Time.asMilliseconds();
if(Elapsed > 0)
{
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if(event.type == sf::Event::Closed)
window.close();
// Close window : ESC key
if(event.key.code == sf::Keyboard::Escape)
window.close();
}
// Clear screen
window.clear();
// Display level
level.addSprites();
level.displayLevel(window);
// Update the window
window.display();
}
// Reset clock
Clock.restart();
}
}
my level header file:
#ifndef LEVEL_1_H
#define LEVEL_1_H
class level_1 {
private:
std::string name;
std::vector<sf::Image> images;
std::vector<sf::Texture> textures;
std::vector<sf::Sprite> sprites;
std::vector<int> spriteXpos;
std::vector<int> spriteYpos;
int posXstart;
int posYstart;
public:
level_1(); // default constructor
void addSprites();
void displayLevel(sf::RenderWindow &win);
int startPosX();
int startPosY();
};
#endif // LEVEL_1_H
and my level .CPP file:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <level_1.h>
level_1::level_1(){
name = "Get to the chopper!";
}
void level_1::addSprites(){
// create the level manually
sf::Image Image1;
Image1.create(20, 100, sf::Color::Blue);
images.push_back(Image1);
sf::Image Image2;
Image2.create(100, 10, sf::Color::Red);
images.push_back(Image2);
sf::Image Image3;
Image3.create(10, 100, sf::Color::Green);
images.push_back(Image3);
// now all the images are created manually
// we will loop through them to create textures
// which we can then convert into sprites
for(std::vector<sf::Image>::iterator pos = images.begin(); pos != images.end(); pos++)
{
// convert the image to a texture and
// put the texture into the vector
sf::Texture Texture;
Texture.loadFromImage(*pos);
textures.push_back(Texture);
}
// now all the textures are created, loop
// through them and create the sprites
for(std::vector<sf::Texture>::iterator pos = textures.begin(); pos != textures.end(); pos++)
{
sf::Sprite Sprite(*pos);
Sprite.setPosition(100, 100);
sprites.push_back(Sprite);
}
}
void level_1::displayLevel(sf::RenderWindow &win){
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
win.draw(*pos);
}
}
int level_1::startPosX(){
return posXstart;
}
int level_1::startPosY(){
return posYstart;
}
I hope someone knows what is wrong, as I have been struggling with this for a week now.
Best regards
P.S. I moved the line "level.addSprites();" out of the loop and pasted it right below where I create a new instance of the level_1 class right before the while(window.isOpen()) loop. Problem still exists though.