1
Général / Re : QT SFML - Le programme s'est terminé subitement
« le: Décembre 05, 2015, 11:08:35 am »
Bonjour à tous !
J'ai fini par trouver une solution à mon problème !
J'ai nettoyer mon dossier qt puis j'ai retelecharger les sources SFML pour les CMAKE puis j'ai make et install avec mingw32-make & mingw32-make install j'ai crée un projet modifier le .pro importer le code de test et ça ne marcher pas, mais j'ai eu l'idée d'utiliser les dll de la version 2.3.2 DW2 (à la place des dll obtenu après le build des sources) et ça fonctionne !
Si ça intéresse quelqu'un voici un projet qui fonctionne (chez moi ^^):
.pro
qsfmlcanvas.h
graphic-qt.cpp
qsfmlcanvas.cpp
J'ai fini par trouver une solution à mon problème !
J'ai nettoyer mon dossier qt puis j'ai retelecharger les sources SFML pour les CMAKE puis j'ai make et install avec mingw32-make & mingw32-make install j'ai crée un projet modifier le .pro importer le code de test et ça ne marcher pas, mais j'ai eu l'idée d'utiliser les dll de la version 2.3.2 DW2 (à la place des dll obtenu après le build des sources) et ça fonctionne !
Si ça intéresse quelqu'un voici un projet qui fonctionne (chez moi ^^):
.pro
(click to show/hide)
#-------------------------------------------------
#
# Project created by QtCreator 2015-11-24T13:23:33
#
#-------------------------------------------------
QT += core
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MARCHE
TEMPLATE = app
SOURCES += \
graphics-qt.cpp \
qsfmlcanvas.cpp
HEADERS += \
qsfmlcanvas.h
FORMS += mainwindow.ui
LIBS += -LC:\Qt\SFML\lib
CONFIG(release, debug|release):LIBS += -lsfml-audio -lsfml-main -lsfml-window -lsfml-graphics -lsfml-system
CONFIG(debug, debug|release):LIBS += -lsfml-audio-d -lsfml-main-d -lsfml-window-d -lsfml-graphics-d -lsfml-system-d
INCLUDEPATH += C:\Qt\SFML\include
DEPENDPATH += C:\Qt\SFML\include
#
# Project created by QtCreator 2015-11-24T13:23:33
#
#-------------------------------------------------
QT += core
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MARCHE
TEMPLATE = app
SOURCES += \
graphics-qt.cpp \
qsfmlcanvas.cpp
HEADERS += \
qsfmlcanvas.h
FORMS += mainwindow.ui
LIBS += -LC:\Qt\SFML\lib
CONFIG(release, debug|release):LIBS += -lsfml-audio -lsfml-main -lsfml-window -lsfml-graphics -lsfml-system
CONFIG(debug, debug|release):LIBS += -lsfml-audio-d -lsfml-main-d -lsfml-window-d -lsfml-graphics-d -lsfml-system-d
INCLUDEPATH += C:\Qt\SFML\include
DEPENDPATH += C:\Qt\SFML\include
qsfmlcanvas.h
(click to show/hide)
#ifndef QSFMLCANVAS_HPP
#define QSFMLCANVAS_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <QWidget.h>
#include <QTimer.h>
////////////////////////////////////////////////////////////
/// QSFMLCanvas allows to run SFML in a Qt control
////////////////////////////////////////////////////////////
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
public :
////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
///
/// \param Parent : Parent of the widget
/// \param Position : Position of the widget
/// \param Size : Size of the widget
/// \param FrameTime : Frame duration, in milliseconds (0 by default)
///
////////////////////////////////////////////////////////////
QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 0);
////////////////////////////////////////////////////////////
/// Destructor
///
////////////////////////////////////////////////////////////
virtual ~QSFMLCanvas();
private :
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
///
////////////////////////////////////////////////////////////
virtual void onInit();
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
///
////////////////////////////////////////////////////////////
virtual void onUpdate();
////////////////////////////////////////////////////////////
/// Return the paint engine used by the widget to draw itself
///
////////////////////////////////////////////////////////////
virtual QPaintEngine* paintEngine() const;
////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
///
////////////////////////////////////////////////////////////
virtual void showEvent(QShowEvent*);
////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
///
////////////////////////////////////////////////////////////
virtual void paintEvent(QPaintEvent*);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
QTimer myTimer; ///< Timer used to update the view
bool myInitialized; ///< Tell whether the SFML window has been initialized or not
};
#endif // QSFMLCANVAS_HPP
#define QSFMLCANVAS_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <QWidget.h>
#include <QTimer.h>
////////////////////////////////////////////////////////////
/// QSFMLCanvas allows to run SFML in a Qt control
////////////////////////////////////////////////////////////
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
public :
////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
///
/// \param Parent : Parent of the widget
/// \param Position : Position of the widget
/// \param Size : Size of the widget
/// \param FrameTime : Frame duration, in milliseconds (0 by default)
///
////////////////////////////////////////////////////////////
QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 0);
////////////////////////////////////////////////////////////
/// Destructor
///
////////////////////////////////////////////////////////////
virtual ~QSFMLCanvas();
private :
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
///
////////////////////////////////////////////////////////////
virtual void onInit();
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
///
////////////////////////////////////////////////////////////
virtual void onUpdate();
////////////////////////////////////////////////////////////
/// Return the paint engine used by the widget to draw itself
///
////////////////////////////////////////////////////////////
virtual QPaintEngine* paintEngine() const;
////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
///
////////////////////////////////////////////////////////////
virtual void showEvent(QShowEvent*);
////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
///
////////////////////////////////////////////////////////////
virtual void paintEvent(QPaintEvent*);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
QTimer myTimer; ///< Timer used to update the view
bool myInitialized; ///< Tell whether the SFML window has been initialized or not
};
#endif // QSFMLCANVAS_HPP
graphic-qt.cpp
(click to show/hide)
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "qsfmlcanvas.h"
#include <QApplication.h>
#include <QFrame.h>
////////////////////////////////////////////////////////////
/// Custom SFML canvas
////////////////////////////////////////////////////////////
class MyCanvas : public QSFMLCanvas
{
public :
////////////////////////////////////////////////////////////
/// Construct the canvas
///
////////////////////////////////////////////////////////////
MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) :
QSFMLCanvas(Parent, Position, Size)
{
}
private :
////////////////////////////////////////////////////////////
/// /see QSFMLCanvas::onInit
///
////////////////////////////////////////////////////////////
void onInit()
{
// Load the image
myTexture.loadFromFile("qt-sfml.png");
// Setup the sprite
mySprite.setTexture(myTexture);
mySprite.setOrigin(sf::Vector2f(myTexture.getSize()) / 2.f);
mySprite.setPosition(150,150);
clock.restart();
}
////////////////////////////////////////////////////////////
/// /see QSFMLCanvas::onUpdate
///
////////////////////////////////////////////////////////////
void onUpdate()
{
// Clear screen
clear(sf::Color(0, 0, 0));
// Rotate the sprite
mySprite.rotate(clock.getElapsedTime().asSeconds() * 50.f);
// Draw it
draw(mySprite);
clock.restart();
}
////////////////////////////////////////////////////////////
/// Member data
////////////////////////////////////////////////////////////
sf::Texture myTexture; ///< Some image to show
sf::Sprite mySprite; ///< A sprite to display the image
sf::Clock clock; ///< to get the time of each frame
};
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
QApplication App(argc, argv);
// Create the main frame
QFrame* MainFrame = new QFrame;
MainFrame->setWindowTitle("Qt SFML");
MainFrame->resize(400, 400);
MainFrame->show();
// Create a SFML view inside the main frame
MyCanvas* SFMLView = new MyCanvas(MainFrame, QPoint(20, 20), QSize(360, 360));
SFMLView->show();
return App.exec();
}
// Headers
////////////////////////////////////////////////////////////
#include "qsfmlcanvas.h"
#include <QApplication.h>
#include <QFrame.h>
////////////////////////////////////////////////////////////
/// Custom SFML canvas
////////////////////////////////////////////////////////////
class MyCanvas : public QSFMLCanvas
{
public :
////////////////////////////////////////////////////////////
/// Construct the canvas
///
////////////////////////////////////////////////////////////
MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) :
QSFMLCanvas(Parent, Position, Size)
{
}
private :
////////////////////////////////////////////////////////////
/// /see QSFMLCanvas::onInit
///
////////////////////////////////////////////////////////////
void onInit()
{
// Load the image
myTexture.loadFromFile("qt-sfml.png");
// Setup the sprite
mySprite.setTexture(myTexture);
mySprite.setOrigin(sf::Vector2f(myTexture.getSize()) / 2.f);
mySprite.setPosition(150,150);
clock.restart();
}
////////////////////////////////////////////////////////////
/// /see QSFMLCanvas::onUpdate
///
////////////////////////////////////////////////////////////
void onUpdate()
{
// Clear screen
clear(sf::Color(0, 0, 0));
// Rotate the sprite
mySprite.rotate(clock.getElapsedTime().asSeconds() * 50.f);
// Draw it
draw(mySprite);
clock.restart();
}
////////////////////////////////////////////////////////////
/// Member data
////////////////////////////////////////////////////////////
sf::Texture myTexture; ///< Some image to show
sf::Sprite mySprite; ///< A sprite to display the image
sf::Clock clock; ///< to get the time of each frame
};
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
QApplication App(argc, argv);
// Create the main frame
QFrame* MainFrame = new QFrame;
MainFrame->setWindowTitle("Qt SFML");
MainFrame->resize(400, 400);
MainFrame->show();
// Create a SFML view inside the main frame
MyCanvas* SFMLView = new MyCanvas(MainFrame, QPoint(20, 20), QSize(360, 360));
SFMLView->show();
return App.exec();
}
qsfmlcanvas.cpp
(click to show/hide)
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "qsfmlcanvas.h"
// Platform-specific headers
#ifdef Q_WS_X11
#include <Qt/qx11info_x11.h>
#include <X11/Xlib.h>
#endif
////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
////////////////////////////////////////////////////////////
QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) :
QWidget (Parent),
myInitialized (false)
{
// Setup some states to allow direct rendering into the widget
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
// Set strong focus to enable keyboard events to be received
setFocusPolicy(Qt::StrongFocus);
// Setup the widget geometry
move(Position);
resize(Size);
// Setup the timer
myTimer.setInterval(FrameTime);
}
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
QSFMLCanvas::~QSFMLCanvas()
{
// Nothing to do...
}
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
////////////////////////////////////////////////////////////
void QSFMLCanvas::onInit()
{
// Nothing to do by default...
}
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
////////////////////////////////////////////////////////////
void QSFMLCanvas::onUpdate()
{
// Nothing to do by default...
}
////////////////////////////////////////////////////////////
/// Return the paint engine used by the widget to draw itself
////////////////////////////////////////////////////////////
QPaintEngine* QSFMLCanvas::paintEngine() const
{
return 0;
}
////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
////////////////////////////////////////////////////////////
void QSFMLCanvas::showEvent(QShowEvent*)
{
if (!myInitialized)
{
// Under X11, we need to flush the commands sent to the server to ensure that
// SFML will get an updated view of the windows
#ifdef Q_WS_X11
XFlush(QX11Info::display());
#endif
// Create the SFML window with the widget handle
// The qualification is needed because QWidget::create also exists
//RenderWindow::create(winId());
RenderWindow::create(reinterpret_cast<sf::WindowHandle>(winId()));
// Let the derived class do its specific stuff
onInit();
// Setup the timer to trigger a refresh at specified framerate
connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
myTimer.start();
myInitialized = true;
}
}
////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
////////////////////////////////////////////////////////////
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
// Let the derived class do its specific stuff
onUpdate();
// Display on screen
display();
}
// Headers
////////////////////////////////////////////////////////////
#include "qsfmlcanvas.h"
// Platform-specific headers
#ifdef Q_WS_X11
#include <Qt/qx11info_x11.h>
#include <X11/Xlib.h>
#endif
////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
////////////////////////////////////////////////////////////
QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) :
QWidget (Parent),
myInitialized (false)
{
// Setup some states to allow direct rendering into the widget
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_NoSystemBackground);
// Set strong focus to enable keyboard events to be received
setFocusPolicy(Qt::StrongFocus);
// Setup the widget geometry
move(Position);
resize(Size);
// Setup the timer
myTimer.setInterval(FrameTime);
}
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
QSFMLCanvas::~QSFMLCanvas()
{
// Nothing to do...
}
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
////////////////////////////////////////////////////////////
void QSFMLCanvas::onInit()
{
// Nothing to do by default...
}
////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
////////////////////////////////////////////////////////////
void QSFMLCanvas::onUpdate()
{
// Nothing to do by default...
}
////////////////////////////////////////////////////////////
/// Return the paint engine used by the widget to draw itself
////////////////////////////////////////////////////////////
QPaintEngine* QSFMLCanvas::paintEngine() const
{
return 0;
}
////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
////////////////////////////////////////////////////////////
void QSFMLCanvas::showEvent(QShowEvent*)
{
if (!myInitialized)
{
// Under X11, we need to flush the commands sent to the server to ensure that
// SFML will get an updated view of the windows
#ifdef Q_WS_X11
XFlush(QX11Info::display());
#endif
// Create the SFML window with the widget handle
// The qualification is needed because QWidget::create also exists
//RenderWindow::create(winId());
RenderWindow::create(reinterpret_cast<sf::WindowHandle>(winId()));
// Let the derived class do its specific stuff
onInit();
// Setup the timer to trigger a refresh at specified framerate
connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
myTimer.start();
myInitialized = true;
}
}
////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
////////////////////////////////////////////////////////////
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
// Let the derived class do its specific stuff
onUpdate();
// Display on screen
display();
}