1 | /*************************************************************************** |
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | ***************************************************************************/ |
10 | #include "KBlocksRepWin.h" |
11 | |
12 | #include <KLocale> |
13 | #include <KStatusBar> |
14 | #include <QPixmapCache> |
15 | |
16 | #include <qdatetime.h> |
17 | |
18 | KBlocksRepWin::KBlocksRepWin(const char * replayFile, bool binaryMode) : KMainWindow() |
19 | { |
20 | //Use up to 3MB for global application pixmap cache |
21 | QPixmapCache::setCacheLimit(3*1024); |
22 | |
23 | mpGameReplayer = new KBlocksGameReplayer(replayFile, binaryMode); |
24 | |
25 | mGameCount = mpGameReplayer->getGameCount(); |
26 | if (mGameCount == 0) |
27 | { |
28 | return; |
29 | } |
30 | |
31 | mpGameLogic = new KBlocksGameLogic(mpGameReplayer); |
32 | if (mpGameReplayer->isSameSeed()) |
33 | { |
34 | mpGameLogic->setGameSeed(mpGameReplayer->getGameSeed()); |
35 | } |
36 | else |
37 | { |
38 | mpGameLogic->setGameSeed(-mpGameReplayer->getGameSeed()); |
39 | } |
40 | mpGameLogic->setGamePunish(false); |
41 | mpGameLogic->setGameStandbyMode(false); |
42 | mpGameLogic->setInitInterval(0); |
43 | mpGameLogic->setLevelUpInterval(0); |
44 | |
45 | mpGameScene = new KBlocksScene(mpGameLogic, mGameCount); |
46 | |
47 | mpGameView = new KBlocksView(mpGameScene, this); |
48 | mpGameView->show(); |
49 | setCentralWidget(mpGameView); |
50 | |
51 | mUpdateInterval = 1000; |
52 | mUpdateTimer.setInterval(mUpdateInterval); |
53 | connect(&mUpdateTimer, SIGNAL(timeout()), SLOT(replayOneStep())); |
54 | mUpdateTimer.stop(); |
55 | |
56 | mSnapshotFilename = QString("" ); |
57 | mSnapshotFolder = QString("./snapshot/" ); |
58 | } |
59 | |
60 | KBlocksRepWin::~KBlocksRepWin() |
61 | { |
62 | delete mpGameView; |
63 | delete mpGameScene; |
64 | delete mpGameLogic; |
65 | delete mpGameReplayer; |
66 | } |
67 | |
68 | void KBlocksRepWin::setGamesPerLine(int count) |
69 | { |
70 | mpGameScene->setGamesPerLine(count); |
71 | } |
72 | |
73 | void KBlocksRepWin::setUpdateInterval(int interval) |
74 | { |
75 | mUpdateInterval = interval; |
76 | mUpdateTimer.setInterval(mUpdateInterval); |
77 | mpGameScene->setUpdateInterval(interval); |
78 | } |
79 | |
80 | void KBlocksRepWin::setReplayStepLength(int stepLen) |
81 | { |
82 | mpGameReplayer->setStepLength(stepLen); |
83 | } |
84 | |
85 | void KBlocksRepWin::setSnapshotFolder(const QString& folder) |
86 | { |
87 | mSnapshotFolder = folder; |
88 | } |
89 | |
90 | void KBlocksRepWin::setSnapshotFilename(const QString& fileName) |
91 | { |
92 | mSnapshotFilename = fileName; |
93 | } |
94 | |
95 | bool KBlocksRepWin::replayLoaded() |
96 | { |
97 | return (mGameCount != 0); |
98 | } |
99 | |
100 | void KBlocksRepWin::startReplay() |
101 | { |
102 | mpGameLogic->startGame(mGameCount); |
103 | |
104 | mpGameScene->createGameItemGroups(mGameCount); |
105 | mpGameScene->startGame(); |
106 | |
107 | mUpdateTimer.start(); |
108 | } |
109 | |
110 | void KBlocksRepWin::stopReplay() |
111 | { |
112 | mUpdateTimer.stop(); |
113 | |
114 | mpGameScene->stopGame(); |
115 | mpGameScene->deleteGameItemGroups(); |
116 | |
117 | mpGameLogic->stopGame(); |
118 | } |
119 | |
120 | QString KBlocksRepWin::getTimeString() |
121 | { |
122 | QDate tmpDate = QDate::currentDate(); |
123 | QTime tmpTime = QTime::currentTime(); |
124 | QString result; |
125 | result = QString("%1-%2-%3_%4-%5-%6_%7" ) |
126 | .arg(tmpDate.year(), 4, 10, QLatin1Char('0')) |
127 | .arg(tmpDate.month(), 2, 10, QLatin1Char('0')) |
128 | .arg(tmpDate.day(), 2, 10, QLatin1Char('0')) |
129 | .arg(tmpTime.hour(), 2, 10, QLatin1Char('0')) |
130 | .arg(tmpTime.minute(), 2, 10, QLatin1Char('0')) |
131 | .arg(tmpTime.second(), 2, 10, QLatin1Char('0')) |
132 | .arg(tmpTime.msec(), 3, 10, QLatin1Char('0')); |
133 | return result; |
134 | } |
135 | |
136 | void KBlocksRepWin::snapshotView() |
137 | { |
138 | if (!mSnapshotFilename.isEmpty()) |
139 | { |
140 | //mSnapshoter = QPixmap::grabWindow(mpGameView->winId()); |
141 | mSnapshoter = QPixmap::grabWidget(this); |
142 | QString tmpFilename = mSnapshotFolder + mSnapshotFilename + QString("_" ) |
143 | + getTimeString() + QString(".png" ); |
144 | mSnapshoter.save(tmpFilename); |
145 | } |
146 | } |
147 | |
148 | void KBlocksRepWin::replayOneStep() |
149 | { |
150 | int tmpPieceChanged = 0; |
151 | if (!mpGameLogic->playRecordOneStep(&tmpPieceChanged)) |
152 | { |
153 | printf("Finished Replay!\n" ); |
154 | mUpdateTimer.stop(); |
155 | } |
156 | if (tmpPieceChanged != 0) |
157 | { |
158 | snapshotView(); |
159 | } |
160 | } |
161 | |