MixedView/mainwindow.cpp
changeset 1 3e3364c5ae1c
parent 0 061910b224a4
child 6 4c7de3e5789a
equal deleted inserted replaced
0:061910b224a4 1:3e3364c5ae1c
    33 
    33 
    34     QRect sceneRect = ui->mainGraphicsView->rect();
    34     QRect sceneRect = ui->mainGraphicsView->rect();
    35     mScene->setSceneRect(sceneRect);
    35     mScene->setSceneRect(sceneRect);
    36    // scene->setBackgroundBrush (QBrush(Qt::blue));
    36    // scene->setBackgroundBrush (QBrush(Qt::blue));
    37 
    37 
       
    38     // now set up the boundries
       
    39     setupBoundries();
       
    40 
    38     QPixmap mazeImage(":/media/maze.jpg");
    41     QPixmap mazeImage(":/media/maze.jpg");
    39     int width = screenSize.width();
    42     int width = screenSize.width();
    40     mazeImage = mazeImage.scaledToWidth(width);
    43     mazeImage = mazeImage.scaledToWidth(width);
    41     QGraphicsPixmapItem *backGround = mScene->addPixmap(mazeImage);
    44     QGraphicsPixmapItem *backGround = mScene->addPixmap(mazeImage);
    42     backGround->setPos(-140,-200);
    45     backGround->setPos(-140,-200);
    48 
    51 
    49     // lets place a pause button in the view
    52     // lets place a pause button in the view
    50     pauseButton = new QPushButton("Pause");
    53     pauseButton = new QPushButton("Pause");
    51     connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    54     connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
    52     pauseButton->setMaximumHeight(40);
    55     pauseButton->setMaximumHeight(40);
    53     pauseButton->setMaximumWidth(300);
       
    54     QGraphicsProxyWidget *buttonProxy = mScene->addWidget(pauseButton);
    56     QGraphicsProxyWidget *buttonProxy = mScene->addWidget(pauseButton);
    55     buttonProxy->setPos(0, -220);
    57     buttonProxy->setPos(0, -220);
    56 
       
    57     // now set up the boundries
       
    58     setupBoundries();
       
    59 
    58 
    60     ui->mainGraphicsView->setScene(mScene);
    59     ui->mainGraphicsView->setScene(mScene);
    61     ui->mainGraphicsView->show();
    60     ui->mainGraphicsView->show();
    62 
    61 
    63     mPacman->setPos(20,-8);
    62     mPacman->setPos(20,-8);
    67     connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(updateGraphics()));
    66     connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(updateGraphics()));
    68     mUpdateTimer->start(250);
    67     mUpdateTimer->start(250);
    69 
    68 
    70     // connect the accelerometer to the readingChanged signal
    69     // connect the accelerometer to the readingChanged signal
    71     connect(mAccelerometer, SIGNAL(readingChanged()), this, SLOT(updateReading()));
    70     connect(mAccelerometer, SIGNAL(readingChanged()), this, SLOT(updateReading()));
    72 
       
    73     //mPlayer = new QMediaPlayer;
       
    74     QMediaResource res(QUrl::fromLocalFile(":/media/pacman_intro.wav"));
       
    75     QString codec = res.audioCodec();
       
    76     QAudioFormat format;
       
    77     // Set up the format, eg.
       
    78     format.setFrequency(8000);
       
    79     format.setChannels(1);
       
    80     format.setSampleSize(8);
       
    81     format.setCodec("audio/pcm");
       
    82     format.setByteOrder(QAudioFormat::LittleEndian);
       
    83     format.setSampleType(QAudioFormat::UnSignedInt);
       
    84 
       
    85     QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
       
    86     if (!info.isFormatSupported(format)) {
       
    87         //qWarning()<<"raw audio format not supported by backend, cannot play audio.";
       
    88         return;
       
    89     }
       
    90       // file.setFileName(":/media/pacman_intro.wav");
       
    91       //  file.open(QIODevice::ReadOnly);
       
    92 
       
    93 
       
    94       //  int size = file.size();
       
    95 
       
    96        // mAudio = new QAudioOutput(format, this);
       
    97       //  file.setParent(mAudio);
       
    98 
       
    99       //  mAudio->start(&file);
       
   100        // QSound::play(":/media/pacman_intro.wav");
       
   101 
       
   102        // mPlayer->setMedia(QUrl::fromLocalFile(":/media/intro.mp3"));
       
   103         //int leg = mPlayer->duration();
       
   104        // mPlayer->setVolume(100);
       
   105         //mPlayer->play();
       
   106 
       
   107 
       
   108     mNumHits = 1;
    71     mNumHits = 1;
   109 }
    72 }
   110 
    73 
   111 MainWindow::~MainWindow()
    74 MainWindow::~MainWindow()
   112 {
    75 {
   113     delete ui;
    76     delete ui;
   114 }
    77 }
   115 
    78 
   116 void MainWindow::updateGraphics() {
    79 void MainWindow::updateGraphics() {
   117     if (mPause || mNumHits > 1)
    80     if (mPause)
   118         return;
    81         return;
   119 
    82 
   120     if (!bIsMoving)
    83     if (!bIsMoving)
   121         return;
    84         return;
       
    85     // update the pacman mouth state
       
    86     // will eventually change this to a graphic
   122     switch (mPacState) {
    87     switch (mPacState) {
   123     case 0:
    88     case 0:
   124         mPacman->setSpanAngle(5700);
    89         mPacman->setSpanAngle(5700);
   125         break;
    90         break;
   126     case 1:
    91     case 1:
   160     if (pt.y()<-156)
   125     if (pt.y()<-156)
   161         pt.setY(-156);
   126         pt.setY(-156);
   162     else if (pt.y()>145)
   127     else if (pt.y()>145)
   163         pt.setY(145);
   128         pt.setY(145);
   164 
   129 
   165 
       
   166     QList<QGraphicsItem*> itemList = mScene->items();
       
   167     int itemCount = itemList.count();
       
   168     bool contains = false;
       
   169     for (int i=0;i < itemCount; i++) {
       
   170         QGraphicsItem *item = itemList.at(i);
       
   171         if (item->contains(pt))
       
   172             contains = true;
       
   173     }
       
   174     if (mNumHits == 1) {
   130     if (mNumHits == 1) {
   175         mPacman->setPos(pt);
   131         mPacman->setPos(pt);      
   176 
       
   177     }
   132     }
   178     else
   133     else
   179         mPacman->setPos(mLastPt);
   134         mPacman->setPos(mLastPt);
   180 
       
   181 
   135 
   182 }
   136 }
   183 
   137 
   184 void MainWindow::pause() {
   138 void MainWindow::pause() {
   185     mPause = !mPause;
   139     mPause = !mPause;