File indexing completed on 2024-04-21 04:02:08

0001 /******************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                     *
0003 *   SPDX-FileCopyrightText: 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com>      *
0004 *                           Julian Helfferich <julian.helfferich@mailbox.org> *
0005 *                                                                             *
0006 *   SPDX-License-Identifier: GPL-2.0-or-later
0007 ******************************************************************************/
0008 #include "KBlocksRepWin.h"
0009 
0010 #include <QPixmapCache>
0011 
0012 #include <QDateTime>
0013 
0014 #include "kblocks_replay_debug.h"
0015 
0016 KBlocksRepWin::KBlocksRepWin(
0017     GraphicsInterface *graphics,
0018     SoundInterface *sound,
0019     const char *replayFile,
0020     bool binaryMode
0021 ) : KMainWindow()
0022 {
0023     //Use up to 3MB for global application pixmap cache
0024     QPixmapCache::setCacheLimit(3 * 1024);
0025 
0026     mUpdateInterval = 1000;
0027 
0028     mpGameReplayer = new KBlocksGameReplayer(replayFile, binaryMode);
0029 
0030     mGameCount = mpGameReplayer->getGameCount();
0031     if (mGameCount == 0) {
0032         mpGameScene = nullptr;
0033         mpGameView = nullptr;
0034         mpGameLogic = nullptr;
0035         return;
0036     }
0037 
0038     mpGameLogic = new KBlocksGameLogic(mpGameReplayer);
0039     if (mpGameReplayer->isSameSeed()) {
0040         mpGameLogic->setGameSeed(mpGameReplayer->getGameSeed());
0041     } else {
0042         mpGameLogic->setGameSeed(-mpGameReplayer->getGameSeed());
0043     }
0044     mpGameLogic->setGamePunish(false);
0045     mpGameLogic->setGameStandbyMode(false);
0046     mpGameLogic->setInitInterval(0);
0047     mpGameLogic->setLevelUpInterval(0);
0048 
0049     mpGameScene = new KBlocksScene(mpGameLogic, graphics, sound, mGameCount);
0050 
0051     mpGameView = new KBlocksView(mpGameScene, this);
0052     mpGameView->show();
0053     setCentralWidget(mpGameView);
0054 
0055     mUpdateTimer.setInterval(mUpdateInterval);
0056     connect(&mUpdateTimer, &QTimer::timeout, this, &KBlocksRepWin::replayOneStep);
0057     mUpdateTimer.stop();
0058 
0059     mSnapshotFilename = QLatin1String("");
0060     mSnapshotFolder = QStringLiteral("./snapshot/");
0061 }
0062 
0063 KBlocksRepWin::~KBlocksRepWin()
0064 {
0065     delete mpGameView;
0066     delete mpGameScene;
0067     delete mpGameLogic;
0068     delete mpGameReplayer;
0069 }
0070 
0071 void KBlocksRepWin::setGamesPerLine(int count)
0072 {
0073     if (mpGameScene) {
0074         mpGameScene->setGamesPerLine(count);
0075     }
0076 }
0077 
0078 void KBlocksRepWin::setUpdateInterval(int interval)
0079 {
0080     if (mpGameScene) {
0081         mUpdateInterval = interval;
0082         mUpdateTimer.setInterval(mUpdateInterval);
0083         mpGameScene->setUpdateInterval(interval);
0084     }
0085 }
0086 
0087 void KBlocksRepWin::setReplayStepLength(int stepLen)
0088 {
0089     mpGameReplayer->setStepLength(stepLen);
0090 }
0091 
0092 void KBlocksRepWin::setSnapshotFolder(const QString &folder)
0093 {
0094     mSnapshotFolder = folder;
0095 }
0096 
0097 void KBlocksRepWin::setSnapshotFilename(const QString &fileName)
0098 {
0099     mSnapshotFilename = fileName;
0100 }
0101 
0102 bool KBlocksRepWin::replayLoaded()
0103 {
0104     return (mGameCount != 0);
0105 }
0106 
0107 void KBlocksRepWin::startReplay()
0108 {
0109     if (mpGameLogic) {
0110         mpGameLogic->startGame(mGameCount);
0111     }
0112 
0113     if (mpGameScene) {
0114         mpGameScene->createGameItemGroups(mGameCount);
0115         mpGameScene->startGame();
0116     }
0117 
0118     mUpdateTimer.start();
0119 }
0120 
0121 void KBlocksRepWin::stopReplay()
0122 {
0123     mUpdateTimer.stop();
0124 
0125     if (mpGameScene) {
0126         mpGameScene->stopGame();
0127         mpGameScene->deleteGameItemGroups();
0128     }
0129 
0130     if (mpGameLogic) {
0131         mpGameLogic->stopGame();
0132     }
0133 }
0134 
0135 QString KBlocksRepWin::getTimeString()
0136 {
0137     QDate tmpDate = QDate::currentDate();
0138     QTime tmpTime = QTime::currentTime();
0139     QString result;
0140     result = QStringLiteral("%1-%2-%3_%4-%5-%6_%7")
0141              .arg(tmpDate.year(), 4, 10, QLatin1Char('0'))
0142              .arg(tmpDate.month(), 2, 10, QLatin1Char('0'))
0143              .arg(tmpDate.day(), 2, 10, QLatin1Char('0'))
0144              .arg(tmpTime.hour(), 2, 10, QLatin1Char('0'))
0145              .arg(tmpTime.minute(), 2, 10, QLatin1Char('0'))
0146              .arg(tmpTime.second(), 2, 10, QLatin1Char('0'))
0147              .arg(tmpTime.msec(), 3, 10, QLatin1Char('0'));
0148     return result;
0149 }
0150 
0151 void KBlocksRepWin::snapshotView()
0152 {
0153     if (!mSnapshotFilename.isEmpty()) {
0154         //mSnapshoter = QPixmap::grabWindow(mpGameView->winId());
0155         mSnapshoter = grab();
0156         QString tmpFilename = mSnapshotFolder + mSnapshotFilename + QStringLiteral("_")
0157                               + getTimeString() + QStringLiteral(".png");
0158         mSnapshoter.save(tmpFilename);
0159     }
0160 }
0161 
0162 void KBlocksRepWin::replayOneStep()
0163 {
0164     int tmpPieceChanged = 0;
0165     if (!mpGameLogic->playRecordOneStep(&tmpPieceChanged)) {
0166         qCDebug(KBReplay) << "Finished Replay!";
0167         mUpdateTimer.stop();
0168     }
0169     if (tmpPieceChanged != 0) {
0170         snapshotView();
0171     }
0172 }
0173 
0174 #include "moc_KBlocksRepWin.cpp"