diff src/video/win32/SDL_win32window.c @ 2970:94b634c56455

Added support for translucent icons on Windows
author Sam Lantinga <slouken@libsdl.org>
date Sat, 03 Jan 2009 01:01:53 +0000
parents efe4d0ce2e97
children a3012c6652ff
line wrap: on
line diff
--- a/src/video/win32/SDL_win32window.c	Sat Jan 03 01:00:38 2009 +0000
+++ b/src/video/win32/SDL_win32window.c	Sat Jan 03 01:01:53 2009 +0000
@@ -30,6 +30,7 @@
 #include "SDL_config.h"
 
 #include "../SDL_sysvideo.h"
+#include "../SDL_pixels_c.h"
 #include "../../events/SDL_keyboard_c.h"
 
 #include "SDL_win32video.h"
@@ -312,6 +313,64 @@
 }
 
 void
+WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
+{
+    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
+    HICON hicon = NULL;
+
+    if(icon) {
+        BYTE *icon_bmp;
+        int icon_len;
+        SDL_RWops *dst;
+        SDL_PixelFormat format;
+        SDL_Surface *surface;
+
+        /* Create temporary bitmap buffer */
+        icon_len = 40 + icon->h * icon->w * 4;
+        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
+        dst = SDL_RWFromMem(icon_bmp, icon_len);
+        if (!dst) {
+            SDL_stack_free(icon_bmp);
+            return;
+        }
+
+        /* Write the BITMAPINFO header */
+        SDL_WriteLE32(dst, 40);
+        SDL_WriteLE32(dst, icon->w);
+        SDL_WriteLE32(dst, icon->h*2);
+        SDL_WriteLE16(dst, 1);
+        SDL_WriteLE16(dst, 32);
+        SDL_WriteLE32(dst, BI_RGB);
+        SDL_WriteLE32(dst, icon->h * icon->w * 4);
+        SDL_WriteLE32(dst, 0);
+        SDL_WriteLE32(dst, 0);
+        SDL_WriteLE32(dst, 0);
+        SDL_WriteLE32(dst, 0);
+
+        /* Convert the icon to a 32-bit surface with alpha channel */
+        SDL_InitFormat(&format, 32,
+                       0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
+        surface = SDL_ConvertSurface(icon, &format, 0);
+        if (surface) {
+            /* Write the pixels upside down into the bitmap buffer */
+            int y = surface->h;
+            while (y--) {
+                Uint8 *src = (Uint8 *)surface->pixels + y * surface->pitch;
+                SDL_RWwrite(dst, src, surface->pitch, 1);
+            }
+            SDL_FreeSurface(surface);
+
+            hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
+        }
+        SDL_RWclose(dst);
+        SDL_stack_free(icon_bmp);
+    }
+
+    /* Set the icon */
+    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
+}
+
+void
 WIN_SetWindowPosition(_THIS, SDL_Window * window)
 {
     HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;