Commit 13fc0e56 authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Do not create KBlocksGraphics and KBlocksSound objects in KBlocksScene

Complex objects being created in constructors is always a big headache
for unit tests, particularly in C++, because constructor calls can't be
avoided and neither can the work be delegated to a virtual member
function.

Instead of creating the objects, KBlocksScene now takes them as input
parameters.

This reduces the number of warnings in the UnitTest-KBlocksWin test
suite to one.
parent 140e1188
......@@ -12,7 +12,9 @@
#include "KBlocksGameLogic.h"
#include "KBlocksWin.h"
#include "Testing/MockGameLogic.h"
#include "Testing/MockGraphics.h"
#include "Testing/MockScene.h"
#include "Testing/MockSound.h"
#include "Testing/TestingKBlocksWin.h"
class testKBlocksWin : public QObject
......@@ -32,7 +34,9 @@ void testKBlocksWin::callStopGameBeforeSingleGamesAreDeleted()
* final game action items.
*/
MockGameLogic logic;
TestingKBlocksWin win(&logic, nullptr, 0, 0);
MockGraphics graphics;
MockSound sound;
TestingKBlocksWin win(&logic, &graphics, &sound, nullptr, 0, 0);
MockScene *scene = new MockScene(&logic);
win.replaceScene(scene);
win.callStopGame();
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include "KBlocksDisplay.h"
#include "AI/KBlocksAILog.h"
......@@ -16,7 +17,16 @@
#include <QPixmapCache>
#include <QLabel>
KBlocksDisplay::KBlocksDisplay(int gameCount, const string &serverIP, int localPort) : KMainWindow()
class GraphicsInterface;
class SoundInterface;
KBlocksDisplay::KBlocksDisplay(
GraphicsInterface *graphics,
SoundInterface *sound,
int gameCount,
const string &serverIP,
int localPort
) : KMainWindow()
{
//Use up to 3MB for global application pixmap cache
QPixmapCache::setCacheLimit(3 * 1024);
......@@ -36,7 +46,7 @@ KBlocksDisplay::KBlocksDisplay(int gameCount, const string &serverIP, int localP
mpGameLogic->setInitInterval(0);
mpGameLogic->setLevelUpInterval(0);
mpGameScene = new KBlocksScene(mpGameLogic, mGameCount);
mpGameScene = new KBlocksScene(mpGameLogic, graphics, sound, mGameCount);
mpGameScene->setGameAnimEnabled(false);
mpGameScene->setWaitForAllUpdate(false);
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#ifndef KBLOCKSDISPLAY_H
#define KBLOCKSDISPLAY_H
......@@ -22,13 +23,23 @@
#include "KBlocksNetClient.h"
using std::string;
class QLabel;
class GraphicsInterface;
class SoundInterface;
class KBlocksDisplay : public KMainWindow
{
Q_OBJECT
public:
KBlocksDisplay(int gameCount, const string &serverIP, int localPort);
KBlocksDisplay(
GraphicsInterface* graphics,
SoundInterface* sound,
int gameCount,
const std::string& serverIP,
int localPort
);
~KBlocksDisplay();
public:
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include "KBlocksRepWin.h"
#include <QPixmapCache>
......@@ -15,7 +16,12 @@
#include "kblocks_replay_debug.h"
KBlocksRepWin::KBlocksRepWin(const char *replayFile, bool binaryMode) : KMainWindow()
KBlocksRepWin::KBlocksRepWin(
GraphicsInterface *graphics,
SoundInterface *sound,
const char *replayFile,
bool binaryMode
) : KMainWindow()
{
//Use up to 3MB for global application pixmap cache
QPixmapCache::setCacheLimit(3 * 1024);
......@@ -43,7 +49,7 @@ KBlocksRepWin::KBlocksRepWin(const char *replayFile, bool binaryMode) : KMainWin
mpGameLogic->setInitInterval(0);
mpGameLogic->setLevelUpInterval(0);
mpGameScene = new KBlocksScene(mpGameLogic, mGameCount);
mpGameScene = new KBlocksScene(mpGameLogic, graphics, sound, mGameCount);
mpGameView = new KBlocksView(mpGameScene, this);
mpGameView->show();
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#ifndef KBLOCKSREPWIN_H
#define KBLOCKSREPWIN_H
......@@ -22,12 +23,20 @@
using std::string;
class GraphicsInterface;
class SoundInterface;
class KBlocksRepWin : public KMainWindow
{
Q_OBJECT
public:
explicit KBlocksRepWin(const char *replayFile, bool binaryMode = true);
explicit KBlocksRepWin(
GraphicsInterface *graphics,
SoundInterface *sound,
const char *replayFile,
bool binaryMode = true
);
~KBlocksRepWin();
public:
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Mauricio Piacentini <mauricio@tabuleiro.com> *
* Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Mauricio Piacentini <mauricio@tabuleiro.com> *
* Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include "KBlocksScene.h"
#include "settings.h"
......@@ -15,7 +16,15 @@
#include <QVarLengthArray>
#include <KLocalizedString>
KBlocksScene::KBlocksScene(GameLogicInterface *p, int capacity)
#include "GraphicsInterface.h"
#include "SoundInterface.h"
KBlocksScene::KBlocksScene(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
int capacity
)
{
mpGameLogic = p;
mGameStarted = false;
......@@ -32,9 +41,8 @@ KBlocksScene::KBlocksScene(GameLogicInterface *p, int capacity)
maGameScoreList = new KBlocksScore*[capacity]();
maGameReadySignal = new bool[capacity]();
QString themeFile(Settings::theme());
mpGrafx = new KBlocksGraphics(themeFile);
mpSnd = new KBlocksSound(themeFile);
mpGrafx = graphics;
mpSnd = sound;
int width = (capacity >= mSceneGamesPerLine) ? mSceneGamesPerLine : (capacity % mSceneGamesPerLine);
int height = (int)(capacity / (mSceneGamesPerLine + 1)) + 1;
......@@ -56,9 +64,6 @@ KBlocksScene::~KBlocksScene()
delete [] maGameReadySignal;
delete [] maGameScoreList;
delete [] maGroupList;
delete mpGrafx;
delete mpSnd;
}
KBlocksItemGroup *KBlocksScene::getItemGroup(int index)
......
......@@ -27,12 +27,20 @@
#include "KBlocksDefine.h"
#include "SceneInterface.h"
class SoundInterface;
class GraphicsInterface;
class KBlocksScene : public SceneInterface
{
Q_OBJECT
public:
explicit KBlocksScene(GameLogicInterface *p, int capacity = 1);
explicit KBlocksScene(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
int capacity = 1
);
virtual ~KBlocksScene();
KBlocksItemGroup *getItemGroup(int index);
......@@ -81,8 +89,8 @@ private:
GameLogicInterface *mpGameLogic = nullptr;
bool mGameStarted;
KBlocksGraphics *mpGrafx = nullptr;
KBlocksSound *mpSnd = nullptr;
GraphicsInterface *mpGrafx = nullptr;
SoundInterface *mpSnd = nullptr;
int mSceneGamesPerLine;
bool mGameAnimEnabled;
......
......@@ -36,7 +36,14 @@
#include "SceneInterface.h"
#include "settings.h"
KBlocksWin::KBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capacity, int gamecount) : KXmlGuiWindow()
KBlocksWin::KBlocksWin(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
KBlocksPlayManager *pM,
int capacity,
int gamecount
) : KXmlGuiWindow()
{
//Use up to 3MB for global application pixmap cache
QPixmapCache::setCacheLimit(3 * 1024);
......@@ -52,7 +59,7 @@ KBlocksWin::KBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capaci
mpPlayManager = pM;
mpGameScene = new KBlocksScene(mpGameLogic, capacity);
mpGameScene = new KBlocksScene(mpGameLogic, graphics, sound, capacity);
connect(mpKBPlayer, &KBlocksKeyboardPlayer::blockMoved,
mpGameScene, &SceneInterface::playMoveSound);
......
......@@ -22,13 +22,22 @@
#include "KBlocksPlayManager.h"
class QLabel;
class GameLogicInterface;
class GraphicsInterface;
class SceneInterface;
class SoundInterface;
class KBlocksWin : public KXmlGuiWindow
{
Q_OBJECT
public:
KBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capacity, int gamecount);
KBlocksWin(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
KBlocksPlayManager *pM,
int capacity,
int gamecount
);
~KBlocksWin();
public:
......
......@@ -10,8 +10,14 @@
#include "TestingKBlocksWin.h"
TestingKBlocksWin::TestingKBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capacity, int gamecount)
: KBlocksWin(p, pM, capacity, gamecount)
TestingKBlocksWin::TestingKBlocksWin(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
KBlocksPlayManager *pM,
int capacity,
int gamecount
) : KBlocksWin(p, graphics, sound, pM, capacity, gamecount)
{
}
......
......@@ -15,12 +15,21 @@
#include "SceneInterface.h"
class GameLogicInterface;
class GraphicsInterface;
class KBlocksPlayManager;
class SoundInterface;
class TestingKBlocksWin : private KBlocksWin
{
public:
TestingKBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capacity, int gamecount);
TestingKBlocksWin(
GameLogicInterface *p,
GraphicsInterface *graphics,
SoundInterface *sound,
KBlocksPlayManager *pM,
int capacity,
int gamecount
);
virtual ~TestingKBlocksWin() = default;
void callStopGame();
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2009 Mauricio Piacentini <mauricio@tabuleiro.com> *
* Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2009-2021 Mauricio Piacentini <mauricio@tabuleiro.com> *
* Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include <ctime>
#include <string>
#include <vector>
......@@ -43,6 +44,7 @@ using namespace std;
#include "KBlocksNetClient.h"
#include "kblocks_version.h"
#include "settings.h"
KBlocksGameLogic *mpKBlocksGameLogic;
KBlocksPlayManager *mpKBlocksPlayManager;
......@@ -75,7 +77,18 @@ int gameDesktopMode(const QApplication &app)
mpKBlocksPlayManager = new KBlocksPlayManager(mpKBlocksGameLogic, 2);
mpKBlocksWindow = new KBlocksWin(mpKBlocksGameLogic, mpKBlocksPlayManager, 2, 1);
QString themeFile(Settings::theme());
KBlocksGraphics graphics(themeFile);
KBlocksSound sound(themeFile);
mpKBlocksWindow = new KBlocksWin(
mpKBlocksGameLogic,
&graphics,
&sound,
mpKBlocksPlayManager,
2,
1
);
mpKBlocksWindow->setUpdateInterval(50);
mpKBlocksWindow->setGamesPerLine(4);
mpKBlocksWindow->setGameAnimEnabled(true);
......@@ -159,7 +172,18 @@ int gameGuiMode(KBlocksConfigManager *config, const QApplication &app)
printf("\tUpdate Interval = %d\n", updateInterval);
printf("\tLocal Port = %d\n", localPort);
printf("\tServer IP = %s\n", serverIP.c_str());
mpKBlocksDisplay = new KBlocksDisplay(gameCount, serverIP, localPort);
QString themeFile(Settings::theme());
KBlocksGraphics graphics(themeFile);
KBlocksSound sound(themeFile);
mpKBlocksDisplay = new KBlocksDisplay(
&graphics,
&sound,
gameCount,
serverIP,
localPort
);
mpKBlocksDisplay->setGamesPerLine(gamesPerLine);
mpKBlocksDisplay->setUpdateInterval(updateInterval);
mpKBlocksDisplay->show();
......@@ -206,7 +230,16 @@ int gameReplayMode(KBlocksConfigManager *config, const QApplication &app)
printf("\tRecord File = %s\n", recordFile.c_str());
printf("\tRecord Type = %s\n", recordBinary ? "Binary" : "Text");
KBlocksRepWin *mpKBlocksRepWin = new KBlocksRepWin(recordFile.c_str(), recordBinary);
QString themeFile(Settings::theme());
KBlocksGraphics graphics(themeFile);
KBlocksSound sound(themeFile);
KBlocksRepWin *mpKBlocksRepWin = new KBlocksRepWin(
&graphics,
&sound,
recordFile.c_str(),
recordBinary
);
if (!mpKBlocksRepWin->replayLoaded()) {
printf("Error loading replay file: Failed to load replay file!\n");
return -2;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment