Back to index

libsfml  1.6+dfsg2
Main.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include "QSFMLCanvas.hpp"
00006 #include <QApplication>
00007 #include <QVBoxLayout>
00008 #include <QFrame>
00009 #include <QLabel>
00010 
00011 
00015 class MyCanvas : public QSFMLCanvas
00016 {
00017 public :
00018 
00023     MyCanvas(QWidget* Parent = NULL) :
00024     QSFMLCanvas(QSize(100, 100), 0, Parent)
00025     {
00026 
00027     }
00028 
00029 private :
00030 
00035     virtual void OnInit()
00036     {
00037         // Load the image
00038         myImage.LoadFromFile("datas/qt/sfml.png");
00039 
00040         // Setup the sprite
00041         mySprite.SetImage(myImage);
00042         mySprite.SetCenter(mySprite.GetSize() / 2.f);
00043     }
00044 
00049     virtual void OnUpdate()
00050     {
00051         sf::Event Event;
00052         while (GetEvent(Event))
00053         {
00054             // Stick the sprite to the mouse cursor
00055             if (Event.Type == sf::Event::MouseMoved)
00056             {
00057                 mySprite.SetPosition(ConvertCoords(Event.MouseMove.X, Event.MouseMove.Y));
00058             }
00059 
00060             // Adjust the size of the default view when the widget is resized
00061             if (Event.Type == sf::Event::Resized)
00062             {
00063                 GetDefaultView().SetFromRect(sf::FloatRect(0, 0, Event.Size.Width, Event.Size.Height));
00064             }
00065         }
00066 
00067         // Rotate the sprite
00068         mySprite.Rotate(GetFrameTime() * 100.f);
00069 
00070         // Clear the view
00071         Clear(sf::Color(0, 128, 0));
00072 
00073         // Draw it
00074         Draw(mySprite);
00075     }
00076 
00080     sf::Image  myImage;  
00081     sf::Sprite mySprite; 
00082 };
00083 
00084 
00091 int main(int argc, char **argv)
00092 {
00093     QApplication App(argc, argv);
00094 
00095     // Create the main frame
00096     QFrame* MainFrame = new QFrame;
00097     MainFrame->setWindowTitle("Qt SFML");
00098     MainFrame->resize(400, 400);
00099     MainFrame->show();
00100 
00101     // Create a label for showing some text
00102     QLabel* Label = new QLabel("This is a SFML window\nembedded into a Qt frame :", MainFrame);
00103     Label->setFont(QFont("courier new", 14, 1, false));
00104 
00105     // Create a SFML view inside the main frame
00106     MyCanvas* SFMLView = new MyCanvas(MainFrame);
00107 
00108     // Create the main layout
00109     QVBoxLayout* Layout = new QVBoxLayout;
00110     Layout->addWidget(Label, 0);
00111     Layout->addWidget(SFMLView, 1);
00112     MainFrame->setLayout(Layout);
00113 
00114     return App.exec();
00115 }