summaryrefslogtreecommitdiffstats
path: root/pacman-c++
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++')
-rw-r--r--pacman-c++/actor.cpp38
-rw-r--r--pacman-c++/actor.h7
-rw-r--r--pacman-c++/audio.cpp68
-rw-r--r--pacman-c++/audio.h39
-rw-r--r--pacman-c++/client.cpp93
-rw-r--r--pacman-c++/client.h16
-rw-r--r--pacman-c++/mainwidget.cpp23
-rw-r--r--pacman-c++/mainwidget.h10
8 files changed, 198 insertions, 96 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp
index 56e8e86..3af304c 100644
--- a/pacman-c++/actor.cpp
+++ b/pacman-c++/actor.cpp
@@ -10,7 +10,7 @@ static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal
10 10
11Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) 11Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent)
12 : GameEntity(color, parent),m_direction(Actor::None), m_local(local), 12 : GameEntity(color, parent),m_direction(Actor::None), m_local(local),
13 m_wakaPlayer1(NULL), m_wakaPlayer2(NULL), m_roundPoints(0), m_gamePoints(0) 13 m_wakaPlayer(NULL), m_roundPoints(0), m_gamePoints(0)
14{ 14{
15 /* DON'T set any pixmap here. we've a pixmap in the animation 15 /* DON'T set any pixmap here. we've a pixmap in the animation
16 * but we need a sprite for the collision detection 16 * but we need a sprite for the collision detection
@@ -56,19 +56,7 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent)
56 56
57 /* setup waka sound */ 57 /* setup waka sound */
58 if (local) 58 if (local)
59 { 59 m_wakaPlayer = new GaplessAudioPlayer(Sound::WakaWaka, 100, this);
60 m_wakaPlayer1 = new AudioPlayer(this);
61 m_wakaPlayer2 = new AudioPlayer(this);
62 if (m_wakaPlayer1->isWorking() && m_wakaPlayer2->isWorking())
63 {
64 m_wakaPlayer1->setPrefinishMark(100);
65 m_wakaPlayer2->setPrefinishMark(100);
66 connect(m_wakaPlayer1, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer2()));
67 connect(m_wakaPlayer2, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer1()));
68 AudioManager::self()->registerAudioPlayer(m_wakaPlayer1);
69 AudioManager::self()->registerAudioPlayer(m_wakaPlayer2);
70 }
71 }
72 60
73 /* make the picture showing the current direction visible */ 61 /* make the picture showing the current direction visible */
74 m_images[m_direction]->setVisible(true); 62 m_images[m_direction]->setVisible(true);
@@ -237,30 +225,16 @@ void Actor::eatingPacman()
237 225
238void Actor::startEating() 226void Actor::startEating()
239{ 227{
240 if (!m_local || !m_wakaPlayer1->isWorking()) 228 if (!m_local || !m_wakaPlayer->isWorking())
241 return; 229 return;
242 if (m_wakaPlayer1->state() != Phonon::PlayingState && m_wakaPlayer2->state() != Phonon::PlayingState) 230 m_wakaPlayer->play();
243 startPlayer1();
244} 231}
245 232
246void Actor::stopEating() 233void Actor::stopEating()
247{ 234{
248 if (!m_local || !m_wakaPlayer1->isWorking()) 235 if (!m_local || !m_wakaPlayer->isWorking())
249 return; 236 return;
250 if (m_wakaPlayer1->state() != Phonon::PausedState) 237 m_wakaPlayer->pause();
251 m_wakaPlayer1->pause();
252 if (m_wakaPlayer2->state() != Phonon::PausedState)
253 m_wakaPlayer2->pause();
254}
255
256void Actor::startPlayer1()
257{
258 m_wakaPlayer1->play(Sound::WakaWaka);
259}
260
261void Actor::startPlayer2()
262{
263 m_wakaPlayer2->play(Sound::WakaWaka);
264} 238}
265 239
266unsigned int Actor::getRoundPoints() 240unsigned int Actor::getRoundPoints()
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h
index 1739d9f..507a8e2 100644
--- a/pacman-c++/actor.h
+++ b/pacman-c++/actor.h
@@ -46,17 +46,12 @@ public:
46private: 46private:
47 void moveByServer(Movement direction); 47 void moveByServer(Movement direction);
48 48
49private slots:
50 void startPlayer1();
51 void startPlayer2();
52
53private: 49private:
54 QPixmap m_pix; 50 QPixmap m_pix;
55 Movement m_direction; 51 Movement m_direction;
56 PixmapItem m_icon; 52 PixmapItem m_icon;
57 bool m_local; 53 bool m_local;
58 AudioPlayer *m_wakaPlayer1; 54 GaplessAudioPlayer *m_wakaPlayer;
59 AudioPlayer *m_wakaPlayer2;
60 55
61 unsigned int m_roundPoints; 56 unsigned int m_roundPoints;
62 unsigned int m_gamePoints; 57 unsigned int m_gamePoints;
diff --git a/pacman-c++/audio.cpp b/pacman-c++/audio.cpp
index 6365a15..c0cc5b0 100644
--- a/pacman-c++/audio.cpp
+++ b/pacman-c++/audio.cpp
@@ -155,9 +155,9 @@ QFile *AudioManager::sound(Sound::Type sound)
155AudioPlayer::AudioPlayer(QObject *parent) 155AudioPlayer::AudioPlayer(QObject *parent)
156 : Phonon::MediaObject(parent) 156 : Phonon::MediaObject(parent)
157{ 157{
158 m_working = AudioManager::m_working;
158 m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); 159 m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
159 Phonon::createPath(this, m_output); 160 Phonon::createPath(this, m_output);
160 m_working = AudioManager::m_working;
161} 161}
162 162
163bool AudioPlayer::isWorking() const 163bool AudioPlayer::isWorking() const
@@ -255,6 +255,72 @@ void AudioPlayer::loopEnqueue()
255 255
256/* --------------------------------------------------------------- */ 256/* --------------------------------------------------------------- */
257 257
258GaplessAudioPlayer::GaplessAudioPlayer(Sound::Type sound, qint32 mark, QObject *parent)
259 : QObject(parent), m_sound(sound)
260{
261 m_working = AudioManager::m_working;
262 if (!m_working)
263 return;
264
265 m_player1 = new AudioPlayer(this);
266 m_player2 = new AudioPlayer(this);
267
268 m_player2->setPrefinishMark(mark);
269 m_player1->setPrefinishMark(mark);
270
271 connect(m_player1, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer2()));
272 connect(m_player2, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer1()));
273
274 AudioManager::self()->registerAudioPlayer(m_player1);
275 AudioManager::self()->registerAudioPlayer(m_player2);
276}
277
278bool GaplessAudioPlayer::isWorking() const
279{
280 return m_working;
281}
282
283void GaplessAudioPlayer::setMuted(bool mute)
284{
285 m_player1->setMuted(mute);
286 m_player2->setMuted(mute);
287}
288
289bool GaplessAudioPlayer::isMuted() const
290{
291 return m_player1->isMuted() && m_player2->isMuted();
292}
293
294void GaplessAudioPlayer::play()
295{
296 if (!m_working)
297 return;
298 if (m_player1->state() != Phonon::PlayingState && m_player2->state() != Phonon::PlayingState)
299 startPlayer1();
300}
301
302void GaplessAudioPlayer::pause()
303{
304 if (!m_working)
305 return;
306 if (m_player1->state() != Phonon::PausedState)
307 m_player1->pause();
308 if (m_player2->state() != Phonon::PausedState)
309 m_player2->pause();
310}
311
312void GaplessAudioPlayer::startPlayer1()
313{
314 m_player1->play(m_sound);
315}
316
317void GaplessAudioPlayer::startPlayer2()
318{
319 m_player2->play(m_sound);
320}
321
322/* --------------------------------------------------------------- */
323
258void AudioPlayer::Sleeper::sleep(unsigned long secs) 324void AudioPlayer::Sleeper::sleep(unsigned long secs)
259{ 325{
260 QThread::sleep(secs); 326 QThread::sleep(secs);
diff --git a/pacman-c++/audio.h b/pacman-c++/audio.h
index 49d29a9..3e76f50 100644
--- a/pacman-c++/audio.h
+++ b/pacman-c++/audio.h
@@ -25,6 +25,8 @@ namespace Sound
25 }; 25 };
26}; 26};
27 27
28/* --------------------------------------------------------------- */
29
28class AudioPlayer 30class AudioPlayer
29 : public Phonon::MediaObject 31 : public Phonon::MediaObject
30{ 32{
@@ -46,32 +48,61 @@ public:
46 bool isWorking() const; 48 bool isWorking() const;
47 void setMuted(bool mute = true); 49 void setMuted(bool mute = true);
48 bool isMuted() const; 50 bool isMuted() const;
49 void setLoop(QFile *sound);
50 void setLoop(Sound::Type sound); 51 void setLoop(Sound::Type sound);
51 void play(); 52 void play();
52 void play(Sound::Type sound); 53 void play(Sound::Type sound);
53 54
54private: 55protected:
55 void test(QFile *testsound); 56 void test(QFile *testsound);
57 void setLoop(QFile *sound);
56 58
57public slots: 59public slots:
58 void loopEnqueue(); 60 void loopEnqueue();
59 61
60private slots: 62protected slots:
61 void testFinished(); 63 void testFinished();
62 void stateChanged_ex(Phonon::State newstate, Phonon::State oldstate); 64 void stateChanged_ex(Phonon::State newstate, Phonon::State oldstate);
63 65
64private: 66protected:
65 bool m_working; 67 bool m_working;
66 QFile *m_loopsound; 68 QFile *m_loopsound;
67 Phonon::AudioOutput *m_output; 69 Phonon::AudioOutput *m_output;
68}; 70};
69 71
72/* --------------------------------------------------------------- */
73
74class GaplessAudioPlayer
75 : public QObject
76{
77 Q_OBJECT
78
79public:
80 GaplessAudioPlayer(Sound::Type sound, qint32 mark, QObject *parent = 0);
81 bool isWorking() const;
82 void setMuted(bool mute = true);
83 bool isMuted() const;
84 void play();
85 void pause();
86
87protected slots:
88 void startPlayer1();
89 void startPlayer2();
90
91protected:
92 bool m_working;
93 Sound::Type m_sound;
94 AudioPlayer *m_player1;
95 AudioPlayer *m_player2;
96};
97
98/* --------------------------------------------------------------- */
99
70class AudioManager 100class AudioManager
71 : public QObject 101 : public QObject
72{ 102{
73 Q_OBJECT 103 Q_OBJECT
74 friend class AudioPlayer; 104 friend class AudioPlayer;
105 friend class GaplessAudioPlayer;
75 106
76public: 107public:
77 AudioManager(); 108 AudioManager();
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp
index 5cc9279..fc0fdfc 100644
--- a/pacman-c++/client.cpp
+++ b/pacman-c++/client.cpp
@@ -4,63 +4,106 @@
4#include "pacman.pb.h" 4#include "pacman.pb.h"
5 5
6Client::Client() 6Client::Client()
7 : m_ambientMuted(false)
7{ 8{
8 m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this); 9 m_settings = new QSettings(qApp->organizationName(), qApp->applicationName(), this);
9 createMenu(); 10 createMenu();
10 m_mainWidget = new MainWidget(this); 11 m_mainWidget = new MainWidget(this);
12 m_mainWidget->setAmbientMuted(m_ambientMuted);
11 setCentralWidget(m_mainWidget); 13 setCentralWidget(m_mainWidget);
12} 14}
13 15
14void Client::createMenu() 16void Client::createMenu()
15{ 17{
16 QAction *quitAction = new QAction("E&xit", this);
17 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
18
19 QMenu *fileMenu = menuBar()->addMenu("File"); 18 QMenu *fileMenu = menuBar()->addMenu("File");
20 fileMenu->addAction(quitAction);
21 19
20 bool sound = AudioManager::self()->isWorking();
21 bool muted = !sound || m_settings->value("muted", false).toBool();
22 AudioManager::self()->setMuted(muted);
23
24 /* toggle sound: corner icon */
22 ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this); 25 ClickLabel *toggleSound = new ClickLabel("Toggle Sound", this);
23 toggleSound->setToolTip("Toggle Sound"); 26 toggleSound->setToolTip("Toggle Sound");
24 toggleSound->setFixedWidth(20); 27 toggleSound->setFixedWidth(20);
25 toggleSound->setFixedHeight(16); 28 toggleSound->setFixedHeight(16);
26 toggleSound->setAlignment(Qt::AlignBottom); 29 toggleSound->setAlignment(Qt::AlignBottom);
27 30 toggleSound->setPixmap(soundIcon(!muted));
28 bool sound = AudioManager::self()->isWorking();
29 bool muted = !sound || m_settings->value("muted", false).toBool();
30 AudioManager::self()->setMuted(muted);
31
32 QImage img(muted ? ":/soundoff" : ":/soundon");
33 img.setColor(1, menuBar()->palette().color(
34 muted ? QPalette::Disabled : QPalette::Active,
35 QPalette::ButtonText).rgba());
36 toggleSound->setPixmap(QPixmap::fromImage(img));
37
38 if (sound) 31 if (sound)
39 { 32 {
40 connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); 33 connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound()));
41 connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); 34 connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool)));
42 } 35 }
43
44 menuBar()->setCornerWidget(toggleSound); 36 menuBar()->setCornerWidget(toggleSound);
37
38 /* toggle sound: menu */
39 QAction *toggleSoundAction = new QAction("Sound", this);
40 toggleSoundAction->setToolTip("Toggle Sound");
41 toggleSoundAction->setCheckable(true);
42 toggleSoundAction->setChecked(!muted);
43 toggleSoundAction->setDisabled(!sound);
44 fileMenu->addAction(toggleSoundAction);
45 if (sound)
46 {
47 connect(toggleSoundAction, SIGNAL(triggered()), this, SLOT(toggleSound()));
48 connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleSoundAction, SLOT(setChecked(bool)));
49 }
50
51 /* toggle ambient sound: menu */
52 m_ambientMuted = muted || m_settings->value("ambientMuted", false).toBool();
53 QAction *toggleAmbientAction = new QAction("Ambient Sound", this);
54 toggleAmbientAction->setToolTip("Toggle Ambient Sound");
55 toggleAmbientAction->setCheckable(true);
56 toggleAmbientAction->setChecked(!m_ambientMuted);
57 toggleAmbientAction->setDisabled(!sound);
58 fileMenu->addAction(toggleAmbientAction);
59 if (sound)
60 {
61 connect(toggleAmbientAction, SIGNAL(triggered(bool)), this, SLOT(enableAmbientSound(bool)));
62 connect(this, SIGNAL(setMuteActionsChecked(bool)), toggleAmbientAction, SLOT(setEnabled(bool)));
63 }
64
65 /* exit entry */
66 fileMenu->addSeparator();
67 QAction *quitAction = new QAction("E&xit", this);
68 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
69 fileMenu->addAction(quitAction);
45} 70}
46 71
47void Client::toggleSound() const 72void Client::toggleSound()
48{ 73{
49 if (!AudioManager::self()->isWorking()) 74 if (!AudioManager::self()->isWorking())
50 return; 75 return;
51 AudioManager::self()->setMuted(!AudioManager::self()->isMuted()); 76 bool muted = !AudioManager::self()->isMuted();
77 AudioManager::self()->setMuted(muted);
78 /* mute ambient sound again if explicitly muted */
79 if (!muted && m_ambientMuted)
80 m_mainWidget->setAmbientMuted(true);
52} 81}
53 82
54void Client::mutedChanged(bool muted) const 83void Client::mutedChanged(bool muted)
55{ 84{
56 QImage img(muted ? ":/soundoff" : ":/soundon");
57 img.setColor(1, menuBar()->palette().color(
58 muted ? QPalette::Disabled : QPalette::Active,
59 QPalette::ButtonText).rgba());
60 ClickLabel *tmp = qobject_cast<ClickLabel *>(menuBar()->cornerWidget()); 85 ClickLabel *tmp = qobject_cast<ClickLabel *>(menuBar()->cornerWidget());
61 tmp->setPixmap(QPixmap::fromImage(img)); 86 tmp->setPixmap(soundIcon(!muted));
62
63 m_settings->setValue("muted", muted); 87 m_settings->setValue("muted", muted);
88 emit setMuteActionsChecked(!muted);
89}
90
91void Client::enableAmbientSound(bool enabled)
92{
93 if (!AudioManager::self()->isWorking())
94 return;
95 m_ambientMuted = !enabled;
96 m_mainWidget->setAmbientMuted(m_ambientMuted);
97 m_settings->setValue("ambientMuted", m_ambientMuted);
98}
99
100QPixmap Client::soundIcon(bool enabled) const
101{
102 QImage img(enabled ? ":/soundon" : ":/soundoff");
103 img.setColor(1, menuBar()->palette().color(
104 enabled ? QPalette::Active : QPalette::Disabled,
105 QPalette::ButtonText).rgba());
106 return QPixmap::fromImage(img);
64} 107}
65 108
66bool Constants::server = false; 109bool Constants::server = false;
diff --git a/pacman-c++/client.h b/pacman-c++/client.h
index 2d507e2..2583826 100644
--- a/pacman-c++/client.h
+++ b/pacman-c++/client.h
@@ -12,16 +12,26 @@ public:
12 Client(); 12 Client();
13 QSettings *settings(); 13 QSettings *settings();
14 14
15public slots: 15signals:
16 void toggleSound() const; 16 /* signal gets emitted if mute buttons should update their checked-state */
17 void mutedChanged(bool) const; 17 void setMuteActionsChecked(bool enabled);
18
19private slots:
20 /* toggles sound */
21 void toggleSound();
22 /* mute was changed (emitted by audioplayer/phonon) */
23 void mutedChanged(bool);
24 /* enable ambient (emitted by action) */
25 void enableAmbientSound(bool);
18 26
19private: 27private:
20 void createMenu(); 28 void createMenu();
29 QPixmap soundIcon(bool enabled = true) const;
21 30
22private: 31private:
23 MainWidget *m_mainWidget; 32 MainWidget *m_mainWidget;
24 QSettings *m_settings; 33 QSettings *m_settings;
34 bool m_ambientMuted;
25}; 35};
26 36
27#endif // CLIENT_H 37#endif // CLIENT_H
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp
index 9507e85..6930b1a 100644
--- a/pacman-c++/mainwidget.cpp
+++ b/pacman-c++/mainwidget.cpp
@@ -9,17 +9,7 @@ MainWidget::MainWidget(QWidget *parent)
9 : QWidget(parent), m_currentKey(Transmission::none), m_running(false) 9 : QWidget(parent), m_currentKey(Transmission::none), m_running(false)
10{ 10{
11 /* create audio player */ 11 /* create audio player */
12 m_sirenPlayer1 = new AudioPlayer(this); 12 m_ambientPlayer = new GaplessAudioPlayer(Sound::Ambient, 100, this);
13 m_sirenPlayer2 = new AudioPlayer(this);
14 if (m_sirenPlayer1->isWorking() && m_sirenPlayer2->isWorking())
15 {
16 m_sirenPlayer1->setPrefinishMark(100);
17 m_sirenPlayer2->setPrefinishMark(100);
18 connect(m_sirenPlayer1, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer2()));
19 connect(m_sirenPlayer2, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(startPlayer1()));
20 AudioManager::self()->registerAudioPlayer(m_sirenPlayer1);
21 AudioManager::self()->registerAudioPlayer(m_sirenPlayer2);
22 }
23 13
24 Color::Color color = connectToServer(); 14 Color::Color color = connectToServer();
25 if (color == Color::none) 15 if (color == Color::none)
@@ -209,17 +199,12 @@ void MainWidget::keyReleaseEvent(QKeyEvent* event)
209void MainWidget::startGame() 199void MainWidget::startGame()
210{ 200{
211 m_running = true; 201 m_running = true;
212 startPlayer1(); 202 m_ambientPlayer->play();
213}
214
215void MainWidget::startPlayer1()
216{
217 m_sirenPlayer1->play(Sound::Ambient);
218} 203}
219 204
220void MainWidget::startPlayer2() 205void MainWidget::setAmbientMuted(bool muted)
221{ 206{
222 m_sirenPlayer2->play(Sound::Ambient); 207 m_ambientPlayer->setMuted(muted);
223} 208}
224 209
225void MainWidget::playerScoreClicked() 210void MainWidget::playerScoreClicked()
diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h
index a316d9e..6ecd812 100644
--- a/pacman-c++/mainwidget.h
+++ b/pacman-c++/mainwidget.h
@@ -19,19 +19,18 @@ class MainWidget
19public: 19public:
20 MainWidget(QWidget *parent = 0); 20 MainWidget(QWidget *parent = 0);
21 bool connected(); 21 bool connected();
22 void setAmbientMuted(bool muted);
22 23
23protected: 24protected:
24 /* handling of current key */ 25 /* handling of current key */
25 virtual void keyPressEvent(QKeyEvent* ); 26 virtual void keyPressEvent(QKeyEvent *);
26 virtual void keyReleaseEvent(QKeyEvent* ); 27 virtual void keyReleaseEvent(QKeyEvent *);
27 28
28private slots: 29private slots:
29 void startGame(); 30 void startGame();
30 void playerScoreClicked(); 31 void playerScoreClicked();
31 void tick(); 32 void tick();
32 void sendKeyUpdate(); 33 void sendKeyUpdate();
33 void startPlayer1();
34 void startPlayer2();
35 34
36private: 35private:
37 void createGui(); 36 void createGui();
@@ -51,8 +50,7 @@ private:
51 50
52 /* game running */ 51 /* game running */
53 bool m_running; 52 bool m_running;
54 AudioPlayer *m_sirenPlayer1; 53 GaplessAudioPlayer *m_ambientPlayer;
55 AudioPlayer *m_sirenPlayer2;
56 54
57 QTcpSocket *m_socket; 55 QTcpSocket *m_socket;
58 SceneHolder *m_scene; 56 SceneHolder *m_scene;