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

Author Topic: Graphics slow to load  (Read 2765 times)

0 Members and 1 Guest are viewing this topic.

manamissam

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Graphics slow to load
« on: December 30, 2013, 11:21:55 pm »
I have a program that simply loads and displays an image in a window. I made the same program with all the code inside the main method and I had no problems with loading time. However, now that I've separated some parts into objects, the loading time for the image is at least 5 seconds. Before, it took less than a second. I just need some advice as to how to get this thing to load faster. Thanks.

07.cpp
#include <SFML/Graphics.hpp>
#include "07.h"

int main() {

        sf::RenderWindow window(sf::VideoMode(800,600), "Objects");
        window.setFramerateLimit(45);

        sf::Event e;
        sf::Image p;
        p.loadFromFile("Bird.png");
        Bird b(p);
        sf::Texture tex;
        sf::Sprite sprite;
        tex.loadFromImage(b.getImage());
        sprite.setTexture(tex);

        while(window.isOpen()) {

                if(window.pollEvent(e) && e.type == sf::Event::Closed) window.close();
                window.clear(sf::Color::Black);

                window.draw(sprite);
               
                window.display();

        }

}

07 - Bird.cpp
#include "07.h"

sf::Image image;

Bird::Bird(sf::Image i) {

        image = i;

}

sf::Image Bird::getImage(){

        return image;

}

07.h
#ifndef BIRD_H
#define BIRD_H

#include <SFML/Graphics.hpp>

class Bird {

        public:
                Bird(sf::Image i);
                sf::Image getImage();

};

#endif

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Graphics slow to load
« Reply #1 on: December 30, 2013, 11:34:20 pm »
You copy the image unnecessarily. Use references to const for parameters and return types.

And remove the global image in Bird.cpp.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

manamissam

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Graphics slow to load
« Reply #2 on: December 30, 2013, 11:42:36 pm »
Thanks for the response. Did you mean like this? Same issue - very slow loading.

07.cpp
#include <SFML/Graphics.hpp>
#include "07.h"

int main() {

        sf::RenderWindow window(sf::VideoMode(800,600), "Objects");
        window.setFramerateLimit(45);

        sf::Event e;
        Bird b;
        sf::Texture tex;
        sf::Sprite sprite;
        tex.loadFromImage(b.getImage());
        sprite.setTexture(tex);

        while(window.isOpen()) {

                if(window.pollEvent(e) && e.type == sf::Event::Closed) window.close();
                window.clear(sf::Color::Black);

                window.draw(sprite);
               
                window.display();

        }

}

07 - Bird.cpp
#include "07.h"

sf::Image image;

Bird::Bird() {

        image.loadFromFile("Bird.png");

}

sf::Image Bird::getImage(){

        return image;

}

07.h
#ifndef BIRD_H
#define BIRD_H

#include <SFML/Graphics.hpp>

class Bird {

        public:
                Bird();
                sf::Image getImage();

};

#endif

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Graphics slow to load
« Reply #3 on: December 31, 2013, 12:20:12 am »
You made some unrelated change, but you did not use references (the root of your problem) nor remove the global variable (replace it with a member to avoid later problems if you use more than one image).
Searching the net for something like "C++ references" and "C++ member variables" may yield some useful information for you.

manamissam

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Graphics slow to load
« Reply #4 on: December 31, 2013, 04:04:21 pm »
I researched what you said, but I'm still a little confused. I am using member variables and references, yet it still loads very slowly. Should I being changing something in the 07.cpp file? Thank you for the help by the way.

07.cpp
#include <SFML/Graphics.hpp>
#include "07.h"

int main() {

    sf::RenderWindow window(sf::VideoMode(800,600), "Objects");
    window.setFramerateLimit(45);

    sf::Event e;
    sf::Image p;
    p.loadFromFile("Bird.png");
    Bird b(p);
    sf::Texture tex;
    sf::Sprite sprite;
    tex.loadFromImage(b.getImage());
    sprite.setTexture(tex);

    while(window.isOpen()) {

        if(window.pollEvent(e) && e.type == sf::Event::Closed) window.close();
        window.clear(sf::Color::Black);

        window.draw(sprite);
       
        window.display();

    }

}

07 - Bird.cpp
#include "07.h"

sf::Image Bird::Image;

Bird::Bird(const sf::Image& i) {

    Bird::Image = i;

}

sf::Image Bird::getImage(){

    return Bird::Image;

}

07.h
#ifndef BIRD_H
#define BIRD_H

#include <SFML/Graphics.hpp>

class Bird {

        public:
                Bird(const sf::Image& i);
                sf::Image getImage();
        private:
                static sf::Image Image;

};

#endif

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10913
    • View Profile
    • development blog
    • Email
Re: Graphics slow to load
« Reply #5 on: December 31, 2013, 06:08:36 pm »
You're making a copy of the image here:
    Bird::Image = i;

and here:
return Bird::Image;

Besides that, you're using a static variable, which essentially is just a global variable bound to a class. There's really no reason to use globals.

And why are you using images instead of using textures directly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/