rnd-20001203-1-src
[rocksndiamonds.git] / src / init.c
index b95b99c290fa5c81e620e2dee5e2da680b7174e6..1c56fc0a3d9b7f6cfc782690452bc007333b5ae0 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,15 +33,12 @@ 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 *);
@@ -79,7 +74,7 @@ void OpenAll(int argc, char *argv[])
   signal(SIGINT, CloseAllAndExit);
   signal(SIGTERM, CloseAllAndExit);
 
-  InitBufferedDisplay(&backbuffer, &window);
+  InitDisplay();
   InitEventFilter(FilterMouseMotionEvents);
 
   InitGfx();
@@ -146,34 +141,7 @@ void InitSound()
 {
   int i;
 
-  if (sound_status == SOUND_OFF)
-    return;
-
-#if defined(TARGET_SDL)
-  if (InitAudio())
-  {
-    sound_status = SOUND_AVAILABLE;
-    sound_loops_allowed = TRUE;
-  }
-  else
-  {
-    sound_status = SOUND_OFF;
-  }
-#else /* !TARGET_SDL */
-
-#if defined(PLATFORM_UNIX)
-  if ((sound_status = CheckAudio(sound_device_name)) == SOUND_OFF)
-    return;
-
-#ifdef VOXWARE
-  sound_loops_allowed = TRUE;
-#endif
-
-#else /* !PLATFORM_UNIX */
-  sound_loops_allowed = TRUE;
-
-#endif /* !PLATFORM_UNIX */
-#endif /* !TARGET_SDL */
+  OpenAudio(&audio);
 
   for(i=0; i<NUM_SOUNDS; i++)
   {
@@ -181,8 +149,8 @@ void InitSound()
 
     if (!LoadSound(&Sound[i]))
     {
-      sound_status = SOUND_OFF;
-      sound_loops_allowed = FALSE;
+      audio.sound_available = FALSE;
+      audio.loops_available = FALSE;
       return;
     }
   }
@@ -190,35 +158,35 @@ void InitSound()
 
 void InitSoundServer()
 {
-  if (sound_status == SOUND_OFF)
+  if (!audio.sound_available)
     return;
 
 #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 /* !PLATFORM_UNIX */
 
@@ -340,6 +308,24 @@ void InitJoysticks()
 #endif /* !TARGET_SDL */
 }
 
+void InitDisplay()
+{
+  char *gfx_dir = getPath2(options.ro_base_directory, GRAPHICS_DIRECTORY);
+  char *x11_icon_filename = getPath2(gfx_dir, X11_ICON_FILENAME);
+  char *x11_iconmask_filename = getPath2(gfx_dir, X11_ICONMASK_FILENAME);
+  char *msdos_pointer_filename = getPath2(gfx_dir, MSDOS_POINTER_FILENAME);
+
+  free(gfx_dir);
+
+  InitProgramInfo(program_name, PROGRAM_TITLE_STRING, WINDOW_TITLE_STRING,
+                 ICON_TITLE_STRING, x11_icon_filename, x11_iconmask_filename,
+                 msdos_pointer_filename);
+
+  InitVideoDisplay();
+  InitVideoBuffer(&backbuffer, &window, WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH,
+                 setup.fullscreen);
+}
+
 void InitGfx()
 {
   int i, j;
@@ -434,12 +420,22 @@ void InitGfx()
     { -1, 0 }
   };
 
+  /* initialize playfield properties */
+
+  InitPlayfieldInfo(SX, SY, SXSIZE, SYSIZE,
+                   REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
+  InitDoor1Info(DX, DY, DXSIZE, DYSIZE);
+  InitDoor2Info(VX, VY, VXSIZE, VYSIZE);
+  InitScrollbufferInfo(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);
 #if defined(PLATFORM_MSDOS)
@@ -452,6 +448,8 @@ void InitGfx()
     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)
@@ -498,21 +496,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);
     }
   }
 
@@ -532,12 +532,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);
     }
   }
 
@@ -548,7 +549,7 @@ void InitGfxBackground()
 {
   int x, y;
 
-  drawto = backbuffer = pix[PIX_DB_BACK];
+  drawto = backbuffer;
   fieldbuffer = pix[PIX_DB_FIELD];
   SetDrawtoField(DRAW_BACKBUFFER);
 
@@ -587,6 +588,8 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
     rest(100);
 #endif
 
+    pix[pos] = CreateBitmapStruct();
+
 #if defined(TARGET_SDL)
     /* load image to temporary surface */
     if ((sdl_image_tmp = IMG_Load(filename)) == NULL)
@@ -607,8 +610,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:
@@ -627,19 +631,21 @@ 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);
 
+#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 && !clipmask[pos])
+  if (pic->picture_with_mask && !pix[pos]->clip_mask)
     Error(ERR_EXIT, "cannot get clipmask for '%s'", pic->picture_filename);
 #endif
 }
@@ -1809,34 +1815,25 @@ void CloseAllAndExit(int exit_value)
   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]);
-
-#if defined(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]);
 
 #if defined(TARGET_SDL)
   KeyboardAutoRepeatOn();
 #else
+
+#if 0
   if (gc)
     XFreeGC(display, gc);
+#endif
 
   if (display)
   {