fixed aborting program due to '-psn' parameter when launched via GUI on Mac
[rocksndiamonds.git] / src / libgame / sdl.c
index e7e985052cb7e9f7b8ebd88f4863620e1b939514..a9fff4eaccc04c495129a84054c91cae7765e7ea 100644 (file)
@@ -60,6 +60,23 @@ static void UpdateScreen(SDL_Rect *rect)
 
   LimitScreenUpdates(FALSE);
 
+#if 0
+  {
+    static int LastFrameCounter = 0;
+    boolean changed = (FrameCounter != LastFrameCounter);
+
+    printf("::: FrameCounter == %d [%s]\n", FrameCounter,
+          (changed ? "-" : "SAME FRAME UPDATED"));
+
+    LastFrameCounter = FrameCounter;
+
+    /*
+    if (FrameCounter % 2)
+      return;
+    */
+  }
+#endif
+
 #if defined(TARGET_SDL2)
 #if USE_RENDERER
   SDL_Surface *screen = backbuffer->surface;
@@ -198,6 +215,9 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
 
   new_surface = SDL_ConvertSurface(*surface, backbuffer->surface->format, 0);
 
+  if (new_surface == NULL)
+    Error(ERR_EXIT, "SDL_ConvertSurface() failed: %s", SDL_GetError());
+
   SDL_FreeSurface(*surface);
 
   *surface = new_surface;
@@ -207,24 +227,20 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
 
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
 {
+  SDL_Surface *new_surface;
+
   if (surface == NULL)
     return NULL;
 
-  if (backbuffer == NULL ||
-      backbuffer->surface == NULL)
-    return SDL_ConvertSurface(surface, surface->format, 0);
-
-  return SDL_ConvertSurface(surface, backbuffer->surface->format, 0);
-}
+  if (backbuffer && backbuffer->surface)
+    new_surface = SDL_ConvertSurface(surface, backbuffer->surface->format, 0);
+  else
+    new_surface = SDL_ConvertSurface(surface, surface->format, 0);
 
-SDL_Surface *SDL_DisplayFormat(SDL_Surface *surface)
-{
-  if (surface == NULL ||
-      backbuffer == NULL ||
-      backbuffer->surface == NULL)
-    return NULL;
+  if (new_surface == NULL)
+    Error(ERR_EXIT, "SDL_ConvertSurface() failed: %s", SDL_GetError());
 
-  return SDL_ConvertSurface(surface, backbuffer->surface->format, 0);
+  return new_surface;
 }
 
 #else
@@ -233,7 +249,9 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
 {
   SDL_Surface *new_surface;
 
-  if (surface == NULL)
+  if (surface == NULL ||
+      *surface == NULL ||
+      !video.initialized)
     return FALSE;
 
   new_surface = SDL_DisplayFormat(*surface);
@@ -250,10 +268,17 @@ boolean SDLSetNativeSurface(SDL_Surface **surface)
 
 SDL_Surface *SDLGetNativeSurface(SDL_Surface *surface)
 {
-  SDL_Surface *new_surface = SDL_DisplayFormat(surface);
+  SDL_Surface *new_surface;
+
+  if (video.initialized)
+    new_surface = SDL_DisplayFormat(surface);
+  else
+    new_surface = SDL_ConvertSurface(surface, surface->format, SURFACE_FLAGS);
 
   if (new_surface == NULL)
-    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
+    Error(ERR_EXIT, "%s() failed: %s",
+         (video.initialized ? "SDL_DisplayFormat" : "SDL_ConvertSurface"),
+         SDL_GetError());
 
   return new_surface;
 }
@@ -446,7 +471,7 @@ void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
 
   /* !!! SDL2 can only set the window icon if the window already exists !!! */
   /* set window icon */
-  SDLSetWindowIcon(program.sdl_icon_filename);
+  SDLSetWindowIcon(program.icon_filename);
 
   /* set window and icon title */
 #if defined(TARGET_SDL2)
@@ -708,9 +733,12 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
   SDLRedrawWindow();                   // map window
 #endif
 
+#ifdef DEBUG
+#if defined(PLATFORM_WIN32)
+  // experimental drag and drop code
+
   SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
 
-#if defined(PLATFORM_WIN32)
   {
     SDL_SysWMinfo wminfo;
     HWND hwnd;
@@ -735,6 +763,7 @@ boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
       DragAcceptFiles(hwnd, TRUE);
     }
   }
+#endif
 #endif
 
   return success;
@@ -2365,7 +2394,10 @@ void SDLNextEvent(Event *event)
 
 void SDLHandleWindowManagerEvent(Event *event)
 {
+#ifdef DEBUG
 #if defined(PLATFORM_WIN32)
+  // experimental drag and drop code
+
   SDL_SysWMEvent *syswmevent = (SDL_SysWMEvent *)event;
   SDL_SysWMmsg *syswmmsg = (SDL_SysWMmsg *)(syswmevent->msg);
 
@@ -2403,6 +2435,7 @@ void SDLHandleWindowManagerEvent(Event *event)
 #endif
   }
 #endif
+#endif
 }