1
/***************************************************************************
2
* KBlocks, a falling blocks game for KDE *
3
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
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 "KBlocksDisplay.h"
12
#include "AI/KBlocksAILog.h"
16
#include <QPixmapCache>
18
KBlocksDisplay::KBlocksDisplay(int gameCount, const string& serverIP, int localPort) : KMainWindow()
20
//Use up to 3MB for global application pixmap cache
21
QPixmapCache::setCacheLimit(3*1024);
23
for (int i = 0; i < 8; ++i)
28
mpNetClient = new KBlocksNetClient(serverIP.c_str(), localPort);
29
connect(mpNetClient, SIGNAL(dataArrived(int)), this, SLOT(updateGameDisplay(int)));
31
mGameCount = gameCount;
32
mpGameLogic = new KBlocksGameLogic(mGameCount);
33
mpGameLogic->setGameSeed(0);
34
mpGameLogic->setGamePunish(false);
35
mpGameLogic->setGameStandbyMode(true);
36
mpGameLogic->setInitInterval(0);
37
mpGameLogic->setLevelUpInterval(0);
39
mpGameScene = new KBlocksScene(mpGameLogic, mGameCount);
40
mpGameScene->setGameAnimEnabled(false);
41
mpGameScene->setWaitForAllUpdate(false);
43
mpGameView = new KBlocksView(mpGameScene, this);
45
setCentralWidget(mpGameView);
47
mUpdateInterval = 1000;
48
mUpdateTimer.setInterval(mUpdateInterval);
49
connect(&mUpdateTimer, SIGNAL(timeout()), this, SLOT(updateEvent()));
52
statusBar()->insertItem( i18n("Score List : 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0"), 0 );
55
KBlocksDisplay::~KBlocksDisplay()
57
mpGameLogic->stopGame();
66
void KBlocksDisplay::setGamesPerLine(int count)
68
mpGameScene->setGamesPerLine(count);
71
void KBlocksDisplay::setUpdateInterval(int interval)
73
mUpdateInterval = interval;
74
mUpdateTimer.setInterval(mUpdateInterval);
75
mpGameScene->setUpdateInterval(interval);
78
void KBlocksDisplay::startDisplay()
80
mpGameLogic->startGame(mGameCount);
81
for(int i = 0; i < mGameCount; i++)
83
mpGameLogic->getSingleGame(i)->stopGame();
86
mpGameScene->createGameItemGroups(mGameCount, true);
87
mpGameScene->startGame();
92
void KBlocksDisplay::stopDisplay()
96
mpGameScene->stopGame();
97
mpGameScene->deleteGameItemGroups();
99
mpGameLogic->stopGame();
102
int KBlocksDisplay::formIntFromByte(char * data)
105
value += ((int)data[0]) & 0x000000FF;
106
value += (((int)data[1]) << 8) & 0x0000FF00;
107
value += (((int)data[2]) << 16) & 0x00FF0000;
108
value += (((int)data[3]) << 24) & 0xFF000000;
112
void KBlocksDisplay::updateScore()
114
statusBar()->changeItem( i18n("Score List : %1 - %2 - %3 - %4 - %5 - %6 - %7 - %8",
115
maScoreList[0], maScoreList[1], maScoreList[2], maScoreList[3],
116
maScoreList[4], maScoreList[5], maScoreList[6], maScoreList[7]), 0 );
119
void KBlocksDisplay::updateEvent()
121
char tmpByteData[5] = {'|', 'r', 'g', '|', '\0'};
122
mpNetClient->sendData(5, tmpByteData);
125
void KBlocksDisplay::updateGameDisplay(int size)
127
char* tmpByteData = new char[size];
129
int ret = mpNetClient->recvData(size, tmpByteData);
135
int gameID = tmpByteData[0];
137
//int scorePoint = formIntFromByte(tmpByteData + 1);
138
//int lineCount = formIntFromByte(tmpByteData + 5);
139
//int gameLevel = formIntFromByte(tmpByteData + 9);
140
maScoreList[gameID] = formIntFromByte(tmpByteData + 5);
142
int tmpPieceCount = formIntFromByte(tmpByteData + 13);
143
for (int i = 0; i < tmpPieceCount; ++i)
145
mpGameLogic->getSingleGame(gameID)->getPiece(i)->decodeData((unsigned char*)tmpByteData + 17 + i * 4);
148
formIntFromByte(tmpByteData + 17 + tmpPieceCount * 4);
149
mpGameLogic->getSingleGame(gameID)->getField()->decodeData((unsigned char*)tmpByteData + 18 + tmpPieceCount * 4);
153
delete [] tmpByteData;