rnd-20000917-1-src
[rocksndiamonds.git] / src / init.c
index 64f748019cac0e9050ef5048b0939bb616c9a7c1..c591c0db5634dd3f660c7c404c2e9aac58cbc7a2 100644 (file)
@@ -41,9 +41,12 @@ struct IconFileInfo
   char *picturemask_filename;
 };
 
+#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);
 static void InitDisplay(void);
 static void InitSound(void);
@@ -56,18 +59,24 @@ 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();
@@ -88,7 +97,7 @@ void OpenAll(int argc, char *argv[])
   InitGfx();
   InitElementProperties();     /* initializes IS_CHAR() for el2gfx() */
 
-  InitLevelAndPlayerInfo();
+  InitLevelInfo();
   InitGadgets();               /* needs to know number of level series */
 
   DrawMainMenu();
@@ -96,7 +105,7 @@ void OpenAll(int argc, char *argv[])
   InitNetworkServer();
 }
 
-void InitLevelAndPlayerInfo()
+void InitPlayerInfo()
 {
   int i;
 
@@ -111,22 +120,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 +165,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 +185,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");
@@ -198,14 +211,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 +228,7 @@ void InitSound()
     if (!LoadSound(&Sound[i]))
     {
       sound_status = SOUND_OFF;
+      sound_loops_allowed = FALSE;
       return;
     }
   }
@@ -230,11 +244,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 +272,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 +294,7 @@ void InitJoysticks()
 
   joystick_status = JOYSTICK_OFF;
 
-#ifdef USE_SDL_LIBRARY
+#ifdef USE_SDL_JOYSTICK
 
   if (!sdl_joystick_subsystem_initialized)
   {
@@ -288,7 +302,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 +334,7 @@ void InitJoysticks()
     joystick_status = JOYSTICK_AVAILABLE;
   }
 
-#else /* !USE_SDL_LIBRARY */
+#else /* !USE_SDL_JOYSTICK */
 
 #ifndef MSDOS
   for (i=0; i<MAX_PLAYERS; i++)
@@ -384,7 +398,7 @@ 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 +454,15 @@ void InitWindow(int argc, char *argv[])
 {
 #ifdef USE_SDL_LIBRARY
   /* open SDL video output device (window or fullscreen mode) */
+#if 0
   if ((window = 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);
@@ -694,11 +713,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);
@@ -719,10 +738,10 @@ void InitGfx()
                                            WIN_XSIZE, WIN_YSIZE,
                                            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());
 
   if ((pix[PIX_DB_BACK] = 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);
 
@@ -731,10 +750,10 @@ void InitGfx()
                                            3 * DXSIZE, DYSIZE + VYSIZE,
                                            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());
 
   if ((pix[PIX_DB_DOOR] = 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);
 
@@ -743,10 +762,10 @@ void InitGfx()
                                            FXSIZE, FYSIZE,
                                            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());
 
   if ((pix[PIX_DB_FIELD] = 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);
 
@@ -772,13 +791,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);
 
@@ -906,17 +925,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);
@@ -2193,12 +2212,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 +2267,7 @@ void CloseAllAndExit(int exit_value)
   }
 #endif
 
-#ifdef MSDOS
+#if defined(MSDOS) || defined(WIN32)
   dumpErrorFile();
 #endif