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