rnd-20001204-3-src
[rocksndiamonds.git] / src / init.c
index 411d54070a0170ff274c47e19b095bfe8a8caa81..d83b754235cc37dce7569864cbae08d712a1bc94 100644 (file)
 
 #include <signal.h>
 
+#include "libgame/libgame.h"
+
 #include "init.h"
 #include "events.h"
-#include "misc.h"
-#include "sound.h"
 #include "screens.h"
 #include "editor.h"
 #include "game.h"
@@ -24,8 +24,6 @@
 #include "tools.h"
 #include "files.h"
 #include "joystick.h"
-#include "image.h"
-#include "pcx.h"
 #include "network.h"
 #include "netserv.h"
 
@@ -35,39 +33,41 @@ struct PictureFileInfo
   boolean picture_with_mask;
 };
 
-#ifndef TARGET_SDL
-static int sound_process_id = 0;
-#endif
-
 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(int argc, char *argv[])
+void OpenAll(void)
 {
-#if defined(MSDOS) || defined(WIN32)
+#if !defined(PLATFORM_UNIX)
   initErrorFile();
 #endif
 
   if (options.serveronly)
   {
-#ifdef WIN32
-    Error(ERR_WARN, "networking not supported in Windows version");
-#else
+#if defined(PLATFORM_UNIX)
     NetworkServer(options.server_port, options.serveronly);
+#else
+    Error(ERR_WARN, "networking not supported in Windows version");
 #endif
 
     /* never reached */
     exit(0);
   }
 
+  InitProgramInfo(UNIX_USERDATA_DIRECTORY,
+                 PROGRAM_TITLE_STRING, WINDOW_TITLE_STRING,
+                 ICON_TITLE_STRING, X11_ICON_FILENAME, X11_ICONMASK_FILENAME,
+                 MSDOS_POINTER_FILENAME);
+
   InitPlayerInfo();
 
   InitCounter();
@@ -79,7 +79,7 @@ void OpenAll(int argc, char *argv[])
   signal(SIGINT, CloseAllAndExit);
   signal(SIGTERM, CloseAllAndExit);
 
-  InitBufferedDisplay(&backbuffer, &window);
+  InitDisplay();
   InitEventFilter(FilterMouseMotionEvents);
 
   InitGfx();
@@ -121,14 +121,14 @@ void InitLevelInfo()
 
 void InitNetworkServer()
 {
-#if !defined(MSDOS) && !defined(WIN32)
+#if defined(PLATFORM_UNIX)
   int nr_wanted;
 #endif
 
   if (!options.network)
     return;
 
-#if !defined(MSDOS) && !defined(WIN32)
+#if defined(PLATFORM_UNIX)
   nr_wanted = Request("Choose player", REQ_PLAYER | REQ_STAY_CLOSED);
 
   if (!ConnectToServer(options.server_host, options.server_port))
@@ -146,43 +146,18 @@ void InitSound()
 {
   int i;
 
-  if (sound_status == SOUND_OFF)
-    return;
-
-#ifdef TARGET_SDL
-  if (InitAudio())
-  {
-    sound_status = SOUND_AVAILABLE;
-    sound_loops_allowed = TRUE;
-  }
-  else
-  {
-    sound_status = SOUND_OFF;
-  }
-#else /* !TARGET_SDL */
-
-#if !defined(MSDOS) && !defined(WIN32)
-  if ((sound_status = CheckAudio(sound_device_name)) == SOUND_OFF)
-    return;
+  OpenAudio(&audio);
 
-#ifdef VOXWARE
-  sound_loops_allowed = TRUE;
-#endif
-
-#else /* MSDOS || WIN32 */
-  sound_loops_allowed = TRUE;
-
-#endif /* MSDOS || WIN32 */
-#endif /* !TARGET_SDL */
+  AllocSoundArray(NUM_SOUNDS);
 
   for(i=0; i<NUM_SOUNDS; i++)
   {
-    Sound[i].name = sound_name[i];
-
-    if (!LoadSound(&Sound[i]))
+    if (!LoadSound(i, sound_name[i]))
     {
-      sound_status = SOUND_OFF;
-      sound_loops_allowed = FALSE;
+      audio.sound_available = FALSE;
+      audio.loops_available = FALSE;
+      audio.sound_enabled = FALSE;
+
       return;
     }
   }
@@ -190,49 +165,47 @@ void InitSound()
 
 void InitSoundServer()
 {
-  if (sound_status == SOUND_OFF)
+  if (!audio.sound_available)
     return;
 
-#ifndef TARGET_SDL
-
-#if !defined(MSDOS) && !defined(WIN32)
+#if !defined(TARGET_SDL)
+#if defined(PLATFORM_UNIX)
 
-  if (pipe(sound_pipe)<0)
+  if (pipe(audio.soundserver_pipe) < 0)
   {
     Error(ERR_WARN, "cannot create pipe - no sounds");
-    sound_status = SOUND_OFF;
+    audio.sound_available = FALSE;
     return;
   }
 
-  if ((sound_process_id = fork()) < 0)
+  if ((audio.soundserver_pid = fork()) < 0)
   {       
     Error(ERR_WARN, "cannot create sound server process - no sounds");
-    sound_status = SOUND_OFF;
+    audio.sound_available = FALSE;
     return;
   }
 
-  if (!sound_process_id)       /* we are child */
+  if (audio.soundserver_pid == 0)      /* we are child */
   {
     SoundServer();
 
     /* never reached */
     exit(0);
   }
-  else                         /* we are parent */
-    close(sound_pipe[0]);      /* no reading from pipe needed */
+  else                                 /* we are parent */
+    close(audio.soundserver_pipe[0]); /* no reading from pipe needed */
 
-#else /* MSDOS || WIN32 */
+#else /* !PLATFORM_UNIX */
 
   SoundServer();
 
-#endif /* MSDOS */
-
+#endif /* !PLATFORM_UNIX */
 #endif /* !TARGET_SDL */
 }
 
 void InitJoysticks()
 {
-#ifdef USE_SDL_JOYSTICK
+#if defined(TARGET_SDL)
   static boolean sdl_joystick_subsystem_initialized = FALSE;
 #endif
 
@@ -243,7 +216,7 @@ void InitJoysticks()
 
   joystick_status = JOYSTICK_OFF;
 
-#ifdef USE_SDL_JOYSTICK
+#if defined(TARGET_SDL)
 
   if (!sdl_joystick_subsystem_initialized)
   {
@@ -283,9 +256,9 @@ void InitJoysticks()
     joystick_status = JOYSTICK_AVAILABLE;
   }
 
-#else /* !USE_SDL_JOYSTICK */
+#else /* !TARGET_SDL */
 
-#ifndef MSDOS
+#if defined(PLATFORM_UNIX)
   for (i=0; i<MAX_PLAYERS; i++)
   {
     char *device_name = setup.input[i].joy.device_name;
@@ -315,7 +288,7 @@ void InitJoysticks()
     joystick_status = JOYSTICK_AVAILABLE;
   }
 
-#else /* MSDOS */
+#else /* !PLATFORM_UNIX */
 
   /* try to access two joysticks; if that fails, try to access just one */
   if (install_joystick(JOY_TYPE_2PADS) == 0 ||
@@ -342,33 +315,24 @@ void InitJoysticks()
 #endif /* !TARGET_SDL */
 }
 
+void InitDisplay()
+{
+  InitVideoDisplay();
+  InitVideoBuffer(&backbuffer, &window, WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH,
+                 setup.fullscreen);
+}
+
 void InitGfx()
 {
-  int i, j;
+  int i;
 
-#ifdef TARGET_SDL
-  SDL_Surface *sdl_image_tmp;
-#else
+#if defined(TARGET_X11)
   GC copy_clipmask_gc;
   XGCValues clip_gc_values;
   unsigned long clip_gc_valuemask;
 #endif
 
-#ifdef MSDOS
-  static struct PictureFileInfo pic[NUM_PICTURES] =
-  {
-    { "Screen",        TRUE },
-    { "Door",  TRUE },
-    { "Heroes",        TRUE },
-    { "Toons", TRUE },
-    { "SP",    TRUE },
-    { "DC",    TRUE },
-    { "More",  TRUE },
-    { "Font",  FALSE },
-    { "Font2", FALSE },
-    { "Font3", FALSE }
-  }; 
-#else
+#if !defined(PLATFORM_MSDOS)
   static struct PictureFileInfo pic[NUM_PICTURES] =
   {
     { "RocksScreen",   TRUE },
@@ -382,8 +346,23 @@ void InitGfx()
     { "RocksFont2",    FALSE },
     { "RocksFont3",    FALSE }
   }; 
+#else
+  static struct PictureFileInfo pic[NUM_PICTURES] =
+  {
+    { "Screen",        TRUE },
+    { "Door",  TRUE },
+    { "Heroes",        TRUE },
+    { "Toons", TRUE },
+    { "SP",    TRUE },
+    { "DC",    TRUE },
+    { "More",  TRUE },
+    { "Font",  FALSE },
+    { "Font2", FALSE },
+    { "Font3", FALSE }
+  }; 
 #endif
 
+#ifdef TARGET_X11
   static struct
   {
     int start;
@@ -435,29 +414,46 @@ void InitGfx()
     { GFX2_SHIELD_ACTIVE, 3 },
     { -1, 0 }
   };
+#endif
 
-  /* create additional image buffers for double-buffering */
+  /* initialize some global variables */
+  global.frames_per_second = 0;
+  global.fps_slowdown = FALSE;
+  global.fps_slowdown_factor = 1;
+
+  /* initialize screen properties */
+  InitGfxFieldInfo(SX, SY, SXSIZE, SYSIZE,
+                  REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
+  InitGfxDoor1Info(DX, DY, DXSIZE, DYSIZE);
+  InitGfxDoor2Info(VX, VY, VXSIZE, VYSIZE);
+  InitGfxScrollbufferInfo(FXSIZE, FYSIZE);
 
+  /* create additional image buffers for double-buffering */
   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]);
+  InitFontInfo(NULL, NULL, pix[PIX_SMALLFONT]);
+
   DrawInitText(WINDOW_TITLE_STRING, 20, FC_YELLOW);
   DrawInitText(WINDOW_SUBTITLE_STRING, 50, FC_RED);
-#ifdef MSDOS
+#if defined(PLATFORM_MSDOS)
   DrawInitText(PROGRAM_DOS_PORT_STRING, 210, FC_BLUE);
   rest(200);
-#endif /* MSDOS */
+#endif
   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 */
 
-#ifdef TARGET_SDL
+#if defined(TARGET_SDL)
 
+#if 0
   /* initialize surface array to 'NULL' */
   for(i=0; i<NUM_TILES; i++)
     tile_masked[i] = NULL;
@@ -467,6 +463,7 @@ void InitGfx()
   {
     for(j=0; j<tile_needs_clipping[i].count; j++)
     {
+      SDL_Surface *sdl_image_tmp;
       int tile = tile_needs_clipping[i].start + j;
       int graphic = tile;
       int src_x, src_y;
@@ -474,7 +471,7 @@ void InitGfx()
       Bitmap src_bitmap;
 
       getGraphicSource(graphic, &bitmap_nr, &src_x, &src_y);
-      src_bitmap = pix_masked[bitmap_nr];
+      src_bitmap = pix[bitmap_nr];
 
       /* create surface for masked tile graphic */
       if ((sdl_image_tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, TILEX, TILEY,
@@ -493,6 +490,7 @@ void InitGfx()
       BlitBitmap(src_bitmap, tile_masked[tile], src_x,src_y, TILEX,TILEY, 0,0);
     }
   }
+#endif
 
 #else /* !TARGET_SDL */
 
@@ -500,21 +498,23 @@ void InitGfx()
   clip_gc_values.graphics_exposures = False;
   clip_gc_valuemask = GCGraphicsExposures;
   copy_clipmask_gc =
-    XCreateGC(display, clipmask[PIX_BACK], clip_gc_valuemask, &clip_gc_values);
+    XCreateGC(display, pix[PIX_BACK]->clip_mask,
+             clip_gc_valuemask, &clip_gc_values);
 
   clip_gc_values.graphics_exposures = False;
   clip_gc_valuemask = GCGraphicsExposures;
   tile_clip_gc =
-    XCreateGC(display, window, clip_gc_valuemask, &clip_gc_values);
+    XCreateGC(display, window->drawable, clip_gc_valuemask, &clip_gc_values);
 
   for(i=0; i<NUM_BITMAPS; i++)
   {
-    if (clipmask[i])
+    if (pix[i]->clip_mask)
     {
       clip_gc_values.graphics_exposures = False;
-      clip_gc_values.clip_mask = clipmask[i];
+      clip_gc_values.clip_mask = pix[i]->clip_mask;
       clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
-      clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
+      pix[i]->stored_clip_gc = XCreateGC(display, window->drawable,
+                                        clip_gc_valuemask,&clip_gc_values);
     }
   }
 
@@ -525,6 +525,8 @@ void InitGfx()
   /* create only those clipping Pixmaps we really need */
   for(i=0; tile_needs_clipping[i].start>=0; i++)
   {
+    int j;
+
     for(j=0; j<tile_needs_clipping[i].count; j++)
     {
       int tile = tile_needs_clipping[i].start + j;
@@ -534,12 +536,13 @@ void InitGfx()
       Pixmap src_pixmap;
 
       getGraphicSource(graphic, &pixmap_nr, &src_x, &src_y);
-      src_pixmap = clipmask[pixmap_nr];
+      src_pixmap = pix[pixmap_nr]->clip_mask;
 
-      tile_clipmask[tile] = XCreatePixmap(display, window, TILEX,TILEY, 1);
+      tile_clipmask[tile] = XCreatePixmap(display, window->drawable,
+                                         TILEX, TILEY, 1);
 
-      XCopyArea(display,src_pixmap,tile_clipmask[tile],copy_clipmask_gc,
-               src_x,src_y, TILEX,TILEY, 0,0);
+      XCopyArea(display, src_pixmap, tile_clipmask[tile], copy_clipmask_gc,
+               src_x, src_y, TILEX, TILEY, 0, 0);
     }
   }
 
@@ -550,7 +553,7 @@ void InitGfxBackground()
 {
   int x, y;
 
-  drawto = backbuffer = pix[PIX_DB_BACK];
+  drawto = backbuffer;
   fieldbuffer = pix[PIX_DB_FIELD];
   SetDrawtoField(DRAW_BACKBUFFER);
 
@@ -570,11 +573,11 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
   char basefilename[256];
   char filename[256];
 
-#ifdef TARGET_SDL
+#if defined(TARGET_SDL)
   SDL_Surface *sdl_image_tmp;
-#else /* !TARGET_SDL */
+#else
   int pcx_err;
-#endif /* !TARGET_SDL */
+#endif
   char *picture_ext = ".pcx";
 
   /* Grafik laden */
@@ -585,23 +588,25 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
     sprintf(filename, "%s/%s/%s",
            options.ro_base_directory, GRAPHICS_DIRECTORY, basefilename);
 
-#ifdef MSDOS
+#if defined(PLATFORM_MSDOS)
     rest(100);
-#endif /* MSDOS */
+#endif
 
-#ifdef TARGET_SDL
+    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] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
+    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_masked[pos] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
+    if ((pix[pos]->surface_masked = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
       Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
 
     /* free temporary surface */
@@ -609,8 +614,9 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
 
 #else /* !TARGET_SDL */
 
-    pcx_err = Read_PCX_to_Pixmap(display, window, gc, filename,
-                                &pix[pos], &clipmask[pos]);
+    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:
@@ -629,21 +635,25 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
        break;
     }
 
-    if (!pix[pos])
+    if (!pix[pos]->drawable)
       Error(ERR_EXIT, "cannot get graphics for '%s'", pic->picture_filename);
 
+    pix[pos]->gc = window->gc;
+
+#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 */
   }
 
-#ifndef TARGET_SDL
-  /* zugehörige Maske laden (wenn vorhanden) */
-  if (pic->picture_with_mask && !clipmask[pos])
+#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 /* !TARGET_SDL */
+#endif
 }
 
 void InitGadgets()
@@ -1807,38 +1817,29 @@ void CloseAllAndExit(int exit_value)
 {
   int i;
 
-#ifdef TARGET_SDL
+#if defined(TARGET_SDL)
   StopSounds();
   FreeSounds(NUM_SOUNDS);
 #else
-  if (sound_process_id)
+  if (audio.soundserver_pid)
   {
     StopSounds();
-    kill(sound_process_id, SIGTERM);
+    kill(audio.soundserver_pid, SIGTERM);
     FreeSounds(NUM_SOUNDS);
   }
 #endif
 
   for(i=0; i<NUM_BITMAPS; i++)
-  {
-    if (pix[i])
-      FreeBitmap(pix[i]);
-
-#ifdef TARGET_SDL
-    FreeBitmap(pix_masked[i]);
-#else
-    if (clipmask[i])
-      FreeBitmap(clipmask[i]);
-    if (clip_gc[i])
-      XFreeGC(display, clip_gc[i]);
-#endif
-  }
+    FreeBitmap(pix[i]);
 
-#ifdef TARGET_SDL
+#if defined(TARGET_SDL)
   KeyboardAutoRepeatOn();
 #else
+
+#if 0
   if (gc)
     XFreeGC(display, gc);
+#endif
 
   if (display)
   {
@@ -1847,7 +1848,7 @@ void CloseAllAndExit(int exit_value)
   }
 #endif
 
-#if defined(MSDOS) || defined(WIN32)
+#if !defined(PLATFORM_UNIX)
   dumpErrorFile();
 #endif