add playing sounds to game base class
Stefan Schuermans

Stefan Schuermans commited on 2019-07-07 12:49:43
Showing 2 changed files, with 39 additions and 2 deletions.

... ...
@@ -3,6 +3,7 @@
3 3
    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4 4
    a blinkenarea.org project */
5 5
 
6
+#include <map>
6 7
 #include <set>
7 8
 #include <sstream>
8 9
 #include <string>
... ...
@@ -16,6 +17,7 @@
16 17
 #include "Game.h"
17 18
 #include "Mgrs.h"
18 19
 #include "Module.h"
20
+#include "NameFile.h"
19 21
 #include "OutStreamFile.h"
20 22
 #include "UIntFile.h"
21 23
 
... ...
@@ -33,7 +35,7 @@ Game::Game(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
33 35
   m_fileBackgroundColor(dirBase.getFile("backgroundColor")),
34 36
   m_fileOutStream(dirBase.getFile("outstream"), mgrs.m_streamMgr),
35 37
   m_height(0), m_width(0), m_channels(0), m_imgBuf(), m_backgroundColor(),
36
-  m_haveTimeStep(false), m_timeStepTime(), m_opConnsClose()
38
+  m_haveTimeStep(false), m_timeStepTime(), m_opConnSounds(), m_opConnsClose()
37 39
 {
38 40
 }
39 41
 
... ...
@@ -88,6 +90,12 @@ void Game::updateConfig()
88 90
 /// callback when requested time reached
89 91
 void Game::timeCall()
90 92
 {
93
+  // play sounds on operator connections
94
+  for (auto & opConnSound : m_opConnSounds) {
95
+    playOpConnSound(opConnSound.first, *opConnSound.second);
96
+  }
97
+  m_opConnSounds.clear();
98
+
91 99
   // close operator connections
92 100
   for (OpConn *pConn : m_opConnsClose) {
93 101
     pConn->close();
... ...
@@ -395,6 +403,22 @@ void Game::unsetTimeStep()
395 403
   planTimeCall();
396 404
 }
397 405
 
406
+/// play a sound on operator connection (NULL ok) (direct)
407
+void Game::playOpConnSound(OpConn *pConn, NameFile const &soundFile)
408
+{
409
+  if (pConn && soundFile.m_valid) {
410
+    pConn->sendPlay(soundFile.m_obj.m_str);
411
+  }
412
+}
413
+
414
+//// request playing a sound on operator connection (NULL ok) (via time call)
415
+void Game::requestOpConnSound(OpConn *pConn, NameFile const &soundFile)
416
+{
417
+  if (pConn) {
418
+    m_opConnSounds[pConn] = &soundFile;
419
+  }
420
+}
421
+
398 422
 /// request closing operator connection (NULL ok) (closed via time call)
399 423
 void Game::requestOpConnClose(OpConn *pConn)
400 424
 {
... ...
@@ -407,6 +431,7 @@ void Game::requestOpConnClose(OpConn *pConn)
407 431
 /// remove operator connection from requests (call when op conn is closed)
408 432
 void Game::forgetOpConn(OpConn *pConn)
409 433
 {
434
+  m_opConnSounds.erase(pConn);
410 435
   m_opConnsClose.erase(pConn);
411 436
   planTimeCall();
412 437
 }
... ...
@@ -450,7 +475,7 @@ void Game::destroyImgBuf()
450 475
 void Game::planTimeCall()
451 476
 {
452 477
   // request immediate time call if there are internal time-based actions
453
-  if (! m_opConnsClose.empty()) {
478
+  if (! m_opConnSounds.empty() || ! m_opConnsClose.empty()) {
454 479
     m_mgrs.m_callMgr.requestTimeCall(this, Time::now());
455 480
     return;
456 481
   }
... ...
@@ -6,6 +6,7 @@
6 6
 #ifndef BLINKER_GAME_H
7 7
 #define BLINKER_GAME_H
8 8
 
9
+#include <map>
9 10
 #include <set>
10 11
 #include <string>
11 12
 #include <vector>
... ...
@@ -19,6 +20,7 @@
19 20
 #include "FormatFile.h"
20 21
 #include "Mgrs.h"
21 22
 #include "Module.h"
23
+#include "NameFile.h"
22 24
 #include "OpConn.h"
23 25
 #include "OutStreamFile.h"
24 26
 #include "Time.h"
... ...
@@ -45,6 +47,9 @@ private:
45 47
   /// set of operator connections
46 48
   typedef std::set<OpConn *> OpConnSet;
47 49
 
50
+  /// sound (via sound name file) to play on operator connections
51
+  typedef std::map<OpConn *, NameFile const *> OpConnSounds;
52
+
48 53
 public:
49 54
   /**
50 55
    * @brief constructor
... ...
@@ -190,6 +195,12 @@ protected:
190 195
   /// unset game time step - do timed action for game
191 196
   void unsetTimeStep();
192 197
 
198
+  /// play a sound on operator connection (NULL ok) (direct)
199
+  void playOpConnSound(OpConn *pConn, NameFile const &soundFile);
200
+
201
+  //// request playing a sound on operator connection (NULL ok) (via time call)
202
+  void requestOpConnSound(OpConn *pConn, NameFile const &soundFile);
203
+
193 204
   /// request closing operator connection (NULL ok) (closed via time call)
194 205
   void requestOpConnClose(OpConn *pConn);
195 206
 
... ...
@@ -219,6 +230,7 @@ protected:
219 230
 private:
220 231
   bool         m_haveTimeStep; ///< if a time step is pending
221 232
   Time         m_timeStepTime; ///< time of next time step
233
+  OpConnSounds m_opConnSounds; ///< sound to play on operator connections
222 234
   OpConnSet    m_opConnsClose; ///< operator connections to be closed
223 235
 }; // class Canvas
224 236
 
225 237