From 09f817a824e9f33911d986900de24cfe2f92f782 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 28 Feb 2011 21:58:37 -0800 Subject: [PATCH] You can use SDL_ConvertSurfaceFormat() now Also, icon is guaranteed not to be NULL going into this function. --- src/video/windows/SDL_windowswindow.c | 84 +++++++++++++-------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c index 44b223d3a..0076fd56f 100755 --- a/src/video/windows/SDL_windowswindow.c +++ b/src/video/windows/SDL_windowswindow.c @@ -286,57 +286,51 @@ 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; + BYTE *icon_bmp; + int icon_len; + SDL_RWops *dst; + 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 */ + surface = SDL_ConvertSurfaceFormat(icon, SDL_PIXELFORMAT_ARGB8888, 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); } - - /* 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, SDL_PIXELFORMAT_ARGB8888); - 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); + SDL_FreeSurface(surface); /* TODO: create the icon in WinCE (CreateIconFromResource isn't available) */ #ifndef _WIN32_WCE - hicon = - CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000); + hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000); #endif - } - SDL_RWclose(dst); - SDL_stack_free(icon_bmp); } + SDL_RWclose(dst); + SDL_stack_free(icon_bmp); /* Set the icon for the window */ SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);