do not use RWOps in WIN_SetWindowIcon

This commit is contained in:
pionere 2022-01-09 15:08:21 +01:00 committed by Ryan C. Gordon
parent 739f350f45
commit 942b01a54c
2 changed files with 22 additions and 21 deletions

View File

@ -76,6 +76,7 @@ assert can have unique static variables associated with it.
#endif #endif
#define SDL_FILE __FILE__ #define SDL_FILE __FILE__
#define SDL_LINE __LINE__ #define SDL_LINE __LINE__
#define SDL_STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1];
/* /*
sizeof (x) makes the compiler still parse the expression even without sizeof (x) makes the compiler still parse the expression even without

View File

@ -442,39 +442,40 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd; HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
HICON hicon = NULL; HICON hicon = NULL;
BYTE *icon_bmp; BYTE *icon_bmp;
int icon_len, mask_len, y; int icon_len, mask_len, row_len, y;
SDL_RWops *dst; BITMAPINFOHEADER *bmi;
Uint8 *dst;
SDL_bool isstack; SDL_bool isstack;
/* Create temporary buffer for ICONIMAGE structure */ /* Create temporary buffer for ICONIMAGE structure */
SDL_STATIC_ASSERT(sizeof(BITMAPINFOHEADER) == 40, WIN_SetWindowIcon_uses_BITMAPINFOHEADER_to_prepare_an_ICONIMAGE);
mask_len = (icon->h * (icon->w + 7)/8); mask_len = (icon->h * (icon->w + 7)/8);
icon_len = 40 + icon->h * icon->w * sizeof(Uint32) + mask_len; icon_len = sizeof(BITMAPINFOHEADER) + icon->h * icon->w * sizeof(Uint32) + mask_len;
icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack); icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
dst = SDL_RWFromMem(icon_bmp, icon_len);
if (!dst) {
SDL_small_free(icon_bmp, isstack);
return;
}
/* Write the BITMAPINFO header */ /* Write the BITMAPINFO header */
SDL_WriteLE32(dst, 40); bmi = (BITMAPINFOHEADER *)icon_bmp;
SDL_WriteLE32(dst, icon->w); bmi->biSize = SDL_SwapLE32(sizeof(BITMAPINFOHEADER));
SDL_WriteLE32(dst, icon->h * 2); bmi->biWidth = SDL_SwapLE32(icon->w);
SDL_WriteLE16(dst, 1); bmi->biHeight = SDL_SwapLE32(icon->h * 2);
SDL_WriteLE16(dst, 32); bmi->biPlanes = SDL_SwapLE16(1);
SDL_WriteLE32(dst, BI_RGB); bmi->biBitCount = SDL_SwapLE16(32);
SDL_WriteLE32(dst, icon->h * icon->w * sizeof(Uint32)); bmi->biCompression = SDL_SwapLE32(BI_RGB);
SDL_WriteLE32(dst, 0); bmi->biSizeImage = SDL_SwapLE32(icon->h * icon->w * sizeof(Uint32));
SDL_WriteLE32(dst, 0); bmi->biXPelsPerMeter = SDL_SwapLE32(0);
SDL_WriteLE32(dst, 0); bmi->biYPelsPerMeter = SDL_SwapLE32(0);
SDL_WriteLE32(dst, 0); bmi->biClrUsed = SDL_SwapLE32(0);
bmi->biClrImportant = SDL_SwapLE32(0);
/* Write the pixels upside down into the bitmap buffer */ /* Write the pixels upside down into the bitmap buffer */
SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888); SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
dst = &icon_bmp[sizeof(BITMAPINFOHEADER)];
row_len = icon->w * sizeof(Uint32);
y = icon->h; y = icon->h;
while (y--) { while (y--) {
Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch; Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
SDL_RWwrite(dst, src, icon->w * sizeof(Uint32), 1); SDL_memcpy(dst, src, row_len);
dst += row_len;
} }
/* Write the mask */ /* Write the mask */
@ -482,7 +483,6 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000); hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
SDL_RWclose(dst);
SDL_small_free(icon_bmp, isstack); SDL_small_free(icon_bmp, isstack);
/* Set the icon for the window */ /* Set the icon for the window */