]> cvs.zerfleddert.de Git - micropolis/blobdiff - src/sim/w_sound.c
XINCLUDE: use /usr/X11R6/include everywhere
[micropolis] / src / sim / w_sound.c
index bbe918a11d81b19377dca28ac905548668b12ceb..a903ae764ed5f75821cac2b03ec71571c4a7d473 100644 (file)
  * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
  * NOT APPLY TO YOU.
  */
+#ifdef WITH_SDL_MIXER
 #include "SDL.h"
 #include "SDL_mixer.h"
+#endif
+
 #include "sim.h"
 
 
 struct sound {
   char *id;
   char *file;
+#ifdef WITH_SDL_MIXER
   Mix_Chunk *wave;
+#else
+  void *wave;
+#endif
 };
 
 struct sound sounds[SIM_NSOUNDS] = {
@@ -129,14 +136,17 @@ struct sound sounds[SIM_NSOUNDS] = {
   { "Zone",            "zone.wav",             NULL }
 };
 
+static int SoundInitialized = 0;
+
+#ifdef WITH_SDL_MIXER
 /* Sound routines */
 
 
-int SoundInitialized = 0;
 Mix_Chunk *rumble;
 
 
-InitializeSound()
+void
+InitializeSound(void)
 {
   int reserved_chans;
   char buf[256];
@@ -175,6 +185,7 @@ InitializeSound()
 }
 
 
+void
 ShutDownSound()
 {
   int i;
@@ -195,6 +206,7 @@ ShutDownSound()
 }
 
 
+void
 MakeSound(char *channel, char *id)
 {
   char buf[256];
@@ -228,53 +240,133 @@ MakeSound(char *channel, char *id)
     fprintf(stderr, "Mix_PlayChannel: %s\n", Mix_GetError());
 }
 
-MakeSoundOn(SimView *view, char *channel, char *id)
+void
+StartBulldozer(void)
 {
   if (!UserSoundOn) return;
   if (!SoundInitialized) return;
 
-  MakeSound(channel, id);
+  if (Mix_PlayChannel(DOZER_CHANNEL, rumble, 4) == -1) {
+    printf("Mix_PlayChannel: %s\n", Mix_GetError());
+    return;
+  }
 }
 
 
-StartBulldozer(void)
+void
+StopBulldozer(void)
 {
   if (!UserSoundOn) return;
   if (!SoundInitialized) return;
 
-  if (Mix_PlayChannel(DOZER_CHANNEL, rumble, 4) == -1) {
-    printf("Mix_PlayChannel: %s\n", Mix_GetError());
+  Mix_HaltChannel(DOZER_CHANNEL);
+}
+
+#else /* WITH_SDL_MIXER */
+void
+InitializeSound()
+{
+  SoundInitialized = 1;
+}
+
+void
+ShutDownSound()
+{
+  SoundInitialized = 0;
+}
+
+void
+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;
+
+  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;
   }
 }
 
+void
+StartBulldozer(void)
+{
+  MakeSound(0, "Rumble");
+}
 
+void
 StopBulldozer(void)
+{
+}
+#endif
+
+
+void
+MakeSoundOn(SimView *view, char *channel, char *id)
 {
   if (!UserSoundOn) return;
   if (!SoundInitialized) return;
 
-  Mix_HaltChannel(DOZER_CHANNEL);
+  MakeSound(channel, id);
 }
 
 
 /* XXX comefrom: doKeyEvent */
+void
 SoundOff(void)
 {
   ShutDownSound();
 }
 
 
+void
 DoStartSound(char *channel, char *id)
 {
   MakeSound(channel, id);
 }
 
+void
 DoStopSound(char *id)
 {
   StopBulldozer();
 }
 
+int
 SoundCmd(CLIENT_ARGS)
 {
   if (!strcmp(argv[2], "Rumble"))
@@ -284,13 +376,15 @@ SoundCmd(CLIENT_ARGS)
   return 0;
 }
 
+int
 DozerCmd(CLIENT_ARGS)
 {
   StopBulldozer();
   return 0;
 }
 
-sound_command_init()
+void
+sound_command_init(void)
 {
   Tcl_CreateCommand(tk_mainInterp, "playsound", SoundCmd,
                    (ClientData)NULL, (void (*)()) NULL);
Impressum, Datenschutz