(C++) QtExample4

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) QtExample4

 

QtQt
CreatorLubuntuUbuntu

 

Qt example 4: moving many sprites over a changing background in 2D is a Qt example that shows how to move many sprites over a changing background in 2D, like this screenshot (png).

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • Qt Qt: version 5.4.1 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppQtExample4/CppQtExample4.pro

 


exists(../../DesktopApplication.pri) {   include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) {   QT += core gui   greaterThan(QT_MAJOR_VERSION, 4): QT += widgets   TEMPLATE = app   CONFIG(debug, debug|release) {     message(Debug mode)   }   CONFIG(release, debug|release) {     message(Release mode)     DEFINES += NDEBUG NTRACE_BILDERBIKKEL   }   QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra   unix {     QMAKE_CXXFLAGS += -Werror   } } exists(../../Libraries/Boost.pri) {   include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) {   win32 {     INCLUDEPATH += \       ../../../Projects/Libraries/boost_1_55_0   } } SOURCES += main.cpp RESOURCES += CppQtExample4.qrc

 

 

 

 

 

./CppQtExample4/main.cpp

 


#include <cassert> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <boost/shared_ptr.hpp> #include <QApplication> #include <QBitmap> #include <QFile> #include <QGraphicsPixmapItem> #include <QGraphicsScene> #include <QGraphicsView> #include <QTimer> #pragma GCC diagnostic pop struct ChangingBackground : public QGraphicsPixmapItem {   explicit ChangingBackground(const int width, const int height)     : z(0)   {     QPixmap m(width,height);     this->setPixmap(m);   }   void advance(int)   {     QImage i = this->pixmap().toImage();     const int width = i.width();     const int height = i.height();     for (int y=0;y!=height;++y)     {       for (int x=0;x!=width;++x)       {         QPoint pos(x,y);         QColor c((x+z)%256,(y+z)%256,(x+y+z)%256);         i.setPixel(pos,c.rgb());       }     }     this->setPixmap(this->pixmap().fromImage(i));     ++z;   }   private:   int z; }; struct TransparentSprite : public QGraphicsPixmapItem {   explicit TransparentSprite(     const std::string& filename,     const QColor& transparency_color = QColor(0,255,0)) //Lime green     : dx( ((std::rand() >> 4) % 3) - 1), //Random direction       dy( ((std::rand() >> 4) % 3) - 1), //Random direction       maxx(0),       maxy(0)   {     QPixmap pixmap(filename.c_str());     const QBitmap mask = pixmap.createMaskFromColor(transparency_color);     pixmap.setMask(mask);     this->setPixmap(pixmap);   }   void advance(int)   {     int new_x = this->x();     int new_y = this->y();     new_x+=dx;     new_y+=dy;     if (new_x<0 || new_x>maxx) dx= -dx;     if (new_y<0 || new_y>maxy) dy= -dy;     this->setX(new_x);     this->setY(new_y);   }   void setRect(const int width, const int height)   {     maxx = width - this->pixmap().width();     maxy = height - this->pixmap().height();   }   private:   int dx;   int dy;   int maxx;   int maxy; }; int main(int argc, char *argv[]) {   QApplication a(argc, argv);   QGraphicsScene s;   QGraphicsView v(&s);   ChangingBackground background(512,512);   s.addItem(&background);   std::vector<boost::shared_ptr<TransparentSprite> > sprites;   //Add multiple sprites   const int n_sprites = 25;   for (int i=0; i!=n_sprites; ++i)   {     assert(QFile::exists(":/images/Bloem.png"));     boost::shared_ptr<TransparentSprite> sprite(new TransparentSprite(":/images/Bloem.png"));     const int maxx = background.pixmap().width() - sprite->pixmap().width();     const int maxy = background.pixmap().height() - sprite->pixmap().height();     sprite->setX(std::rand() % maxx);     sprite->setY(std::rand() % maxy);     sprite->setRect(background.pixmap().width(),background.pixmap().height());     s.addItem(sprite.get());     sprites.push_back(sprite);   }   v.show();   boost::shared_ptr<QTimer> timer(new QTimer(&s));   timer->connect(timer.get(), SIGNAL(timeout()), &s, SLOT(advance()));   timer->start(20);   return a.exec(); }