]> git.zerfleddert.de Git - micropolis/blobdiff - src/sim/w_sound.c
prevent forking a huge number of player processes by limiting them to
[micropolis] / src / sim / w_sound.c
index 1c112023a6f4fa8275cf40c89d989248f33eab18..7558e47013f447d4a4e522e2b512e595ca5300be 100644 (file)
@@ -237,15 +237,6 @@ MakeSound(char *channel, char *id)
     fprintf(stderr, "Mix_PlayChannel: %s\n", Mix_GetError());
 }
 
-MakeSoundOn(SimView *view, char *channel, char *id)
-{
-  if (!UserSoundOn) return;
-  if (!SoundInitialized) return;
-
-  MakeSound(channel, id);
-}
-
-
 StartBulldozer(void)
 {
   if (!UserSoundOn) return;
@@ -269,22 +260,64 @@ StopBulldozer(void)
 #else /* WITH_SDL_MIXER */
 InitializeSound()
 {
+  SoundInitialized = 1;
 }
 
 ShutDownSound()
 {
+  SoundInitialized = 0;
 }
 
 MakeSound(char *channel, char *id)
 {
-}
+  char filename[256], player[256];
+  static struct timeval last = {0, 0};
+  struct timeval now;
+  unsigned int diff;
+  int i;
+  pid_t pid;
 
-MakeSoundOn(SimView *view, char *channel, char *id)
-{
+  gettimeofday(&now, NULL);
+
+  diff = ((now.tv_sec - last.tv_sec) * 1000000) +
+    (now.tv_usec - last.tv_usec);
+  
+  if (diff < 100000)
+    return;
+
+  last = now;
+
+  if (!UserSoundOn) return;
+  if (!SoundInitialized) return;
+
+  for (i = 0; i < SIM_NSOUNDS; i++) {
+    if (!strcmp(sounds[i].id, id))
+      break;
+  }
+  snprintf(filename, sizeof(filename), "%s/sounds/%s", ResourceDir,
+          sounds[i].file);
+
+  snprintf(player, sizeof(player), "%s/sounds/player", ResourceDir);
+
+  pid = fork();
+
+  switch(pid) {
+    case 0:
+      execl(player, player, filename, NULL);
+      exit(1);
+      break;
+    case -1:
+      perror("fork failed");
+      break;
+    default:
+      break;
+  }
 }
 
 StartBulldozer(void)
 {
+  MakeSound(0, "Rumble");
 }
 
 StopBulldozer(void)
@@ -293,6 +326,15 @@ StopBulldozer(void)
 #endif
 
 
+MakeSoundOn(SimView *view, char *channel, char *id)
+{
+  if (!UserSoundOn) return;
+  if (!SoundInitialized) return;
+
+  MakeSound(channel, id);
+}
+
+
 /* XXX comefrom: doKeyEvent */
 SoundOff(void)
 {
Impressum, Datenschutz