Hi! I recently dealt with the work of SFML + sfe, but immediately ran into a problem. When I change the video file ("movie.openFromFile(playlist[file_num]") i get memory leak, maybe I'm doing something wrong ...
*I also tried to allocate memory dynamically, but the operator "delete movie" do not release a memory.
my code:
#include <SFML/Graphics.hpp>
#include <sfeMovie/Movie.hpp>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char *argv[])
{
vector<string> playlist;
playlist.push_back("Skorbaks31.12.13_wmv2.avi");
playlist.push_back("porche_wmw.avi");
playlist.push_back("exp11_01_p1_wmv2_001.avi");
// Some settings
const std::string windowTitle = "sfeMovie Player";
const int windowWidth = 800;
const int windowHeight = 600;
bool fullscreen = false;
std::string movieFile;
if (argc < 2)
{
std::cout << "Usage: " << std::string(argv[0]) << " movie_path" << std::endl;
//return 1;
movieFile=playlist[0];
}
else
{
movieFile = std::string(argv[1]);
}
std::cout << "Going to open movie file \"" << movieFile << "\"" << std::endl;
// Create window
// sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), windowTitle, sf::Style::Close);
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), windowTitle, sf::Style::Fullscreen);
window.setFramerateLimit(60);
// Create and open movie
sfe::Movie movie;
//movie=new sfe::Movie();
if (!movie.openFromFile(movieFile))
return 1;
// Scale movie to the window drawing area and enable VSync
movie.resizeToFrame(0, 0, window.getSize().x, window.getSize().y);
window.setVerticalSyncEnabled(true);
sf::Texture pTexture;
sf::Font font1;
font1.loadFromFile("arial.ttf");
sf::Text text1;
text1.setFont(font1);
text1.setStyle(sf::Text::Bold);
text1.setColor(sf::Color::Green);
text1.setPosition(300,50);
sf::Sprite playerImage;
if (!pTexture.loadFromFile("image.png")) //,sf::IntRect(32,0,32,32))
cout<<"Error loadd image"<<endl;
playerImage.setTexture(pTexture);
float timer1=0;
sf::Clock cl1;
// Start movie playback
// movie.play();
int counter=0;
int file_num=0;
char *buf=new char(255);
playerImage.move(500,300);
movie.play();
while (window.isOpen())
{
/*
timer1 =cl1.getElapsedTime().asMilliseconds();
//itoa((int)1000/timer1,buf,10);
sprintf(buf, "%f", 1000/timer1);
text1.setString(buf);
cl1.restart();
*/
if (movie.getStatus()<2){
//movie.play();
movie.stop();
//delete movie;
//movie=NULL;
//movie=new sfe::Movie();
text1.setString("OFF");
file_num++;
if (file_num>=playlist.size())
file_num=0;
if (movie.openFromFile(playlist[file_num]))
{
window.setVerticalSyncEnabled(true);
movie.resizeToFrame(0, 0, window.getSize().x, window.getSize().y);
movie.play();
}
else
{
exit(0);
}
}
else
{
text1.setString("ON");
}
sf::Event ev;
while (window.pollEvent(ev))
{
// Window closure
if (ev.type == sf::Event::Closed ||
(ev.type == sf::Event::KeyPressed &&
ev.key.code == sf::Keyboard::Escape))
{
window.close();
}
// Handle basic controls
else if (ev.type == sf::Event::KeyPressed)
{
// Play/Pause
if (ev.key.code == sf::Keyboard::Space)
{
if (movie.getStatus() != sfe::Movie::Playing)
movie.play();
else
movie.pause();
}
// Stop
if (ev.key.code == sf::Keyboard::S)
movie.stop();
// Restart playback
if (ev.key.code == sf::Keyboard::R)
{
movie.stop();
movie.play();
}
// Toggle fullscreen mode
if (ev.key.code == sf::Keyboard::F)
{
fullscreen = !fullscreen;
// We want to switch to the full screen mode
if (fullscreen)
{
window.create(sf::VideoMode::getDesktopMode(), windowTitle, sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
movie.resizeToFrame(0, 0, window.getSize().x, window.getSize().y);
}
// We want to switch back to the windowed mode
else
{
window.create(sf::VideoMode(windowWidth, windowHeight), windowTitle, sf::Style::Close);
window.setVerticalSyncEnabled(true);
movie.resizeToFrame(0, 0, window.getSize().x, window.getSize().y);
}
}
}
}
// Render movie
counter++;
// Render movie
if (counter>2){
playerImage.rotate(0.2);
counter=0;
}
window.clear();
window.draw(movie);
window.draw(playerImage);
window.draw(text1);
window.display();
}
return 0;
}