My little program was working fine until I moved the code that loads the images from main in to another class.
Instead of my images I only get white squares.
This is my current code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "shapeHandler.h"
using namespace std;
// Create the main window
sf::RenderWindow App(sf::VideoMode(640, 400, 32), "SFML Window");
ShapeHandler shapes;
int main(){
shapes.init(&App);
// Start main loop
while (App.IsOpened())
{
App.Clear();
shapes.draw();
App.Display();
shapes.update();
}
return EXIT_SUCCESS;
}
#ifndef SHAPEHANDLER_H
#define SHAPEHANDLER_H
#include <vector>
#include "shape.h"
#include <SFML/Graphics.hpp>
class ShapeHandler
{
private:
std::vector<Shape> shapeList;
sf::Image Image;
public:
ShapeHandler();
void update();
void init(sf::RenderWindow* r);
void draw();
};
#endif
void ShapeHandler::init(sf::RenderWindow* r)
{
Image.LoadFromFile("circle.png");
Shape circle(40,sf::Vector2<float>(5,5),sf::Vector2<float>(-1,1),Image,r);
Image.LoadFromFile("square.png");
Shape square(40,sf::Vector2<float>(80,85),sf::Vector2<float>(0,1),Image,r);
Image.LoadFromFile("star.png");
Shape star(40,sf::Vector2<float>(35,25),sf::Vector2<float>(1,0),Image,r);
Image.LoadFromFile("stary.png");
Shape stary(40,sf::Vector2<float>(15,65),sf::Vector2<float>(1,-1),Image,r);
Image.LoadFromFile("triangle.png");
Shape triangle(40,sf::Vector2<float>(35,95),sf::Vector2<float>(-1,-1),Image,r);
shapeList.push_back(circle);
shapeList.push_back(square);
shapeList.push_back(star);
shapeList.push_back(stary);
shapeList.push_back(triangle);
};
And here is the code when it worked:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "shape.h"
using namespace std;
// Create the main window
sf::RenderWindow App(sf::VideoMode(640, 400, 32), "SFML Window");
int main(){
sf::Image Image;
Image.LoadFromFile("circle.png");
Shape circle(40,sf::Vector2<float>(5,5),sf::Vector2<float>(-1,1),Image,&App);
Image.LoadFromFile("square.png");
Shape square(40,sf::Vector2<float>(80,85),sf::Vector2<float>(0,1),Image,&App);
Image.LoadFromFile("star.png");
Shape star(40,sf::Vector2<float>(35,25),sf::Vector2<float>(1,0),Image,&App);
Image.LoadFromFile("stary.png");
Shape stary(40,sf::Vector2<float>(15,65),sf::Vector2<float>(1,-1),Image,&App);
Image.LoadFromFile("triangle.png");
Shape triangle(40,sf::Vector2<float>(35,95),sf::Vector2<float>(-1,-1),Image,&App);
// Start main loop
while (App.IsOpened())
{
App.Clear();
circle.draw();
App.Display();
circle.update();
}
return EXIT_SUCCESS;
}