File indexing completed on 2024-05-05 08:05:16
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 "KBlocksDisplay.h" 0009 0010 #include "AI/KBlocksAILog.h" 0011 0012 #include <KLocalizedString> 0013 #include <QStatusBar> 0014 #include <QPixmapCache> 0015 #include <QLabel> 0016 0017 class GraphicsInterface; 0018 class SoundInterface; 0019 0020 KBlocksDisplay::KBlocksDisplay( 0021 GraphicsInterface *graphics, 0022 SoundInterface *sound, 0023 int gameCount, 0024 const string &serverIP, 0025 int localPort 0026 ) : KMainWindow() 0027 { 0028 //Use up to 3MB for global application pixmap cache 0029 QPixmapCache::setCacheLimit(3 * 1024); 0030 0031 for (int i = 0; i < 8; ++i) { 0032 maScoreList[i] = 0; 0033 } 0034 0035 mpNetClient = new KBlocksNetClient(QString::fromStdString(serverIP), localPort); 0036 connect(mpNetClient, &KBlocksNetClient::dataArrived, this, &KBlocksDisplay::updateGameDisplay); 0037 0038 mGameCount = gameCount; 0039 mpGameLogic = new KBlocksGameLogic(mGameCount); 0040 mpGameLogic->setGameSeed(0); 0041 mpGameLogic->setGamePunish(false); 0042 mpGameLogic->setGameStandbyMode(true); 0043 mpGameLogic->setInitInterval(0); 0044 mpGameLogic->setLevelUpInterval(0); 0045 0046 mpGameScene = new KBlocksScene(mpGameLogic, graphics, sound, mGameCount); 0047 mpGameScene->setGameAnimEnabled(false); 0048 mpGameScene->setWaitForAllUpdate(false); 0049 0050 mpGameView = new KBlocksView(mpGameScene, this); 0051 mpGameView->show(); 0052 setCentralWidget(mpGameView); 0053 0054 mUpdateInterval = 1000; 0055 mUpdateTimer.setInterval(mUpdateInterval); 0056 connect(&mUpdateTimer, &QTimer::timeout, this, &KBlocksDisplay::updateEvent); 0057 mUpdateTimer.stop(); 0058 mScore = new QLabel(i18n("Score List : 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0")); 0059 statusBar()->addPermanentWidget(mScore); 0060 } 0061 0062 KBlocksDisplay::~KBlocksDisplay() 0063 { 0064 mpGameLogic->stopGame(); 0065 delete mpGameLogic; 0066 0067 delete mpGameView; 0068 delete mpGameScene; 0069 0070 delete mpNetClient; 0071 } 0072 0073 void KBlocksDisplay::setGamesPerLine(int count) 0074 { 0075 mpGameScene->setGamesPerLine(count); 0076 } 0077 0078 void KBlocksDisplay::setUpdateInterval(int interval) 0079 { 0080 mUpdateInterval = interval; 0081 mUpdateTimer.setInterval(mUpdateInterval); 0082 mpGameScene->setUpdateInterval(interval); 0083 } 0084 0085 void KBlocksDisplay::startDisplay() 0086 { 0087 mpGameLogic->startGame(mGameCount); 0088 for (int i = 0; i < mGameCount; i++) { 0089 mpGameLogic->getSingleGame(i)->stopGame(); 0090 } 0091 0092 mpGameScene->createGameItemGroups(mGameCount, true); 0093 mpGameScene->startGame(); 0094 0095 mUpdateTimer.start(); 0096 } 0097 0098 void KBlocksDisplay::stopDisplay() 0099 { 0100 mUpdateTimer.stop(); 0101 0102 mpGameScene->stopGame(); 0103 mpGameScene->deleteGameItemGroups(); 0104 0105 mpGameLogic->stopGame(); 0106 } 0107 0108 int KBlocksDisplay::formIntFromByte(char *data) 0109 { 0110 int value = 0; 0111 value += ((int)data[0]) & 0x000000FF; 0112 value += (((int)data[1]) << 8) & 0x0000FF00; 0113 value += (((int)data[2]) << 16) & 0x00FF0000; 0114 value += (((int)data[3]) << 24) & 0xFF000000; 0115 return value; 0116 } 0117 0118 void KBlocksDisplay::updateScore() 0119 { 0120 mScore->setText(i18n("Score List : %1 - %2 - %3 - %4 - %5 - %6 - %7 - %8", 0121 maScoreList[0], maScoreList[1], maScoreList[2], maScoreList[3], 0122 maScoreList[4], maScoreList[5], maScoreList[6], maScoreList[7])); 0123 } 0124 0125 void KBlocksDisplay::updateEvent() 0126 { 0127 char tmpByteData[5] = {'|', 'r', 'g', '|', '\0'}; 0128 mpNetClient->sendData(5, tmpByteData); 0129 } 0130 0131 void KBlocksDisplay::updateGameDisplay(int size) 0132 { 0133 char *tmpByteData = new char[size]; 0134 0135 int ret = mpNetClient->recvData(size, tmpByteData); 0136 if (ret < size) { 0137 return; 0138 } 0139 0140 int gameID = tmpByteData[0]; 0141 0142 //int scorePoint = formIntFromByte(tmpByteData + 1); 0143 //int lineCount = formIntFromByte(tmpByteData + 5); 0144 //int gameLevel = formIntFromByte(tmpByteData + 9); 0145 maScoreList[gameID] = formIntFromByte(tmpByteData + 5); 0146 0147 int tmpPieceCount = formIntFromByte(tmpByteData + 13); 0148 for (int i = 0; i < tmpPieceCount; ++i) { 0149 mpGameLogic->getSingleGame(gameID)->getPiece(i)->decodeData((unsigned char *)tmpByteData + 17 + i * 4); 0150 } 0151 0152 formIntFromByte(tmpByteData + 17 + tmpPieceCount * 4); 0153 mpGameLogic->getSingleGame(gameID)->getField()->decodeData((unsigned char *)tmpByteData + 18 + tmpPieceCount * 4); 0154 0155 updateScore(); 0156 0157 delete [] tmpByteData; 0158 } 0159 0160 #include "moc_KBlocksDisplay.cpp"