rnd-20001125-1-src
[rocksndiamonds.git] / src / sdl.c
index f3846da7491d6862b55416414cab925328575252..85a2760be1e96624810a4a8d9bef95f1b3cfc66b 100644 (file)
--- a/src/sdl.c
+++ b/src/sdl.c
 #include "main.h"
 #include "misc.h"
 
+inline void SDLInitBufferedDisplay(DrawBuffer *backbuffer, DrawWindow *window)
+{
+  /* initialize SDL video */
+  if (SDL_Init(SDL_INIT_VIDEO) < 0)
+    Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
+
+  /* automatically cleanup SDL stuff after exit() */
+  atexit(SDL_Quit);
+
+  /* open SDL video output device (window or fullscreen mode) */
+  if (!SDLSetVideoMode(backbuffer, window))
+    Error(ERR_EXIT, "setting video mode failed");
+
+  /* set window and icon title */
+  SDL_WM_SetCaption(WINDOW_TITLE_STRING, WINDOW_TITLE_STRING);
+
+  /* create additional buffer for double-buffering */
+
+  pix[PIX_DB_BACK] = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
+
+  /* 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 */
+}
+
+inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, DrawWindow *window)
+{
+  boolean success = TRUE;
+
+  if (setup.fullscreen && !fullscreen_enabled && fullscreen_available)
+  {
+    /* switch display to fullscreen mode, if available */
+    DrawWindow window_old = *backbuffer;
+    DrawWindow window_new;
+
+    if ((window_new = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
+                                      SDL_HWSURFACE|SDL_FULLSCREEN))
+       == NULL)
+    {
+      /* switching display to fullscreen mode failed */
+      Error(ERR_WARN, "SDL_SetVideoMode() failed: %s", SDL_GetError());
+
+      /* do not try it again */
+      fullscreen_available = FALSE;
+      success = FALSE;
+    }
+    else
+    {
+      if (window_old)
+       SDL_FreeSurface(window_old);
+      *backbuffer = window_new;
+
+      fullscreen_enabled = TRUE;
+      success = TRUE;
+    }
+  }
+
+  if ((!setup.fullscreen && fullscreen_enabled) || !*backbuffer)
+  {
+    /* switch display to window mode */
+    DrawWindow window_old = *backbuffer;
+    DrawWindow window_new;
+
+    if ((window_new = SDL_SetVideoMode(WIN_XSIZE, WIN_YSIZE, WIN_SDL_DEPTH,
+                                      SDL_HWSURFACE))
+       == NULL)
+    {
+      /* switching display to window mode failed -- should not happen */
+      Error(ERR_WARN, "SDL_SetVideoMode() failed: %s", SDL_GetError());
+
+      success = FALSE;
+    }
+    else
+    {
+      if (window_old)
+       SDL_FreeSurface(window_old);
+      *backbuffer = window_new;
+
+      fullscreen_enabled = FALSE;
+      success = TRUE;
+    }
+  }
+
+  return success;
+}
+
 inline void SDLCopyArea(SDL_Surface *src_surface, SDL_Surface *dst_surface,
                        int src_x, int src_y,
                        int width, int height,