rnd-20001205-3-src
[rocksndiamonds.git] / src / libgame / sdl.c
index 97f8503f1b5d82bc3fe04efac970d5509f187222..7c38aa940d26fa1c45f5fc9639a9ca1fd7b17306 100644 (file)
@@ -29,12 +29,9 @@ inline void SDLInitVideoDisplay(void)
 
   /* set default SDL depth */
   video.default_depth = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
-
-  /* set exit function to automatically cleanup SDL stuff after exit() */
-  atexit(SDL_Quit);
 }
 
-inline void SDLInitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window,
+inline void SDLInitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
                               boolean fullscreen)
 {
   /* open SDL video output device (window or fullscreen mode) */
@@ -61,7 +58,7 @@ inline void SDLInitVideoBuffer(DrawBuffer *backbuffer, DrawWindow *window,
   *window = CreateBitmap(video.width, video.height, video.depth);
 }
 
-inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, boolean fullscreen)
+inline boolean SDLSetVideoMode(DrawBuffer **backbuffer, boolean fullscreen)
 {
   boolean success = TRUE;
   int surface_flags = SDL_HWSURFACE | (fullscreen ? SDL_FULLSCREEN : 0);
@@ -69,8 +66,8 @@ inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, boolean fullscreen)
   if (fullscreen && !video.fullscreen_enabled && video.fullscreen_available)
   {
     /* switch display to fullscreen mode, if available */
-    DrawWindow window_old = *backbuffer;
-    DrawWindow window_new = CreateBitmapStruct();
+    DrawWindow *window_old = *backbuffer;
+    DrawWindow *window_new = CreateBitmapStruct();
 
     if ((window_new->surface = SDL_SetVideoMode(video.width, video.height,
                                                video.depth, surface_flags))
@@ -97,8 +94,8 @@ inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, boolean fullscreen)
   if ((!fullscreen && video.fullscreen_enabled) || !*backbuffer)
   {
     /* switch display to window mode */
-    DrawWindow window_old = *backbuffer;
-    DrawWindow window_new = CreateBitmapStruct();
+    DrawWindow *window_old = *backbuffer;
+    DrawWindow *window_new = CreateBitmapStruct();
 
     if ((window_new->surface = SDL_SetVideoMode(video.width, video.height,
                                                video.depth, surface_flags))
@@ -123,12 +120,12 @@ inline boolean SDLSetVideoMode(DrawBuffer *backbuffer, boolean fullscreen)
   return success;
 }
 
-inline void SDLCopyArea(Bitmap src_bitmap, Bitmap dst_bitmap,
+inline void SDLCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
                        int src_x, int src_y,
                        int width, int height,
                        int dst_x, int dst_y, int copy_mode)
 {
-  Bitmap real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+  Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
   SDL_Rect src_rect, dst_rect;
 
   src_rect.x = src_x;
@@ -150,10 +147,10 @@ inline void SDLCopyArea(Bitmap src_bitmap, Bitmap dst_bitmap,
     SDL_UpdateRect(backbuffer->surface, dst_x, dst_y, width, height);
 }
 
-inline void SDLFillRectangle(Bitmap dst_bitmap, int x, int y,
+inline void SDLFillRectangle(Bitmap *dst_bitmap, int x, int y,
                             int width, int height, unsigned int color)
 {
-  Bitmap real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
+  Bitmap *real_dst_bitmap = (dst_bitmap == window ? backbuffer : dst_bitmap);
   SDL_Rect rect;
   unsigned int color_r = (color >> 16) && 0xff;
   unsigned int color_g = (color >>  8) && 0xff;
@@ -678,6 +675,31 @@ void sge_LineRGB(SDL_Surface *Surface, Sint16 x1, Sint16 y1, Sint16 x2,
   sge_Line(Surface, x1, y1, x2, y2, SDL_MapRGB(Surface->format, R, G, B));
 }
 
+Bitmap *SDLLoadImage(char *filename)
+{
+  Bitmap *new_bitmap = CreateBitmapStruct();
+  SDL_Surface *sdl_image_tmp;
+
+  /* load image to temporary surface */
+  if ((sdl_image_tmp = IMG_Load(filename)) == NULL)
+    Error(ERR_EXIT, "IMG_Load() failed: %s", SDL_GetError());
+
+  /* create native non-transparent surface for current image */
+  if ((new_bitmap->surface = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
+    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 ((new_bitmap->surface_masked = SDL_DisplayFormat(sdl_image_tmp)) == NULL)
+    Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
+
+  /* free temporary surface */
+  SDL_FreeSurface(sdl_image_tmp);
+
+  return new_bitmap;
+}
+
 
 /* ========================================================================= */
 /* audio functions                                                           */