rnd-20001125-1-src
[rocksndiamonds.git] / src / init.c
index 64f748019cac0e9050ef5048b0939bb616c9a7c1..88a5edf097e61bec42c3971f4ee12e83571fdfd8 100644 (file)
@@ -35,39 +35,55 @@ struct PictureFileInfo
   boolean picture_with_mask;
 };
 
+#if 0
 struct IconFileInfo
 {
   char *picture_filename;
   char *picturemask_filename;
 };
+#endif
 
+#ifndef USE_SDL_LIBRARY
 static int sound_process_id = 0;
+#endif
 
-static void InitLevelAndPlayerInfo(void);
+static void InitPlayerInfo(void);
+static void InitLevelInfo(void);
 static void InitNetworkServer(void);
+#if 0
 static void InitDisplay(void);
+#endif
 static void InitSound(void);
 static void InitSoundServer(void);
+#if 0
 static void InitWindow(int, char **);
+#endif
 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[])
 {
-#ifdef MSDOS
+#if defined(MSDOS) || defined(WIN32)
   initErrorFile();
 #endif
 
   if (options.serveronly)
   {
+#ifdef WIN32
+    Error(ERR_WARN, "networking not supported in Windows version");
+#else
     NetworkServer(options.server_port, options.serveronly);
+#endif
 
     /* never reached */
     exit(0);
   }
 
+  InitPlayerInfo();
+
   InitCounter();
   InitSound();
   InitSoundServer();
@@ -77,26 +93,41 @@ void OpenAll(int argc, char *argv[])
   signal(SIGINT, CloseAllAndExit);
   signal(SIGTERM, CloseAllAndExit);
 
+
+  InitBufferedDisplay(&backbuffer, &window);
+  InitEventFilter(FilterMouseMotionEvents);
+
+
+
+#if 0
+
   InitDisplay();
   InitWindow(argc, argv);
 
+#if 0
 #ifndef USE_SDL_LIBRARY
   XMapWindow(display, window);
   FlushDisplay();
 #endif
+#endif
+
+#endif
+
+
 
   InitGfx();
   InitElementProperties();     /* initializes IS_CHAR() for el2gfx() */
 
-  InitLevelAndPlayerInfo();
+  InitLevelInfo();
   InitGadgets();               /* needs to know number of level series */
 
+  InitGfxBackground();
   DrawMainMenu();
 
   InitNetworkServer();
 }
 
-void InitLevelAndPlayerInfo()
+void InitPlayerInfo()
 {
   int i;
 
@@ -111,22 +142,26 @@ void InitLevelAndPlayerInfo()
 
   local_player->connected = TRUE;
 
-  LoadLevelInfo();                             /* global level info */
   LoadSetup();                                 /* global setup info */
+}
+
+void InitLevelInfo()
+{
+  LoadLevelInfo();                             /* global level info */
   LoadLevelSetup_LastSeries();                 /* last played series info */
   LoadLevelSetup_SeriesInfo();                 /* last played level info */
 }
 
 void InitNetworkServer()
 {
-#ifndef MSDOS
+#if !defined(MSDOS) && !defined(WIN32)
   int nr_wanted;
 #endif
 
   if (!options.network)
     return;
 
-#ifndef MSDOS
+#if !defined(MSDOS) && !defined(WIN32)
   nr_wanted = Request("Choose player", REQ_PLAYER | REQ_STAY_CLOSED);
 
   if (!ConnectToServer(options.server_host, options.server_port))
@@ -152,14 +187,14 @@ void InitSound()
 
   if (SDL_Init(SDL_INIT_AUDIO) < 0)
   {
-    Error(ERR_WARN, "SDL_Init() failed: %s\n", SDL_GetError());
+    Error(ERR_WARN, "SDL_Init() failed: %s", SDL_GetError());
     sound_status = SOUND_OFF;
     return;
   }
 
-  if (Mix_OpenAudio(22050, AUDIO_S16, 2, 256) < 0)
+  if (Mix_OpenAudio(22050, AUDIO_S16, 2, 512) < 0)
   {
-    Error(ERR_WARN, "Mix_OpenAudio() failed: %s\n", SDL_GetError());
+    Error(ERR_WARN, "Mix_OpenAudio() failed: %s", SDL_GetError());
     sound_status = SOUND_OFF;
     return;
   }
@@ -172,7 +207,7 @@ void InitSound()
 
 #else /* !USE_SDL_LIBRARY */
 
-#ifndef MSDOS
+#if !defined(MSDOS) && !defined(WIN32)
   if (access(sound_device_name, W_OK) != 0)
   {
     Error(ERR_WARN, "cannot access sound device - no sounds");
@@ -180,7 +215,7 @@ void InitSound()
     return;
   }
 
-  if ((sound_device = open(sound_device_name,O_WRONLY))<0)
+  if ((sound_device = OpenAudio(sound_device_name)) < 0)
   {
     Error(ERR_WARN, "cannot open sound device - no sounds");
     sound_status = SOUND_OFF;
@@ -198,14 +233,14 @@ void InitSound()
   */
 
 #endif
-#else /* MSDOS */
+#else /* MSDOS || WIN32 */
   sound_loops_allowed = TRUE;
 
   /*
   setup.sound_loops_on = TRUE;
   */
 
-#endif /* MSDOS */
+#endif /* MSDOS || WIN32 */
 #endif /* !USE_SDL_LIBRARY */
 
   for(i=0; i<NUM_SOUNDS; i++)
@@ -215,6 +250,7 @@ void InitSound()
     if (!LoadSound(&Sound[i]))
     {
       sound_status = SOUND_OFF;
+      sound_loops_allowed = FALSE;
       return;
     }
   }
@@ -230,11 +266,9 @@ void InitSoundServer()
   if (sound_status == SOUND_OFF)
     return;
 
-#ifdef USE_SDL_LIBRARY
-  return;
-#endif
+#ifndef USE_SDL_LIBRARY
 
-#ifndef MSDOS
+#if !defined(MSDOS) && !defined(WIN32)
 
   if (pipe(sound_pipe)<0)
   {
@@ -260,16 +294,18 @@ void InitSoundServer()
   else                         /* we are parent */
     close(sound_pipe[0]);      /* no reading from pipe needed */
 
-#else /* MSDOS */
+#else /* MSDOS || WIN32 */
 
   SoundServer();
 
 #endif /* MSDOS */
+
+#endif /* !USE_SDL_LIBRARY */
 }
 
 void InitJoysticks()
 {
-#ifdef USE_SDL_LIBRARY
+#ifdef USE_SDL_JOYSTICK
   static boolean sdl_joystick_subsystem_initialized = FALSE;
 #endif
 
@@ -280,7 +316,7 @@ void InitJoysticks()
 
   joystick_status = JOYSTICK_OFF;
 
-#ifdef USE_SDL_LIBRARY
+#ifdef USE_SDL_JOYSTICK
 
   if (!sdl_joystick_subsystem_initialized)
   {
@@ -288,7 +324,7 @@ void InitJoysticks()
 
     if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
     {
-      Error(ERR_EXIT, "SDL_Init() failed: %s\n", SDL_GetError());
+      Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
       return;
     }
   }
@@ -320,7 +356,7 @@ void InitJoysticks()
     joystick_status = JOYSTICK_AVAILABLE;
   }
 
-#else /* !USE_SDL_LIBRARY */
+#else /* !USE_SDL_JOYSTICK */
 
 #ifndef MSDOS
   for (i=0; i<MAX_PLAYERS; i++)
@@ -379,12 +415,16 @@ void InitJoysticks()
 #endif /* !USE_SDL_LIBRARY */
 }
 
+
+
+#if 0
+
 void InitDisplay()
 {
 #ifdef USE_SDL_LIBRARY
   /* initialize SDL video */
   if (SDL_Init(SDL_INIT_VIDEO) < 0)
-    Error(ERR_EXIT, "SDL_Init() failed: %s\n", SDL_GetError());
+    Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
 
   /* automatically cleanup SDL stuff after exit() */
   atexit(SDL_Quit);
@@ -440,10 +480,15 @@ void InitWindow(int argc, char *argv[])
 {
 #ifdef USE_SDL_LIBRARY
   /* open SDL video output device (window or fullscreen mode) */
-  if ((window = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
+#if 0
+  if ((backbuffer = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
                                 SDL_HWSURFACE))
       == NULL)
-    Error(ERR_EXIT, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
+    Error(ERR_EXIT, "SDL_SetVideoMode() failed: %s", SDL_GetError());
+#else
+  if (!SetVideoMode())
+    Error(ERR_EXIT, "setting video mode failed");
+#endif
 
   /* set window and icon title */
   SDL_WM_SetCaption(WINDOW_TITLE_STRING, WINDOW_TITLE_STRING);
@@ -570,9 +615,13 @@ void InitWindow(int argc, char *argv[])
 #endif /* !USE_SDL_LIBRARY */
 }
 
+#endif
+
+
+
 void InitGfx()
 {
-  int i,j;
+  int i, j;
 
 #ifdef USE_SDL_LIBRARY
   SDL_Surface *sdl_image_tmp;
@@ -679,6 +728,42 @@ void InitGfx()
     { -1, 0 }
   };
 
+  /* create additional image buffers for double-buffering */
+
+#if 0
+  pix[PIX_DB_BACK] = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
+#endif
+  pix[PIX_DB_DOOR] = CreateBitmap(3 * DXSIZE, DYSIZE + VYSIZE, DEFAULT_DEPTH);
+  pix[PIX_DB_FIELD] = CreateBitmap(FXSIZE, FYSIZE, DEFAULT_DEPTH);
+
+
+
+#if 0
+
+#ifdef USE_SDL_LIBRARY
+
+  /* SDL cannot directly draw to the visible video framebuffer like X11,
+     but always uses a backbuffer, which is then blitted to the visible
+     video framebuffer with 'SDL_UpdateRect' (or replaced with the current
+     visible video framebuffer with 'SDL_Flip', if the hardware supports
+     this). Therefore do not use an additional backbuffer for drawing, but
+     use a symbolic buffer (distinguishable from the SDL backbuffer) called
+     'window', which indicates that the SDL backbuffer should be updated to
+     the visible video framebuffer when attempting to blit to it.
+
+     For convenience, it seems to be a good idea to create this symbolic
+     buffer 'window' at the same size as the SDL backbuffer. Although it
+     should never be drawn to directly, it would do no harm nevertheless. */
+
+  window = pix[PIX_DB_BACK];           /* 'window' is only symbolic buffer */
+  pix[PIX_DB_BACK] = backbuffer;       /* 'backbuffer' is SDL screen buffer */
+
+#endif /* !USE_SDL_LIBRARY */
+
+#endif
+
+
+
 #if DEBUG_TIMING
   debug_print_timestamp(0, NULL);      /* initialize timestamp function */
 #endif
@@ -694,11 +779,11 @@ void InitGfx()
 #endif
 #endif
 
-  LoadGfx(PIX_SMALLFONT,&pic[PIX_SMALLFONT]);
-  DrawInitText(WINDOW_TITLE_STRING,20,FC_YELLOW);
-  DrawInitText(COPYRIGHT_STRING,50,FC_RED);
+  LoadGfx(PIX_SMALLFONT, &pic[PIX_SMALLFONT]);
+  DrawInitText(WINDOW_TITLE_STRING, 20, FC_YELLOW);
+  DrawInitText(WINDOW_SUBTITLE_STRING, 50, FC_RED);
 #ifdef MSDOS
-  DrawInitText("MSDOS version done by Guido Schulz",210,FC_BLUE);
+  DrawInitText(PROGRAM_DOS_PORT_STRING, 210, FC_BLUE);
   rest(200);
 #endif /* MSDOS */
   DrawInitText("Loading graphics:",120,FC_GREEN);
@@ -711,44 +796,9 @@ void InitGfx()
   debug_print_timestamp(0, "SUMMARY LOADING ALL GRAPHICS:");
 #endif
 
-#ifdef USE_SDL_LIBRARY
-  /* create some native image surfaces for double-buffer purposes */
-
-  /* create double-buffer surface for background image */
-  if ((sdl_image_tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                                           WIN_XSIZE, WIN_YSIZE,
-                                           WIN_SDL_DEPTH, 0, 0, 0, 0))
-      == NULL)
-    Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s\n", SDL_GetError());
-
-  if ((pix[PIX_DB_BACK] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s\n", SDL_GetError());
-
-  SDL_FreeSurface(sdl_image_tmp);
-
-  /* create double-buffer surface for door image */
-  if ((sdl_image_tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                                           3 * DXSIZE, DYSIZE + VYSIZE,
-                                           WIN_SDL_DEPTH, 0, 0, 0, 0))
-      == NULL)
-    Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s\n", SDL_GetError());
-
-  if ((pix[PIX_DB_DOOR] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s\n", SDL_GetError());
+  /* create additional image buffers for masking of graphics */
 
-  SDL_FreeSurface(sdl_image_tmp);
-
-  /* create double-buffer surface for field image */
-  if ((sdl_image_tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                                           FXSIZE, FYSIZE,
-                                           WIN_SDL_DEPTH, 0, 0, 0, 0))
-      == NULL)
-    Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s\n", SDL_GetError());
-
-  if ((pix[PIX_DB_FIELD] = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
-    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s\n", SDL_GetError());
-
-  SDL_FreeSurface(sdl_image_tmp);
+#ifdef USE_SDL_LIBRARY
 
   /* initialize surface array to 'NULL' */
   for(i=0; i<NUM_TILES; i++)
@@ -772,13 +822,13 @@ void InitGfx()
       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\n", SDL_GetError());
+       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\n", SDL_GetError());
+       Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
 
       SDL_FreeSurface(sdl_image_tmp);
 
@@ -788,25 +838,27 @@ void InitGfx()
 
 #else /* !USE_SDL_LIBRARY */
 
-  pix[PIX_DB_BACK] = XCreatePixmap(display, window,
-                                  WIN_XSIZE,WIN_YSIZE,
-                                  XDefaultDepth(display,screen));
-  pix[PIX_DB_DOOR] = XCreatePixmap(display, window,
-                                  3*DXSIZE,DYSIZE+VYSIZE,
-                                  XDefaultDepth(display,screen));
-  pix[PIX_DB_FIELD] = XCreatePixmap(display, window,
-                                   FXSIZE,FYSIZE,
-                                   XDefaultDepth(display,screen));
-
+  /* create graphic context structures needed for clipping */
   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, clipmask[PIX_BACK], 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, clip_gc_valuemask, &clip_gc_values);
+
+  for(i=0; i<NUM_BITMAPS; i++)
+  {
+    if (clipmask[i])
+    {
+      clip_gc_values.graphics_exposures = False;
+      clip_gc_values.clip_mask = clipmask[i];
+      clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
+      clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
+    }
+  }
 
   /* initialize pixmap array to Pixmap 'None' */
   for(i=0; i<NUM_TILES; i++)
@@ -833,33 +885,24 @@ void InitGfx()
     }
   }
 
-  if (!pix[PIX_DB_BACK] || !pix[PIX_DB_DOOR])
-    Error(ERR_EXIT, "cannot create additional pixmaps");
-
-  for(i=0; i<NUM_BITMAPS; i++)
-  {
-    if (clipmask[i])
-    {
-      clip_gc_values.graphics_exposures = False;
-      clip_gc_values.clip_mask = clipmask[i];
-      clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
-      clip_gc[i] = XCreateGC(display,window,clip_gc_valuemask,&clip_gc_values);
-    }
-  }
-
 #endif /* !USE_SDL_LIBRARY */
+}
+
+void InitGfxBackground()
+{
+  int x, y;
 
   drawto = backbuffer = pix[PIX_DB_BACK];
   fieldbuffer = pix[PIX_DB_FIELD];
   SetDrawtoField(DRAW_BACKBUFFER);
 
   BlitBitmap(pix[PIX_BACK], backbuffer, 0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
-  ClearRectangle(pix[PIX_DB_BACK], REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
+  ClearRectangle(backbuffer, REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE);
   ClearRectangle(pix[PIX_DB_DOOR], 0,0, 3*DXSIZE,DYSIZE+VYSIZE);
 
-  for(i=0; i<MAX_BUF_XSIZE; i++)
-    for(j=0; j<MAX_BUF_YSIZE; j++)
-      redraw[i][j] = 0;
+  for(x=0; x<MAX_BUF_XSIZE; x++)
+    for(y=0; y<MAX_BUF_YSIZE; y++)
+      redraw[x][y] = 0;
   redraw_tiles = 0;
   redraw_mask = REDRAW_ALL;
 }
@@ -906,17 +949,17 @@ void LoadGfx(int pos, struct PictureFileInfo *pic)
 #ifdef USE_SDL_LIBRARY
     /* load image to temporary surface */
     if ((sdl_image_tmp = IMG_Load(filename)) == NULL)
-      Error(ERR_EXIT, "IMG_Load() failed: %s\n", SDL_GetError());
+      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)
-      Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s\n", SDL_GetError());
+      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)
-      Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s\n", SDL_GetError());
+      Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
 
     /* free temporary surface */
     SDL_FreeSurface(sdl_image_tmp);
@@ -2185,7 +2228,7 @@ void InitElementProperties()
     for(j=0; j<*(ep2_num[i]); j++)
       Elementeigenschaften2[(ep2_array[i])[j]] |= ep2_bit[i];
 
-  for(i=EL_CHAR_START; i<EL_CHAR_END; i++)
+  for(i=EL_CHAR_START; i<=EL_CHAR_END; i++)
     Elementeigenschaften1[i] |= (EP_BIT_CHAR | EP_BIT_INACTIVE);
 }
 
@@ -2193,12 +2236,17 @@ void CloseAllAndExit(int exit_value)
 {
   int i;
 
+#ifdef USE_SDL_LIBRARY
+  StopSounds();
+  FreeSounds(NUM_SOUNDS);
+#else
   if (sound_process_id)
   {
     StopSounds();
     kill(sound_process_id, SIGTERM);
     FreeSounds(NUM_SOUNDS);
   }
+#endif
 
   for(i=0; i<NUM_BITMAPS; i++)
   {
@@ -2243,7 +2291,7 @@ void CloseAllAndExit(int exit_value)
   }
 #endif
 
-#ifdef MSDOS
+#if defined(MSDOS) || defined(WIN32)
   dumpErrorFile();
 #endif