Added support for translucent icons on Windows
authorSam Lantinga <slouken@libsdl.org>
Sat, 03 Jan 2009 01:01:53 +0000
changeset 2970 94b634c56455
parent 2969 1ee69e7e7cea
child 2971 a3012c6652ff
Added support for translucent icons on Windows
src/video/win32/SDL_win32video.c
src/video/win32/SDL_win32window.c
src/video/win32/SDL_win32window.h
--- a/src/video/win32/SDL_win32video.c	Sat Jan 03 01:00:38 2009 +0000
+++ b/src/video/win32/SDL_win32video.c	Sat Jan 03 01:01:53 2009 +0000
@@ -146,6 +146,7 @@
     device->CreateWindow = WIN_CreateWindow;
     device->CreateWindowFrom = WIN_CreateWindowFrom;
     device->SetWindowTitle = WIN_SetWindowTitle;
+    device->SetWindowIcon = WIN_SetWindowIcon;
     device->SetWindowPosition = WIN_SetWindowPosition;
     device->SetWindowSize = WIN_SetWindowSize;
     device->ShowWindow = WIN_ShowWindow;
--- 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;
--- a/src/video/win32/SDL_win32window.h	Sat Jan 03 01:00:38 2009 +0000
+++ b/src/video/win32/SDL_win32window.h	Sat Jan 03 01:01:53 2009 +0000
@@ -38,6 +38,7 @@
 extern int WIN_CreateWindow(_THIS, SDL_Window * window);
 extern int WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data);
 extern void WIN_SetWindowTitle(_THIS, SDL_Window * window);
+extern void WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon);
 extern void WIN_SetWindowPosition(_THIS, SDL_Window * window);
 extern void WIN_SetWindowSize(_THIS, SDL_Window * window);
 extern void WIN_ShowWindow(_THIS, SDL_Window * window);