sound support for pong game
Stefan Schuermans

Stefan Schuermans commited on 2019-07-07 12:55:58
Showing 11 changed files, with 88 additions and 0 deletions.

... ...
@@ -89,6 +89,36 @@
89 89
       No frames are sent when the game is inactive (i.e. no player has
90 90
       joined yet).
91 91
     </p>
92
+    <h3>Sound Names</h3>
93
+    <p>
94
+      It is possible to request playing a sound on certain events occuring
95
+      during the game.
96
+      The names of the sounds to play are configurable via the following
97
+      files:
98
+      <table>
99
+        <tr><td><b>file name</b></td>
100
+            <td width="2em"></td>
101
+            <td><b>play sound on event</b></td></tr>
102
+        <tr><td><code>leftPlayerSound</code></td>
103
+            <td></td>
104
+            <td>player entered the game as left player</td></tr>
105
+        <tr><td><code>rightPlayerSound</code></td>
106
+            <td></td>
107
+            <td>player entered the game as right player</td></tr>
108
+        <tr><td><code>scoreSound</code></td>
109
+            <td></td>
110
+            <td>player scored a goal</td></tr>
111
+        <tr><td><code>otherScoreSound</code></td>
112
+            <td></td>
113
+            <td>other player scored a goal</td></tr>
114
+        <tr><td><code>winSound</code></td>
115
+            <td></td>
116
+            <td>player wins game</td></tr>
117
+        <tr><td><code>looseSound</code></td>
118
+            <td></td>
119
+            <td>player looses game</td></tr>
120
+      </table>
121
+    </p>
92 122
     <h2>Operator Connection Interfaces</h2>
93 123
     <p>
94 124
     The name of the operator connection interfaces provided by this module
... ...
@@ -369,6 +369,14 @@ void Game::valueFromFile(UIntFile &valueFile, ValueDescr const &descr,
369 369
   }
370 370
 }
371 371
 
372
+/// process update of sound name file
373
+void Game::soundUpdate(NameFile &soundFile)
374
+{
375
+  if (soundFile.checkModified()) {
376
+    soundFile.update();
377
+  }
378
+}
379
+
372 380
 /// send current image buffer as frame to output stream
373 381
 void Game::sendFrame()
374 382
 {
... ...
@@ -186,6 +186,9 @@ protected:
186 186
   static void valueFromFile(UIntFile &valueFile, ValueDescr const &descr,
187 187
                             unsigned int &value);
188 188
 
189
+  /// process update of sound name file
190
+  static void soundUpdate(NameFile &soundFile);
191
+
189 192
   /// send current image buffer as frame to output stream
190 193
   void sendFrame();
191 194
 
... ...
@@ -16,6 +16,7 @@
16 16
 #include "Game.h"
17 17
 #include "Mgrs.h"
18 18
 #include "Module.h"
19
+#include "NameFile.h"
19 20
 #include "OpConn.h"
20 21
 #include "OpConnIf.h"
21 22
 #include "OpReqIf.h"
... ...
@@ -53,6 +54,12 @@ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
53 54
   m_fileGoalColor(dirBase.getFile("goalColor")),
54 55
   m_fileDelay(dirBase.getFile("delay")),
55 56
   m_fileMaxScore(dirBase.getFile("maxScore")),
57
+  m_fileLeftPlayerSound(dirBase.getFile("leftPlayerSound")),
58
+  m_fileRightPlayerSound(dirBase.getFile("rightPlayerSound")),
59
+  m_fileScoreSound(dirBase.getFile("scoreSound")),
60
+  m_fileOtherScoreSound(dirBase.getFile("otherScoreSound")),
61
+  m_fileWinSound(dirBase.getFile("winSound")),
62
+  m_fileLoseSound(dirBase.getFile("loseSound")),
56 63
   m_ballColor(), m_lineColor(), m_padColor(), m_computerColor(),
57 64
   m_scoreColor(), m_goalColor(),
58 65
   m_delay(c_delayDescr.default_), m_maxScore(c_maxScoreDescr.default_),
... ...
@@ -101,6 +108,14 @@ bool Pong::updateConfigGame()
101 108
     ret = true;
102 109
   }
103 110
 
111
+  // sound name file was modified -> re-read sound name, no other update needed
112
+  soundUpdate(m_fileLeftPlayerSound);
113
+  soundUpdate(m_fileRightPlayerSound);
114
+  soundUpdate(m_fileScoreSound);
115
+  soundUpdate(m_fileOtherScoreSound);
116
+  soundUpdate(m_fileWinSound);
117
+  soundUpdate(m_fileLoseSound);
118
+
104 119
   return ret;
105 120
 }
106 121
 
... ...
@@ -135,10 +150,12 @@ void Pong::newOpConn(const std::string &name, OpConn *pConn)
135 150
   // left player joins if none there yet
136 151
   if (name == m_name + c_opConnSuffixLeft && ! m_pConnLeft) {
137 152
     m_pConnLeft = pConn;
153
+    requestOpConnSound(m_pConnLeft, m_fileLeftPlayerSound);
138 154
   }
139 155
   // right player joins if none there yet
140 156
   else if (name == m_name + c_opConnSuffixRight && ! m_pConnRight) {
141 157
     m_pConnRight = pConn;
158
+    requestOpConnSound(m_pConnRight, m_fileRightPlayerSound);
142 159
   }
143 160
   // close imcoming connection as soon as possible, nothing else happens
144 161
   else {
... ...
@@ -572,12 +589,28 @@ void Pong::detectGoal()
572 589
   if (m_ballPosX == 0) {
573 590
     m_goalDelay = goalDelay;
574 591
     ++m_scoreRight;
592
+    // play sound - score or win
593
+    if (m_scoreRight >= (int)m_maxScore) {
594
+      playOpConnSound(m_pConnRight, m_fileWinSound);
595
+      playOpConnSound(m_pConnLeft, m_fileLoseSound);
596
+    } else {
597
+      playOpConnSound(m_pConnRight, m_fileScoreSound);
598
+      playOpConnSound(m_pConnLeft, m_fileOtherScoreSound);
599
+    }
575 600
   }
576 601
 
577 602
   // ball at far right - goal for left player
578 603
   else if (m_ballPosX == m_width - 1) {
579 604
     m_goalDelay = goalDelay;
580 605
     ++m_scoreLeft;
606
+    // play sound - score or win
607
+    if (m_scoreLeft >= (int)m_maxScore) {
608
+      playOpConnSound(m_pConnLeft, m_fileWinSound);
609
+      playOpConnSound(m_pConnRight, m_fileLoseSound);
610
+    } else {
611
+      playOpConnSound(m_pConnLeft, m_fileScoreSound);
612
+      playOpConnSound(m_pConnRight, m_fileOtherScoreSound);
613
+    }
581 614
   }
582 615
 }
583 616
 
... ...
@@ -19,6 +19,7 @@
19 19
 #include "Game.h"
20 20
 #include "Mgrs.h"
21 21
 #include "Module.h"
22
+#include "NameFile.h"
22 23
 #include "OpConn.h"
23 24
 #include "OpConnIf.h"
24 25
 #include "OpReqIf.h"
... ...
@@ -186,6 +187,13 @@ protected:
186 187
   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
187 188
   UIntFile  m_fileMaxScore;      ///< file for maximum score (end of game)
188 189
 
190
+  NameFile m_fileLeftPlayerSound;  ///< "left player" sound name file
191
+  NameFile m_fileRightPlayerSound; ///< "right player" sound name file
192
+  NameFile m_fileScoreSound;       ///< "you scored" sound name file
193
+  NameFile m_fileOtherScoreSound;  ///< "other player scored" sound name file
194
+  NameFile m_fileWinSound;         ///< "you win" sound name file
195
+  NameFile m_fileLoseSound;        ///< "you lose" sound name file
196
+
189 197
   ColorData    m_ballColor;     ///< ball color
190 198
   ColorData    m_lineColor;     ///< center line color
191 199
   ColorData    m_padColor;      ///< phone player pad color
192 200