Back to index

libsfml  1.6+dfsg2
QSFMLCanvas.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include "QSFMLCanvas.hpp"
00006 #include <QEvent>
00007 
00008 // Platform-specific headers
00009 #ifdef Q_WS_X11
00010     #include <Qt/qx11info_x11.h>
00011     #include <X11/Xlib.h>
00012 #endif
00013 
00014 
00018 QSFMLCanvas::QSFMLCanvas(const QSize& Size, unsigned int FrameTime, QWidget* Parent) :
00019 QWidget(Parent)
00020 {
00021     // Resize the widget
00022     resize(Size);
00023 
00024     // Setup some states to allow direct rendering into the widget
00025     setAttribute(Qt::WA_PaintOnScreen);
00026     setAttribute(Qt::WA_OpaquePaintEvent);
00027     setAttribute(Qt::WA_NoSystemBackground);
00028 
00029     // Set strong focus to enable keyboard events to be received
00030     setFocusPolicy(Qt::StrongFocus);
00031 
00032     // Setup the timer
00033     myTimer.setInterval(FrameTime);
00034 }
00035 
00036 
00040 QSFMLCanvas::~QSFMLCanvas()
00041 {
00042     // Nothing to do...
00043 }
00044 
00045 
00050 void QSFMLCanvas::OnInit()
00051 {
00052     // Nothing to do by default...
00053 }
00054 
00055 
00060 void QSFMLCanvas::OnUpdate()
00061 {
00062     // Nothing to do by default...
00063 }
00064 
00065 
00069 QPaintEngine* QSFMLCanvas::paintEngine() const
00070 {
00071     return 0;
00072 }
00073 
00074 
00080 bool QSFMLCanvas::event(QEvent* Event)
00081 {
00082     if (Event->type() == QEvent::Polish)
00083     {
00084         // Under X11, we need to flush the commands sent to the server to ensure that
00085         // SFML will get an updated view of the windows
00086         #ifdef Q_WS_X11
00087             XFlush(QX11Info::display());
00088         #endif
00089 
00090         // Create the SFML window with the widget handle
00091         Create(winId());
00092 
00093         // Let the derived class do its specific stuff
00094         OnInit();
00095 
00096         // Setup the timer to trigger a refresh at specified framerate
00097         connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
00098         myTimer.start();
00099     }
00100 
00101     return QWidget::event(Event);
00102 }
00103 
00104 
00109 void QSFMLCanvas::paintEvent(QPaintEvent*)
00110 {
00111     // Let the derived class do its specific stuff
00112     OnUpdate();
00113 
00114     // Display on screen
00115     Display();
00116 }