rnd-20001210-2-src
[rocksndiamonds.git] / src / init.c
index 8d9571c9a88834006fc2934aac026245de8c6f97..dbe878cd7063942f299061ba83186f00eb3b10d5 100644 (file)
@@ -1,14 +1,14 @@
 /***********************************************************
-*  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
+* Rocks'n'Diamonds -- McDuffin Strikes Back!               *
 *----------------------------------------------------------*
-*  (c) 1995-98 Artsoft Entertainment                       *
-*              Holger Schemel                              *
-*              Oststrasse 11a                              *
-*              33604 Bielefeld                             *
-*              phone: ++49 +521 290471                     *
-*              email: aeglos@valinor.owl.de                *
+* (c) 1995-2000 Artsoft Entertainment                      *
+*               Holger Schemel                             *
+*               Detmolder Strasse 189                      *
+*               33604 Bielefeld                            *
+*               Germany                                    *
+*               e-mail: info@artsoft.org                   *
 *----------------------------------------------------------*
-*  init.c                                                  *
+* init.c                                                   *
 ***********************************************************/
 
 #include <signal.h>
 #include "network.h"
 #include "netserv.h"
 
-struct PictureFileInfo
-{
-  char *picture_filename;
-  boolean picture_with_mask;
-};
-
 static void InitPlayerInfo(void);
 static void InitLevelInfo(void);
 static void InitNetworkServer(void);
 static void InitSound(void);
 static void InitSoundServer(void);
-static void InitDisplay(void);
 static void InitGfx(void);
 static void InitGfxBackground(void);
-static void LoadGfx(int, struct PictureFileInfo *);
 static void InitGadgets(void);
 static void InitElementProperties(void);
 
 void OpenAll(void)
 {
-#if !defined(PLATFORM_UNIX)
-  initErrorFile();
-#endif
-
   if (options.serveronly)
   {
 #if defined(PLATFORM_UNIX)
     NetworkServer(options.server_port, options.serveronly);
 #else
-    Error(ERR_WARN, "networking not supported in Windows version");
+    Error(ERR_WARN, "networking only supported in Unix version");
 #endif
-
-    /* never reached */
-    exit(0);
+    exit(0);   /* never reached */
   }
 
   InitProgramInfo(UNIX_USERDATA_DIRECTORY,
@@ -76,10 +62,10 @@ void OpenAll(void)
   InitJoysticks();
   InitRND(NEW_RANDOMIZE);
 
-  signal(SIGINT, CloseAllAndExit);
-  signal(SIGTERM, CloseAllAndExit);
+  InitVideoDisplay();
+  InitVideoBuffer(&backbuffer, &window, WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH,
+                 setup.fullscreen);
 
-  InitDisplay();
   InitEventFilter(FilterMouseMotionEvents);
 
   InitGfx();
@@ -146,13 +132,11 @@ void InitSound()
 {
   int i;
 
-  OpenAudio(&audio);
-
-  AllocSoundArray(NUM_SOUNDS);
+  OpenAudio();
 
   for(i=0; i<NUM_SOUNDS; i++)
   {
-    if (!LoadSound(i, sound_name[i]))
+    if (!LoadSound(sound_name[i]))
     {
       audio.sound_available = FALSE;
       audio.loops_available = FALSE;
@@ -161,6 +145,8 @@ void InitSound()
       return;
     }
   }
+
+  num_bg_loops = LoadMusic();
 }
 
 void InitSoundServer()
@@ -315,13 +301,6 @@ void InitJoysticks()
 #endif /* !TARGET_SDL */
 }
 
-void InitDisplay()
-{
-  InitVideoDisplay();
-  InitVideoBuffer(&backbuffer, &window, WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH,
-                 setup.fullscreen);
-}
-
 void InitGfx()
 {
   int i;
@@ -333,32 +312,32 @@ void InitGfx()
 #endif
 
 #if !defined(PLATFORM_MSDOS)
-  static struct PictureFileInfo pic[NUM_PICTURES] =
+  static char *image_filename[NUM_PICTURES] =
   {
-    { "RocksScreen",   TRUE },
-    { "RocksDoor",     TRUE },
-    { "RocksHeroes",   TRUE },
-    { "RocksToons",    TRUE },
-    { "RocksSP",       TRUE },
-    { "RocksDC",       TRUE },
-    { "RocksMore",     TRUE },
-    { "RocksFont",     FALSE },
-    { "RocksFont2",    FALSE },
-    { "RocksFont3",    FALSE }
+    "RocksScreen.pcx",
+    "RocksDoor.pcx",
+    "RocksHeroes.pcx",
+    "RocksToons.pcx",
+    "RocksSP.pcx",
+    "RocksDC.pcx",
+    "RocksMore.pcx",
+    "RocksFont.pcx",
+    "RocksFont2.pcx",
+    "RocksFont3.pcx"
   }; 
 #else
-  static struct PictureFileInfo pic[NUM_PICTURES] =
+  static char *image_filename[NUM_PICTURES] =
   {
-    { "Screen",        TRUE },
-    { "Door",  TRUE },
-    { "Heroes",        TRUE },
-    { "Toons", TRUE },
-    { "SP",    TRUE },
-    { "DC",    TRUE },
-    { "More",  TRUE },
-    { "Font",  FALSE },
-    { "Font2", FALSE },
-    { "Font3", FALSE }
+    "Screen.pcx",
+    "Door.pcx",
+    "Heroes.pcx",
+    "Toons.pcx",
+    "SP.pcx",
+    "DC.pcx",
+    "More.pcx",
+    "Font.pcx",
+    "Font2.pcx",
+    "Font3.pcx"
   }; 
 #endif
 
@@ -432,7 +411,7 @@ void InitGfx()
   pix[PIX_DB_DOOR] = CreateBitmap(3 * DXSIZE, DYSIZE + VYSIZE, DEFAULT_DEPTH);
   pix[PIX_DB_FIELD] = CreateBitmap(FXSIZE, FYSIZE, DEFAULT_DEPTH);
 
-  LoadGfx(PIX_SMALLFONT, &pic[PIX_SMALLFONT]);
+  pix[PIX_SMALLFONT] = LoadImage(image_filename[PIX_SMALLFONT]);
   InitFontInfo(NULL, NULL, pix[PIX_SMALLFONT]);
 
   DrawInitText(WINDOW_TITLE_STRING, 20, FC_YELLOW);
@@ -444,56 +423,17 @@ void InitGfx()
   DrawInitText("Loading graphics:",120,FC_GREEN);
 
   for(i=0; i<NUM_PICTURES; i++)
-    if (i != PIX_SMALLFONT)
-      LoadGfx(i,&pic[i]);
-
-  InitFontInfo(pix[PIX_BIGFONT], pix[PIX_MEDIUMFONT], pix[PIX_SMALLFONT]);
-
-  /* create additional image buffers for masking of graphics */
-
-#if defined(TARGET_SDL)
-
-#if 0
-  /* initialize surface array to 'NULL' */
-  for(i=0; i<NUM_TILES; i++)
-    tile_masked[i] = NULL;
-
-  /* create only those masked surfaces we really need */
-  for(i=0; tile_needs_clipping[i].start>=0; i++)
   {
-    for(j=0; j<tile_needs_clipping[i].count; j++)
+    if (i != PIX_SMALLFONT)
     {
-      SDL_Surface *sdl_image_tmp;
-      int tile = tile_needs_clipping[i].start + j;
-      int graphic = tile;
-      int src_x, src_y;
-      int bitmap_nr;
-      Bitmap src_bitmap;
-
-      getGraphicSource(graphic, &bitmap_nr, &src_x, &src_y);
-      src_bitmap = pix[bitmap_nr];
-
-      /* create surface for masked tile graphic */
-      if ((sdl_image_tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, TILEX, TILEY,
-                                               WIN_SDL_DEPTH, 0, 0, 0, 0))
-         == NULL)
-       Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
-
-      /* create native transparent surface for current image */
-      SDL_SetColorKey(sdl_image_tmp, SDL_SRCCOLORKEY,
-                     SDL_MapRGB(sdl_image_tmp->format, 0x00, 0x00, 0x00));
-      if ((tile_masked[tile] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-       Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
-
-      SDL_FreeSurface(sdl_image_tmp);
-
-      BlitBitmap(src_bitmap, tile_masked[tile], src_x,src_y, TILEX,TILEY, 0,0);
+      DrawInitText(image_filename[i], 150, FC_YELLOW);
+      pix[i] = LoadImage(image_filename[i]);
     }
   }
-#endif
 
-#else /* !TARGET_SDL */
+  InitFontInfo(pix[PIX_BIGFONT], pix[PIX_MEDIUMFONT], pix[PIX_SMALLFONT]);
 
+#if defined(TARGET_X11)
   /* create graphic context structures needed for clipping */
   clip_gc_values.graphics_exposures = False;
   clip_gc_valuemask = GCGraphicsExposures;
@@ -545,8 +485,7 @@ void InitGfx()
                src_x, src_y, TILEX, TILEY, 0, 0);
     }
   }
-
-#endif /* !TARGET_SDL */
+#endif /* TARGET_X11 */
 }
 
 void InitGfxBackground()
@@ -568,92 +507,6 @@ void InitGfxBackground()
   redraw_mask = REDRAW_ALL;
 }
 
-void LoadGfx(int pos, struct PictureFileInfo *pic)
-{
-  char basefilename[256];
-  char filename[256];
-
-#if defined(TARGET_SDL)
-  SDL_Surface *sdl_image_tmp;
-#else
-  int pcx_err;
-#endif
-  char *picture_ext = ".pcx";
-
-  /* Grafik laden */
-  if (pic->picture_filename)
-  {
-    sprintf(basefilename, "%s%s", pic->picture_filename, picture_ext);
-    DrawInitText(basefilename, 150, FC_YELLOW);
-    sprintf(filename, "%s/%s/%s",
-           options.ro_base_directory, GRAPHICS_DIRECTORY, basefilename);
-
-#if defined(PLATFORM_MSDOS)
-    rest(100);
-#endif
-
-    pix[pos] = CreateBitmapStruct();
-
-#if defined(TARGET_SDL)
-    /* load image to temporary surface */
-    if ((sdl_image_tmp = IMG_Load(filename)) == NULL)
-      Error(ERR_EXIT, "IMG_Load() failed: %s", SDL_GetError());
-
-    /* create native non-transparent surface for current image */
-    if ((pix[pos]->surface = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-      Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
-
-    /* create native transparent surface for current image */
-    SDL_SetColorKey(sdl_image_tmp, SDL_SRCCOLORKEY,
-                   SDL_MapRGB(sdl_image_tmp->format, 0x00, 0x00, 0x00));
-    if ((pix[pos]->surface_masked = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-      Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
-
-    /* free temporary surface */
-    SDL_FreeSurface(sdl_image_tmp);
-
-#else /* !TARGET_SDL */
-
-    pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc,
-                                filename,
-                                &pix[pos]->drawable, &pix[pos]->clip_mask);
-    switch(pcx_err)
-    {
-      case PCX_Success:
-        break;
-      case PCX_OpenFailed:
-        Error(ERR_EXIT, "cannot open PCX file '%s'", filename);
-      case PCX_ReadFailed:
-        Error(ERR_EXIT, "cannot read PCX file '%s'", filename);
-      case PCX_FileInvalid:
-       Error(ERR_EXIT, "invalid PCX file '%s'", filename);
-      case PCX_NoMemory:
-       Error(ERR_EXIT, "not enough memory for PCX file '%s'", filename);
-      case PCX_ColorFailed:
-       Error(ERR_EXIT, "cannot get colors for PCX file '%s'", filename);
-      default:
-       break;
-    }
-
-    if (!pix[pos]->drawable)
-      Error(ERR_EXIT, "cannot get graphics for '%s'", pic->picture_filename);
-
-#if 0
-    /* setting pix_masked[] to pix[] allows BlitBitmapMasked() to always
-       use pix_masked[], although they are the same when not using SDL */
-    pix_masked[pos] = pix[pos];
-#endif
-
-#endif /* !TARGET_SDL */
-  }
-
-#if defined(TARGET_X11)
-  /* check if clip mask was correctly created */
-  if (pic->picture_with_mask && !pix[pos]->clip_mask)
-    Error(ERR_EXIT, "cannot get clipmask for '%s'", pic->picture_filename);
-#endif
-}
-
 void InitGadgets()
 {
   CreateLevelEditorGadgets();
@@ -1815,40 +1668,22 @@ void CloseAllAndExit(int exit_value)
 {
   int i;
 
-#if defined(TARGET_SDL)
   StopSounds();
   FreeSounds(NUM_SOUNDS);
-#else
+
+#if !defined(TARGET_SDL)
   if (audio.soundserver_pid)
-  {
-    StopSounds();
     kill(audio.soundserver_pid, SIGTERM);
-    FreeSounds(NUM_SOUNDS);
-  }
 #endif
 
   for(i=0; i<NUM_BITMAPS; i++)
     FreeBitmap(pix[i]);
 
-#if defined(TARGET_SDL)
   KeyboardAutoRepeatOn();
-#else
 
-#if 0
-  if (gc)
-    XFreeGC(display, gc);
-#endif
+  CloseVideoDisplay();
 
-  if (display)
-  {
-    KeyboardAutoRepeatOn();
-    XCloseDisplay(display);
-  }
-#endif
-
-#if !defined(PLATFORM_UNIX)
-  dumpErrorFile();
-#endif
+  ClosePlatformDependantStuff();
 
   exit(exit_value);
 }