Salut, j'ai un soucis lorsque je veux dessiner une image en utilisant la SFML avec Qt, lorsque je n'utilise pas de vue l'image s'affiche bien par contre lorsque j'utilise une vue l'image ne s'affiche plus.
Voici mon code :
Les fichiers en tête :
#ifndef QSFMLCANVAS_HPP
#define QSFMLCANVAS_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <Qt/qwidget.h>
#include <Qt/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
#ifndef MYCANVAS_H
#define MYCANVAS_H
#include "qSFMLcanvas.h"
#include <SFML/Graphics.hpp>
class MyCanvas : public QSFMLCanvas {
public :
MyCanvas(QWidget *parent, const QPoint& position, const QSize& size);
void onInit();
void onUpdate();
~MyCanvas();
private :
sf::Sprite tile;
sf::Texture *text;
sf::View view;
};
#endif
Et les fichier sources :
////////////////////////////////////////////////////////////
// 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);
// 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
sf::Window::create(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();
}
#include "mycanvas.h"
#include <iostream>
using namespace sf;
using namespace std;
MyCanvas::MyCanvas(QWidget *parent, const QPoint& position, const QSize& size) : QSFMLCanvas (parent, position, size) {
}
void MyCanvas::onInit() {
view.setViewport(FloatRect(0, 0, getSize().x, getSize().y));
text = new Texture();
if (!text->loadFromFile("herbe.png"))
cout<<"Erreur while loading texture."<<endl;
tile = Sprite(*text);
tile.setPosition(0, 0);
setView(view);
}
void MyCanvas::onUpdate() {
clear();
draw(tile);
}
MyCanvas::~MyCanvas() {
delete text;
}
Le main :
#include "mycanvas.h"
#include <QApplication>
#include <QMainWindow>
int main (int argc, char* argv[]) {
QApplication app (argc, argv);
QMainWindow* frame = new QMainWindow();
MyCanvas *canvas = new MyCanvas (frame, QPoint(0, 0), QSize (500, 500));
frame->setCentralWidget(canvas);
frame->show();
return app.exec();
}
Voila quand je n'utilise pas de vue ça marche, avec la vue je n'avais pas de problème avec la SFML 1.6 mais là. :/