Atlas - SDL_windowsframebuffer.c
Home / ext / SDL / src / video / windows Lines: 1 | Size: 4737 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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#if defined(SDL_VIDEO_DRIVER_WINDOWS) && !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) 24 25#include "SDL_windowsvideo.h" 26 27bool WIN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch) 28{ 29 SDL_WindowData *data = window->internal; 30 bool isstack; 31 size_t size; 32 LPBITMAPINFO info; 33 HBITMAP hbm; 34 int w, h; 35 36 SDL_GetWindowSizeInPixels(window, &w, &h); 37 38 // Free the old framebuffer surface 39 if (data->mdc) { 40 DeleteDC(data->mdc); 41 } 42 if (data->hbm) { 43 DeleteObject(data->hbm); 44 } 45 46 // Find out the format of the screen 47 size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD); 48 info = (LPBITMAPINFO)SDL_small_alloc(Uint8, size, &isstack); 49 if (!info) { 50 return false; 51 } 52 53 SDL_memset(info, 0, size); 54 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 55 56 // The second call to GetDIBits() fills in the bitfields 57 hbm = CreateCompatibleBitmap(data->hdc, 1, 1); 58 GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS); 59 GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS); 60 DeleteObject(hbm); 61 62 // Check if a transparent channel is required 63 bool need_alpha = (window->flags & SDL_WINDOW_TRANSPARENT) != 0; 64 65 *format = SDL_PIXELFORMAT_UNKNOWN; 66 if (info->bmiHeader.biCompression == BI_BITFIELDS) { 67 int bpp; 68 Uint32 *masks; 69 70 bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount; 71 masks = (Uint32 *)((Uint8 *)info + info->bmiHeader.biSize); 72 *format = SDL_GetPixelFormatForMasks(bpp, masks[0], masks[1], masks[2], 0); 73 } 74 if (*format == SDL_PIXELFORMAT_UNKNOWN || need_alpha) { 75 // We'll use RGB or BGRA32 format for now 76 *format = need_alpha ? SDL_PIXELFORMAT_BGRA32 : SDL_PIXELFORMAT_XRGB8888; 77 78 // Create a new one 79 SDL_memset(info, 0, size); 80 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 81 info->bmiHeader.biPlanes = 1; 82 info->bmiHeader.biBitCount = 32; 83 info->bmiHeader.biCompression = need_alpha ? BI_BITFIELDS : BI_RGB; 84 85 if (need_alpha) { 86 int tmpbpp; 87 Uint32 *bgr32masks = (Uint32 *)((Uint8 *)info + info->bmiHeader.biSize); 88 SDL_GetMasksForPixelFormat(SDL_PIXELFORMAT_BGRA32, &tmpbpp, &bgr32masks[0], &bgr32masks[1], &bgr32masks[2], &bgr32masks[3]); 89 } 90 } 91 92 // Fill in the size information 93 *pitch = (((w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3); 94 info->bmiHeader.biWidth = w; 95 info->bmiHeader.biHeight = -h; // negative for topdown bitmap 96 info->bmiHeader.biSizeImage = (DWORD)h * (*pitch); 97 98 data->mdc = CreateCompatibleDC(data->hdc); 99 data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0); 100 SDL_small_free(info, isstack); 101 102 if (!data->hbm) { 103 return WIN_SetError("Unable to create DIB"); 104 } 105 SelectObject(data->mdc, data->hbm); 106 107 return true; 108} 109 110bool WIN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects) 111{ 112 SDL_WindowData *data = window->internal; 113 int i; 114 115 for (i = 0; i < numrects; ++i) { 116 BitBlt(data->hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h, 117 data->mdc, rects[i].x, rects[i].y, SRCCOPY); 118 } 119 return true; 120} 121 122void WIN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window) 123{ 124 SDL_WindowData *data = window->internal; 125 126 if (!data) { 127 // The window wasn't fully initialized 128 return; 129 } 130 131 if (data->mdc) { 132 DeleteDC(data->mdc); 133 data->mdc = NULL; 134 } 135 if (data->hbm) { 136 DeleteObject(data->hbm); 137 data->hbm = NULL; 138 } 139} 140 141#endif // SDL_VIDEO_DRIVER_WINDOWS 142[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.