Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: white images  (Read 2377 times)

0 Members and 1 Guest are viewing this topic.

zilcho

  • Newbie
  • *
  • Posts: 6
    • View Profile
white images
« on: August 20, 2012, 04:17:53 pm »
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;

}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: white images
« Reply #1 on: August 20, 2012, 04:24:28 pm »
Using the search function could've answered your question in no time... ::)

It's a basic principle of C++, thus you probably should over your books again, and is caused through your init function. You create an instance of sf::Image and assign it to a sprite and as soon as you leave the init function the instance of the sf::Image will get deleted (i.e. scope lifetime) thus there doesn't exist an image anymore that could be shown since the sprite only references the sf::Image and doesn't copy it (since this would be a waste).
You'll have to keep the sf::Image alive.

On top of this I highly and strongly advice you to use SFML 2, since there are already precompiled binaries and it's less buggy than SFML 1.6. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zilcho

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: white images
« Reply #2 on: August 20, 2012, 05:16:59 pm »
But the Shape class contains an sf Image so when I create them wouldn't the image be saved there? Since in the Shape constructor I set the image to the image in the argument.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: white images
« Reply #3 on: August 20, 2012, 05:29:54 pm »
Oh so you're using your own shape class, didn't notice that. ;)
But you don't show the code of the shape class so I don't know how things work in there...

What compiler are you using?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zilcho

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: white images
« Reply #4 on: August 20, 2012, 05:32:53 pm »
#ifndef SHAPE_H
#define SHAPE_H

#include <SFML/Graphics.hpp>

class Shape
{
private:
        sf::Image image;
        sf::Sprite sprite;
        sf::Vector2<float> pos;
        sf::Vector2<float> dir;
        sf::RenderWindow *a;
        int radius;

public:

        Shape(int boundingRadius, sf::Vector2<float> position,sf::Vector2<float> direction, sf::Image i, sf::RenderWindow *window);
        void update();
        void draw();
        sf::Vector2<float> getPosition();
        int getRadius();

};

#endif

#include "shape.h"

Shape::Shape(int boundingRadius, sf::Vector2<float> position, sf::Vector2<float> direction,sf::Image i, sf::RenderWindow *window)
{
        image = i;
        pos = position;
        dir = direction;
        radius = boundingRadius;
        sprite.SetImage(image);
        sprite.SetPosition(pos);
        a = window;
}

void Shape::draw()
{
        a->Draw(sprite);
};
sf::Vector2<float> Shape::getPosition()
{
        return pos;
};
int Shape::getRadius()
{
        return radius;
};

void Shape::update()
{

        pos = pos+dir;
        sprite.SetPosition(pos);
       
        if(pos.x<-20)
        {
                dir.x = dir.x *(-1);

        }
        if(pos.x>560)
        {
                dir.x = dir.x *(-1);
        }
        if(pos.y<-20)
        {
                dir.y = dir.y *(-1);
        }
        if(pos.y>360)
        {
                dir.y = dir.y *(-1);
        }

};


Also I use sfml 1.6 atm because I was hacing some trouble with sfml2 .

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: white images
« Reply #5 on: August 20, 2012, 05:37:06 pm »
What about if you don't but the ShapeHandler instance in the global scope? You know one should always (if possible) avoid global instances! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: white images
« Reply #6 on: August 20, 2012, 06:33:34 pm »
Shapes are in a vector so they are at some point copied but you don't provide any code to manage that (you need to implement a copy ctor and, maybe, also the op=).

You must update the sprite's link to its image after both were copied.
SFML / OS X developer

zilcho

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: white images
« Reply #7 on: August 20, 2012, 07:17:16 pm »
Thanks for the help. :)

Adding a cop constructor fixed it for me. Huzaas to you two!.