summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c
new file mode 100644
index 0000000..f2bbd59
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsframebuffer.c
@@ -0,0 +1,132 @@
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#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 *format = SDL_PIXELFORMAT_UNKNOWN;
63 if (info->bmiHeader.biCompression == BI_BITFIELDS) {
64 int bpp;
65 Uint32 *masks;
66
67 bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
68 masks = (Uint32 *)((Uint8 *)info + info->bmiHeader.biSize);
69 *format = SDL_GetPixelFormatForMasks(bpp, masks[0], masks[1], masks[2], 0);
70 }
71 if (*format == SDL_PIXELFORMAT_UNKNOWN) {
72 // We'll use RGB format for now
73 *format = SDL_PIXELFORMAT_XRGB8888;
74
75 // Create a new one
76 SDL_memset(info, 0, size);
77 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
78 info->bmiHeader.biPlanes = 1;
79 info->bmiHeader.biBitCount = 32;
80 info->bmiHeader.biCompression = BI_RGB;
81 }
82
83 // Fill in the size information
84 *pitch = (((w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
85 info->bmiHeader.biWidth = w;
86 info->bmiHeader.biHeight = -h; // negative for topdown bitmap
87 info->bmiHeader.biSizeImage = (DWORD)h * (*pitch);
88
89 data->mdc = CreateCompatibleDC(data->hdc);
90 data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
91 SDL_small_free(info, isstack);
92
93 if (!data->hbm) {
94 return WIN_SetError("Unable to create DIB");
95 }
96 SelectObject(data->mdc, data->hbm);
97
98 return true;
99}
100
101bool WIN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
102{
103 SDL_WindowData *data = window->internal;
104 int i;
105
106 for (i = 0; i < numrects; ++i) {
107 BitBlt(data->hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
108 data->mdc, rects[i].x, rects[i].y, SRCCOPY);
109 }
110 return true;
111}
112
113void WIN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
114{
115 SDL_WindowData *data = window->internal;
116
117 if (!data) {
118 // The window wasn't fully initialized
119 return;
120 }
121
122 if (data->mdc) {
123 DeleteDC(data->mdc);
124 data->mdc = NULL;
125 }
126 if (data->hbm) {
127 DeleteObject(data->hbm);
128 data->hbm = NULL;
129 }
130}
131
132#endif // SDL_VIDEO_DRIVER_WINDOWS