1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2021 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 SDL_VIDEO_DRIVER_WINDOWS
24 
25 #include "SDL_windowsvideo.h"
26 
WIN_CreateWindowFramebuffer(_THIS,SDL_Window * window,Uint32 * format,void ** pixels,int * pitch)27 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
28 {
29     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
30     SDL_bool isstack;
31     size_t size;
32     LPBITMAPINFO info;
33     HBITMAP hbm;
34 
35     /* Free the old framebuffer surface */
36     if (data->mdc) {
37         DeleteDC(data->mdc);
38     }
39     if (data->hbm) {
40         DeleteObject(data->hbm);
41     }
42 
43     /* Find out the format of the screen */
44     size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
45     info = (LPBITMAPINFO)SDL_small_alloc(Uint8, size, &isstack);
46     if (!info) {
47         return SDL_OutOfMemory();
48     }
49 
50     SDL_memset(info, 0, size);
51     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
52 
53     /* The second call to GetDIBits() fills in the bitfields */
54     hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
55     GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
56     GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
57     DeleteObject(hbm);
58 
59     *format = SDL_PIXELFORMAT_UNKNOWN;
60     if (info->bmiHeader.biCompression == BI_BITFIELDS) {
61         int bpp;
62         Uint32 *masks;
63 
64         bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
65         masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
66         *format = SDL_MasksToPixelFormatEnum(bpp, masks[0], masks[1], masks[2], 0);
67     }
68     if (*format == SDL_PIXELFORMAT_UNKNOWN)
69     {
70         /* We'll use RGB format for now */
71         *format = SDL_PIXELFORMAT_RGB888;
72 
73         /* Create a new one */
74         SDL_memset(info, 0, size);
75         info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
76         info->bmiHeader.biPlanes = 1;
77         info->bmiHeader.biBitCount = 32;
78         info->bmiHeader.biCompression = BI_RGB;
79     }
80 
81     /* Fill in the size information */
82     *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
83     info->bmiHeader.biWidth = window->w;
84     info->bmiHeader.biHeight = -window->h;  /* negative for topdown bitmap */
85     info->bmiHeader.biSizeImage = window->h * (*pitch);
86 
87     data->mdc = CreateCompatibleDC(data->hdc);
88     data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
89     SDL_small_free(info, isstack);
90 
91     if (!data->hbm) {
92         return WIN_SetError("Unable to create DIB");
93     }
94     SelectObject(data->mdc, data->hbm);
95 
96     return 0;
97 }
98 
WIN_UpdateWindowFramebuffer(_THIS,SDL_Window * window,const SDL_Rect * rects,int numrects)99 int WIN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
100 {
101     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
102     int i;
103 
104     for (i = 0; i < numrects; ++i) {
105         BitBlt(data->hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
106                data->mdc, rects[i].x, rects[i].y, SRCCOPY);
107     }
108     return 0;
109 }
110 
WIN_DestroyWindowFramebuffer(_THIS,SDL_Window * window)111 void WIN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
112 {
113     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
114 
115     if (!data) {
116         /* The window wasn't fully initialized */
117         return;
118     }
119 
120     if (data->mdc) {
121         DeleteDC(data->mdc);
122         data->mdc = NULL;
123     }
124     if (data->hbm) {
125         DeleteObject(data->hbm);
126         data->hbm = NULL;
127     }
128 }
129 
130 #endif /* SDL_VIDEO_DRIVER_WINDOWS */
131 
132 /* vi: set ts=4 sw=4 expandtab: */
133