0

I'm trying to, only using SMFL and no other external libraries, capture 120 frames per second and saving them into a folder called "captured_frames" which I have to manually add in the executable folder before running the code. The problem is whenever I run the below code I repeatedly get the error "Failed to save image ''" error message

I tried to save it as induvial png's directly into the folder of the executable but that doesn't worth either. I also tried lowering the number of frames to be captured in a second, which also failed. I changed the folder from read-only to read-write, and the folder is in the same folder as the .vcxproj (Im using VS). This is the code, in bold is of interest:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics/Image.hpp>
#include <iostream>
#include <vector>
int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Frame Capture");
    sf::Clock clock;
    const float captureInterval = 1.0f / 120.0f;
    int frameCount = 0;
    sf::RectangleShape rectangle1(sf::Vector2f(200, 100));
    rectangle1.setPosition(0, 200);
    rectangle1.setFillColor(sf::Color::Blue);

    sf::RectangleShape rectangle2(sf::Vector2f(200, 100));
    rectangle2.setPosition(300, 200);
    rectangle2.setFillColor(sf::Color::Green);

    sf::RectangleShape rectangle3(sf::Vector2f(200, 100));
    rectangle3.setPosition(700, 200);
    rectangle3.setFillColor(sf::Color::Red);
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
//capturing frames

        

> if (clock.getElapsedTime().asSeconds() >= captureInterval) {
>             sf::Texture texture;
>             texture.create(window.getSize().x, window.getSize().y);
>             texture.update(window);
> 
>             sf::Image frameImage = texture.copyToImage();
>             std::string filename = "captured_frames/frame_" + std::to_string(frameCount) + ".png";
>             frameImage.saveToFile(filename);
> 
>             std::cout << "Frame captured and saved: " << filename << std::endl;
> 
>             clock.restart();
>             ++frameCount;
>         }

        window.clear(sf::Color::Black);

        // Draw graphics
        window.draw(rectangle1);
        window.draw(rectangle2);
        window.draw(rectangle3);

        window.display();
    }

    return 0;
}

Edit: I am getting this error, which I assume means the code doesn't have permission to memory: Exception thrown at 0x00007FFFC6367646 (sfml-graphics-2.dll) in SFML.exe: 0xC0000005: Access violation reading location 0x00000001F4F761E7

JKombos
  • 1
  • 1
  • 1
    Welcome to SO! What does `frameImage.getSize()` returns? Image should not be empty. And does the app have permission to write in that folder? – Ripi2 Aug 23 '23 at 18:51
  • @Ripi2 The frame isn't empty, it returns the correct x and y value. By permission do you mean through file explorer? Or is there something in VS I have to do. In file explorer all access is granted. – JKombos Aug 23 '23 at 20:35
  • @Ripi2 I opened VS as admin, still doesn't work – JKombos Aug 23 '23 at 21:27
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Aug 24 '23 at 04:33
  • @JesperJuhl The page was a good read but couldnt you post that link to any question? – JKombos Aug 25 '23 at 00:00

0 Answers0