rnd-20060730-2-src
[rocksndiamonds.git] / src / libgame / sdl.c
index 14dac92953cf915af882920958446274e3c54e53..406c022638499282e91080006e5e328b7150e11c 100644 (file)
@@ -275,11 +275,44 @@ void SDLFadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
   static SDL_Surface *surface_black = NULL;
   SDL_Surface *surface_screen = backbuffer->surface;
   SDL_Surface *surface_cross;          /* initialized later */
-  boolean fade_reverse;                        /* initialized later */
+  SDL_Rect src_rect, dst_rect;
+  int src_x = 0, src_y = 0;
+  int dst_x = 0, dst_y = 0;
+  boolean fade_reverse = (fade_mode == FADE_MODE_FADE_IN ? TRUE : FALSE);
   unsigned int time_last, time_current;
   float alpha;
   int alpha_final;
 
+  src_rect.x = src_x;
+  src_rect.y = src_y;
+  src_rect.w = video.width;
+  src_rect.h = video.height;
+
+#ifdef FULLSCREEN_BUG
+  dst_x += video_xoffset;
+  dst_y += video_yoffset;
+#endif
+
+  dst_rect.x = dst_x;
+  dst_rect.y = dst_y;
+  dst_rect.w = video.width;
+  dst_rect.h = video.height;
+
+#if 0
+  if (!initialization_needed)
+  {
+    /* check if screen size has changed (can happen when toggling fullscreen) */
+    if (surface_screen_copy->w != surface_screen->w ||
+       surface_screen_copy->h != surface_screen->h)
+    {
+      SDL_FreeSurface(surface_screen_copy);
+      SDL_FreeSurface(surface_black);
+
+      initialization_needed = TRUE;
+    }
+  }
+#endif
+
   if (initialization_needed)
   {
     unsigned int flags = SDL_SRCALPHA;
@@ -293,8 +326,13 @@ void SDLFadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
     /* create surface for temporary copy of screen buffer */
     if ((surface_screen_copy =
         SDL_CreateRGBSurface(flags,
+#if 1
+                             video.width,
+                             video.height,
+#else
                              surface_screen->w,
                              surface_screen->h,
+#endif
                              surface_screen->format->BitsPerPixel,
                              surface_screen->format->Rmask,
                              surface_screen->format->Gmask,
@@ -305,8 +343,13 @@ void SDLFadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
     /* create black surface for fading from/to black */
     if ((surface_black =
         SDL_CreateRGBSurface(flags,
+#if 1
+                             video.width,
+                             video.height,
+#else
                              surface_screen->w,
                              surface_screen->h,
+#endif
                              surface_screen->format->BitsPerPixel,
                              surface_screen->format->Rmask,
                              surface_screen->format->Gmask,
@@ -322,9 +365,8 @@ void SDLFadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
   }
 
   /* copy the current screen backbuffer to the temporary screen copy buffer */
-  SDL_BlitSurface(surface_screen, NULL, surface_screen_copy, NULL);
+  SDL_BlitSurface(surface_screen, &dst_rect, surface_screen_copy, &src_rect);
 
-  fade_reverse = (fade_mode == FADE_MODE_FADE_IN ? TRUE : FALSE);
   surface_cross = (fade_mode == FADE_MODE_CROSSFADE ? bitmap_cross->surface :
                   surface_black);
 
@@ -339,11 +381,11 @@ void SDLFadeScreen(Bitmap *bitmap_cross, int fade_mode, int fade_delay,
     alpha_final = MIN(MAX(0, alpha_final), 255);
 
     /* draw existing image to screen buffer */
-    SDL_BlitSurface(surface_screen_copy, NULL, surface_screen, NULL);
+    SDL_BlitSurface(surface_screen_copy, &src_rect, surface_screen, &dst_rect);
 
     /* draw new image to screen buffer using alpha blending */
     SDL_SetAlpha(surface_cross, SDL_SRCALPHA, alpha_final);
-    SDL_BlitSurface(surface_cross, NULL, surface_screen, NULL);
+    SDL_BlitSurface(surface_cross, &src_rect, surface_screen, &dst_rect);
 
     /* draw screen buffer to visible display */
     SDL_Flip(surface_screen);
@@ -1004,12 +1046,69 @@ typedef struct
   Uint8 a;
 } tColorRGBA;
 
+int zoomSurfaceRGBA_scaleDownBy2(SDL_Surface *src, SDL_Surface *dst)
+{
+  int x, y;
+  tColorRGBA *sp, *csp, *dp;
+  int sgap, dgap;
+
+  /* pointer setup */
+  sp = csp = (tColorRGBA *) src->pixels;
+  dp = (tColorRGBA *) dst->pixels;
+  sgap = src->pitch - src->w * 4;
+  dgap = dst->pitch - dst->w * 4;
+
+  for (y = 0; y < dst->h; y++)
+  {
+    sp = csp;
+
+    for (x = 0; x < dst->w; x++)
+    {
+      tColorRGBA *sp0 = sp;
+      tColorRGBA *sp1 = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
+      tColorRGBA *sp00 = &sp0[0];
+      tColorRGBA *sp01 = &sp0[1];
+      tColorRGBA *sp10 = &sp1[0];
+      tColorRGBA *sp11 = &sp1[1];
+      tColorRGBA new;
+
+      /* create new color pixel from all four source color pixels */
+      new.r = (sp00->r + sp01->r + sp10->r + sp11->r) / 4;
+      new.g = (sp00->g + sp01->g + sp10->g + sp11->g) / 4;
+      new.b = (sp00->b + sp01->b + sp10->b + sp11->b) / 4;
+      new.a = (sp00->a + sp01->a + sp10->a + sp11->a) / 4;
+
+      /* draw */
+      *dp = new;
+
+      /* advance source pointers */
+      sp += 2;
+
+      /* advance destination pointer */
+      dp++;
+    }
+
+    /* advance source pointer */
+    csp = (tColorRGBA *) ((Uint8 *) csp + 2 * src->pitch);
+
+    /* advance destination pointers */
+    dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
+  }
+
+  return 0;
+}
+
 int zoomSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst)
 {
   int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy;
   tColorRGBA *sp, *csp, *dp;
   int sgap, dgap;
 
+  /* use specialized zoom function when scaling down to exactly half size */
+  if (src->w == 2 * dst->w &&
+      src->h == 2 * dst->h)
+    return zoomSurfaceRGBA_scaleDownBy2(src, dst);
+
   /* variable setup */
   sx = (int) (65536.0 * (float) src->w / (float) dst->w);
   sy = (int) (65536.0 * (float) src->h / (float) dst->h);