diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c b/contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c new file mode 100644 index 0000000..7f9245c --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/windows/SDL_surface_utils.c | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #include "SDL_surface_utils.h" | ||
| 24 | |||
| 25 | #include "../SDL_surface_c.h" | ||
| 26 | |||
| 27 | #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) | ||
| 28 | HICON CreateIconFromSurface(SDL_Surface *surface) | ||
| 29 | { | ||
| 30 | SDL_Surface *s = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888); | ||
| 31 | if (!s) { | ||
| 32 | return NULL; | ||
| 33 | } | ||
| 34 | |||
| 35 | /* The dimensions will be needed after s is freed */ | ||
| 36 | const int width = s->w; | ||
| 37 | const int height = s->h; | ||
| 38 | |||
| 39 | BITMAPINFO bmpInfo; | ||
| 40 | SDL_zero(bmpInfo); | ||
| 41 | bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | ||
| 42 | bmpInfo.bmiHeader.biWidth = width; | ||
| 43 | bmpInfo.bmiHeader.biHeight = -height; /* Top-down bitmap */ | ||
| 44 | bmpInfo.bmiHeader.biPlanes = 1; | ||
| 45 | bmpInfo.bmiHeader.biBitCount = 32; | ||
| 46 | bmpInfo.bmiHeader.biCompression = BI_RGB; | ||
| 47 | |||
| 48 | HDC hdc = GetDC(NULL); | ||
| 49 | void* pBits = NULL; | ||
| 50 | HBITMAP hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, NULL, 0); | ||
| 51 | if (!hBitmap) { | ||
| 52 | ReleaseDC(NULL, hdc); | ||
| 53 | SDL_DestroySurface(s); | ||
| 54 | return NULL; | ||
| 55 | } | ||
| 56 | |||
| 57 | SDL_memcpy(pBits, s->pixels, width * height * 4); | ||
| 58 | |||
| 59 | SDL_DestroySurface(s); | ||
| 60 | |||
| 61 | HBITMAP hMask = CreateBitmap(width, height, 1, 1, NULL); | ||
| 62 | if (!hMask) { | ||
| 63 | DeleteObject(hBitmap); | ||
| 64 | ReleaseDC(NULL, hdc); | ||
| 65 | return NULL; | ||
| 66 | } | ||
| 67 | |||
| 68 | HDC hdcMem = CreateCompatibleDC(hdc); | ||
| 69 | HGDIOBJ oldBitmap = SelectObject(hdcMem, hMask); | ||
| 70 | |||
| 71 | for (int y = 0; y < height; y++) { | ||
| 72 | for (int x = 0; x < width; x++) { | ||
| 73 | BYTE* pixel = (BYTE*)pBits + (y * width + x) * 4; | ||
| 74 | BYTE alpha = pixel[3]; | ||
| 75 | COLORREF maskColor = (alpha == 0) ? RGB(0, 0, 0) : RGB(255, 255, 255); | ||
| 76 | SetPixel(hdcMem, x, y, maskColor); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | ICONINFO iconInfo; | ||
| 81 | iconInfo.fIcon = TRUE; | ||
| 82 | iconInfo.xHotspot = 0; | ||
| 83 | iconInfo.yHotspot = 0; | ||
| 84 | iconInfo.hbmMask = hMask; | ||
| 85 | iconInfo.hbmColor = hBitmap; | ||
| 86 | |||
| 87 | HICON hIcon = CreateIconIndirect(&iconInfo); | ||
| 88 | |||
| 89 | SelectObject(hdcMem, oldBitmap); | ||
| 90 | DeleteDC(hdcMem); | ||
| 91 | DeleteObject(hBitmap); | ||
| 92 | DeleteObject(hMask); | ||
| 93 | ReleaseDC(NULL, hdc); | ||
| 94 | |||
| 95 | return hIcon; | ||
| 96 | } | ||
| 97 | #endif | ||
