rnd-20020601-1-src
[rocksndiamonds.git] / src / init.c
index 5923322a7b265303c68486889628a3fba959c527..1f724a11a8088294c6c0781013a7202c5e60d909 100644 (file)
@@ -44,15 +44,25 @@ static void InitSetup(void);
 static void InitPlayerInfo(void);
 static void InitLevelInfo(void);
 static void InitArtworkInfo(void);
+static void InitLevelArtworkInfo(void);
 static void InitNetworkServer(void);
+static void InitMixer(void);
 static void InitSound(void);
 static void InitGfx(void);
 static void InitGfxBackground(void);
 static void InitGadgets(void);
 static void InitElementProperties(void);
+static void Execute_Debug_Command(char *);
 
 void OpenAll(void)
 {
+  if (options.debug_command)
+  {
+    Execute_Debug_Command(options.debug_command);
+
+    exit(0);
+  }
+
   if (options.serveronly)
   {
 #if defined(PLATFORM_UNIX)
@@ -74,7 +84,7 @@ void OpenAll(void)
   InitArtworkInfo();           /* needed before loading gfx, sound & music */
 
   InitCounter();
-  InitSound();
+  InitMixer();
   InitJoysticks();
   InitRND(NEW_RANDOMIZE);
 
@@ -88,11 +98,15 @@ void OpenAll(void)
   InitElementProperties();     /* initializes IS_CHAR() for el2gfx() */
 
   InitLevelInfo();
+  InitLevelArtworkInfo();
   InitGadgets();               /* needs to know number of level series */
+  InitSound();                 /* needs to know current level directory */
 
   InitGfxBackground();
   InitToons();
 
+  InitGameEngine();
+
   DrawMainMenu();
 
   InitNetworkServer();
@@ -128,6 +142,11 @@ void InitArtworkInfo()
   LoadArtworkInfo();
 }
 
+void InitLevelArtworkInfo()
+{
+  LoadLevelArtworkInfo();
+}
+
 void InitNetworkServer()
 {
 #if defined(PLATFORM_UNIX)
@@ -151,39 +170,29 @@ void InitNetworkServer()
 #endif
 }
 
-void InitSound()
+static void InitMixer()
 {
-  int i;
-
   OpenAudio();
+  InitSoundList(sound_effects, NUM_SOUND_EFFECTS);
 
-  for(i=0; i<NUM_SOUNDS; i++)
-  {
-    if (!LoadCustomSound(sound_name[i]))
-    {
-      Error(ERR_WARN, "sounds deactivated");
-
-      audio.sound_available = FALSE;
-      audio.loops_available = FALSE;
-      audio.sound_enabled = FALSE;
-
-      return;
-    }
-  }
-
-  num_bg_loops = LoadCustomMusic();
+  StartMixer();
+}
 
-  StartSoundserver();
+static void InitSound()
+{
+  InitReloadSounds(artwork.snd_current->name);
+  InitReloadMusic(artwork.mus_current->name);
 }
 
-void InitTileClipmasks()
+static void InitTileClipmasks()
 {
 #if defined(TARGET_X11)
-  GC copy_clipmask_gc;
   XGCValues clip_gc_values;
   unsigned long clip_gc_valuemask;
 
 #if defined(TARGET_X11_NATIVE)
+  GC copy_clipmask_gc;
+
   static struct
   {
     int start;
@@ -326,7 +335,7 @@ void FreeTileClipmasks()
 
   for(i=0; i<NUM_BITMAPS; i++)
   {
-    if (pix[i]->stored_clip_gc)
+    if (pix[i] != NULL && pix[i]->stored_clip_gc)
     {
       XFreeGC(display, pix[i]->stored_clip_gc);
       pix[i]->stored_clip_gc = None;
@@ -361,11 +370,8 @@ void InitGfx()
 
   DrawInitText(WINDOW_TITLE_STRING, 20, FC_YELLOW);
   DrawInitText(WINDOW_SUBTITLE_STRING, 50, FC_RED);
-#if defined(PLATFORM_MSDOS)
-  DrawInitText(PROGRAM_DOS_PORT_STRING, 210, FC_BLUE);
-  rest(200);
-#endif
-  DrawInitText("Loading graphics:",120,FC_GREEN);
+
+  DrawInitText("Loading graphics:", 120, FC_GREEN);
 
   for(i=0; i<NUM_PICTURES; i++)
   {
@@ -403,7 +409,23 @@ void InitGfxBackground()
 
 void ReloadCustomArtwork()
 {
-  if (artwork.graphics_set_current != artwork.gfx_current->name)
+  static char *leveldir_current_name = NULL;
+  static boolean last_override_level_graphics = FALSE;
+  static boolean last_override_level_sounds = FALSE;
+  static boolean last_override_level_music = FALSE;
+
+  if (leveldir_current_name != leveldir_current->name)
+  {
+    /* force reload of custom artwork after new level series was selected */
+    artwork.graphics_set_current_name = NULL;
+    artwork.sounds_set_current_name = NULL;
+    artwork.music_set_current_name = NULL;
+
+    leveldir_current_name = leveldir_current->name;
+  }
+
+  if (artwork.graphics_set_current_name != artwork.gfx_current->name ||
+      last_override_level_graphics != setup.override_level_graphics)
   {
     int i;
 
@@ -421,21 +443,26 @@ void ReloadCustomArtwork()
 
     SetDoorState(DOOR_OPEN_1 | DOOR_CLOSE_2);
 
-    artwork.graphics_set_current = artwork.gfx_current->name;
+    artwork.graphics_set_current_name = artwork.gfx_current->name;
+    last_override_level_graphics = setup.override_level_graphics;
   }
 
-  if (artwork.sounds_set_current != artwork.snd_current->name)
+  if (artwork.sounds_set_current_name != artwork.snd_current->name ||
+      last_override_level_sounds != setup.override_level_sounds)
   {
     InitReloadSounds(artwork.snd_current->name);
 
-    artwork.sounds_set_current = artwork.snd_current->name;
+    artwork.sounds_set_current_name = artwork.snd_current->name;
+    last_override_level_sounds = setup.override_level_sounds;
   }
 
-  if (artwork.music_set_current != artwork.mus_current->name)
+  if (artwork.music_set_current_name != artwork.mus_current->name ||
+      last_override_level_music != setup.override_level_music)
   {
     InitReloadMusic(artwork.mus_current->name);
 
-    artwork.music_set_current = artwork.mus_current->name;
+    artwork.music_set_current_name = artwork.mus_current->name;
+    last_override_level_music = setup.override_level_music;
   }
 }
 
@@ -1597,13 +1624,31 @@ void InitElementProperties()
     Elementeigenschaften1[i] |= (EP_BIT_CHAR | EP_BIT_INACTIVE);
 }
 
+void Execute_Debug_Command(char *command)
+{
+  if (strcmp(command, "create soundinfo.conf") == 0)
+  {
+    int i;
+
+    printf("# You can configure additional/alternative sound effects here\n");
+    printf("# (The sounds below are default and therefore commented out.)\n");
+    printf("\n");
+
+    for (i=0; i<NUM_SOUND_EFFECTS; i++)
+      printf("# %s\n",
+            getFormattedSetupEntry(sound_effects[i].text,
+                                   sound_effects[i].default_filename));
+  }
+}
+
 void CloseAllAndExit(int exit_value)
 {
   int i;
 
   StopSounds();
-  FreeSounds(NUM_SOUNDS);
-  CloseAudio();
+  FreeAllSounds();
+  FreeAllMusic();
+  CloseAudio();                /* called after freeing sounds (needed for SDL) */
 
   FreeTileClipmasks();
   for(i=0; i<NUM_BITMAPS; i++)