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

Author Topic: Image Sequence Displaying too slowly  (Read 1360 times)

0 Members and 1 Guest are viewing this topic.

Brokkoli075

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Image Sequence Displaying too slowly
« on: December 18, 2023, 02:59:39 am »
Hey everyone,

I need some help with getting the following code to work for a 3D printer light engine. My problem is that the code was working fine at 1920x1080 resolution but at 4k the system seems to overload. I have a 100 pictures to load and the total size of the folder is around 20 MB. If I try to run the program, the pictures are displayed in very irregular time intervals. Is there any way for me to fix the overloading issue? Is there something fundamental that I'm doing wrong here? I'm a Mechanical Engineer so I'm not too adept at coding. I would be very thankful for any help here.


#include <SFML/Graphics.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <vector>
#include <string>
#include <filesystem>

#ifdef USING_WINDOWS
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
#endif

namespace fs = std::filesystem;

int main() {
    // Directory containing images
    std::string directoryPath = "C:/Lab/trial2";

    // Vector to store image paths
    std::vector<std::string> imagePaths;


    std::cout << "Adding paths to imagePaths vector." << std::endl;

    // Iterate over files in the directory and add image paths to the vector
    for (const auto& entry : fs::directory_iterator(directoryPath)) {
        imagePaths.push_back(entry.path().string());
    }

    std::cout << "Finished." << std::endl;
        // Create a window
    sf::RenderWindow window(sf::VideoMode(3840, 2160), "Projector Display", sf::Style ::None);

    // Retrieve the desktop mode of the primary monitor
    sf::VideoMode desktop = sf::VideoMode::getDesktopMode();

    // Position the window to start at the beginning of the second monitor
    // Assuming the second monitor is to the right of the primary
    window.setPosition(sf::Vector2i(desktop.width, 0));


    std::cout << "Loading images into texture." << std::endl;

    // Load images
    std::vector<sf::Texture> textures(imagePaths.size());
    for (size_t i = 0; i < imagePaths.size(); ++i) {
        if (!textures[i].loadFromFile(imagePaths[i]))
            return -1;
    }

    std::cout << "Finished. Starting main operation." << std::endl;
    // Main loop
    size_t currentImage = 0;
    sf::Clock clock, imageClock, darkScreenClock;
    bool showImage = true;


    // Pre-calculate the scale for all sprites
    float scaleX = static_cast<float>(window.getSize().x) / textures[0].getSize().x;
    float scaleY = static_cast<float>(window.getSize().y) / textures[0].getSize().y;

    // Main loop

    // Set time intervals (in seconds)
    float imageDisplayTime = 0.1f; // Time to display each image
    float darkScreenTime = 0.1f; // Time to display the dark screen
    bool allImagesShown = false;
    bool finalDarkScreenShown = false;


    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }



        // Handle image and dark screen logic
        if (clock.getElapsedTime().asSeconds() >= (showImage ? imageDisplayTime : darkScreenTime)) {
            clock.restart(); // Reset the clock for the next phase

            if (showImage) {
                currentImage++;
                if (currentImage >= textures.size()) {
                    allImagesShown = true; // All images have been shown
                    currentImage = 0;
                }
            }


            showImage = !showImage; // Toggle between image and dark screen
        }



        // Display image or dark screen
        if (showImage) {
            sf::Sprite sprite(textures[currentImage]);
            sprite.setScale(scaleX,scaleY);

            window.clear();
            window.draw(sprite);
        }
        else {
            window.clear();
        }

        window.display();

        // Exit after showing the final dark screen
        if (allImagesShown && !showImage && !finalDarkScreenShown) {
            finalDarkScreenShown = true;
            showImage = false; // Ensure one last dark screen
        }
        else if (finalDarkScreenShown && !showImage) {
            break;
        }
    }

    return 0;
       
}

 


kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Image Sequence Displaying too slowly
« Reply #1 on: December 18, 2023, 07:38:40 am »
What sort of graphics hardware are you running on? 100 images at 4k each would take up around 3GB of video ram. If your GPU is running low, it may be swapping the textures in and out of video memory.

Brokkoli075

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Image Sequence Displaying too slowly
« Reply #2 on: December 19, 2023, 12:44:20 am »
I'm running my program on a dell laptop (UHD Graphics 620). According to my task manager, my GPU should have 3.9 GB of available space. What are other options that I could use to implement this program?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Image Sequence Displaying too slowly
« Reply #3 on: December 19, 2023, 09:54:36 am »
As a test, can you r educe the number of images to 50 or so and see if it's really just the amount of pictures?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/