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;
}