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